Bug 36233: Set select2 width to 100%
[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::Context;
27 use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
28 use C4::Output  qw( output_html_with_http_headers );
29
30 use Koha::Account::DebitTypes;
31 use Koha::AuthorisedValues;
32 use Koha::Cash::Registers;
33 use Koha::Charges::Sales;
34 use Koha::Database;
35 use Koha::Libraries;
36
37 my $input     = CGI->new();
38 my $sessionID = $input->cookie('CGISESSID');
39 my $session   = get_session($sessionID);
40
41 my ( $template, $loggedinuser, $cookie, $user_flags ) = get_template_and_user(
42     {
43         template_name => 'pos/pay.tt',
44         query         => $input,
45         type          => 'intranet',
46         flagsrequired => { cash_management => 'takepayment' },
47     }
48 );
49 my $logged_in_user = Koha::Patrons->find($loggedinuser) or die "Not logged in";
50
51 my $library_id = C4::Context->userenv->{'branch'};
52 my $registerid = $input->param('registerid');
53 my $action     = $input->param('action') || '';
54
55 my $invoice_types =
56   Koha::Account::DebitTypes->search_with_library_limits(
57     { can_be_sold => 1, archived => 0 },
58     {}, $library_id );
59 $template->param( invoice_types => $invoice_types );
60
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(
66         {
67             cash_register => $cash_register,
68             staff_id      => $logged_in_user->id
69         }
70     );
71
72     my @sales = $input->multi_param('sales');
73     for my $item (@sales) {
74         $item = from_json $item;
75         $sale->add_item($item);
76     }
77
78     my $payment = $sale->purchase( { payment_type => $payment_type } );
79
80     $template->param(
81         payment_id => $payment->accountlines_id,
82         collected  => scalar $input->param('collected'),
83         change     => scalar $input->param('change')
84     );
85 }
86
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');
92
93     # Create our letter from the template
94     my $letter = GetPreparedLetter(
95         module                 => 'pos',
96         letter_code            => 'RECEIPT',
97         branchcode             => C4::Context->userenv->{'branch'},
98         message_transport_type => 'email',
99         tables                 => {
100             credits => $payment_id,
101         },
102         substitute => {
103             collected => $collected,
104             change    => $change
105         }
106     );
107
108     # Add letter to the queue
109     my $message_id = EnqueueLetter(
110         {
111             letter                 => $letter,
112             message_transport_type => 'email',
113             from_address => C4::Context->preference('KohaAdminEmailAddress'),
114             to_address   => $toaddr,
115         }
116     );
117
118     # Send immediately
119     SendQueuedMessages( { message_id => $message_id } ) if $message_id;
120
121     # Set variables for template to allow printing still
122     $template->param(
123         payment_id => $payment_id,
124         collected  => $collected,
125         change     => $change
126     );
127 }
128
129 output_html_with_http_headers( $input, $cookie, $template->output );
130
131 1;