Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
# |
3 |
# Copyright 2018 ByWater Solutions |
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 |
use Modern::Perl; |
21 |
|
22 |
use Test::More tests => 5; |
23 |
use Test::Exception; |
24 |
|
25 |
use Koha::Database; |
26 |
|
27 |
use t::lib::Mocks; |
28 |
use t::lib::TestBuilder; |
29 |
|
30 |
BEGIN { |
31 |
use_ok('Koha::Charges::Sales'); |
32 |
} |
33 |
|
34 |
my $schema = Koha::Database->new->schema; |
35 |
my $builder = t::lib::TestBuilder->new(); |
36 |
|
37 |
subtest 'new' => sub { |
38 |
|
39 |
plan tests => 4; |
40 |
|
41 |
$schema->storage->txn_begin; |
42 |
|
43 |
throws_ok { Koha::Charges::Sales->new( { staff_id => 1 } ) } |
44 |
'Koha::Exceptions::MissingParameter', |
45 |
'Exception thrown if cash_register parameter missing'; |
46 |
|
47 |
throws_ok { Koha::Charges::Sales->new( { cash_register => 1 } ) } |
48 |
'Koha::Exceptions::MissingParameter', |
49 |
'Exception thrown if staff_id parameter missing'; |
50 |
|
51 |
throws_ok { |
52 |
Koha::Charges::Sales->new( { staff_id => 1, cash_register => 1 } ) |
53 |
} |
54 |
qr/Koha::Cash::Register/, |
55 |
'Exception thrown if cash_register is not a Koha::Cash::Register'; |
56 |
|
57 |
my $cash_register = |
58 |
$builder->build_object( { class => 'Koha::Cash::Registers' } ); |
59 |
|
60 |
my $sale = Koha::Charges::Sales->new( |
61 |
{ staff_id => 1, cash_register => $cash_register } ); |
62 |
ok( |
63 |
$sale->isa('Koha::Charges::Sales'), |
64 |
'New returns a Koha::Charges::Sales object' |
65 |
); |
66 |
|
67 |
$schema->storage->txn_rollback; |
68 |
}; |
69 |
|
70 |
subtest 'payment_type (_get_valid_payments) tests' => sub { |
71 |
plan tests => 5; |
72 |
|
73 |
$schema->storage->txn_begin; |
74 |
|
75 |
my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); |
76 |
my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); |
77 |
my $cash_register = $builder->build_object( |
78 |
{ |
79 |
class => 'Koha::Cash::Registers', |
80 |
value => { branchcode => $library1->branchcode } |
81 |
} |
82 |
); |
83 |
|
84 |
my $sale = Koha::Charges::Sales->new( |
85 |
{ staff_id => 1, cash_register => $cash_register } ); |
86 |
|
87 |
is( $sale->payment_type, undef, "payment_type does not have a default" ); |
88 |
|
89 |
throws_ok { $sale->payment_type('BOBBYRANDOM') } |
90 |
'Koha::Exceptions::Account::UnrecognisedType', |
91 |
"Exception thrown if passed a payment type that doesn't exist"; |
92 |
|
93 |
my $av = Koha::AuthorisedValue->new( |
94 |
{ |
95 |
category => 'PAYMENT_TYPE', |
96 |
authorised_value => 'BOBBYRANDOM', |
97 |
lib => 'Test bobbyrandom', |
98 |
lib_opac => 'Test bobbyrandom', |
99 |
} |
100 |
)->store; |
101 |
$av->replace_library_limits( [ $library2->branchcode ] ); |
102 |
|
103 |
throws_ok { $sale->payment_type('BOBBYRANDOM') } |
104 |
'Koha::Exceptions::Account::UnrecognisedType', |
105 |
'Exception thrown if passed payment type that is not valid for the cash registers branch'; |
106 |
|
107 |
$av->replace_library_limits(); |
108 |
$sale->{valid_payments} = undef; # Flush object cache for 'valid_payments' |
109 |
|
110 |
my $pt = $sale->payment_type('BOBBYRANDOM'); |
111 |
is( $pt, 'BOBBYRANDOM', 'Payment type set successfully' ); |
112 |
is( $sale->payment_type, 'BOBBYRANDOM', |
113 |
'Getter returns the current payment_type' ); |
114 |
|
115 |
$schema->storage->txn_rollback; |
116 |
}; |
117 |
|
118 |
subtest 'add_item (_get_valid_items) tests' => sub { |
119 |
plan tests => 6; |
120 |
|
121 |
$schema->storage->txn_begin; |
122 |
|
123 |
my $staff = $builder->build_object( { class => 'Koha::Patrons' } ); |
124 |
my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); |
125 |
my $cash_register = $builder->build_object( |
126 |
{ |
127 |
class => 'Koha::Cash::Registers', |
128 |
value => { branchcode => $library1->branchcode } |
129 |
} |
130 |
); |
131 |
|
132 |
my $sale = Koha::Charges::Sales->new( |
133 |
{ staff_id => $staff->borrowernumber, cash_register => $cash_register } |
134 |
); |
135 |
|
136 |
throws_ok { $sale->add_item( { price => 1.00, quantity => 1 } ) } |
137 |
'Koha::Exceptions::MissingParameter', |
138 |
'Exception thrown if `code` parameter is missing'; |
139 |
|
140 |
my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); |
141 |
my $av = Koha::AuthorisedValue->new( |
142 |
{ |
143 |
category => 'MANUAL_INV', |
144 |
authorised_value => 'BOBBYRANDOM', |
145 |
lib => 'Test bobbyrandom', |
146 |
lib_opac => 'Test bobbyrandom', |
147 |
} |
148 |
)->store; |
149 |
$av->replace_library_limits( [ $library2->branchcode ] ); |
150 |
|
151 |
throws_ok { $sale->add_item( { code => 'BOBBYRANDOM' } ) } |
152 |
'Koha::Exceptions::Account::UnrecognisedType', |
153 |
'Exception thrown if passed an item code that is not valid for the cash registers branch'; |
154 |
|
155 |
$av->replace_library_limits(); |
156 |
$sale->{valid_items} = undef; # Flush object cache for 'valid_items' |
157 |
|
158 |
throws_ok { |
159 |
$sale->add_item( { code => 'BOBBYRANDOM', quantity => 1 } ) |
160 |
} |
161 |
'Koha::Exceptions::MissingParameter', |
162 |
'Exception thrown if `price` parameter is missing'; |
163 |
|
164 |
throws_ok { |
165 |
$sale->add_item( { code => 'BOBBYRANDOM', price => 1.00 } ) |
166 |
} |
167 |
'Koha::Exceptions::MissingParameter', |
168 |
'Exception thrown if `quantity` parameter is missing'; |
169 |
|
170 |
is( |
171 |
ref( |
172 |
$sale->add_item( |
173 |
{ code => 'BOBBYRANDOM', price => 1.00, quantity => 1 } |
174 |
) |
175 |
), |
176 |
'Koha::Charges::Sales', |
177 |
'Original object returned succesfully' |
178 |
); |
179 |
|
180 |
is( scalar @{ $sale->{items} }, 1, 'Item added successfully' ); |
181 |
|
182 |
$schema->storage->txn_rollback; |
183 |
}; |
184 |
|
185 |
subtest 'purchase tests' => sub { |
186 |
plan tests => 12; |
187 |
|
188 |
$schema->storage->txn_begin; |
189 |
|
190 |
my $staff = $builder->build_object( { class => 'Koha::Patrons' } ); |
191 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
192 |
my $cash_register = $builder->build_object( |
193 |
{ |
194 |
class => 'Koha::Cash::Registers', |
195 |
value => { branchcode => $library->branchcode } |
196 |
} |
197 |
); |
198 |
|
199 |
my $payment_type = Koha::AuthorisedValue->new( |
200 |
{ |
201 |
category => 'PAYMENT_TYPE', |
202 |
authorised_value => 'CASH', |
203 |
lib => 'Cash transaction', |
204 |
lib_opac => 'Cash transaction', |
205 |
} |
206 |
)->store; |
207 |
|
208 |
my $item1 = Koha::AuthorisedValue->new( |
209 |
{ |
210 |
category => 'MANUAL_INV', |
211 |
authorised_value => 'COPYRANDOM', |
212 |
lib => 'Copier fee', |
213 |
lib_opac => 'Copier fee', |
214 |
} |
215 |
)->store; |
216 |
my $item2 = Koha::AuthorisedValue->new( |
217 |
{ |
218 |
category => 'MANUAL_INV', |
219 |
authorised_value => 'CARDRANDOM', |
220 |
lib => 'New card fee', |
221 |
lib_opac => 'New card fee', |
222 |
} |
223 |
)->store; |
224 |
|
225 |
my $sale = Koha::Charges::Sales->new( |
226 |
{ staff_id => $staff->borrowernumber, cash_register => $cash_register } |
227 |
); |
228 |
|
229 |
throws_ok { |
230 |
$sale->purchase() |
231 |
} |
232 |
'Koha::Exceptions::MissingParameter', |
233 |
'Exception thrown if `payment_type` is neither set nor passed'; |
234 |
|
235 |
$sale->payment_type('CASH'); # Set payment_type |
236 |
throws_ok { |
237 |
$sale->purchase() |
238 |
} |
239 |
'Koha::Exceptions::NoChanges', |
240 |
'Exception thrown if `add_item` is not called before `purchase`'; |
241 |
|
242 |
$sale->add_item( { code => 'COPYRANDOM', price => 1.00, quantity => 1 } ); |
243 |
$sale->add_item( { code => 'CARDRANDOM', price => 2.00, quantity => 2 } ); |
244 |
|
245 |
$sale->{payment_type} = undef; # Flush payment_type cache in object |
246 |
|
247 |
my $credit; |
248 |
ok( $credit = $sale->purchase( { payment_type => 'CASH' } ), |
249 |
"No exception when payment_type passed" ); |
250 |
|
251 |
is(ref($credit), 'Koha::Account::Line', "Koha::Account::Line returned"); |
252 |
ok($credit->is_credit, "return is a credit for payment"); |
253 |
is($credit->accounttype, 'Purchase', "accounttype set correctly to 'Purchase' for payment"); |
254 |
is($credit->amount, -5.00, "amount is calculated correctly for payment"); |
255 |
is($credit->amountoutstanding, 0.00, "amountoutstanding is set to zero for payment"); |
256 |
is($credit->manager_id, $staff->borrowernumber, "manager_id set correctionly for payment"); |
257 |
is($credit->register_id, $cash_register->id, "register_id set correctly for payment"); |
258 |
is($credit->payment_type, 'CASH', "payment_type set correctly for payment"); |
259 |
|
260 |
my $offsets = Koha::Account::Offsets->search({credit_id => $credit->accountlines_id}); |
261 |
is($offsets->count, 2, "One offset was added for each item added"); |
262 |
|
263 |
#ensure relevant fields are set |
264 |
#ensure register_id is only ever set with a corresponding payment_type having been set |
265 |
|
266 |
$schema->storage->txn_rollback; |
267 |
}; |