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 debit_type_code => "OVERDUE",
77 amountoutstanding => -10,
78 interface => 'commandline',
82 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
83 is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
85 my $credit_2 = Koha::Account::Line->new(
86 { borrowernumber => $patron->id,
87 debit_type_code => "OVERDUE",
90 amountoutstanding => -10,
91 interface => 'commandline',
95 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
96 is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
98 my $credit_3 = Koha::Account::Line->new(
99 { borrowernumber => $patron->id,
100 debit_type_code => "OVERDUE",
101 status => "RETURNED",
103 amountoutstanding => -100,
104 interface => 'commandline',
108 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
109 is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
111 $schema->storage->txn_rollback;
114 subtest 'total() tests' => sub {
118 $schema->storage->txn_begin;
120 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
122 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
123 is( $lines->total, 0, 'total returns 0 if no lines (undef case)' );
125 my $debit_1 = Koha::Account::Line->new(
126 { borrowernumber => $patron->id,
127 debit_type_code => "OVERDUE",
128 status => "RETURNED",
130 amountoutstanding => 10,
131 interface => 'commandline',
135 my $debit_2 = Koha::Account::Line->new(
136 { borrowernumber => $patron->id,
137 debit_type_code => "OVERDUE",
138 status => "RETURNED",
140 amountoutstanding => 10,
141 interface => 'commandline',
145 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
146 is( $lines->total, 20, 'total sums correctly' );
148 my $credit_1 = Koha::Account::Line->new(
149 { borrowernumber => $patron->id,
150 debit_type_code => "OVERDUE",
151 status => "RETURNED",
153 amountoutstanding => -10,
154 interface => 'commandline',
158 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
159 is( $lines->total, 10, 'total sums correctly' );
161 my $credit_2 = Koha::Account::Line->new(
162 { borrowernumber => $patron->id,
163 debit_type_code => "OVERDUE",
164 status => "RETURNED",
166 amountoutstanding => -10,
167 interface => 'commandline',
171 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
172 is( $lines->total, 0, 'total sums correctly' );
174 my $credit_3 = Koha::Account::Line->new(
175 { borrowernumber => $patron->id,
176 debit_type_code => "OVERDUE",
177 status => "RETURNED",
179 amountoutstanding => -100,
180 interface => 'commandline',
184 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
185 is( $lines->total, -100, 'total sums correctly' );
187 $schema->storage->txn_rollback;
190 subtest 'credits_total() tests' => sub {
194 $schema->storage->txn_begin;
196 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
198 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
199 is( $lines->credits_total, 0, 'credits_total returns 0 if no lines (undef case)' );
201 my $debit_1 = Koha::Account::Line->new(
202 { borrowernumber => $patron->id,
203 debit_type_code => "OVERDUE",
204 status => "RETURNED",
206 amountoutstanding => 10,
207 interface => 'commandline',
211 my $debit_2 = Koha::Account::Line->new(
212 { borrowernumber => $patron->id,
213 debit_type_code => "OVERDUE",
214 status => "RETURNED",
216 amountoutstanding => 10,
217 interface => 'commandline',
221 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
222 is( $lines->credits_total, 0, 'credits_total sums correctly' );
224 my $credit_1 = Koha::Account::Line->new(
225 { borrowernumber => $patron->id,
226 debit_type_code => "OVERDUE",
227 status => "RETURNED",
229 amountoutstanding => -10,
230 interface => 'commandline',
234 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
235 is( $lines->credits_total, -10, 'credits_total sums correctly' );
237 my $credit_2 = Koha::Account::Line->new(
238 { borrowernumber => $patron->id,
239 debit_type_code => "OVERDUE",
240 status => "RETURNED",
242 amountoutstanding => -10,
243 interface => 'commandline',
247 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
248 is( $lines->credits_total, -20, 'credits_total sums correctly' );
250 my $credit_3 = Koha::Account::Line->new(
251 { borrowernumber => $patron->id,
252 debit_type_code => "OVERDUE",
253 status => "RETURNED",
255 amountoutstanding => -100,
256 interface => 'commandline',
260 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
261 is( $lines->credits_total, -120, 'credits_total sums correctly' );
263 $schema->storage->txn_rollback;
266 subtest 'debits_total() tests' => sub {
270 $schema->storage->txn_begin;
272 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
274 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
275 is( $lines->debits_total, 0, 'debits_total returns 0 if no lines (undef case)' );
277 my $debit_1 = Koha::Account::Line->new(
278 { borrowernumber => $patron->id,
279 debit_type_code => "OVERDUE",
280 status => "RETURNED",
282 amountoutstanding => 0,
283 interface => 'commandline',
287 my $debit_2 = Koha::Account::Line->new(
288 { borrowernumber => $patron->id,
289 debit_type_code => "OVERDUE",
290 status => "RETURNED",
292 amountoutstanding => 0,
293 interface => 'commandline',
297 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
298 is( $lines->debits_total, 20, 'debits_total sums correctly' );
300 my $credit_1 = Koha::Account::Line->new(
301 { borrowernumber => $patron->id,
302 debit_type_code => "OVERDUE",
303 status => "RETURNED",
305 amountoutstanding => 0,
306 interface => 'commandline',
310 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
311 is( $lines->debits_total, 20, 'debits_total sums correctly' );
313 my $credit_2 = Koha::Account::Line->new(
314 { borrowernumber => $patron->id,
315 debit_type_code => "OVERDUE",
316 status => "RETURNED",
318 amountoutstanding => 0,
319 interface => 'commandline',
323 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
324 is( $lines->debits_total, 20, 'debits_total sums correctly' );
326 my $credit_3 = Koha::Account::Line->new(
327 { borrowernumber => $patron->id,
328 debit_type_code => "OVERDUE",
329 status => "RETURNED",
331 amountoutstanding => 0,
332 interface => 'commandline',
336 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
337 is( $lines->debits_total, 20, 'debits_total sums correctly' );
339 $schema->storage->txn_rollback;