Bug 32496: Some fixes
[koha.git] / Koha / Account / Offsets.pm
1 package Koha::Account::Offsets;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20
21 use Koha::Database;
22
23 use Koha::Account::Offset;
24
25 use base qw(Koha::Objects);
26
27 =head1 NAME
28
29 Koha::Account::Offsets - Koha Account Offset Object set class
30
31 Account offsets track the changes made to the balance of account lines
32
33 =head1 API
34
35 =head2 Class methods
36
37 =head3 total
38
39     my $offsets = Koha::Account::Offsets->search({ ...  });
40     my $total   = $offsets->total;
41
42 Returns the sum of the amounts of the account offsets resultset. If the resultset is
43 empty it returns 0.
44
45 =cut
46
47 sub total {
48     my ($self) = @_;
49
50     my $offsets = $self->search(
51         {},
52         {
53             select => [ { sum => 'me.amount' } ],
54             as     => ['total_amount'],
55         }
56     );
57
58     return $offsets->count
59       ? $offsets->next->get_column('total_amount') + 0
60       : 0;
61 }
62
63 =head3 filter_by_non_reversible
64
65     my $non_reversible_offsets = Koha::Account::Offsets->search(..)->filter_by_non_reversible;
66
67 Filter offsets so only non-reversible ones are left in the resultset.
68
69 =cut
70
71 sub filter_by_non_reversible {
72     my ($self) = @_;
73
74     my $me = $self->_resultset()->current_source_alias;
75
76     my $where = {
77         debit_id                  => { '!='      => undef },
78         credit_id                 => { '!='      => undef },
79         type                      => 'APPLY',
80         'credit.credit_type_code' => [ 'WRITEOFF', 'DISCOUNT', 'CANCELLATION' ],
81         'credit.status'           => [ { '!=' => 'VOID' }, undef ],
82         $me . '.amount'           => { '<' => 0 }
83     };
84     my $attr = { join => 'credit' };
85
86     return $self->search( $where, $attr );
87 }
88
89 =head3 filter_by_reversible
90
91     my $reversible_offsets = Koha::Account::Offsets->search(..)->filter_by_reversible;
92
93 Filter offsets so only reversible ones are left in the resultset.
94
95 =cut
96
97 sub filter_by_reversible {
98     my ($self) = @_;
99
100     my $me = $self->_resultset()->current_source_alias;
101
102     my $where = {
103         debit_id                  => { '!='      => undef },
104         credit_id                 => { '!='      => undef },
105         type                      => 'APPLY',
106         'credit.credit_type_code' => { -not_in => [ 'WRITEOFF', 'DISCOUNT', 'CANCELLATION' ] },
107         'credit.status'           => [ { '!=' => 'VOID' }, undef ],
108         $me . '.amount'           => { '<' => 0 }
109     };
110     my $attr = { join => 'credit' };
111
112     return $self->search( $where, $attr );
113 }
114
115 =head2 Internal methods
116
117 =head3 _type
118
119 =cut
120
121 sub _type {
122     return 'AccountOffset';
123 }
124
125 =head3 object_class
126
127 =cut
128
129 sub object_class {
130     return 'Koha::Account::Offset';
131 }
132
133 1;