Line 0
Link Here
|
|
|
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 $outgoing_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('accountlines_id')->as_query }, |
100 |
'me.debit_id' => { '!=' => undef } |
101 |
}, |
102 |
{ |
103 |
join => { 'debit' => 'debit_type_code' }, |
104 |
group_by => [ 'debit.debit_type_code', 'debit_type_code.description' ], |
105 |
'select' => [ { sum => 'me.amount' }, 'debit.debit_type_code', 'debit_type_code.description' ], |
106 |
'as' => [ 'total', 'debit_type_code', 'debit_description' ], |
107 |
} |
108 |
); |
109 |
|
110 |
my $outgoing_summary = Koha::Account::Offsets->search( |
111 |
{ |
112 |
'me.debit_id' => |
113 |
{ '-in' => $outgoing_transactions->_resultset->get_column('accountlines_id')->as_query }, |
114 |
'me.credit_id' => { '!=' => undef } |
115 |
}, |
116 |
{ |
117 |
join => { 'credit' => 'credit_type_code' }, |
118 |
group_by => [ 'credit.credit_type_code', 'credit_type_code.description' ], |
119 |
'select' => [ { sum => 'me.amount' }, 'credit.credit_type_code', 'credit_type_code.description' ], |
120 |
'as' => [ 'total', 'credit_type_code', 'credit_description' ], |
121 |
} |
122 |
); |
123 |
|
124 |
my @income = map { |
125 |
{ |
126 |
total => $_->get_column('total'), |
127 |
debit_type_code => $_->get_column('debit_type_code'), |
128 |
debit_type => { description => $_->get_column('debit_description') } |
129 |
} |
130 |
} $income_summary->as_list; |
131 |
my @outgoing = map { |
132 |
{ |
133 |
total => $_->get_column('total'), |
134 |
credit_type_code => $_->get_column('credit_type_code'), |
135 |
credit_type => { description => $_->get_column('credit_description') } |
136 |
} |
137 |
} $outgoing_summary->as_list; |
138 |
|
139 |
$summary = { |
140 |
from_date => $previous ? $previous->timestamp : undef, |
141 |
to_date => $self->timestamp, |
142 |
income => \@income, |
143 |
outgoing => \@outgoing, |
144 |
income_transactions => $income_transactions, |
145 |
outgoing_transactions => $outgoing_transactions, |
146 |
}; |
147 |
|
148 |
return $summary; |
149 |
} |
150 |
|
151 |
=head3 to_api_mapping |
152 |
|
153 |
This method returns the mapping for representing a Koha::Cash::Regiser::Cashup object |
154 |
on the API. |
155 |
|
156 |
=cut |
157 |
|
158 |
sub to_api_mapping { |
159 |
return { |
160 |
id => 'cashup_id', |
161 |
register_id => 'cash_register_id', |
162 |
code => undef |
163 |
}; |
164 |
} |
165 |
|
166 |
1; |