Bug 26734: (QA follow-up) Add missing copyright statement
[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/:DEFAULT get_session/;
26 use C4::Output;
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( { can_be_sold => 1 },
55     {}, $library_id );
56 $template->param( invoice_types => $invoice_types );
57
58 my $total_paid = $input->param('paid');
59 if ( $total_paid and $total_paid ne '0.00' ) {
60     my $cash_register = Koha::Cash::Registers->find( { id => $registerid } );
61     my $payment_type  = $input->param('payment_type');
62     my $sale          = Koha::Charges::Sales->new(
63         {
64             cash_register => $cash_register,
65             staff_id      => $logged_in_user->id
66         }
67     );
68
69     my @sales = $input->multi_param('sales');
70     for my $item (@sales) {
71         $item = from_json $item;
72         $sale->add_item($item);
73     }
74
75     my $payment = $sale->purchase( { payment_type => $payment_type } );
76
77     $template->param(
78         payment_id => $payment->accountlines_id,
79         collected  => scalar $input->param('collected'),
80         change     => scalar $input->param('change')
81     );
82 }
83
84 output_html_with_http_headers( $input, $cookie, $template->output );
85
86 1;