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