Bug 22435: ->apply() should always use 'APPLY' for offset_type
[koha.git] / Koha / Account / Lines.pm
1 package Koha::Account::Lines;
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 use Koha::Account::Line;
23
24 use base qw(Koha::Objects);
25
26 =head1 NAME
27
28 Koha::Account::Lines - Koha Account Line Object set class
29
30 =head1 API
31
32 =head2 Class Methods
33
34 =head3 total_outstanding
35
36     my $lines = Koha::Account::Lines->search({ ...  });
37     my $total = $lines->total_outstanding;
38
39 Returns the sum of the outstanding amounts of the resultset. If the resultset is
40 empty it returns 0.
41
42 =cut
43
44 sub total_outstanding {
45     my ($self) = @_;
46
47     my $me = $self->_resultset()->current_source_alias . ".";
48     my $lines = $self->search(
49         {},
50         {
51             select => [ { sum => $me.'amountoutstanding' } ],
52             as     => ['total_amountoutstanding'],
53         }
54     );
55
56     return $lines->count
57       ? $lines->next->get_column('total_amountoutstanding') + 0
58       : 0;
59 }
60
61 =head3 total
62
63     my $lines = Koha::Account::Lines->search({ ...  });
64     my $total = $lines->total;
65
66 Returns the sum of the amounts of the resultset. If the resultset is
67 empty it returns 0.
68
69 =cut
70
71 sub total {
72     my ( $self, $conditions ) = @_;
73
74     $conditions //= {};
75     my $me = $self->_resultset()->current_source_alias . ".";
76     my $lines = $self->search(
77         $conditions,
78         {
79             select => [ { sum => $me.'amount' } ],
80             as     => ['total']
81         }
82     );
83     return $lines->count ? $lines->next->get_column('total') + 0 : 0;
84 }
85
86 =head3 credits_total
87
88     my $lines = Koha::Account::Lines->search({ ...  });
89     my $credits_total = $lines->credits_total;
90
91 Returns the sum of the amounts of the resultset. If the resultset is
92 empty it returns 0.
93
94 =cut
95
96 sub credits_total {
97     my ( $self, $conditions ) = @_;
98
99     my $me = $self->_resultset()->current_source_alias . ".";
100     my $local_conditions = { $me.'amount' => { '<' => 0 } };
101     $conditions //= {};
102     my $merged_conditions = { %{$conditions}, %{$local_conditions} };
103
104     my $lines = $self->search(
105         $merged_conditions,
106         {
107             select => [ { sum => $me.'amount' } ],
108             as     => ['total']
109         }
110     );
111     return $lines->count ? $lines->next->get_column('total') + 0 : 0;
112 }
113
114 =head3 debits_total
115
116     my $lines = Koha::Account::Lines->search({ ...  });
117     my $debits_total = $lines->debits_total;
118
119 Returns the sum of the amounts of the resultset. If the resultset is
120 empty it returns 0.
121
122 =cut
123
124 sub debits_total {
125     my ( $self, $conditions ) = @_;
126
127     my $me = $self->_resultset()->current_source_alias . ".";
128     my $local_conditions = { $me.'amount' => { '>' => 0 } };
129     $conditions //= {};
130     my $merged_conditions = { %{$conditions}, %{$local_conditions} };
131
132     my $lines = $self->search(
133         $merged_conditions,
134         {
135             select => [ { sum => $me.'amount' } ],
136             as     => ['total']
137         }
138     );
139     return $lines->count ? $lines->next->get_column('total') + 0 : 0;
140 }
141
142 =head2 Internal methods
143
144 =head3 _type
145
146 =cut
147
148 sub _type {
149     return 'Accountline';
150 }
151
152 sub object_class {
153     return 'Koha::Account::Line';
154 }
155
156 1;