ee942527ee977773bee61e8e560db5938b8245c4
[srvgit] / t / db_dependent / Koha / Account / Offsets.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 => 1;
23 use Test::Exception;
24
25 use Koha::Account::Offsets;
26
27 use t::lib::TestBuilder;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 subtest 'total() tests' => sub {
33
34     plan tests => 4;
35
36     $schema->storage->txn_begin;
37
38     my $line = $builder->build_object(
39         {
40             class => 'Koha::Account::Lines',
41             value => { debit_type_code => 'OVERDUE', credit_type_code => undef }
42         }
43     );
44
45     my $amount_1 = 100;
46     my $amount_2 = 200;
47     my $amount_3 = -100;
48     my $amount_4 = -300;
49     my $amount_5 = 500;
50
51     my $offset_1 = Koha::Account::Offset->new(
52         { type => 'OVERDUE_INCREASE', amount => $amount_1, debit_id => $line->id } )->store;
53     my $offset_2 = Koha::Account::Offset->new(
54         { type => 'OVERDUE_INCREASE', amount => $amount_2, debit_id => $line->id } )->store;
55     my $offset_3 = Koha::Account::Offset->new(
56         { type => 'OVERDUE_DECREASE', amount => $amount_3, debit_id => $line->id } )->store;
57     my $offset_4 = Koha::Account::Offset->new(
58         { type => 'OVERDUE_DECREASE', amount => $amount_4, debit_id => $line->id } )->store;
59     my $offset_5 = Koha::Account::Offset->new(
60         { type => 'OVERDUE_INCREASE', amount => $amount_5, debit_id => $line->id } )->store;
61
62     my $debits = Koha::Account::Offsets->search( { type => 'OVERDUE_INCREASE', debit_id => $line->id } );
63     is( $debits->total, $amount_1 + $amount_2 + $amount_5 );
64
65     my $credits = Koha::Account::Offsets->search( { type => 'OVERDUE_DECREASE', debit_id => $line->id } );
66     is( $credits->total, $amount_3 + $amount_4 );
67
68     my $all = Koha::Account::Offsets->search( { debit_id => $line->id } );
69     is( $all->total, $amount_1 + $amount_2 + $amount_3 + $amount_4 + $amount_5 );
70
71     my $none = Koha::Account::Offsets->search( { debit_id => $line->id + 1 } );
72     is( $none->total, 0, 'No offsets, returns 0' );
73
74     $schema->storage->txn_rollback;
75 };