Bug 20434: Update UNIMARC framework - authorised values
[koha.git] / t / db_dependent / Accounts.t
blob4a450632ebcab3a7694ac640e41fb139dc333798
1 #!/usr/bin/perl
3 # Copyright 2015 BibLibre
5 # This file is part of Koha.
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.
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.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, see <http://www.gnu.org/licenses>.
19 use Modern::Perl;
21 use Test::More tests => 33;
22 use Test::MockModule;
23 use Test::Warn;
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
28 use Koha::Account;
29 use Koha::Account::Lines;
30 use Koha::Account::Offsets;
31 use Koha::Notice::Messages;
32 use Koha::Notice::Templates;
33 use Koha::DateUtils qw( dt_from_string );
35 use C4::Circulation qw( MarkIssueReturned );
37 BEGIN {
38 use_ok('C4::Accounts');
39 use_ok('Koha::Object');
40 use_ok('Koha::Patron');
41 use_ok('Data::Dumper');
44 can_ok( 'C4::Accounts',
45 qw(
46 chargelostitem
47 manualinvoice
48 purge_zero_balance_fees )
51 my $schema = Koha::Database->new->schema;
52 $schema->storage->txn_begin;
53 my $dbh = C4::Context->dbh;
55 my $builder = t::lib::TestBuilder->new;
56 my $library = $builder->build( { source => 'Branch' } );
58 $dbh->do(q|DELETE FROM accountlines|);
59 $dbh->do(q|DELETE FROM issues|);
60 $dbh->do(q|DELETE FROM borrowers|);
62 my $branchcode = $library->{branchcode};
63 my $borrower_number;
65 my $context = new Test::MockModule('C4::Context');
66 $context->mock( 'userenv', sub {
67 return {
68 flags => 1,
69 id => 'my_userid',
70 branch => $branchcode,
72 });
73 $context->mock( 'interface', sub { return "commandline" } );
74 my $userenv_branchcode = $branchcode;
76 # Test manualinvoice
77 my $itemtype = $builder->build( { source => 'Itemtype' } );
78 my $item = $builder->build( { source => 'Item', value => { itype => $itemtype->{itemtype} } } );
79 my $patron = $builder->build( { source => 'Borrower' } );
80 my $amount = '5.000000';
81 my $description = "Test fee!";
82 my $type = 'LOST';
83 my $note = 'Test note!';
84 warning_like {
85 C4::Accounts::manualinvoice( $patron->{borrowernumber},
86 $item->{itemnumber}, $description, $type, $amount, $note )
88 qr/C4::Accounts::manualinvoice is deprecated in favor of Koha::Account->add_debit/,
89 "deprecation warning received for manualinvoice";
90 my ($accountline) = Koha::Account::Lines->search(
92 borrowernumber => $patron->{borrowernumber}
95 is( $accountline->accounttype, $type, 'Accountline type set correctly for manualinvoice' );
96 is( $accountline->amount, $amount, 'Accountline amount set correctly for manualinvoice' );
97 ok( $accountline->description =~ /^$description/, 'Accountline description set correctly for manualinvoice' );
98 is( $accountline->note, $note, 'Accountline note set correctly for manualinvoice' );
99 is( $accountline->branchcode, $branchcode, 'Accountline branchcode set correctly for manualinvoice' );
101 $dbh->do(q|DELETE FROM accountlines|);
103 # Testing purge_zero_balance_fees
105 # The 3rd value in the insert is 'days ago' --
106 # 0 => today
107 # 1 => yesterday
108 # etc.
110 my $sth = $dbh->prepare(
111 "INSERT INTO accountlines (
112 borrowernumber,
113 amountoutstanding,
114 date,
115 description,
116 interface
118 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ?, ? )"
121 my $days = 5;
123 my @test_data = (
124 { amount => 0 , days_ago => 0 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0 } ,
125 { amount => 0 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0 } ,
126 { amount => 0 , days_ago => $days , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0 } ,
127 { amount => 0 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1 } ,
128 { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1 } ,
129 { amount => 5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0 } ,
130 { amount => 5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0 } ,
131 { amount => 5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0 } ,
132 { amount => -5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
133 { amount => -5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0 } ,
134 { amount => -5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day' , delete => 0 }
136 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
137 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
139 for my $data ( @test_data ) {
140 $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description}, 'commandline');
143 purge_zero_balance_fees( $days );
145 $sth = $dbh->prepare(
146 "select count(*) = 0 as deleted
147 from accountlines
148 where description = ?"
152 sub is_delete_correct {
153 my $should_delete = shift;
154 my $description = shift;
155 $sth->execute( $description );
156 my $test = $sth->fetchrow_hashref();
157 is( $test->{deleted}, $should_delete, $description )
160 for my $data (@test_data) {
161 is_delete_correct( $data->{delete}, $data->{description});
164 $dbh->do(q|DELETE FROM accountlines|);
166 subtest "Koha::Account::pay tests" => sub {
168 plan tests => 14;
170 # Create a borrower
171 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
172 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
174 my $borrower = Koha::Patron->new( {
175 cardnumber => '1234567890',
176 surname => 'McFly',
177 firstname => 'Marty',
178 } );
179 $borrower->categorycode( $categorycode );
180 $borrower->branchcode( $branchcode );
181 $borrower->store;
183 my $account = Koha::Account->new({ patron_id => $borrower->id });
185 my $line1 = $account->add_debit({ type => 'account', amount => 100, interface => 'commandline' });
186 my $line2 = $account->add_debit({ type => 'account', amount => 200, interface => 'commandline' });
188 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
189 $sth->execute;
190 my $count = $sth->fetchrow_array;
191 is($count, 2, 'There is 2 lines as expected');
193 # There is $100 in the account
194 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
195 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
196 my $amountleft = 0;
197 for my $line ( @$amountoutstanding ) {
198 $amountleft += $line;
200 is($amountleft, 300, 'The account has 300$ as expected' );
202 # We make a $20 payment
203 my $borrowernumber = $borrower->borrowernumber;
204 my $data = '20.00';
205 my $payment_note = '$20.00 payment note';
206 my $id = $account->pay( { amount => $data, note => $payment_note, payment_type => "TEST_TYPE" } );
208 my $accountline = Koha::Account::Lines->find( $id );
209 is( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
211 # There is now $280 in the account
212 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
213 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
214 $amountleft = 0;
215 for my $line ( @$amountoutstanding ) {
216 $amountleft += $line;
218 is($amountleft, 280, 'The account has $280 as expected' );
220 # Is the payment note well registered
221 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
222 $sth->execute($borrower->borrowernumber);
223 my $note = $sth->fetchrow_array;
224 is($note,'$20.00 payment note', '$20.00 payment note is registered');
226 # We make a -$30 payment (a NEGATIVE payment)
227 $data = '-30.00';
228 $payment_note = '-$30.00 payment note';
229 $account->pay( { amount => $data, note => $payment_note } );
231 # There is now $310 in the account
232 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
233 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
234 $amountleft = 0;
235 for my $line ( @$amountoutstanding ) {
236 $amountleft += $line;
238 is($amountleft, 310, 'The account has $310 as expected' );
239 # Is the payment note well registered
240 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
241 $sth->execute($borrower->borrowernumber);
242 $note = $sth->fetchrow_array;
243 is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
245 #We make a $150 payment ( > 1stLine )
246 $data = '150.00';
247 $payment_note = '$150.00 payment note';
248 $account->pay( { amount => $data, note => $payment_note } );
250 # There is now $160 in the account
251 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
252 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
253 $amountleft = 0;
254 for my $line ( @$amountoutstanding ) {
255 $amountleft += $line;
257 is($amountleft, 160, 'The account has $160 as expected' );
259 # Is the payment note well registered
260 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
261 $sth->execute($borrower->borrowernumber);
262 $note = $sth->fetchrow_array;
263 is($note,'$150.00 payment note', '$150.00 payment note is registered');
265 #We make a $200 payment ( > amountleft )
266 $data = '200.00';
267 $payment_note = '$200.00 payment note';
268 $account->pay( { amount => $data, note => $payment_note } );
270 # There is now -$40 in the account
271 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
272 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
273 $amountleft = 0;
274 for my $line ( @$amountoutstanding ) {
275 $amountleft += $line;
277 is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
279 # Is the payment note well registered
280 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
281 $sth->execute($borrower->borrowernumber);
282 $note = $sth->fetchrow_array;
283 is($note,'$200.00 payment note', '$200.00 payment note is registered');
285 my $line3 = $account->add_debit({ type => 'account', amount => 42, interface => 'commandline' });
286 my $payment_id = $account->pay( { lines => [$line3], amount => 42 } );
287 my $payment = Koha::Account::Lines->find( $payment_id );
288 is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
289 $line3 = Koha::Account::Lines->find( $line3->id );
290 is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
291 is( $payment->branchcode, undef, 'branchcode passed, then undef' );
294 subtest "Koha::Account::pay particular line tests" => sub {
296 plan tests => 5;
298 # Create a borrower
299 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
300 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
302 my $borrower = Koha::Patron->new( {
303 cardnumber => 'kylemhall',
304 surname => 'Hall',
305 firstname => 'Kyle',
306 } );
307 $borrower->categorycode( $categorycode );
308 $borrower->branchcode( $branchcode );
309 $borrower->store;
311 my $account = Koha::Account->new({ patron_id => $borrower->id });
313 my $line1 = $account->add_debit({ type => 'account', amount => 1, interface => 'commandline' });
314 my $line2 = $account->add_debit({ type => 'account', amount => 2, interface => 'commandline' });
315 my $line3 = $account->add_debit({ type => 'account', amount => 3, interface => 'commandline' });
316 my $line4 = $account->add_debit({ type => 'account', amount => 4, interface => 'commandline' });
318 is( $account->balance(), 10, "Account balance is 10" );
320 $account->pay(
322 lines => [$line2, $line3, $line4],
323 amount => 4,
327 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
329 # Line1 is not paid at all, as it was not passed in the lines param
330 is( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
331 # Line2 was paid in full, as it was the first in the lines list
332 is( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
333 # Line3 was paid partially, as the remaining balance did not cover it entirely
334 is( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
335 # Line4 was not paid at all, as the payment was all used up by that point
336 is( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
339 subtest "Koha::Account::pay writeoff tests" => sub {
341 plan tests => 5;
343 # Create a borrower
344 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
345 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
347 my $borrower = Koha::Patron->new( {
348 cardnumber => 'chelseahall',
349 surname => 'Hall',
350 firstname => 'Chelsea',
351 } );
352 $borrower->categorycode( $categorycode );
353 $borrower->branchcode( $branchcode );
354 $borrower->store;
356 my $account = Koha::Account->new({ patron_id => $borrower->id });
358 my $line = $account->add_debit({ type => 'account', amount => 42, interface => 'commandline' });
360 is( $account->balance(), 42, "Account balance is 42" );
362 my $id = $account->pay(
364 lines => [$line],
365 amount => 42,
366 type => 'writeoff',
370 $line->_result->discard_changes();
372 is( $line->amountoutstanding, "0.000000", "Line was written off" );
374 my $writeoff = Koha::Account::Lines->find( $id );
376 is( $writeoff->accounttype, 'W', 'Type is correct' );
377 is( $writeoff->description, 'Writeoff', 'Description is correct' );
378 is( $writeoff->amount, '-42.000000', 'Amount is correct' );
381 subtest "More Koha::Account::pay tests" => sub {
383 plan tests => 8;
385 # Create a borrower
386 my $category = $builder->build({ source => 'Category' })->{ categorycode };
387 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
388 $branchcode = $branch;
389 my $borrowernumber = $builder->build({
390 source => 'Borrower',
391 value => { categorycode => $category,
392 branchcode => $branch }
393 })->{ borrowernumber };
395 my $amount = 100;
396 my $accountline = $builder->build({ source => 'Accountline',
397 value => { borrowernumber => $borrowernumber,
398 amount => $amount,
399 amountoutstanding => $amount }
402 my $rs = $schema->resultset('Accountline')->search({
403 borrowernumber => $borrowernumber
406 is( $rs->count(), 1, 'Accountline created' );
408 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
409 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
410 # make the full payment
411 $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
413 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next();
414 is( $offset->amount(), '-100.000000', 'Offset amount is -100.00' );
415 is( $offset->type(), 'Payment', 'Offset type is Payment' );
417 my $stat = $schema->resultset('Statistic')->search({
418 branch => $branch,
419 type => 'payment'
420 }, { order_by => { -desc => 'datetime' } })->next();
422 ok( defined $stat, "There's a payment log that matches the branch" );
424 SKIP: {
425 skip "No statistic logged", 4 unless defined $stat;
427 is( $stat->type, 'payment', "Correct statistic type" );
428 is( $stat->branch, $branch, "Correct branch logged to statistics" );
429 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
430 is( $stat->value+0, $amount, "Correct amount logged to statistics" );
434 subtest "Even more Koha::Account::pay tests" => sub {
436 plan tests => 8;
438 # Create a borrower
439 my $category = $builder->build({ source => 'Category' })->{ categorycode };
440 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
441 $branchcode = $branch;
442 my $borrowernumber = $builder->build({
443 source => 'Borrower',
444 value => { categorycode => $category,
445 branchcode => $branch }
446 })->{ borrowernumber };
448 my $amount = 100;
449 my $partialamount = 60;
450 my $accountline = $builder->build({ source => 'Accountline',
451 value => { borrowernumber => $borrowernumber,
452 amount => $amount,
453 amountoutstanding => $amount }
456 my $rs = $schema->resultset('Accountline')->search({
457 borrowernumber => $borrowernumber
460 is( $rs->count(), 1, 'Accountline created' );
462 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
463 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
464 # make the full payment
465 $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
467 my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next();
468 is( $offset->amount, '-60.000000', 'Offset amount is -60.00' );
469 is( $offset->type, 'Payment', 'Offset type is payment' );
471 my $stat = $schema->resultset('Statistic')->search({
472 branch => $branch,
473 type => 'payment'
474 }, { order_by => { -desc => 'datetime' } })->next();
476 ok( defined $stat, "There's a payment log that matches the branch" );
478 SKIP: {
479 skip "No statistic logged", 4 unless defined $stat;
481 is( $stat->type, 'payment', "Correct statistic type" );
482 is( $stat->branch, $branch, "Correct branch logged to statistics" );
483 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
484 is( $stat->value+0, $partialamount, "Correct amount logged to statistics" );
488 subtest 'balance' => sub {
489 plan tests => 2;
491 my $patron = $builder->build({source => 'Borrower'});
492 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
493 my $account = $patron->account;
494 is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
496 my $accountline_1 = $builder->build(
498 source => 'Accountline',
499 value => {
500 borrowernumber => $patron->borrowernumber,
501 amount => 42,
502 amountoutstanding => 42
506 my $accountline_2 = $builder->build(
508 source => 'Accountline',
509 value => {
510 borrowernumber => $patron->borrowernumber,
511 amount => -13,
512 amountoutstanding => -13
517 my $balance = $patron->account->balance;
518 is( int($balance), 29, 'balance should return the correct value');
520 $patron->delete;
523 subtest "C4::Accounts::chargelostitem tests" => sub {
524 plan tests => 3;
526 my $branch = $builder->build( { source => 'Branch' } );
527 my $branchcode = $branch->{branchcode};
529 my $staff = $builder->build( { source => 'Borrower' } );
530 my $staff_id = $staff->{borrowernumber};
532 my $module = Test::MockModule->new('C4::Context');
533 $module->mock(
534 'userenv',
535 sub {
536 return {
537 flags => 1,
538 number => $staff_id,
539 branch => $branchcode,
544 my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
545 rentalcharge => 0,
546 defaultreplacecost => undef,
547 processfee => undef,
548 }});
549 my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
550 rentalcharge => 0,
551 defaultreplacecost => 16.32,
552 processfee => undef,
553 }});
554 my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => {
555 rentalcharge => 0,
556 defaultreplacecost => undef,
557 processfee => 8.16,
558 }});
559 my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => {
560 rentalcharge => 0,
561 defaultreplacecost => 4.08,
562 processfee => 2.04,
563 }});
564 my $cli_borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'};
565 my $cli_itemnumber1 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_no_fee->{itemtype} } })->{'itemnumber'};
566 my $cli_itemnumber2 = $builder->build({ source => 'Item', value => { itype => $itype_replace_no_fee->{itemtype} } })->{'itemnumber'};
567 my $cli_itemnumber3 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_fee->{itemtype} } })->{'itemnumber'};
568 my $cli_itemnumber4 = $builder->build({ source => 'Item', value => { itype => $itype_replace_fee->{itemtype} } })->{'itemnumber'};
570 my $cli_issue_id_1 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1 } })->{issue_id};
571 my $cli_issue_id_2 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2 } })->{issue_id};
572 my $cli_issue_id_3 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3 } })->{issue_id};
573 my $cli_issue_id_4 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
574 my $cli_issue_id_4X = undef;
576 my $lostfine;
577 my $procfee;
579 subtest "fee application tests" => sub {
580 plan tests => 44;
582 t::lib::Mocks::mock_preference('item-level_itypes', '1');
583 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
585 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
586 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'LOST' });
587 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
588 ok( !$lostfine, "No lost fine if no replacementcost or default when pref off");
589 ok( !$procfee, "No processing fee if no processing fee");
590 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
591 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'LOST' });
592 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
593 ok( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set");
594 ok( !$procfee, "No processing fee if no processing fee");
595 $lostfine->delete();
597 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
598 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'LOST' });
599 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
600 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
601 ok( !$procfee, "No processing fee if no processing fee");
602 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
603 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'LOST' });
604 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
605 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
606 ok( !$procfee, "No processing fee if no processing fee");
607 $lostfine->delete();
609 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
610 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'LOST' });
611 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
612 ok( !$lostfine, "No lost fine if no replacementcost and no default set when pref off");
613 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
614 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
615 $procfee->delete();
616 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
617 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'LOST' });
618 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
619 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set");
620 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
621 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
622 $lostfine->delete();
623 $procfee->delete();
625 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
626 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
627 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
628 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
629 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
630 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
631 $procfee->delete();
632 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
633 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
634 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
635 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
636 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
637 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
638 $lostfine->delete();
639 $procfee->delete();
641 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
643 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
644 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'LOST' });
645 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
646 ok( !$lostfine, "No lost fine if no replacementcost or default when pref on");
647 ok( !$procfee, "No processing fee if no processing fee");
648 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
649 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'LOST' });
650 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
651 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
652 ok( !$procfee, "No processing fee if no processing fee");
654 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
655 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'LOST' });
656 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
657 is( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on");
658 ok( !$procfee, "No processing fee if no processing fee");
659 $lostfine->delete();
660 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
661 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'LOST' });
662 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
663 is( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set");
664 ok( !$procfee, "No processing fee if no processing fee");
666 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
667 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'LOST' });
668 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
669 ok( !$lostfine, "No lost fine if no replacementcost and default not set when pref on");
670 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
671 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
672 $procfee->delete();
673 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
674 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'LOST' });
675 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
676 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
677 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
678 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
680 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
681 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
682 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
683 is( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on");
684 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
685 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
686 $lostfine->delete();
687 $procfee->delete();
688 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
689 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
690 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
691 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set");
692 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
693 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
694 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
695 my $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
696 my $procfees = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
697 ok( $lostfines->count == 1 , "Lost fine cannot be double charged for the same issue_id");
698 ok( $procfees->count == 1, "Processing fee cannot be double charged for the same issue_id");
699 MarkIssueReturned($cli_borrowernumber, $cli_itemnumber4);
700 $cli_issue_id_4X = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
701 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
702 $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
703 $procfees = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
704 ok( $lostfines->count == 2 , "Lost fine can be charged twice for the same item if they are distinct issue_id's");
705 ok( $procfees->count == 2, "Processing fee can be charged twice for the same item if they are distinct issue_id's");
706 $lostfines->delete();
707 $procfees->delete();
710 subtest "basic fields tests" => sub {
711 plan tests => 12;
713 t::lib::Mocks::mock_preference('ProcessingFeeNote', 'Test Note');
714 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, '1.99', "Perdedor");
716 # Lost Item Fee
717 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
718 ok($lostfine, "Lost fine created");
719 is($lostfine->manager_id, $staff_id, "Lost fine manager_id set correctly");
720 is($lostfine->issue_id, $cli_issue_id_4X, "Lost fine issue_id set correctly");
721 is($lostfine->description, "Perdedor", "Lost fine issue_id set correctly");
722 is($lostfine->note, '', "Lost fine does not contain a note");
723 is($lostfine->branchcode, $branchcode, "Lost fine branchcode set correctly");
725 # Processing Fee
726 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
727 ok($procfee, "Processing fee created");
728 is($procfee->manager_id, $staff_id, "Processing fee manager_id set correctly");
729 is($procfee->issue_id, $cli_issue_id_4X, "Processing fee issue_id set correctly");
730 is($procfee->description, "Perdedor", "Processing fee issue_id set correctly");
731 is($procfee->note, C4::Context->preference("ProcessingFeeNote"), "Processing fee contains note matching ProcessingFeeNote");
732 is($procfee->branchcode, $branchcode, "Processing fee branchcode set correctly");
733 $lostfine->delete();
734 $procfee->delete();
737 subtest "FinesLog tests" => sub {
738 plan tests => 2;
740 my $action_logs = $schema->resultset('ActionLog')->search()->count;
742 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
743 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
744 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
745 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
746 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No logs were added' );
747 $lostfine->delete();
748 $procfee->delete();
750 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
751 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
752 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
753 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
754 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Logs were added' );
755 $lostfine->delete();
756 $procfee->delete();
759 # Cleanup - this must be replaced with a transaction per subtest
760 Koha::Patrons->find($cli_borrowernumber)->checkouts->delete;
763 subtest "Koha::Account::non_issues_charges tests" => sub {
764 plan tests => 21;
766 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
767 my $account = $patron->account;
769 my $today = dt_from_string;
770 my $res = 3;
771 my $rent = 5;
772 my $manual = 7;
773 $account->add_debit(
775 description => 'a Res fee',
776 type => 'reserve',
777 amount => $res,
778 interface => 'commandline'
781 $account->add_debit(
783 description => 'a Rental fee',
784 type => 'rent',
785 amount => $rent,
786 interface => 'commandline'
789 Koha::Account::Line->new(
791 borrowernumber => $patron->borrowernumber,
792 date => $today,
793 description => 'a Manual invoice fee',
794 accounttype => 'Copie',
795 amountoutstanding => $manual,
796 interface => 'commandline'
798 )->store;
799 Koha::AuthorisedValue->new(
801 category => 'MANUAL_INV',
802 authorised_value => 'Copie',
803 lib => 'Fee for copie',
805 )->store;
808 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
809 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
810 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
811 my ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
812 my $other_charges = $total - $non_issues_charges;
814 $account->balance,
815 $res + $rent + $manual,
816 'Total charges should be Res + Rent + Manual'
818 is( $non_issues_charges, 0,
819 'If 0|0|0 there should not have non issues charges' );
820 is( $other_charges, 15, 'If 0|0|0 there should only have other charges' );
822 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
823 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
824 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
825 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
826 $other_charges = $total - $non_issues_charges;
828 $total,
829 $res + $rent + $manual,
830 'Total charges should be Res + Rent + Manual'
832 is( $non_issues_charges, $manual,
833 'If 0|0|1 Only Manual should be a non issue charge' );
835 $other_charges,
836 $res + $rent,
837 'If 0|0|1 Res + Rent should be other charges'
840 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
841 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
842 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
843 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
844 $other_charges = $total - $non_issues_charges;
846 $total,
847 $res + $rent + $manual,
848 'Total charges should be Res + Rent + Manual'
850 is( $non_issues_charges, $rent,
851 'If 0|1|0 Only Rental should be a non issue charge' );
853 $other_charges,
854 $res + $manual,
855 'If 0|1|0 Rent + Manual should be other charges'
858 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
859 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
860 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
861 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
862 $other_charges = $total - $non_issues_charges;
864 $total,
865 $res + $rent + $manual,
866 'Total charges should be Res + Rent + Manual'
869 $non_issues_charges,
870 $rent + $manual,
871 'If 0|1|1 Rent + Manual should be non issues charges'
873 is( $other_charges, $res, 'If 0|1|1 there should only have other charges' );
875 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
876 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
877 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
878 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
879 $other_charges = $total - $non_issues_charges;
881 $total,
882 $res + $rent + $manual,
883 'Total charges should be Res + Rent + Manual'
885 is( $non_issues_charges, $res,
886 'If 1|0|0 Only Res should be non issues charges' );
888 $other_charges,
889 $rent + $manual,
890 'If 1|0|0 Rent + Manual should be other charges'
893 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
894 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
895 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
896 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
897 $other_charges = $total - $non_issues_charges;
899 $total,
900 $res + $rent + $manual,
901 'Total charges should be Res + Rent + Manual'
904 $non_issues_charges,
905 $res + $rent,
906 'If 1|1|0 Res + Rent should be non issues charges'
908 is( $other_charges, $manual,
909 'If 1|1|0 Only Manual should be other charges' );
911 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
912 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
913 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
914 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
915 $other_charges = $total - $non_issues_charges;
917 $total,
918 $res + $rent + $manual,
919 'Total charges should be Res + Rent + Manual'
922 $non_issues_charges,
923 $res + $rent + $manual,
924 'If 1|1|1 Res + Rent + Manual should be non issues charges'
926 is( $other_charges, 0, 'If 1|1|1 there should not have any other charges' );
929 subtest "Koha::Account::non_issues_charges tests" => sub {
930 plan tests => 9;
932 my $patron = $builder->build_object(
934 class => "Koha::Patrons",
935 value => {
936 firstname => 'Test',
937 surname => 'Patron',
938 categorycode => $categorycode,
939 branchcode => $branchcode
944 my $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0, interface => 'commandline' })->store();
945 my $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => -5, interface => 'commandline' })->store();
946 my $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
947 purge_zero_balance_fees( 1 );
948 my $debit_2 = Koha::Account::Lines->find( $debit->id );
949 my $credit_2 = Koha::Account::Lines->find( $credit->id );
950 ok( $debit_2, 'Debit was correctly not deleted when credit has balance' );
951 ok( $credit_2, 'Credit was correctly not deleted when credit has balance' );
952 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2, "The 2 account lines still exists" );
954 $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 5, interface => 'commanline' })->store();
955 $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0, interface => 'commandline' })->store();
956 $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
957 purge_zero_balance_fees( 1 );
958 $debit_2 = $credit_2 = undef;
959 $debit_2 = Koha::Account::Lines->find( $debit->id );
960 $credit_2 = Koha::Account::Lines->find( $credit->id );
961 ok( $debit_2, 'Debit was correctly not deleted when debit has balance' );
962 ok( $credit_2, 'Credit was correctly not deleted when debit has balance' );
963 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists" );
965 $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0, interface => 'commandline' })->store();
966 $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0, interface => 'commandline' })->store();
967 $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
968 purge_zero_balance_fees( 1 );
969 $debit_2 = Koha::Account::Lines->find( $debit->id );
970 $credit_2 = Koha::Account::Lines->find( $credit->id );
971 ok( !$debit_2, 'Debit was correctly deleted' );
972 ok( !$credit_2, 'Credit was correctly deleted' );
973 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists, the last 2 have been deleted ok" );
977 subtest "Koha::Account::Offset credit & debit tests" => sub {
979 plan tests => 4;
981 # Create a borrower
982 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
983 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
985 my $borrower = Koha::Patron->new( {
986 cardnumber => 'kyliehall',
987 surname => 'Hall',
988 firstname => 'Kylie',
989 } );
990 $borrower->categorycode( $categorycode );
991 $borrower->branchcode( $branchcode );
992 $borrower->store;
994 my $account = Koha::Account->new({ patron_id => $borrower->id });
996 my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10, interface => 'commandline' })->store();
997 my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 20, amountoutstanding => 20, interface => 'commandline' })->store();
999 my $id = $account->pay(
1001 lines => [$line1, $line2],
1002 amount => 30,
1006 # Test debit and credit methods for Koha::Account::Offset
1007 my $account_offset = Koha::Account::Offsets->find( { credit_id => $id, debit_id => $line1->id } );
1008 is( $account_offset->debit->id, $line1->id, "Koha::Account::Offset->debit gets correct accountline" );
1009 is( $account_offset->credit->id, $id, "Koha::Account::Offset->credit gets correct accountline" );
1011 $account_offset = Koha::Account::Offset->new(
1013 credit_id => undef,
1014 debit_id => undef,
1015 type => 'Payment',
1016 amount => 0,
1018 )->store();
1020 is( $account_offset->debit, undef, "Koha::Account::Offset->debit returns undef if no associated debit" );
1021 is( $account_offset->credit, undef, "Koha::Account::Offset->credit returns undef if no associated credit" );
1024 subtest "Payment notice tests" => sub {
1026 plan tests => 8;
1028 Koha::Account::Lines->delete();
1029 Koha::Patrons->delete();
1030 Koha::Notice::Messages->delete();
1031 # Create a borrower
1032 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
1033 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
1035 my $borrower = Koha::Patron->new(
1037 cardnumber => 'chelseahall',
1038 surname => 'Hall',
1039 firstname => 'Chelsea',
1040 email => 'chelsea@example.com',
1041 categorycode => $categorycode,
1042 branchcode => $branchcode,
1044 )->store();
1046 my $manager = $builder->build_object({ class => "Koha::Patrons" });
1047 my $context = new Test::MockModule('C4::Context');
1048 $context->mock( 'userenv', sub {
1049 return {
1050 number => $manager->borrowernumber,
1051 branch => $manager->branchcode,
1054 my $account = Koha::Account->new({ patron_id => $borrower->id });
1056 my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 27, interface => 'commandline' })->store();
1058 my $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_PAYMENT' } );
1059 $letter->content('[%- USE Price -%]A payment of [% credit.amount * -1 | $Price %] has been applied to your account.');
1060 $letter->store();
1062 t::lib::Mocks::mock_preference('UseEmailReceipts', '0');
1063 my $id = $account->pay( { amount => 1 } );
1064 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for payment not sent if UseEmailReceipts is disabled' );
1066 $id = $account->pay( { amount => 1, type => 'writeoff' } );
1067 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for writeoff not sent if UseEmailReceipts is disabled' );
1069 t::lib::Mocks::mock_preference('UseEmailReceipts', '1');
1071 $id = $account->pay( { amount => 12 } );
1072 my $notice = Koha::Notice::Messages->search()->next();
1073 is( $notice->subject, 'Account payment', 'Notice subject is correct for payment' );
1074 is( $notice->letter_code, 'ACCOUNT_PAYMENT', 'Notice letter code is correct for payment' );
1075 is( $notice->content, 'A payment of 12.00 has been applied to your account.', 'Notice content is correct for payment' );
1076 $notice->delete();
1078 $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_WRITEOFF' } );
1079 $letter->content('[%- USE Price -%]A writeoff of [% credit.amount * -1 | $Price %] has been applied to your account.');
1080 $letter->store();
1082 $id = $account->pay( { amount => 13, type => 'writeoff' } );
1083 $notice = Koha::Notice::Messages->search()->next();
1084 is( $notice->subject, 'Account writeoff', 'Notice subject is correct for payment' );
1085 is( $notice->letter_code, 'ACCOUNT_WRITEOFF', 'Notice letter code is correct for writeoff' );
1086 is( $notice->content, 'A writeoff of 13.00 has been applied to your account.', 'Notice content is correct for writeoff' );