Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / Cash / Register / Cashup.pm
1 package Koha::Cash::Register::Cashup;
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 base qw(Koha::Cash::Register::Action);
24
25 =head1 NAME
26
27 Koha::Cash::Register::Actions - Koha Cash Register Action Object set class
28
29 =head1 API
30
31 =head2 Class methods
32
33 =head3 search
34
35     my $cashup = Koha::Cash::Register::Actions::Cashup->search( $where, $attr );
36
37 Returns a list of cash register cashup.
38
39 =cut
40
41 sub search {
42     my ( $self, $where, $attr ) = @_;
43
44     $where->{code} = 'CASHUP';
45
46     unless ( exists $attr->{order_by} ) {
47         $attr->{order_by} =
48           [ { '-asc' => 'register_id' }, { '-desc' => 'timestamp' } ];
49     }
50
51     return $self->SUPER::search( $where, $attr );
52 }
53
54 =head3 summary
55
56   my $summary = $cashup->summary;
57
58 Return a hashref containing a summary of transactions that make up this cashup.
59
60 =cut
61
62 sub summary {
63     my ($self) = @_;
64     my $summary;
65     my $prior_cashup = Koha::Cash::Register::Cashups->search(
66         {
67             'timestamp' => { '<' => $self->timestamp },
68             register_id => $self->register_id
69         },
70         {
71             order_by => { '-desc' => [ 'timestamp', 'id' ] },
72             rows     => 1
73         }
74     );
75
76     my $previous = $prior_cashup->single;
77
78     my $conditions =
79       $previous
80       ? {
81         'date' => {
82             '-between' =>
83               [ $previous->_result->get_column('timestamp'), $self->timestamp ]
84         }
85       }
86       : { 'date' => { '<' => $self->timestamp } };
87
88     my $payout_transactions = $self->register->accountlines->search(
89         { %{$conditions}, credit_type_code => undef },
90     );
91     my $income_transactions = $self->register->accountlines->search(
92         { %{$conditions}, debit_type_code => undef },
93     );
94
95     my $income_summary = Koha::Account::Offsets->search(
96         {
97             'me.credit_id' => {
98                 '-in' => $income_transactions->_resultset->get_column(
99                     'accountlines_id')->as_query
100             },
101             'me.debit_id' => { '!=' => undef }
102         },
103         {
104             join => { 'debit' => 'debit_type_code' },
105             group_by =>
106               [ 'debit.debit_type_code', 'debit_type_code.description' ],
107             'select' => [
108                 { sum => 'me.amount' }, 'debit.debit_type_code',
109                 'debit_type_code.description'
110             ],
111             'as' => [ 'total', 'debit_type_code', 'debit_description' ],
112             order_by => { '-asc' => 'debit_type_code.description' },
113         }
114     );
115
116     my $payout_summary = Koha::Account::Offsets->search(
117         {
118             'me.debit_id' => {
119                 '-in' => $payout_transactions->_resultset->get_column(
120                     'accountlines_id')->as_query
121             },
122             'me.credit_id' => { '!=' => undef }
123         },
124         {
125             join => { 'credit' => 'credit_type_code' },
126             group_by =>
127               [ 'credit.credit_type_code', 'credit_type_code.description' ],
128             'select' => [
129                 { sum => 'me.amount' }, 'credit.credit_type_code',
130                 'credit_type_code.description'
131             ],
132             'as' => [ 'total', 'credit_type_code', 'credit_description' ],
133             order_by => { '-asc' => 'credit_type_code.description' },
134         }
135     );
136
137     my @income = map {
138         {
139             total           => $_->get_column('total') * -1,
140             debit_type_code => $_->get_column('debit_type_code'),
141             debit_type => { description => $_->get_column('debit_description') }
142         }
143     } $income_summary->as_list;
144     my @payout = map {
145         {
146             total            => $_->get_column('total') * -1,
147             credit_type_code => $_->get_column('credit_type_code'),
148             credit_type =>
149               { description => $_->get_column('credit_description') }
150         }
151     } $payout_summary->as_list;
152
153     my $income_total = $income_transactions->total;
154     my $payout_total = $payout_transactions->total;
155     my $total        = ( $income_total + $payout_total );
156
157     my $payment_types = Koha::AuthorisedValues->search(
158         {
159             category => 'PAYMENT_TYPE'
160         },
161         {
162             order_by => ['lib'],
163         }
164     );
165
166     my @total_grouped;
167     for my $type ( $payment_types->as_list ) {
168         my $typed_income = $income_transactions->total( { payment_type => $type->authorised_value } );
169         my $typed_payout = $payout_transactions->total( { payment_type => $type->authorised_value } );
170         my $typed_total = ( $typed_income + $typed_payout );
171         push @total_grouped, { payment_type => $type->lib, total => $typed_total };
172     }
173
174     $summary = {
175         from_date      => $previous ? $previous->timestamp : undef,
176         to_date        => $self->timestamp,
177         income_grouped => \@income,
178         income_total   => abs($income_total),
179         payout_grouped => \@payout,
180         payout_total   => abs($payout_total),
181         total          => $total * -1,
182         total_grouped  => \@total_grouped
183     };
184
185     return $summary;
186 }
187
188 =head3 to_api_mapping
189
190 This method returns the mapping for representing a Koha::Cash::Regiser::Cashup object
191 on the API.
192
193 =cut
194
195 sub to_api_mapping {
196     return {
197         id          => 'cashup_id',
198         register_id => 'cash_register_id',
199         code        => undef
200     };
201 }
202
203 1;
204
205 =head1 AUTHORS
206
207 Martin Renvoize <martin.renvoize@ptfs-europe.com>
208
209 =cut