View | Details | Raw Unified | Return to bug 24255
Collapse All | Expand All

(-)a/Koha/Account/Lines.pm (-2 / +80 lines)
Lines 43-55 empty it returns 0. Link Here
43
=cut
43
=cut
44
44
45
sub total_outstanding {
45
sub total_outstanding {
46
    my ( $self ) = @_;
46
    my ($self) = @_;
47
47
48
    my $lines = $self->search(
48
    my $lines = $self->search(
49
        {},
49
        {},
50
        {
50
        {
51
            select => [ { sum => 'amountoutstanding' } ],
51
            select => [ { sum => 'amountoutstanding' } ],
52
            as => ['total_amountoutstanding'],
52
            as     => ['total_amountoutstanding'],
53
        }
53
        }
54
    );
54
    );
55
55
Lines 58-63 sub total_outstanding { Link Here
58
      : 0;
58
      : 0;
59
}
59
}
60
60
61
=head3 total
62
63
    my $lines = Koha::Account::Lines->search({ ...  });
64
    my $total = $lines->total;
65
66
Returns the sum of the amounts of the resultset. If the resultset is
67
empty it returns 0.
68
69
=cut
70
71
sub total {
72
    my ( $self, $conditions ) = @_;
73
74
    $conditions //= {};
75
    my $lines = $self->search(
76
        $conditions,
77
        {
78
            select => [ { sum => 'me.amount' } ],
79
            as     => ['total']
80
        }
81
    );
82
    return $lines->count ? $lines->next->get_column('total') + 0 : 0;
83
}
84
85
=head3 credits_total
86
87
    my $lines = Koha::Account::Lines->search({ ...  });
88
    my $credits_total = $lines->credits_total;
89
90
Returns the sum of the amounts of the resultset. If the resultset is
91
empty it returns 0.
92
93
=cut
94
95
sub credits_total {
96
    my ( $self, $conditions ) = @_;
97
98
    my $local_conditions = { 'me.amount' => { '<' => 0 } };
99
    $conditions //= {};
100
    my $merged_conditions = { %{$conditions}, %{$local_conditions} };
101
102
    my $lines = $self->search(
103
        $merged_conditions,
104
        {
105
            select => [ { sum => 'me.amount' } ],
106
            as     => ['total']
107
        }
108
    );
109
    return $lines->count ? $lines->next->get_column('total') + 0 : 0;
110
}
111
112
=head3 debits_total
113
114
    my $lines = Koha::Account::Lines->search({ ...  });
115
    my $debits_total = $lines->debits_total;
116
117
Returns the sum of the amounts of the resultset. If the resultset is
118
empty it returns 0.
119
120
=cut
121
122
sub debits_total {
123
    my ( $self, $conditions ) = @_;
124
125
    my $local_conditions = { 'me.amount' => { '>' => 0 } };
126
    $conditions //= {};
127
    my $merged_conditions = { %{$conditions}, %{$local_conditions} };
128
129
    my $lines = $self->search(
130
        $merged_conditions,
131
        {
132
            select => [ { sum => 'me.amount' } ],
133
            as     => ['total']
134
        }
135
    );
136
    return $lines->count ? $lines->next->get_column('total') + 0 : 0;
137
}
138
61
=head2 Internal methods
139
=head2 Internal methods
62
140
63
=head3 _type
141
=head3 _type
(-)a/t/db_dependent/Koha/Account/Lines.t (-2 / +229 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 11;
22
use Test::More tests => 14;
23
use Test::Exception;
23
use Test::Exception;
24
24
25
use C4::Circulation qw/AddIssue AddReturn/;
25
use C4::Circulation qw/AddIssue AddReturn/;
Lines 178-183 subtest 'total_outstanding() tests' => sub { Link Here
178
    $schema->storage->txn_rollback;
178
    $schema->storage->txn_rollback;
179
};
179
};
180
180
181
subtest 'total() tests' => sub {
182
183
    plan tests => 5;
184
185
    $schema->storage->txn_begin;
186
187
    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
188
189
    my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
190
    is( $lines->total, 0, 'total returns 0 if no lines (undef case)' );
191
192
    my $debit_1 = Koha::Account::Line->new(
193
        {   borrowernumber    => $patron->id,
194
            debit_type_code   => "OVERDUE",
195
            status            => "RETURNED",
196
            amount            => 10,
197
            amountoutstanding => 10,
198
            interface         => 'commandline',
199
        }
200
    )->store;
201
202
    my $debit_2 = Koha::Account::Line->new(
203
        {   borrowernumber    => $patron->id,
204
            debit_type_code       => "OVERDUE",
205
            status            => "RETURNED",
206
            amount            => 10,
207
            amountoutstanding => 10,
208
            interface         => 'commandline',
209
        }
210
    )->store;
211
212
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
213
    is( $lines->total, 20, 'total sums correctly' );
214
215
    my $credit_1 = Koha::Account::Line->new(
216
        {   borrowernumber    => $patron->id,
217
            debit_type_code   => "OVERDUE",
218
            status            => "RETURNED",
219
            amount            => -10,
220
            amountoutstanding => -10,
221
            interface         => 'commandline',
222
        }
223
    )->store;
224
225
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
226
    is( $lines->total, 10, 'total sums correctly' );
227
228
    my $credit_2 = Koha::Account::Line->new(
229
        {   borrowernumber    => $patron->id,
230
            debit_type_code   => "OVERDUE",
231
            status            => "RETURNED",
232
            amount            => -10,
233
            amountoutstanding => -10,
234
            interface         => 'commandline',
235
        }
236
    )->store;
237
238
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
239
    is( $lines->total, 0, 'total sums correctly' );
240
241
    my $credit_3 = Koha::Account::Line->new(
242
        {   borrowernumber    => $patron->id,
243
            debit_type_code   => "OVERDUE",
244
            status            => "RETURNED",
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->total, -100, 'total sums correctly' );
253
254
    $schema->storage->txn_rollback;
255
};
256
257
subtest 'credits_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->credits_total, 0, 'credits_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 => 10,
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 => 10,
284
            interface         => 'commandline',
285
        }
286
    )->store;
