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