2daa52ed41db9ccdd4a3f69d6d01fb2aaf882025
[srvgit] / Koha / Cash / Register.pm
1 package Koha::Cash::Register;
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::Account::Lines;
23 use Koha::Account::Offsets;
24 use Koha::Cash::Register::Actions;
25 use Koha::Cash::Register::Cashups;
26 use Koha::Database;
27
28 use base qw(Koha::Object);
29
30 =encoding utf8
31
32 =head1 NAME
33
34 Koha::Cash::Register - Koha cashregister Object class
35
36 =head1 API
37
38 =head2 Class methods
39
40 =cut
41
42 =head3 library
43
44 Return the library linked to this cash register
45
46 =cut
47
48 sub library {
49     my ($self) = @_;
50     return Koha::Library->_new_from_dbic($self->_result->branch);
51 }
52
53 =head3 cashups
54
55 Return a set of cashup actions linked to this cash register
56
57 =cut
58
59 sub cashups {
60     my ( $self, $conditions, $attrs ) = @_;
61
62     my $local_conditions = { code => 'CASHUP' };
63     $conditions //= {};
64     my $merged_conditions = { %{$conditions}, %{$local_conditions} };
65
66     my $rs =
67       $self->_result->search_related( 'cash_register_actions',
68         $merged_conditions, $attrs );
69
70     return Koha::Cash::Register::Cashups->_new_from_dbic($rs);
71 }
72
73 =head3 last_cashup
74
75 Return a set of cashup actions linked to this cash register
76
77 =cut
78
79 sub last_cashup {
80     my ( $self, $conditions, $attrs ) = @_;
81
82     my $rs = $self->_result->search_related(
83         'cash_register_actions',
84         { code     => 'CASHUP' },
85         { order_by => { '-desc' => [ 'timestamp', 'id' ] }, rows => 1 }
86     )->single;
87
88     return unless $rs;
89     return Koha::Cash::Register::Cashup->_new_from_dbic($rs);
90 }
91
92 =head3 accountlines
93
94 Return a set of accountlines linked to this cash register
95
96 =cut
97
98 sub accountlines {
99     my ($self) = @_;
100
101     my $rs = $self->_result->accountlines;
102     return Koha::Account::Lines->_new_from_dbic($rs);
103 }
104
105 =head3 outstanding_accountlines
106
107   my $lines = Koha::Cash::Registers->find($id)->outstanding_accountlines;
108
109 Return a set of accountlines linked to this cash register since the last cashup action
110
111 =cut
112
113 sub outstanding_accountlines {
114     my ( $self, $conditions, $attrs ) = @_;
115
116     my $since = $self->_result->search_related(
117         'cash_register_actions',
118         { 'code' => 'CASHUP' },
119         {
120             order_by => { '-desc' => [ 'timestamp', 'id' ] },
121             rows     => 1
122         }
123     );
124
125     my $local_conditions =
126       $since->count
127       ? { 'date' => { '>' => $since->get_column('timestamp')->as_query } }
128       : {};
129     my $merged_conditions =
130       $conditions
131       ? { %{$conditions}, %{$local_conditions} }
132       : $local_conditions;
133
134     my $rs =
135       $self->_result->search_related( 'accountlines', $merged_conditions,
136         $attrs );
137
138     return Koha::Account::Lines->_new_from_dbic($rs);
139 }
140
141 =head3 store
142
143 Local store method to prevent direct manipulation of the 'branch_default' field
144
145 =cut
146
147 sub store {
148     my ($self) = @_;
149
150     $self->_result->result_source->schema->txn_do(
151         sub {
152             if ( $self->_result->is_column_changed('branch_default') ) {
153                 Koha::Exceptions::Object::ReadOnlyProperty->throw(
154                     property => 'branch_default' );
155             }
156             else {
157                 if (   $self->_result->is_column_changed('branch')
158                     && $self->branch_default )
159                 {
160                 }
161                 $self = $self->SUPER::store;
162             }
163         }
164     );
165
166     return $self;
167 }
168
169 =head3 make_default
170
171 Set the current cash register as the branch default
172
173 =cut
174
175 sub make_default {
176     my ($self) = @_;
177
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 }, { no_triggers => 1 } );
183             $self->set( { branch_default => 1 } );
184             $self->SUPER::store;
185         }
186     );
187
188     return $self;
189 }
190
191 =head3 drop_default
192
193 Drop the current cash register as the branch default
194
195 =cut
196
197 sub drop_default {
198     my ($self) = @_;
199
200     $self->_result->result_source->schema->txn_do(
201         sub {
202             $self->set( { branch_default => 0 } );
203             $self->SUPER::store;
204         }
205     );
206
207     return $self;
208 }
209
210 =head3 add_cashup
211
212     my $cashup = $cash_register->add_cashup(
213         {
214             manager_id => $logged_in_user->id,
215             amount     => $cash_register->outstanding_accountlines->total
216         }
217     );
218
219 Add a new cashup action to the till, returns the added action.
220
221 =cut
222
223 sub add_cashup {
224     my ( $self, $params ) = @_;
225
226     my $rs = $self->_result->add_to_cash_register_actions(
227         {
228             code       => 'CASHUP',
229             manager_id => $params->{manager_id},
230             amount     => $params->{amount}
231         }
232     )->discard_changes;
233
234     return Koha::Cash::Register::Cashup->_new_from_dbic($rs);
235 }
236
237 =head2 Internal methods
238
239 =cut
240
241 =head3 _type
242
243 =cut
244
245 sub _type {
246     return 'CashRegister';
247 }
248
249 1;
250
251 =head1 AUTHORS
252
253 Martin Renvoize <martin.renvoize@ptfs-europe.com>
254
255 =cut