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

(-)a/Koha/Account/Lines.pm (-3 / +85 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 $me = $self->_resultset()->current_source_alias . ".";
48
    my $lines = $self->search(
49
    my $lines = $self->search(
49
        {},
50
        {},
50
        {
51
        {
51
            select => [ { sum => 'amountoutstanding' } ],
52
            select => [ { sum => $me.'amountoutstanding' } ],
52
            as => ['total_amountoutstanding'],
53
            as     => ['total_amountoutstanding'],
53
        }
54
        }
54
    );
55
    );
55
56
Lines 58-63 sub total_outstanding { Link Here
58
      : 0;
59
      : 0;
59
}
60
}
60
61
62
=head3 total
63
64
    my $lines = Koha::Account::Lines->search({ ...  });
65
    my $total = $lines->total;
66
67
Returns the sum of the amounts of the resultset. If the resultset is
68
empty it returns 0.
69
70
=cut
71
72
sub total {
73
    my ( $self, $conditions ) = @_;
74
75
    $conditions //= {};
76
    my $me = $self->_resultset()->current_source_alias . ".";
77
    my $lines = $self->search(
78
        $conditions,
79
        {
80
            select => [ { sum => $me.'amount' } ],
81
            as     => ['total']
82
        }
83
    );
84
    return $lines->count ? $lines->next->get_column('total') + 0 : 0;
85
}
86
87
=head3 credits_total
88
89
    my $lines = Koha::Account::Lines->search({ ...  });
90
    my $credits_total = $lines->credits_total;
91
92
Returns the sum of the amounts of the resultset. If the resultset is
93
empty it returns 0.
94
95
=cut
96
97
sub credits_total {
98
    my ( $self, $conditions ) = @_;
99
100
    my $me = $self->_resultset()->current_source_alias . ".";
101
    my $local_conditions = { $me.'amount' => { '<' => 0 } };
102
    $conditions //= {};
103
    my $merged_conditions = { %{$conditions}, %{$local_conditions} };
104
105
    my $lines = $self->search(
106
        $merged_conditions,
107
        {
108
            select => [ { sum => $me.'amount' } ],
109
            as     => ['total']
110
        }
111
    );
112
    return $lines->count ? $lines->next->get_column('total') + 0 : 0;
113
}
114
115
=head3 debits_total
116
117
    my $lines = Koha::Account::Lines->search({ ...  });
118
    my $debits_total = $lines->debits_total;
119
120
Returns the sum of the amounts of the resultset. If the resultset is
121
empty it returns 0.
122
123
=cut
124
125
sub debits_total {
126
    my ( $self, $conditions ) = @_;
127
128
    my $me = $self->_resultset()->current_source_alias . ".";
129
    my $local_conditions = { $me.'amount' => { '>' => 0 } };
130
    $conditions //= {};
131
    my $merged_conditions = { %{$conditions}, %{$local_conditions} };
132
133
    my $lines = $self->search(
134
        $merged_conditions,
135
        {
136
            select => [ { sum => $me.'amount' } ],
137
            as     => ['total']
138
        }
139
    );
140
    return $lines->count ? $lines->next->get_column('total') + 0 : 0;
141
}
142
61
=head2 Internal methods
143
=head2 Internal methods
62
144
63
=head3 _type
145
=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