3 # Copyright 2020 PTFS-Europe Ltd
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>.
23 use JSON qw( from_json );
25 use C4::Auth qw( get_session get_template_and_user );
27 use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
28 use C4::Output qw( output_html_with_http_headers );
30 use Koha::Account::DebitTypes;
31 use Koha::AuthorisedValues;
32 use Koha::Cash::Registers;
33 use Koha::Charges::Sales;
37 my $input = CGI->new();
38 my $sessionID = $input->cookie('CGISESSID');
39 my $session = get_session($sessionID);
41 my ( $template, $loggedinuser, $cookie, $user_flags ) = get_template_and_user(
43 template_name => 'pos/pay.tt',
46 flagsrequired => { cash_management => 'takepayment' },
49 my $logged_in_user = Koha::Patrons->find($loggedinuser) or die "Not logged in";
51 my $library_id = C4::Context->userenv->{'branch'};
52 my $registerid = $input->param('registerid');
53 my $action = $input->param('action') || '';
56 Koha::Account::DebitTypes->search_with_library_limits(
57 { can_be_sold => 1, archived => 0 },
59 $template->param( invoice_types => $invoice_types );
61 my $total_paid = $input->param('paid');
62 if ( $total_paid and $total_paid ne '0.00' ) {
63 my $cash_register = Koha::Cash::Registers->find( { id => $registerid } );
64 my $payment_type = $input->param('payment_type');
65 my $sale = Koha::Charges::Sales->new(
67 cash_register => $cash_register,
68 staff_id => $logged_in_user->id
72 my @sales = $input->multi_param('sales');
73 for my $item (@sales) {
74 $item = from_json $item;
75 $sale->add_item($item);
78 my $payment = $sale->purchase( { payment_type => $payment_type } );
81 payment_id => $payment->accountlines_id,
82 collected => scalar $input->param('collected'),
83 change => scalar $input->param('change')
87 if ( $action eq 'send' ) {
88 my $payment_id = $input->param('payment_id');
89 my $change = $input->param('change');
90 my $collected = $input->param('collected');
91 my $toaddr = $input->param('toaddr');
93 # Create our letter from the template
94 my $letter = GetPreparedLetter(
96 letter_code => 'RECEIPT',
97 branchcode => C4::Context->userenv->{'branch'},
98 message_transport_type => 'email',
100 credits => $payment_id,
103 collected => $collected,
108 # Add letter to the queue
109 my $message_id = EnqueueLetter(
112 message_transport_type => 'email',
113 from_address => C4::Context->preference('KohaAdminEmailAddress'),
114 to_address => $toaddr,
119 SendQueuedMessages( { message_id => $message_id } );
121 # Set variables for template to allow printing still
123 payment_id => $payment_id,
124 collected => $collected,
129 output_html_with_http_headers( $input, $cookie, $template->output );