Line 0
Link Here
|
|
|
1 |
package t::lib::TestObjects::FinesFactory; |
2 |
|
3 |
# Copyright Vaara-kirjastot 2015 |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
# |
20 |
|
21 |
use Modern::Perl; |
22 |
use Carp; |
23 |
use Scalar::Util qw(blessed); |
24 |
|
25 |
use C4::Members; |
26 |
use C4::Accounts; |
27 |
use Koha::Borrowers; |
28 |
|
29 |
use base qw(t::lib::TestObjects::ObjectFactory); |
30 |
|
31 |
use Koha::Exception::ObjectExists; |
32 |
|
33 |
sub new { |
34 |
my ($class) = @_; |
35 |
|
36 |
my $self = {}; |
37 |
bless($self, $class); |
38 |
return $self; |
39 |
} |
40 |
|
41 |
sub getDefaultHashKey { |
42 |
return 'note'; |
43 |
} |
44 |
sub getObjectType { |
45 |
return 'HASH'; |
46 |
} |
47 |
|
48 |
=head createTestGroup( $data [, $hashKey, $testContexts...] ) |
49 |
@OVERLOADED |
50 |
|
51 |
FinesFactory creates new fines into accountlines. |
52 |
After testing, all fines created by the FinesFactory will be |
53 |
deleted at tearDown. |
54 |
|
55 |
my $fines = t::lib::TestObjects::FinesFactory->createTestGroup([ |
56 |
amount => 10.0, |
57 |
cardnumber => $borrowers->{'superuberadmin'}->cardnumber, |
58 |
accounttype => 'FU', |
59 |
note => 'unique identifier', |
60 |
}, |
61 |
], undef, $testContext1, $testContext2, $testContext3); |
62 |
|
63 |
#Do test stuff... |
64 |
|
65 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext1); |
66 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext2); |
67 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext3); |
68 |
|
69 |
See t::lib::TestObjects::ObjectFactory for more documentation |
70 |
=cut |
71 |
|
72 |
sub handleTestObject { |
73 |
my ($class, $fine, $stashes) = @_; |
74 |
|
75 |
my $borrower = Koha::Borrowers->cast($fine->{cardnumber}); |
76 |
|
77 |
my $accountno = C4::Accounts::getnextacctno($borrower->borrowernumber); |
78 |
|
79 |
C4::Accounts::manualinvoice( |
80 |
$borrower->borrowernumber, # borrowernumber |
81 |
undef, # itemnumber |
82 |
"Test payment", # description |
83 |
$fine->{accounttype}, # accounttype |
84 |
$fine->{amount}, # amountoutstanding |
85 |
$fine->{note} # note, unique identifier |
86 |
); |
87 |
|
88 |
my $new_fine = $fine; |
89 |
|
90 |
$new_fine->{accountno} = $accountno; |
91 |
|
92 |
return $new_fine; |
93 |
} |
94 |
|
95 |
=head validateAndPopulateDefaultValues |
96 |
@OVERLOAD |
97 |
|
98 |
Validates given Object parameters and makes sure that critical fields are given |
99 |
and populates defaults for missing values. |
100 |
=cut |
101 |
|
102 |
sub validateAndPopulateDefaultValues { |
103 |
my ($class, $object, $hashKey) = @_; |
104 |
$class->SUPER::validateAndPopulateDefaultValues($object, $hashKey); |
105 |
|
106 |
unless ($object->{cardnumber}) { |
107 |
Koha::Exception::BadParameter->throw(error => __PACKAGE__."->createTestGroup():> 'cardnumber' is a mandatory parameter!"); |
108 |
} |
109 |
unless ($object->{note}) { |
110 |
Koha::Exception::BadParameter->throw(error => __PACKAGE__."->createTestGroup():> 'note' is a mandatory parameter!"); |
111 |
} |
112 |
|
113 |
$object->{accounttype} = "FU" unless defined $object->{accounttype}; |
114 |
} |
115 |
|
116 |
=head deleteTestGroup |
117 |
@OVERLOADED |
118 |
|
119 |
my $records = createTestGroup(); |
120 |
##Do funky stuff |
121 |
deleteTestGroup($prefs); |
122 |
|
123 |
Removes the given test group from the DB. |
124 |
|
125 |
=cut |
126 |
|
127 |
sub deleteTestGroup { |
128 |
my ($class, $acct) = @_; |
129 |
|
130 |
my $schema = Koha::Database->new_schema(); |
131 |
while( my ($key, $val) = each %$acct) { |
132 |
if ($schema->resultset('Accountline')->find({"accountno" => $val->{accountno}, "borrowernumber" => $val->{borrowernumber} })) { |
133 |
$schema->resultset('Accountline')->find({"accountno" => $val->{accountno}, "borrowernumber" => $val->{borrowernumber} })->delete(); |
134 |
} |
135 |
} |
136 |
} |
137 |
|
138 |
sub _deleteTestGroupFromIdentifiers { |
139 |
my ($self, $testGroupIdentifiers) = @_; |
140 |
|
141 |
my $schema = Koha::Database->new_schema(); |
142 |
foreach my $key (@$testGroupIdentifiers) { |
143 |
if ($schema->resultset('Accountline')->find({"note" => $key})) { |
144 |
$schema->resultset('Accountline')->find({"note" => $key})->delete(); |
145 |
} |
146 |
} |
147 |
} |
148 |
|
149 |
|
150 |
1; |