Bug 24902: Join different mc- limits with AND (elasticsearch)
[koha.git] / Koha / Cash / Register.pm
blobd033ab6ff2f834e4e5c84b8e19cfad0613d08d0a
1 package Koha::Cash::Register;
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::Account::Lines;
23 use Koha::Account::Offsets;
24 use Koha::Cash::Register::Actions;
25 use Koha::Database;
27 use base qw(Koha::Object);
29 =encoding utf8
31 =head1 NAME
33 Koha::Cash::Register - Koha cashregister Object class
35 =head1 API
37 =head2 Class methods
39 =cut
41 =head3 library
43 Return the library linked to this cash register
45 =cut
47 sub library {
48 my ($self) = @_;
49 my $rs = $self->_result->branch;
50 return unless $rs;
51 return Koha::Library->_new_from_dbic($rs);
54 =head3 cashups
56 Return a set of cashup actions linked to this cash register
58 =cut
60 sub cashups {
61 my ( $self, $conditions, $attrs ) = @_;
63 my $local_conditions = { code => 'CASHUP' };
64 $conditions //= {};
65 my $merged_conditions = { %{$conditions}, %{$local_conditions} };
67 my $rs =
68 $self->_result->search_related( 'cash_register_actions',
69 $merged_conditions, $attrs );
70 return unless $rs;
71 return Koha::Cash::Register::Actions->_new_from_dbic($rs);
74 =head3 last_cashup
76 Return a set of cashup actions linked to this cash register
78 =cut
80 sub last_cashup {
81 my ( $self, $conditions, $attrs ) = @_;
83 my $rs = $self->_result->search_related(
84 'cash_register_actions',
85 { code => 'CASHUP' },
86 { order_by => { '-desc' => [ 'timestamp', 'id' ] }, rows => 1 }
87 )->single;
89 return unless $rs;
90 return Koha::Cash::Register::Action->_new_from_dbic($rs);
93 =head3 accountlines
95 Return a set of accountlines linked to this cash register
97 =cut
99 sub accountlines {
100 my ($self) = @_;
102 my $rs = $self->_result->accountlines;
103 return unless $rs;
104 return Koha::Account::Lines->_new_from_dbic($rs);
107 =head3 outstanding_accountlines
109 my $lines = Koha::Cash::Registers->find($id)->outstanding_accountlines;
111 Return a set of accountlines linked to this cash register since the last cashup action
113 =cut
115 sub outstanding_accountlines {
116 my ( $self, $conditions, $attrs ) = @_;
118 my $since = $self->_result->search_related(
119 'cash_register_actions',
120 { 'code' => 'CASHUP' },
122 order_by => { '-desc' => [ 'timestamp', 'id' ] },
123 rows => 1
127 my $local_conditions =
128 $since->count
129 ? { 'timestamp' => { '>' => $since->get_column('timestamp')->as_query } }
130 : {};
131 my $merged_conditions =
132 $conditions
133 ? { %{$conditions}, %{$local_conditions} }
134 : $local_conditions;
136 my $rs =
137 $self->_result->search_related( 'accountlines', $merged_conditions,
138 $attrs );
139 return unless $rs;
140 return Koha::Account::Lines->_new_from_dbic($rs);
143 =head3 store
145 Local store method to prevent direct manipulation of the 'branch_default' field
147 =cut
149 sub store {
150 my ($self) = @_;
151 $self->_result->result_source->schema->txn_do(
152 sub {
153 if ( $self->_result->is_column_changed('branch_default') ) {
154 Koha::Exceptions::Object::ReadOnlyProperty->throw(
155 property => 'branch_default' );
157 else {
158 if ( $self->_result->is_column_changed('branch')
159 && $self->branch_default )
162 $self = $self->SUPER::store;
166 return $self;
169 =head3 make_default
171 Set the current cash register as the branch default
173 =cut
175 sub make_default {
176 my ($self) = @_;
178 $self->_result->result_source->schema->txn_do(
179 sub {
180 my $registers =
181 Koha::Cash::Registers->search( { branch => $self->branch } );
182 $registers->update( { branch_default => 0 } );
183 $self->set( { branch_default => 1 } );
184 $self->SUPER::store;
188 return $self;
191 =head3 drop_default
193 Drop the current cash register as the branch default
195 =cut
197 sub drop_default {
198 my ($self) = @_;
200 $self->_result->result_source->schema->txn_do(
201 sub {
202 $self->set( { branch_default => 0 } );
203 $self->SUPER::store;
207 return $self;
210 =head3 add_cashup
212 my $action = $cash_register->add_cashup(
214 manager_id => $logged_in_user->id,
215 amount => $cash_register->outstanding_accountlines->total
219 Add a new cashup action to the till, returns the added action.
221 =cut
223 sub add_cashup {
224 my ( $self, $params ) = @_;
226 my $rs = $self->_result->add_to_cash_register_actions(
228 code => 'CASHUP',
229 manager_id => $params->{manager_id},
230 amount => $params->{amount}
232 )->discard_changes;
234 return Koha::Cash::Register::Action->_new_from_dbic($rs);
237 =head2 Internal methods
239 =cut
241 =head3 _type
243 =cut
245 sub _type {
246 return 'CashRegister';
251 =head1 AUTHORS
253 Martin Renvoize <martin.renvoize@ptfs-europe.com>
255 =cut