3 # Copyright 2018 Koha Development team
5 # This file is part of Koha
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.
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.
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>
22 use Test::More tests => 2;
24 use Koha::Account::Offsets;
26 use t::lib::TestBuilder;
28 my $schema = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
31 subtest 'total() tests' => sub {
35 $schema->storage->txn_begin;
37 my $line = $builder->build_object(
39 class => 'Koha::Account::Lines',
40 value => { debit_type_code => 'OVERDUE', credit_type_code => undef }
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;
61 my $debits = Koha::Account::Offsets->search( { type => 'OVERDUE_INCREASE', debit_id => $line->id } );
62 is( $debits->total, $amount_1 + $amount_2 + $amount_5 );
64 my $credits = Koha::Account::Offsets->search( { type => 'OVERDUE_DECREASE', debit_id => $line->id } );
65 is( $credits->total, $amount_3 + $amount_4 );
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 );
70 my $none = Koha::Account::Offsets->search( { debit_id => $line->id + 1 } );
71 is( $none->total, 0, 'No offsets, returns 0' );
73 $schema->storage->txn_rollback;
76 subtest 'filter_by_non_reversible() and filter_by_reversible() tests' => sub {
80 $schema->storage->txn_begin;
82 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
83 my $account = $patron->account;
85 my $manual_fee = $account->add_debit({ amount => 11, interface => 'intranet', type => 'MANUAL' });
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' } );
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' );
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' );
104 $schema->storage->txn_rollback;