Bug 24031: Add safety checks in Koha::Plugins::call
[koha.git] / Koha / Cash / Register.pm
blobe54ce5f228d80c76346e1cca86d944c7ad1fdd4a
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 return Koha::Library->_new_from_dbic($self->_result->branch);
52 =head3 cashups
54 Return a set of cashup actions linked to this cash register
56 =cut
58 sub cashups {
59 my ( $self, $conditions, $attrs ) = @_;
61 my $local_conditions = { code => 'CASHUP' };
62 $conditions //= {};
63 my $merged_conditions = { %{$conditions}, %{$local_conditions} };
65 my $rs =
66 $self->_result->search_related( 'cash_register_actions',
67 $merged_conditions, $attrs );
69 return Koha::Cash::Register::Actions->_new_from_dbic($rs);
72 =head3 last_cashup
74 Return a set of cashup actions linked to this cash register
76 =cut
78 sub last_cashup {
79 my ( $self, $conditions, $attrs ) = @_;
81 my $rs = $self->_result->search_related(
82 'cash_register_actions',
83 { code => 'CASHUP' },
84 { order_by => { '-desc' => [ 'timestamp', 'id' ] }, rows => 1 }
85 )->single;
87 return unless $rs;
88 return Koha::Cash::Register::Action->_new_from_dbic($rs);
91 =head3 accountlines
93 Return a set of accountlines linked to this cash register
95 =cut
97 sub accountlines {
98 my ($self) = @_;
100 my $rs = $self->_result->accountlines;
101 return Koha::Account::Lines->_new_from_dbic($rs);
104 =head3 outstanding_accountlines
106 my $lines = Koha::Cash::Registers->find($id)->outstanding_accountlines;
108 Return a set of accountlines linked to this cash register since the last cashup action
110 =cut
112 sub outstanding_accountlines {
113 my ( $self, $conditions, $attrs ) = @_;
115 my $since = $self->_result->search_related(
116 'cash_register_actions',
117 { 'code' => 'CASHUP' },
119 order_by => { '-desc' => [ 'timestamp', 'id' ] },
120 rows => 1
124 my $local_conditions =
125 $since->count
126 ? { 'date' => { '>' => $since->get_column('timestamp')->as_query } }
127 : {};
128 my $merged_conditions =
129 $conditions
130 ? { %{$conditions}, %{$local_conditions} }
131 : $local_conditions;
133 my $rs =
134 $self->_result->search_related( 'accountlines', $merged_conditions,
135 $attrs );
137 return Koha::Account::Lines->_new_from_dbic($rs);
140 =head3 store
142 Local store method to prevent direct manipulation of the 'branch_default' field
144 =cut
146 sub store {
147 my ($self) = @_;
149 $self->_result->result_source->schema->txn_do(
150 sub {
151 if ( $self->_result->is_column_changed('branch_default') ) {
152 Koha::Exceptions::Object::ReadOnlyProperty->throw(
153 property => 'branch_default' );
155 else {
156 if ( $self->_result->is_column_changed('branch')
157 && $self->branch_default )
160 $self = $self->SUPER::store;
165 return $self;
168 =head3 make_default
170 Set the current cash register as the branch default
172 =cut
174 sub make_default {
175 my ($self) = @_;
177 $self->_result->result_source->schema->txn_do(
178 sub {
179 my $registers =
180 Koha::Cash::Registers->search( { branch => $self->branch } );
181 $registers->update( { branch_default => 0 }, { no_triggers => 1 } );
182 $self->set( { branch_default => 1 } );
183 $self->SUPER::store;
187 return $self;
190 =head3 drop_default
192 Drop the current cash register as the branch default
194 =cut
196 sub drop_default {
197 my ($self) = @_;
199 $self->_result->result_source->schema->txn_do(
200 sub {
201 $self->set( { branch_default => 0 } );
202 $self->SUPER::store;
206 return $self;
209 =head3 add_cashup
211 my $action = $cash_register->add_cashup(
213 manager_id => $logged_in_user->id,
214 amount => $cash_register->outstanding_accountlines->total
218 Add a new cashup action to the till, returns the added action.
220 =cut
222 sub add_cashup {
223 my ( $self, $params ) = @_;
225 my $rs = $self->_result->add_to_cash_register_actions(
227 code => 'CASHUP',
228 manager_id => $params->{manager_id},
229 amount => $params->{amount}
231 )->discard_changes;
233 return Koha::Cash::Register::Action->_new_from_dbic($rs);
236 =head2 Internal methods
238 =cut
240 =head3 _type
242 =cut
244 sub _type {
245 return 'CashRegister';
250 =head1 AUTHORS
252 Martin Renvoize <martin.renvoize@ptfs-europe.com>
254 =cut