Bug 35086: Add chunk_size option to elasticsearch configuration
[koha.git] / 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 => 2;
23
24 use Koha::Account::Offsets;
25
26 use t::lib::TestBuilder;
27
28 my $schema  = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
30
31 subtest 'total() tests' => sub {
32
33     plan tests => 4;
34
35     $schema->storage->txn_begin;
36
37     my $line = $builder->build_object(
38         {
39             class => 'Koha::Account::Lines',
40             value => { debit_type_code => 'OVERDUE', credit_type_code => undef }
41         }
42     );
43
44     my $amount_1 = 100;
45     my $amount_2 = 200;
46     my $amount_3 = -100;
47     my $amount_4 = -300;
48     my $amount_5 = 500;
49
50     my $offset_1 = Koha::Account::Offset->new(
51         { type => 'OVERDUE_INCREASE', amount => $amount_1, debit_id => $line->id } )->store;
52     my $offset_2 = Koha::Account::Offset->new(
53         { type => 'OVERDUE_INCREASE', amount => $amount_2, debit_id => $line->id } )->store;
54     my $offset_3 = Koha::Account::Offset->new(
55         { type => 'OVERDUE_DECREASE', amount => $amount_3, debit_id => $line->id } )->store;
56     my $offset_4 = Koha::Account::Offset->new(
57         { type => 'OVERDUE_DECREASE', amount => $amount_4, debit_id => $line->id } )->store;
58     my $offset_5 = Koha::Account::Offset->new(
59         { type => 'OVERDUE_INCREASE', amount => $amount_5, debit_id => $line->id } )->store;
60
61     my $debits = Koha::Account::Offsets->search( { type => 'OVERDUE_INCREASE', debit_id => $line->id } );
62     is( $debits->total, $amount_1 + $amount_2 + $amount_5 );
63
64     my $credits = Koha::Account::Offsets->search( { type => 'OVERDUE_DECREASE', debit_id => $line->id } );
65     is( $credits->total, $amount_3 + $amount_4 );
66
67     my $all = Koha::Account::Offsets->search( { debit_id => $line->id } );
68     is( $all->total, $amount_1 + $amount_2 + $amount_3 + $amount_4 + $amount_5 );
69
70     my $none = Koha::Account::Offsets->search( { debit_id => $line->id + 1 } );
71     is( $none->total, 0, 'No offsets, returns 0' );
72
73     $schema->storage->txn_rollback;
74 };
75
76 subtest 'filter_by_non_reversible() and filter_by_reversible() tests' => sub {
77
78     plan tests => 4;
79
80     $schema->storage->txn_begin;
81
82     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
83     my $account = $patron->account;
84
85     my $manual_fee = $account->add_debit({ amount => 11, interface => 'intranet', type => 'MANUAL' });
86
87     $account->pay( { amount => 1, type => 'WRITEOFF' } );
88     $account->pay( { amount => 2, type => 'DISCOUNT' } );
89     $account->pay( { amount => 3, type => 'CANCELLATION' } );
90     $account->pay( { amount => 4, type => 'PAYMENT' } );
91     $account->pay( { amount => 5, type => 'CREDIT' } );
92
93     # non-reversible offsets
94     is( $manual_fee->debit_offsets->filter_by_non_reversible->count,
95         3, '3 non-reversible offsets' );
96     is( $manual_fee->debit_offsets->filter_by_non_reversible->total,
97         -6, '-6 the total amount of the non-reversible offsets' );
98     # reversible offsets
99     is( $manual_fee->debit_offsets->filter_by_reversible->count,
100         2, 'The right reversible offsets count' );
101     is( $manual_fee->debit_offsets->filter_by_reversible->total,
102         -5, 'The right total amount of the reversible offsets' );
103
104     $schema->storage->txn_rollback;
105 };