Bug 26172: Fix return value for ->register
[koha.git] / Koha / Cash / Register / Action.pm
1 package Koha::Cash::Register::Action;
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::Object);
25
26 =encoding utf8
27
28 =head1 NAME
29
30 Koha::Cash::Register::Action - Koha cashregister::action Object class
31
32 =head1 API
33
34 =head2 Class methods
35
36 =cut
37
38 =head3 manager
39
40 Return the manager linked to this cash register::action
41
42 =cut
43
44 sub manager {
45     my ($self) = @_;
46     my $rs = $self->_result->manager;
47     return unless $rs;
48     return Koha::Patron->_new_from_dbic($rs);
49 }
50
51 =head3 register
52
53 Return the register linked to this cash register::action
54
55 =cut
56
57 sub register {
58     my ($self) = @_;
59     my $rs = $self->_result->register;
60     return Koha::Cash::Register->_new_from_dbic($rs);
61 }
62
63 =head3 cashup_summary
64
65   my $cashup_summary = $action->cashup_summary;
66
67 Return a hashref containing a summary of transactions that make up this cashup action.
68
69 =cut
70
71 sub cashup_summary {
72     my ($self) = @_;
73     my $summary;
74     my $prior_cashup = Koha::Cash::Register::Actions->search(
75         {
76             'code'      => 'CASHUP',
77             'timestamp' => { '<' => $self->timestamp },
78             register_id => $self->register_id
79         },
80         {
81             order_by => { '-desc' => [ 'timestamp', 'id' ] },
82             rows     => 1
83         }
84     );
85
86     my $previous = $prior_cashup->single;
87
88     my $conditions =
89       $previous
90       ? {
91         'date' => {
92             '-between' =>
93               [ $previous->_result->get_column('timestamp'), $self->timestamp ]
94         }
95       }
96       : { 'date' => { '<' => $self->timestamp } };
97
98     my $outgoing_transactions = $self->register->accountlines->search(
99         { %{$conditions}, credit_type_code => undef },
100     );
101     my $income_transactions = $self->register->accountlines->search(
102         { %{$conditions}, debit_type_code => undef },
103     );
104
105     my $income_summary = Koha::Account::Offsets->search(
106         {
107             'me.credit_id' =>
108               { '-in' => $income_transactions->_resultset->get_column('accountlines_id')->as_query },
109             'me.debit_id' => { '!=' => undef }
110         },
111         {
112             join     => { 'debit' => 'debit_type_code' },
113             group_by => [ 'debit.debit_type_code', 'debit_type_code.description' ],
114             order_by => { '-asc' => 'debit.debit_type_code' },
115             'select' => [ { sum => 'me.amount' }, 'debit.debit_type_code', 'debit_type_code.description' ],
116             'as'     => [ 'total', 'debit_type_code', 'debit_description' ],
117         }
118     );
119
120     my $outgoing_summary = Koha::Account::Offsets->search(
121         {
122             'me.debit_id' =>
123               { '-in' => $outgoing_transactions->_resultset->get_column('accountlines_id')->as_query },
124             'me.credit_id' => { '!=' => undef }
125         },
126         {
127             join     => { 'credit' => 'credit_type_code' },
128             group_by => [ 'credit.credit_type_code', 'credit_type_code.description' ],
129             order_by => { '-asc' => 'credit.credit_type_code' },
130             'select' => [ { sum => 'me.amount' }, 'credit.credit_type_code', 'credit_type_code.description' ],
131             'as'     => [ 'total', 'credit_type_code', 'credit_description' ],
132         }
133     );
134
135     my @income = map {
136         {
137             total           => $_->get_column('total'),
138             debit_type_code => $_->get_column('debit_type_code'),
139             debit_type      => { description => $_->get_column('debit_description') }
140         }
141     } $income_summary->as_list;
142     my @outgoing = map {
143         {
144             total            => $_->get_column('total'),
145             credit_type_code => $_->get_column('credit_type_code'),
146             credit_type      => { description => $_->get_column('credit_description') }
147         }
148     } $outgoing_summary->as_list;
149
150     $summary = {
151         from_date             => $previous ? $previous->timestamp : undef,
152         to_date               => $self->timestamp,
153         income                => \@income,
154         outgoing              => \@outgoing,
155         income_transactions   => $income_transactions,
156         outgoing_transactions => $outgoing_transactions,
157     };
158
159     return $summary;
160 }
161
162 =head2 Internal methods
163
164 =cut
165
166 =head3 _type
167
168 =cut
169
170 sub _type {
171     return 'CashRegisterAction';
172 }
173
174 1;
175
176 =head1 AUTHORS
177
178 Martin Renvoize <martin.renvoize@ptfs-europe.com>
179
180 =cut