Bug 18501: (QA follow-up) Add test of *_on functionality
[koha.git] / t / db_dependent / Koha / Account / Lines.t
1 #!/usr/bin/perl
2
3 # Copyright 2018 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>
19
20 use Modern::Perl;
21
22 use Test::More tests => 4;
23 use Test::Exception;
24 use Test::MockModule;
25
26 use DateTime;
27
28 use Koha::Account;
29 use Koha::Account::Lines;
30 use Koha::Account::Offsets;
31
32 use t::lib::Mocks;
33 use t::lib::TestBuilder;
34
35 my $schema = Koha::Database->new->schema;
36 my $builder = t::lib::TestBuilder->new;
37
38 subtest 'total_outstanding() tests' => sub {
39
40     plan tests => 5;
41
42     $schema->storage->txn_begin;
43
44     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
45
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)' );
48
49     my $debit_1 = Koha::Account::Line->new(
50         {   borrowernumber    => $patron->id,
51             debit_type_code   => "OVERDUE",
52             status            => "RETURNED",
53             amount            => 10,
54             amountoutstanding => 10,
55             interface         => 'commandline',
56         }
57     )->store;
58
59     my $debit_2 = Koha::Account::Line->new(
60         {   borrowernumber    => $patron->id,
61             debit_type_code   => "OVERDUE",
62             status            => "RETURNED",
63             amount            => 10,
64             amountoutstanding => 10,
65             interface         => 'commandline',
66         }
67     )->store;
68
69     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
70     is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
71
72     my $credit_1 = Koha::Account::Line->new(
73         {   borrowernumber    => $patron->id,
74             debit_type_code   => "OVERDUE",
75             status            => "RETURNED",
76             amount            => -10,
77             amountoutstanding => -10,
78             interface         => 'commandline',
79         }
80     )->store;
81
82     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
83     is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
84
85     my $credit_2 = Koha::Account::Line->new(
86         {   borrowernumber    => $patron->id,
87             debit_type_code   => "OVERDUE",
88             status            => "RETURNED",
89             amount            => -10,
90             amountoutstanding => -10,
91             interface         => 'commandline',
92         }
93     )->store;
94
95     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
96     is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
97
98     my $credit_3 = Koha::Account::Line->new(
99         {   borrowernumber    => $patron->id,
100             debit_type_code   => "OVERDUE",
101             status            => "RETURNED",
102             amount            => -100,
103             amountoutstanding => -100,
104             interface         => 'commandline',
105         }
106     )->store;
107
108     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
109     is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
110
111     $schema->storage->txn_rollback;
112 };
113
114 subtest 'total() tests' => sub {
115
116     plan tests => 5;
117
118     $schema->storage->txn_begin;
119
120     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
121
122     my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
123     is( $lines->total, 0, 'total returns 0 if no lines (undef case)' );
124
125     my $debit_1 = Koha::Account::Line->new(
126         {   borrowernumber    => $patron->id,
127             debit_type_code   => "OVERDUE",
128             status            => "RETURNED",
129             amount            => 10,
130             amountoutstanding => 10,
131             interface         => 'commandline',
132         }
133     )->store;
134
135     my $debit_2 = Koha::Account::Line->new(
136         {   borrowernumber    => $patron->id,
137             debit_type_code       => "OVERDUE",
138             status            => "RETURNED",
139             amount            => 10,
140             amountoutstanding => 10,
141             interface         => 'commandline',
142         }
143     )->store;
144
145     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
146     is( $lines->total, 20, 'total sums correctly' );
147
148     my $credit_1 = Koha::Account::Line->new(
149         {   borrowernumber    => $patron->id,
150             debit_type_code   => "OVERDUE",
151             status            => "RETURNED",
152             amount            => -10,
153             amountoutstanding => -10,
154             interface         => 'commandline',
155         }
156     )->store;
157
158     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
159     is( $lines->total, 10, 'total sums correctly' );
160
161     my $credit_2 = Koha::Account::Line->new(
162         {   borrowernumber    => $patron->id,
163             debit_type_code   => "OVERDUE",
164             status            => "RETURNED",
165             amount            => -10,
166             amountoutstanding => -10,
167             interface         => 'commandline',
168         }
169     )->store;
170
171     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
172     is( $lines->total, 0, 'total sums correctly' );
173
174     my $credit_3 = Koha::Account::Line->new(
175         {   borrowernumber    => $patron->id,
176             debit_type_code   => "OVERDUE",
177             status            => "RETURNED",
178             amount            => -100,
179             amountoutstanding => -100,
180             interface         => 'commandline',
181         }
182     )->store;
183
184     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
185     is( $lines->total, -100, 'total sums correctly' );
186
187     $schema->storage->txn_rollback;
188 };
189
190 subtest 'credits_total() tests' => sub {
191
192     plan tests => 5;
193
194     $schema->storage->txn_begin;
195
196     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
197
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)' );
200
201     my $debit_1 = Koha::Account::Line->new(
202         {   borrowernumber    => $patron->id,
203             debit_type_code   => "OVERDUE",
204             status            => "RETURNED",
205             amount            => 10,
206             amountoutstanding => 10,
207             interface         => 'commandline',
208         }
209     )->store;
210
211     my $debit_2 = Koha::Account::Line->new(
212         {   borrowernumber    => $patron->id,
213             debit_type_code   => "OVERDUE",
214             status            => "RETURNED",
215             amount            => 10,
216             amountoutstanding => 10,
217             interface         => 'commandline',
218         }
219     )->store;
220
221     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
222     is( $lines->credits_total, 0, 'credits_total sums correctly' );
223
224     my $credit_1 = Koha::Account::Line->new(
225         {   borrowernumber    => $patron->id,
226             debit_type_code   => "OVERDUE",
227             status            => "RETURNED",
228             amount            => -10,
229             amountoutstanding => -10,
230             interface         => 'commandline',
231         }
232     )->store;
233
234     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
235     is( $lines->credits_total, -10, 'credits_total sums correctly' );
236
237     my $credit_2 = Koha::Account::Line->new(
238         {   borrowernumber    => $patron->id,
239             debit_type_code   => "OVERDUE",
240             status            => "RETURNED",
241             amount            => -10,
242             amountoutstanding => -10,
243             interface         => 'commandline',
244         }
245     )->store;
246
247     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
248     is( $lines->credits_total, -20, 'credits_total sums correctly' );
249
250     my $credit_3 = Koha::Account::Line->new(
251         {   borrowernumber    => $patron->id,
252             debit_type_code   => "OVERDUE",
253             status            => "RETURNED",
254             amount            => -100,
255             amountoutstanding => -100,
256             interface         => 'commandline',
257         }
258     )->store;
259
260     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
261     is( $lines->credits_total, -120, 'credits_total sums correctly' );
262
263     $schema->storage->txn_rollback;
264 };
265
266 subtest 'debits_total() tests' => sub {
267
268     plan tests => 5;
269
270     $schema->storage->txn_begin;
271
272     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
273
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)' );
276
277     my $debit_1 = Koha::Account::Line->new(
278         {   borrowernumber    => $patron->id,
279             debit_type_code   => "OVERDUE",
280             status            => "RETURNED",
281             amount            => 10,
282             amountoutstanding => 0,
283             interface         => 'commandline',
284         }
285     )->store;
286
287     my $debit_2 = Koha::Account::Line->new(
288         {   borrowernumber    => $patron->id,
289             debit_type_code   => "OVERDUE",
290             status            => "RETURNED",
291             amount            => 10,
292             amountoutstanding => 0,
293             interface         => 'commandline',
294         }
295     )->store;
296
297     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
298     is( $lines->debits_total, 20, 'debits_total sums correctly' );
299
300     my $credit_1 = Koha::Account::Line->new(
301         {   borrowernumber    => $patron->id,
302             debit_type_code   => "OVERDUE",
303             status            => "RETURNED",
304             amount            => -10,
305             amountoutstanding => 0,
306             interface         => 'commandline',
307         }
308     )->store;
309
310     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
311     is( $lines->debits_total, 20, 'debits_total sums correctly' );
312
313     my $credit_2 = Koha::Account::Line->new(
314         {   borrowernumber    => $patron->id,
315             debit_type_code   => "OVERDUE",
316             status            => "RETURNED",
317             amount            => -10,
318             amountoutstanding => 0,
319             interface         => 'commandline',
320         }
321     )->store;
322
323     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
324     is( $lines->debits_total, 20, 'debits_total sums correctly' );
325
326     my $credit_3 = Koha::Account::Line->new(
327         {   borrowernumber    => $patron->id,
328             debit_type_code   => "OVERDUE",
329             status            => "RETURNED",
330             amount            => -100,
331             amountoutstanding => 0,
332             interface         => 'commandline',
333         }
334     )->store;
335
336     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
337     is( $lines->debits_total, 20, 'debits_total sums correctly' );
338
339     $schema->storage->txn_rollback;
340 };
341
342 1;