3 # Copyright 2018 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>
22 use Test::More tests => 1;
25 use Koha::Account::Lines;
27 use t::lib::TestBuilder;
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
32 subtest 'outstanding_debits() tests' => sub {
36 $schema->storage->txn_begin;
38 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
41 push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 1 })->store;
42 push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 2 })->store;
43 push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 3 })->store;
44 push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 4 })->store;
46 my $account = $patron->account;
47 my $lines = $account->outstanding_debits();
49 is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
52 foreach my $line ( @{ $lines->as_list } ) {
53 my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
54 is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
58 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
59 Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
60 my $just_one = Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => 3 })->store;
61 Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -6 })->store;
62 $lines = $patron_2->account->outstanding_debits();
63 is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
64 is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
65 my $the_line = Koha::Account::Lines->find( $just_one->id );
66 is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
68 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
69 Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
70 Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -20 })->store;
71 Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -200 })->store;
72 $lines = $patron_3->account->outstanding_debits();
73 is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
74 is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
76 my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
77 $lines = $patron_4->account->outstanding_debits();
78 is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
79 is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
81 $schema->storage->txn_rollback;