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 => 2;
24 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 'item' => sub {
36 $schema->storage->txn_begin;
38 my $library = $builder->build( { source => 'Branch' } );
39 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
40 my $patron = $builder->build( { source => 'Borrower' } );
41 my $item = Koha::Item->new(
43 biblionumber => $biblioitem->{biblionumber},
44 biblioitemnumber => $biblioitem->{biblioitemnumber},
45 homebranch => $library->{branchcode},
46 holdingbranch => $library->{branchcode},
47 barcode => 'some_barcode_12',
51 my $line = Koha::Account::Line->new(
53 borrowernumber => $patron->{borrowernumber},
54 itemnumber => $item->itemnumber,
59 my $account_line_item = $line->item;
60 is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
61 is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
63 $schema->storage->txn_rollback;
66 subtest 'total_outstanding' => sub {
70 $schema->storage->txn_begin;
72 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
74 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
75 is( $lines->total_outstanding, 0, 'total_outstanding returns 0 if no lines (undef case)' );
77 my $debit_1 = Koha::Account::Line->new(
78 { borrowernumber => $patron->id,
81 amountoutstanding => 10
85 my $debit_2 = Koha::Account::Line->new(
86 { borrowernumber => $patron->id,
89 amountoutstanding => 10
93 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
94 is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
96 my $credit_1 = Koha::Account::Line->new(
97 { borrowernumber => $patron->id,
100 amountoutstanding => -10
104 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
105 is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
107 my $credit_2 = Koha::Account::Line->new(
108 { borrowernumber => $patron->id,
111 amountoutstanding => -10
115 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
116 is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
118 my $credit_3 = Koha::Account::Line->new(
119 { borrowernumber => $patron->id,
122 amountoutstanding => -100
126 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
127 is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
129 $schema->storage->txn_rollback;