Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[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             credit_type_code  => "PAYMENT",
75             amount            => -10,
76             amountoutstanding => -10,
77             interface         => 'commandline',
78         }
79     )->store;
80
81     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
82     is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
83
84     my $credit_2 = Koha::Account::Line->new(
85         {   borrowernumber    => $patron->id,
86             credit_type_code  => "PAYMENT",
87             amount            => -10,
88             amountoutstanding => -10,
89             interface         => 'commandline',
90         }
91     )->store;
92
93     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
94     is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
95
96     my $credit_3 = Koha::Account::Line->new(
97         {   borrowernumber    => $patron->id,
98             credit_type_code  => "PAYMENT",
99             amount            => -100,
100             amountoutstanding => -100,
101             interface         => 'commandline',
102         }
103     )->store;
104
105     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
106     is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
107
108     $schema->storage->txn_rollback;
109 };
110
111 subtest 'total() tests' => sub {
112
113     plan tests => 5;
114
115     $schema->storage->txn_begin;
116
117     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
118
119     my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
120     is( $lines->total, 0, 'total returns 0 if no lines (undef case)' );
121
122     my $debit_1 = Koha::Account::Line->new(
123         {   borrowernumber    => $patron->id,
124             debit_type_code   => "OVERDUE",
125             status            => "RETURNED",
126             amount            => 10,
127             amountoutstanding => 10,
128             interface         => 'commandline',
129         }
130     )->store;
131
132     my $debit_2 = Koha::Account::Line->new(
133         {   borrowernumber    => $patron->id,
134             debit_type_code       => "OVERDUE",
135             status            => "RETURNED",
136             amount            => 10,
137             amountoutstanding => 10,
138             interface         => 'commandline',
139         }
140     )->store;
141
142     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
143     is( $lines->total, 20, 'total sums correctly' );
144
145     my $credit_1 = Koha::Account::Line->new(
146         {   borrowernumber    => $patron->id,
147             credit_type_code  => "PAYMENT",
148             amount            => -10,
149             amountoutstanding => -10,
150             interface         => 'commandline',
151         }
152     )->store;
153
154     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
155     is( $lines->total, 10, 'total sums correctly' );
156
157     my $credit_2 = Koha::Account::Line->new(
158         {   borrowernumber    => $patron->id,
159             credit_type_code  => "PAYMENT",
160             amount            => -10,
161             amountoutstanding => -10,
162             interface         => 'commandline',
163         }
164     )->store;
165
166     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
167     is( $lines->total, 0, 'total sums correctly' );
168
169     my $credit_3 = Koha::Account::Line->new(
170         {   borrowernumber    => $patron->id,
171             credit_type_code  => "PAYMENT",
172             amount            => -100,
173             amountoutstanding => -100,
174             interface         => 'commandline',
175         }
176     )->store;
177
178     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
179     is( $lines->total, -100, 'total sums correctly' );
180
181     $schema->storage->txn_rollback;
182 };
183
184 subtest 'credits_total() tests' => sub {
185
186     plan tests => 5;
187
188     $schema->storage->txn_begin;
189
190     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
191
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)' );
194
195     my $debit_1 = Koha::Account::Line->new(
196         {   borrowernumber    => $patron->id,
197             debit_type_code   => "OVERDUE",
198             status            => "RETURNED",
199             amount            => 10,
200             amountoutstanding => 10,
201             interface         => 'commandline',
202         }
203     )->store;
204
205     my $debit_2 = Koha::Account::Line->new(
206         {   borrowernumber    => $patron->id,
207             debit_type_code   => "OVERDUE",
208             status            => "RETURNED",
209             amount            => 10,
210             amountoutstanding => 10,
211             interface         => 'commandline',
212         }
213     )->store;
214
215     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
216     is( $lines->credits_total, 0, 'credits_total sums correctly' );
217
218     my $credit_1 = Koha::Account::Line->new(
219         {   borrowernumber    => $patron->id,
220             credit_type_code  => "PAYMENT",
221             amount            => -10,
222             amountoutstanding => -10,
223             interface         => 'commandline',
224         }
225     )->store;
226
227     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
228     is( $lines->credits_total, -10, 'credits_total sums correctly' );
229
230     my $credit_2 = Koha::Account::Line->new(
231         {   borrowernumber    => $patron->id,
232             credit_type_code  => "PAYMENT",
233             amount            => -10,
234             amountoutstanding => -10,
235             interface         => 'commandline',
236         }
237     )->store;
238
239     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
240     is( $lines->credits_total, -20, 'credits_total sums correctly' );
241
242     my $credit_3 = Koha::Account::Line->new(
243         {   borrowernumber    => $patron->id,
244             credit_type_code  => "PAYMENT",
245             amount            => -100,
246             amountoutstanding => -100,
247             interface         => 'commandline',
248         }
249     )->store;
250
251     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
252     is( $lines->credits_total, -120, 'credits_total sums correctly' );
253
254     $schema->storage->txn_rollback;
255 };
256
257 subtest 'debits_total() tests' => sub {
258
259     plan tests => 5;
260
261     $schema->storage->txn_begin;
262
263     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
264
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)' );
267
268     my $debit_1 = Koha::Account::Line->new(
269         {   borrowernumber    => $patron->id,
270             debit_type_code   => "OVERDUE",
271             status            => "RETURNED",
272             amount            => 10,
273             amountoutstanding => 0,
274             interface         => 'commandline',
275         }
276     )->store;
277
278     my $debit_2 = Koha::Account::Line->new(
279         {   borrowernumber    => $patron->id,
280             debit_type_code   => "OVERDUE",
281             status            => "RETURNED",
282             amount            => 10,
283             amountoutstanding => 0,
284             interface         => 'commandline',
285         }
286     )->store;
287
288     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
289     is( $lines->debits_total, 20, 'debits_total sums correctly' );
290
291     my $credit_1 = Koha::Account::Line->new(
292         {   borrowernumber    => $patron->id,
293             credit_type_code  => "PAYMENT",
294             amount            => -10,
295             amountoutstanding => 0,
296             interface         => 'commandline',
297         }
298     )->store;
299
300     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
301     is( $lines->debits_total, 20, 'debits_total sums correctly' );
302
303     my $credit_2 = Koha::Account::Line->new(
304         {   borrowernumber    => $patron->id,
305             credit_type_code  => "PAYMENT",
306             amount            => -10,
307             amountoutstanding => 0,
308             interface         => 'commandline',
309         }
310     )->store;
311
312     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
313     is( $lines->debits_total, 20, 'debits_total sums correctly' );
314
315     my $credit_3 = Koha::Account::Line->new(
316         {   borrowernumber    => $patron->id,
317             credit_type_code  => "PAYMENT",
318             amount            => -100,
319             amountoutstanding => 0,
320             interface         => 'commandline',
321         }
322     )->store;
323
324     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
325     is( $lines->debits_total, 20, 'debits_total sums correctly' );
326
327     $schema->storage->txn_rollback;
328 };
329
330 1;