1 package Koha::Charges::Sales;
3 # Copyright 2019 PTFS Europe
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Koha::Account::Lines;
23 use Koha::Account::DebitTypes;
24 use Koha::Account::Offsets;
25 use Koha::DateUtils qw( dt_from_string );
30 Koha::Charges::Sale - Module for collecting sales in Koha
34 use Koha::Charges::Sale;
37 Koha::Charges::Sale->new( { cash_register => $register, staff_id => $staff_id } );
38 $sale->add_item($item);
39 $sale->purchase( { payment_type => 'CASH' } );
45 Koha::Charges::Sale->new(
47 cash_register => $cash_register,
48 staff_id => $staff_id,
49 [ payment_type => $payment_type ],
51 [ patron => $patron ],
58 my ( $class, $params ) = @_;
60 Koha::Exceptions::MissingParameter->throw(
61 "Missing mandatory parameter: cash_register")
62 unless $params->{cash_register};
64 Koha::Exceptions::MissingParameter->throw(
65 "Missing mandatory parameter: staff_id")
66 unless $params->{staff_id};
68 Carp::confess("Key 'cash_register' is not a Koha::Cash::Register object!")
69 unless $params->{cash_register}->isa('Koha::Cash::Register');
71 return bless( $params, $class );
76 my $payment_type = $sale->payment_type( $payment_type );
78 A getter/setter for this instances associated payment type.
83 my ( $self, $payment_type ) = @_;
86 Koha::Exceptions::Account::UnrecognisedType->throw(
87 error => 'Type of payment not recognised' )
88 unless ( exists( $self->_get_valid_payments->{$payment_type} ) );
90 $self->{payment_type} = $payment_type;
93 return $self->{payment_type};
96 =head3 _get_valid_payments
98 my $valid_payments = $sale->_get_valid_payments;
100 A getter which returns a hashref whose keys represent valid payment types.
104 sub _get_valid_payments {
107 $self->{valid_payments} //= {
108 map { $_ => 1 } Koha::AuthorisedValues->search_with_library_limits(
110 category => 'PAYMENT_TYPE'
113 $self->{cash_register}->branch # filter by cash_register branch
114 )->get_column('authorised_value')
117 return $self->{valid_payments};
122 my $item = { price => 0.25, quantity => 1, code => 'COPY' };
123 $sale->add_item( $item );
128 my ( $self, $item ) = @_;
130 Koha::Exceptions::MissingParameter->throw(
131 "Missing mandatory parameter: code")
132 unless $item->{code};
134 Koha::Exceptions::Account::UnrecognisedType->throw(
135 error => 'Type of debit not recognised' )
136 unless ( exists( $self->_get_valid_items->{ $item->{code} } ) );
138 Koha::Exceptions::MissingParameter->throw(
139 "Missing mandatory parameter: price")
140 unless $item->{price};
142 Koha::Exceptions::MissingParameter->throw(
143 "Missing mandatory parameter: quantity")
144 unless $item->{quantity};
146 push @{ $self->{items} }, $item;
150 =head3 _get_valid_items
152 my $valid_items = $sale->_get_valid_items;
154 A getter which returns a hashref whose keys represent valid sale items.
158 sub _get_valid_items {
161 $self->{valid_items} //= {
163 Koha::Account::DebitTypes->search_with_library_limits( {}, {},
164 $self->{cash_register}->branch )->get_column('code')
167 return $self->{valid_items};
172 my $credit_line = $sale->purchase;
177 my ( $self, $params ) = @_;
179 if ( $params->{payment_type} ) {
180 Koha::Exceptions::Account::UnrecognisedType->throw(
181 error => 'Type of payment not recognised' )
183 exists( $self->_get_valid_payments->{ $params->{payment_type} } ) );
185 $self->{payment_type} = $params->{payment_type};
188 Koha::Exceptions::MissingParameter->throw(
189 "Missing mandatory parameter: payment_type")
190 unless $self->{payment_type};
192 Koha::Exceptions::NoChanges->throw(
193 "Cannot purchase before calling add_item")
194 unless $self->{items};
196 my $schema = Koha::Database->new->schema;
197 my $dt = dt_from_string();
204 # Add accountlines for each item being purchased
206 for my $item ( @{ $self->{items} } ) {
208 my $amount = $item->{quantity} * $item->{price};
209 $total_owed = $total_owed + $amount;
211 # Insert the account line
212 my $debit = Koha::Account::Line->new(
215 debit_type_code => $item->{code},
216 amountoutstanding => 0,
217 note => $item->{quantity},
218 manager_id => $self->{staff_id},
219 interface => 'intranet',
220 branchcode => $self->{cash_register}->branch,
224 push @{$debits}, $debit;
226 # Record the account offset
227 my $account_offset = Koha::Account::Offset->new(
229 debit_id => $debit->id,
236 # Add accountline for payment
237 $credit = Koha::Account::Line->new(
239 amount => 0 - $total_owed,
240 credit_type_code => 'PURCHASE',
241 payment_type => $self->{payment_type},
242 amountoutstanding => 0,
243 manager_id => $self->{staff_id},
244 interface => 'intranet',
245 branchcode => $self->{cash_register}->branch,
246 register_id => $self->{cash_register}->id,
252 # Record the account offset
253 my $credit_offset = Koha::Account::Offset->new(
255 credit_id => $credit->id,
257 amount => $credit->amount
261 # Link payment to debits
262 for my $debit ( @{$debits} ) {
263 Koha::Account::Offset->new(
265 credit_id => $credit->accountlines_id,
266 debit_id => $debit->id,
267 amount => $debit->amount * -1,
280 Martin Renvoize <martin.renvoize@ptfs-europe.com>