Bug 28869: DBRev 23.12.00.058
[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 $op     = $input->param('op') || '';
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 if ( $op eq 'cud-pay' ) {
62     my $total_paid = $input->param('paid');
63     if ( $total_paid and $total_paid ne '0.00' ) {
64         my $cash_register = Koha::Cash::Registers->find( { id => $registerid } );
65         my $payment_type  = $input->param('payment_type');
66         my $sale          = Koha::Charges::Sales->new(
67             {
68                 cash_register => $cash_register,
69                 staff_id      => $logged_in_user->id
70             }
71         );
72
73         my @sales = $input->multi_param('sales');
74         for my $item (@sales) {
75             $item = from_json $item;
76             $sale->add_item($item);
77         }
78
79         my $payment = $sale->purchase( { payment_type => $payment_type } );
80
81         $template->param(
82             payment_id => $payment->accountlines_id,
83             collected  => scalar $input->param('collected'),
84             change     => scalar $input->param('change')
85         );
86     }
87 }
88
89 if ( $op eq 'cud-send' ) {
90     my $payment_id = $input->param('payment_id');
91     my $change     = $input->param('change');
92     my $collected  = $input->param('collected');
93     my $toaddr     = $input->param('toaddr');
94
95     # Create our letter from the template
96     my $letter = GetPreparedLetter(
97         module                 => 'pos',
98         letter_code            => 'RECEIPT',
99         branchcode             => C4::Context->userenv->{'branch'},
100         message_transport_type => 'email',
101         tables                 => {
102             credits => $payment_id,
103         },
104         substitute => {
105             collected => $collected,
106             change    => $change
107         }
108     );
109
110     # Add letter to the queue
111     my $message_id = EnqueueLetter(
112         {
113             letter                 => $letter,
114             message_transport_type => 'email',
115             from_address => C4::Context->preference('KohaAdminEmailAddress'),
116             to_address   => $toaddr,
117         }
118     );
119
120     # Send immediately
121     SendQueuedMessages( { message_id => $message_id } ) if $message_id;
122
123     # Set variables for template to allow printing still
124     $template->param(
125         payment_id => $payment_id,
126         collected  => $collected,
127         change     => $change
128     );
129 }
130
131 output_html_with_http_headers( $input, $cookie, $template->output );
132
133 1;