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 => 4;
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 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
193 my $remaining_credit = $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
194 is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
195 $credit->discard_changes;
196 is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
199 $debit_1->discard_changes;
200 is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
202 my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
203 is( $offsets->count, 1, 'Only one offset is generated' );
204 my $THE_offset = $offsets->next;
205 is( $THE_offset->amount * 1, 10, 'Amount was calculated correctly (less than the available credit)' );
206 is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
208 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
209 $remaining_credit = $credit->apply( { debits => $debits } );
210 is( $remaining_credit, 0, 'No remaining credit left' );
211 $credit->discard_changes;
212 is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
213 $debit_2->discard_changes;
214 is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
216 $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
217 is( $offsets->count, 1, 'Only one offset is generated' );
218 $THE_offset = $offsets->next;
219 is( $THE_offset->amount * 1, 90, 'Amount was calculated correctly (less than the available credit)' );
220 is( $THE_offset->type, 'credit_applied', 'Defaults to credit_applied offset type' );
222 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
224 { $credit->apply({ debits => $debits }); }
225 'Koha::Exceptions::Account::NoAvailableCredit',
226 '->apply() can only be used with outstanding credits';
228 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
230 { $debit_1->apply({ debits => $debits }); }
231 'Koha::Exceptions::Account::IsNotCredit',
232 '->apply() can only be used with credits';
234 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
235 my $credit_3 = $account->add_credit({ amount => 1 });
237 { $credit_3->apply({ debits => $debits }); }
238 'Koha::Exceptions::Account::IsNotDebit',
239 '->apply() can only be applied to credits';
241 my $credit_2 = $account->add_credit({ amount => 20 });
242 my $debit_3 = Koha::Account::Line->new(
243 { borrowernumber => $patron->id,
246 amountoutstanding => 100
250 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
252 $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } ); }
253 'Koha::Exceptions::Account::IsNotDebit',
254 '->apply() rolls back if any of the passed lines is not a debit';
256 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
257 is( $debit_2->discard_changes->amountoutstanding * 1, 10, 'Debit cancelled' );
258 is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
259 is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
261 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
262 $remaining_credit = $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } );
264 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
265 is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' );
266 is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
267 is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
269 $schema->storage->txn_rollback;