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 => 3;
26 use Koha::Account::Lines;
27 use Koha::Account::Offsets;
30 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 subtest 'item() tests' => sub {
39 $schema->storage->txn_begin;
41 my $library = $builder->build( { source => 'Branch' } );
42 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
43 my $patron = $builder->build( { source => 'Borrower' } );
44 my $item = Koha::Item->new(
46 biblionumber => $biblioitem->{biblionumber},
47 biblioitemnumber => $biblioitem->{biblioitemnumber},
48 homebranch => $library->{branchcode},
49 holdingbranch => $library->{branchcode},
50 barcode => 'some_barcode_12',
54 my $line = Koha::Account::Line->new(
56 borrowernumber => $patron->{borrowernumber},
57 itemnumber => $item->itemnumber,
62 my $account_line_item = $line->item;
63 is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
64 is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
66 $schema->storage->txn_rollback;
69 subtest 'total_outstanding() tests' => sub {
73 $schema->storage->txn_begin;
75 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
77 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
78 is( $lines->total_outstanding, 0, 'total_outstanding returns 0 if no lines (undef case)' );
80 my $debit_1 = Koha::Account::Line->new(
81 { borrowernumber => $patron->id,
84 amountoutstanding => 10
88 my $debit_2 = Koha::Account::Line->new(
89 { borrowernumber => $patron->id,
92 amountoutstanding => 10
96 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
97 is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
99 my $credit_1 = Koha::Account::Line->new(
100 { borrowernumber => $patron->id,
103 amountoutstanding => -10
107 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
108 is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
110 my $credit_2 = Koha::Account::Line->new(
111 { borrowernumber => $patron->id,
114 amountoutstanding => -10
118 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
119 is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
121 my $credit_3 = Koha::Account::Line->new(
122 { borrowernumber => $patron->id,
125 amountoutstanding => -100
129 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
130 is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
132 $schema->storage->txn_rollback;
135 subtest 'is_credit() and is_debit() tests' => sub {
139 $schema->storage->txn_begin;
141 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
142 my $account = $patron->account;
144 my $credit = $account->add_credit({ amount => 100, user_id => $patron->id });
146 ok( $credit->is_credit, 'is_credit detects credits' );
147 ok( !$credit->is_debit, 'is_debit detects credits' );
149 my $debit = Koha::Account::Line->new(
151 borrowernumber => $patron->id,
156 ok( !$debit->is_credit, 'is_credit detects debits' );
157 ok( $debit->is_debit, 'is_debit detects debits');
159 $schema->storage->txn_rollback;
162 subtest 'apply() tests' => sub {
166 $schema->storage->txn_begin;
168 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
169 my $account = $patron->account;
171 my $credit = $account->add_credit( { amount => 100, user_id => $patron->id } );
173 my $debit_1 = Koha::Account::Line->new(
174 { borrowernumber => $patron->id,
177 amountoutstanding => 10
181 my $debit_2 = Koha::Account::Line->new(
182 { borrowernumber => $patron->id,
185 amountoutstanding => 100
189 $credit->discard_changes;
190 $debit_1->discard_changes;
192 my $remaining_credit = $credit->apply( { debit => $debit_1, offset_type => 'Manual Credit' } );
193 is( $remaining_credit, 90, 'Remaining credit is correctly calculated' );
194 $credit->discard_changes;
195 is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
198 $debit_1->discard_changes;
199 is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
201 my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
202 is( $offsets->count, 1, 'Only one offset is generated' );
203 my $THE_offest = $offsets->next;
204 is( $THE_offest->amount * 1, 10, 'Amount was calculated correctly (less than the available credit)' );
205 is( $THE_offest->type, 'Manual Credit', 'Passed type stored correctly' );
207 $remaining_credit = $credit->apply( { debit => $debit_2, offset_type => 'Manual Credit' } );
208 is( $remaining_credit, 0, 'No remaining credit left' );
209 $credit->discard_changes;
210 is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
211 $debit_2->discard_changes;
212 is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
215 { $debit_1->apply({ debit => $credit }); }
216 'Koha::Exceptions::Account::IsNotCredit',
217 '->apply() can only be used with credits';
220 { $credit->apply({ debit => $credit }); }
221 'Koha::Exceptions::Account::IsNotDebit',
222 '->apply() can only be applied to credits';
225 { $credit->apply({ debit => $debit_1 }); }
226 'Koha::Exceptions::Account::NoAvailableCredit',
227 '->apply() can only be used with outstanding credits';
230 $schema->storage->txn_rollback;