Bug 23355: Add CASHUP actions
[koha.git] / 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::Database;
26
27 use base qw(Koha::Object);
28
29 =encoding utf8
30
31 =head1 NAME
32
33 Koha::Cash::Register - Koha cashregister Object class
34
35 =head1 API
36
37 =head2 Class methods
38
39 =cut
40
41 =head3 library
42
43 Return the library linked to this cash register
44
45 =cut
46
47 sub library {
48     my ($self) = @_;
49     my $rs = $self->_result->branch;
50     return unless $rs;
51     return Koha::Library->_new_from_dbic($rs);
52 }
53
54 =head3 cashups
55
56 Return a set of cashup actions linked to this cash register
57
58 =cut
59
60 sub cashups {
61     my ( $self, $conditions, $attrs ) = @_;
62
63     my $local_conditions = { code => 'CASHUP' };
64     $conditions //= {};
65     my $merged_conditions = { %{$conditions}, %{$local_conditions} };
66
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);
72 }
73
74 =head3 last_cashup
75
76 Return a set of cashup actions linked to this cash register
77
78 =cut
79
80 sub last_cashup {
81     my ( $self, $conditions, $attrs ) = @_;
82
83     my $rs = $self->_result->search_related(
84         'cash_register_actions',
85         { code     => 'CASHUP' },
86         { order_by => { '-desc' => [ 'timestamp', 'id' ] }, rows => 1 }
87     )->single;
88
89     return unless $rs;
90     return Koha::Cash::Register::Action->_new_from_dbic($rs);
91 }
92
93 =head3 accountlines
94
95 Return a set of accountlines linked to this cash register
96
97 =cut
98
99 sub accountlines {
100     my ($self) = @_;
101
102     my $rs = $self->_result->accountlines;
103     return unless $rs;
104     return Koha::Account::Lines->_new_from_dbic($rs);
105 }
106
107 =head3 outstanding_accountlines
108
109   my $lines = Koha::Cash::Registers->find($id)->outstanding_accountlines;
110
111 Return a set of accountlines linked to this cash register since the last cashup action
112
113 =cut
114
115 sub outstanding_accountlines {
116     my ( $self, $conditions, $attrs ) = @_;
117
118     my $since = $self->_result->search_related(
119         'cash_register_actions',
120         { 'code' => 'CASHUP' },
121         {
122             order_by => { '-desc' => [ 'timestamp', 'id' ] },
123             rows     => 1
124         }
125     );
126
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;
135
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);
141 }
142
143 =head3 store
144
145 Local store method to prevent direct manipulation of the 'branch_default' field
146
147 =cut
148
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' );
156             }
157             else {
158                 if (   $self->_result->is_column_changed('branch')
159                     && $self->branch_default )
160                 {
161                 }
162                 $self = $self->SUPER::store;
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 } );
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 Add a new cashup action to the till, returns the added action.
213
214 =cut
215
216 sub add_cashup {
217     my ( $self, $params ) = @_;
218
219     my $rs = $self->_result->add_to_cash_register_actions(
220         {
221             code       => 'CASHUP',
222             manager_id => $params->{staff_id},
223             amount     => $params->{amount}
224         }
225     )->discard_changes;
226
227     return Koha::Cash::Register::Action->_new_from_dbic($rs);
228 }
229
230 =head2 Internal methods
231
232 =cut
233
234 =head3 _type
235
236 =cut
237
238 sub _type {
239     return 'CashRegister';
240 }
241
242 1;
243
244 =head1 AUTHORS
245
246 Martin Renvoize <martin.renvoize@ptfs-europe.com>
247
248 =cut