287
288
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
289
    is( $lines->credits_total, 0, 'credits_total sums correctly' );
290
291
    my $credit_1 = Koha::Account::Line->new(
292
        {   borrowernumber    => $patron->id,
293
            debit_type_code   => "OVERDUE",
294
            status            => "RETURNED",
295
            amount            => -10,
296
            amountoutstanding => -10,
297
            interface         => 'commandline',
298
        }
299
    )->store;
300
301
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
302
    is( $lines->credits_total, -10, 'credits_total sums correctly' );
303
304
    my $credit_2 = Koha::Account::Line->new(
305
        {   borrowernumber    => $patron->id,
306
            debit_type_code   => "OVERDUE",
307
            status            => "RETURNED",
308
            amount            => -10,
309
            amountoutstanding => -10,
310
            interface         => 'commandline',
311
        }
312
    )->store;
313
314
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
315
    is( $lines->credits_total, -20, 'credits_total sums correctly' );
316
317
    my $credit_3 = Koha::Account::Line->new(
318
        {   borrowernumber    => $patron->id,
319
            debit_type_code   => "OVERDUE",
320
            status            => "RETURNED",
321
            amount            => -100,
322
            amountoutstanding => -100,
323
            interface         => 'commandline',
324
        }
325
    )->store;
326
327
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
328
    is( $lines->credits_total, -120, 'credits_total sums correctly' );
329
330
    $schema->storage->txn_rollback;
331
};
332
333
subtest 'debits_total() tests' => sub {
334
335
    plan tests => 5;
336
337
    $schema->storage->txn_begin;
338
339
    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
340
341
    my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
342
    is( $lines->debits_total, 0, 'debits_total returns 0 if no lines (undef case)' );
343
344
    my $debit_1 = Koha::Account::Line->new(
345
        {   borrowernumber    => $patron->id,
346
            debit_type_code   => "OVERDUE",
347
            status            => "RETURNED",
348
            amount            => 10,
349
            amountoutstanding => 0,
350
            interface         => 'commandline',
351
        }
352
    )->store;
353
354
    my $debit_2 = Koha::Account::Line->new(
355
        {   borrowernumber    => $patron->id,
356
            debit_type_code   => "OVERDUE",
357
            status            => "RETURNED",
358
            amount            => 10,
359
            amountoutstanding => 0,
360
            interface         => 'commandline',
361
        }
362
    )->store;
363
364
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
365
    is( $lines->debits_total, 20, 'debits_total sums correctly' );
366
367
    my $credit_1 = Koha::Account::Line->new(
368
        {   borrowernumber    => $patron->id,
369
            debit_type_code   => "OVERDUE",
370
            status            => "RETURNED",
371
            amount            => -10,
372
            amountoutstanding => 0,
373
            interface         => 'commandline',
374
        }
375
    )->store;
376
377
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
378
    is( $lines->debits_total, 20, 'debits_total sums correctly' );
379
380
    my $credit_2 = Koha::Account::Line->new(
381
        {   borrowernumber    => $patron->id,
382
            debit_type_code   => "OVERDUE",
383
            status            => "RETURNED",
384
            amount            => -10,
385
            amountoutstanding => 0,
386
            interface         => 'commandline',
387
        }
388
    )->store;
389
390
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
391
    is( $lines->debits_total, 20, 'debits_total sums correctly' );
392
393
    my $credit_3 = Koha::Account::Line->new(
394
        {   borrowernumber    => $patron->id,
395
            debit_type_code   => "OVERDUE",
396
            status            => "RETURNED",
397
            amount            => -100,
398
            amountoutstanding => 0,
399
            interface         => 'commandline',
400
        }
401
    )->store;
402
403
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
404
    is( $lines->debits_total, 20, 'debits_total sums correctly' );
405
406
    $schema->storage->txn_rollback;
407
};
408
181
subtest 'is_credit() and is_debit() tests' => sub {
409
subtest 'is_credit() and is_debit() tests' => sub {
182
410
183
    plan tests => 4;
411
    plan tests => 4;
184
- 

Return to bug 24255