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;
29 use Koha::Account::Lines;
30 use Koha::Account::Offsets;
33 use t::lib::TestBuilder;
35 my $schema = Koha::Database->new->schema;
36 my $builder = t::lib::TestBuilder->new;
38 subtest 'total_outstanding() tests' => sub {
42 $schema->storage->txn_begin;
44 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
46 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
47 is( $lines->total_outstanding, 0, 'total_outstanding returns 0 if no lines (undef case)' );
49 my $debit_1 = Koha::Account::Line->new(
50 { borrowernumber => $patron->id,
51 debit_type_code => "OVERDUE",
54 amountoutstanding => 10,
55 interface => 'commandline',
59 my $debit_2 = Koha::Account::Line->new(
60 { borrowernumber => $patron->id,
61 debit_type_code => "OVERDUE",
64 amountoutstanding => 10,
65 interface => 'commandline',
69 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
70 is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
72 my $credit_1 = Koha::Account::Line->new(
73 { borrowernumber => $patron->id,
74 credit_type_code => "PAYMENT",
76 amountoutstanding => -10,
77 interface => 'commandline',
81 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
82 is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
84 my $credit_2 = Koha::Account::Line->new(
85 { borrowernumber => $patron->id,
86 credit_type_code => "PAYMENT",
88 amountoutstanding => -10,
89 interface => 'commandline',
93 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
94 is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
96 my $credit_3 = Koha::Account::Line->new(
97 { borrowernumber => $patron->id,
98 credit_type_code => "PAYMENT",
100 amountoutstanding => -100,
101 interface => 'commandline',
105 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
106 is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
108 $schema->storage->txn_rollback;
111 subtest 'total() tests' => sub {
115 $schema->storage->txn_begin;
117 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
119 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
120 is( $lines->total, 0, 'total returns 0 if no lines (undef case)' );
122 my $debit_1 = Koha::Account::Line->new(
123 { borrowernumber => $patron->id,
124 debit_type_code => "OVERDUE",
125 status => "RETURNED",
127 amountoutstanding => 10,
128 interface => 'commandline',
132 my $debit_2 = Koha::Account::Line->new(
133 { borrowernumber => $patron->id,
134 debit_type_code => "OVERDUE",
135 status => "RETURNED",
137 amountoutstanding => 10,
138 interface => 'commandline',
142 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
143 is( $lines->total, 20, 'total sums correctly' );
145 my $credit_1 = Koha::Account::Line->new(
146 { borrowernumber => $patron->id,
147 credit_type_code => "PAYMENT",
149 amountoutstanding => -10,
150 interface => 'commandline',
154 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
155 is( $lines->total, 10, 'total sums correctly' );
157 my $credit_2 = Koha::Account::Line->new(
158 { borrowernumber => $patron->id,
159 credit_type_code => "PAYMENT",
161 amountoutstanding => -10,
162 interface => 'commandline',
166 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
167 is( $lines->total, 0, 'total sums correctly' );
169 my $credit_3 = Koha::Account::Line->new(
170 { borrowernumber => $patron->id,
171 credit_type_code => "PAYMENT",
173 amountoutstanding => -100,
174 interface => 'commandline',
178 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
179 is( $lines->total, -100, 'total sums correctly' );
181 $schema->storage->txn_rollback;
184 subtest 'credits_total() tests' => sub {
188 $schema->storage->txn_begin;
190 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
192 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
193 is( $lines->credits_total, 0, 'credits_total returns 0 if no lines (undef case)' );
195 my $debit_1 = Koha::Account::Line->new(
196 { borrowernumber => $patron->id,
197 debit_type_code => "OVERDUE",
198 status => "RETURNED",
200 amountoutstanding => 10,
201 interface => 'commandline',
205 my $debit_2 = Koha::Account::Line->new(
206 { borrowernumber => $patron->id,
207 debit_type_code => "OVERDUE",
208 status => "RETURNED",
210 amountoutstanding => 10,
211 interface => 'commandline',
215 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
216 is( $lines->credits_total, 0, 'credits_total sums correctly' );
218 my $credit_1 = Koha::Account::Line->new(
219 { borrowernumber => $patron->id,
220 credit_type_code => "PAYMENT",
222 amountoutstanding => -10,
223 interface => 'commandline',
227 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
228 is( $lines->credits_total, -10, 'credits_total sums correctly' );
230 my $credit_2 = Koha::Account::Line->new(
231 { borrowernumber => $patron->id,
232 credit_type_code => "PAYMENT",
234 amountoutstanding => -10,
235 interface => 'commandline',
239 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
240 is( $lines->credits_total, -20, 'credits_total sums correctly' );
242 my $credit_3 = Koha::Account::Line->new(
243 { borrowernumber => $patron->id,
244 credit_type_code => "PAYMENT",
246 amountoutstanding => -100,
247 interface => 'commandline',
251 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
252 is( $lines->credits_total, -120, 'credits_total sums correctly' );
254 $schema->storage->txn_rollback;
257 subtest 'debits_total() tests' => sub {
261 $schema->storage->txn_begin;
263 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
265 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
266 is( $lines->debits_total, 0, 'debits_total returns 0 if no lines (undef case)' );
268 my $debit_1 = Koha::Account::Line->new(
269 { borrowernumber => $patron->id,
270 debit_type_code => "OVERDUE",
271 status => "RETURNED",
273 amountoutstanding => 0,
274 interface => 'commandline',
278 my $debit_2 = Koha::Account::Line->new(
279 { borrowernumber => $patron->id,
280 debit_type_code => "OVERDUE",
281 status => "RETURNED",
283 amountoutstanding => 0,
284 interface => 'commandline',
288 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
289 is( $lines->debits_total, 20, 'debits_total sums correctly' );
291 my $credit_1 = Koha::Account::Line->new(
292 { borrowernumber => $patron->id,
293 credit_type_code => "PAYMENT",
295 amountoutstanding => 0,
296 interface => 'commandline',
300 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
301 is( $lines->debits_total, 20, 'debits_total sums correctly' );
303 my $credit_2 = Koha::Account::Line->new(
304 { borrowernumber => $patron->id,
305 credit_type_code => "PAYMENT",
307 amountoutstanding => 0,
308 interface => 'commandline',
312 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
313 is( $lines->debits_total, 20, 'debits_total sums correctly' );
315 my $credit_3 = Koha::Account::Line->new(
316 { borrowernumber => $patron->id,
317 credit_type_code => "PAYMENT",
319 amountoutstanding => 0,
320 interface => 'commandline',
324 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
325 is( $lines->debits_total, 20, 'debits_total sums correctly' );
327 $schema->storage->txn_rollback;