Bug 29788: Fix batch delete item
[koha.git] / pos / pay.pl
1 #!/usr/bin/perl
2
3 # Copyright 2020 PTFS-Europe Ltd
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use CGI;
23 use JSON qw( from_json );
24
25 use C4::Auth qw( get_session get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Context;
28
29 use Koha::Account::DebitTypes;
30 use Koha::AuthorisedValues;
31 use Koha::Cash::Registers;
32 use Koha::Charges::Sales;
33 use Koha::Database;
34 use Koha::Libraries;
35
36 my $input     = CGI->new();
37 my $sessionID = $input->cookie('CGISESSID');
38 my $session   = get_session($sessionID);
39
40 my ( $template, $loggedinuser, $cookie, $user_flags ) = get_template_and_user(
41     {
42         template_name   => 'pos/pay.tt',
43         query           => $input,
44         type            => 'intranet',
45         flagsrequired   => { cash_management => 'takepayment' },
46     }
47 );
48 my $logged_in_user = Koha::Patrons->find($loggedinuser) or die "Not logged in";
49
50 my $library_id         = C4::Context->userenv->{'branch'};
51 my $registerid         = $input->param('registerid');
52
53 my $invoice_types =
54   Koha::Account::DebitTypes->search_with_library_limits(
55     { can_be_sold => 1, archived => 0 },
56     {}, $library_id );
57 $template->param( invoice_types => $invoice_types );
58
59 my $total_paid = $input->param('paid');
60 if ( $total_paid and $total_paid ne '0.00' ) {
61     my $cash_register = Koha::Cash::Registers->find( { id => $registerid } );
62     my $payment_type  = $input->param('payment_type');
63     my $sale          = Koha::Charges::Sales->new(
64         {
65             cash_register => $cash_register,
66             staff_id      => $logged_in_user->id
67         }
68     );
69
70     my @sales = $input->multi_param('sales');
71     for my $item (@sales) {
72         $item = from_json $item;
73         $sale->add_item($item);
74     }
75
76     my $payment = $sale->purchase( { payment_type => $payment_type } );
77
78     $template->param(
79         payment_id => $payment->accountlines_id,
80         collected  => scalar $input->param('collected'),
81         change     => scalar $input->param('change')
82     );
83 }
84
85 output_html_with_http_headers( $input, $cookie, $template->output );
86
87 1;