Bug 26172: Add cashup summary view modal
[koha.git] / Koha / Cash / Register / Action.pm
blob23b9f6ffb31bb2b204ba82e969a6ebbac01383b4
1 package Koha::Cash::Register::Action;
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Carp;
22 use Koha::Database;
24 use base qw(Koha::Object);
26 =encoding utf8
28 =head1 NAME
30 Koha::Cash::Register::Action - Koha cashregister::action Object class
32 =head1 API
34 =head2 Class methods
36 =cut
38 =head3 manager
40 Return the manager linked to this cash register::action
42 =cut
44 sub manager {
45 my ($self) = @_;
46 my $rs = $self->_result->manager;
47 return unless $rs;
48 return Koha::Patron->_new_from_dbic($rs);
51 =head3 register
53 Return the register linked to this cash register::action
55 =cut
57 sub register {
58 my ($self) = @_;
59 my $rs = $self->_result->register;
60 return unless $rs;
61 return Koha::Cash::Register->_new_from_dbic($rs);
64 =head3 cashup_summary
66 my $cashup_summary = $action->cashup_summary;
68 Return a hashref containing a summary of transactions that make up this cashup action.
70 =cut
72 sub cashup_summary {
73 my ($self) = @_;
74 my $summary;
75 my $prior_cashup = Koha::Cash::Register::Actions->search(
77 'code' => 'CASHUP',
78 'timestamp' => { '<' => $self->timestamp },
79 register_id => $self->register_id
82 order_by => { '-desc' => [ 'timestamp', 'id' ] },
83 rows => 1
87 my $previous = $prior_cashup->single;
89 my $conditions =
90 $previous
91 ? {
92 'date' => {
93 '-between' =>
94 [ $previous->_result->get_column('timestamp'), $self->timestamp ]
97 : { 'date' => { '<' => $self->timestamp } };
99 my $outgoing_transactions = $self->register->accountlines->search(
100 { %{$conditions}, credit_type_code => undef },
101 { select => 'accountlines_id' } );
102 my $income_transactions = $self->register->accountlines->search(
103 { %{$conditions}, debit_type_code => undef },
104 { select => 'accountlines_id' } );
106 my $income_summary = Koha::Account::Offsets->search(
108 'me.credit_id' =>
109 { '-in' => $income_transactions->_resultset->as_query },
110 'me.debit_id' => { '!=' => undef }
113 join => { 'debit' => 'debit_type_code' },
114 group_by => [ 'debit.debit_type_code', 'debit_type_code.description' ],
115 'select' => [ { sum => 'me.amount' }, 'debit.debit_type_code', 'debit_type_code.description' ],
116 'as' => [ 'total', 'debit_type_code', 'debit_description' ],
120 my $outgoing_summary = Koha::Account::Offsets->search(
122 'me.debit_id' =>
123 { '-in' => $outgoing_transactions->_resultset->as_query },
124 'me.credit_id' => { '!=' => undef }
127 join => { 'credit' => 'credit_type_code' },
128 group_by => [ 'credit.credit_type_code', 'credit_type_code.description' ],
129 'select' => [ { sum => 'me.amount' }, 'credit.credit_type_code', 'credit_type_code.description' ],
130 'as' => [ 'total', 'credit_type_code', 'credit_description' ],
134 my @income = map {
136 total => $_->get_column('total'),
137 debit_type_code => $_->get_column('debit_type_code'),
138 debit_type => { description => $_->get_column('debit_description') }
140 } $income_summary->as_list;
141 my @outgoing = map {
143 total => $_->get_column('total'),
144 credit_type_code => $_->get_column('credit_type_code'),
145 credit_type => { description => $_->get_column('credit_description') }
147 } $outgoing_summary->as_list;
148 $summary = {
149 from_date => $previous ? $previous->timestamp : undef,
150 to_date => $self->timestamp,
151 income => \@income,
152 outgoing => \@outgoing,
153 total => ( $outgoing_transactions->total * -1 ) +
154 ( $income_transactions->total * -1 ),
155 bankable => (
156 $outgoing_transactions->search( { payment_type => 'CASH' } )
157 ->total * -1
158 ) + (
159 $income_transactions->search( { payment_type => 'CASH' } )->total *
164 return $summary;
167 =head2 Internal methods
169 =cut
171 =head3 _type
173 =cut
175 sub _type {
176 return 'CashRegisterAction';
181 =head1 AUTHORS
183 Martin Renvoize <martin.renvoize@ptfs-europe.com>
185 =cut