Bug 28479: Use primary keys to check object existence in TestBuilder
[koha.git] / pos / pay.pl
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use CGI;
6 use JSON qw( from_json );
7
8 use C4::Auth qw/:DEFAULT get_session/;
9 use C4::Output;
10 use C4::Context;
11
12 use Koha::Account::DebitTypes;
13 use Koha::AuthorisedValues;
14 use Koha::Cash::Registers;
15 use Koha::Charges::Sales;
16 use Koha::Database;
17 use Koha::Libraries;
18
19 my $input     = CGI->new();
20 my $sessionID = $input->cookie('CGISESSID');
21 my $session   = get_session($sessionID);
22
23 my ( $template, $loggedinuser, $cookie, $user_flags ) = get_template_and_user(
24     {
25         template_name   => 'pos/pay.tt',
26         query           => $input,
27         type            => 'intranet',
28         flagsrequired   => { cash_management => 'takepayment' },
29     }
30 );
31 my $logged_in_user = Koha::Patrons->find($loggedinuser) or die "Not logged in";
32
33 my $library_id         = C4::Context->userenv->{'branch'};
34 my $registerid         = $input->param('registerid');
35
36 my $invoice_types =
37   Koha::Account::DebitTypes->search_with_library_limits(
38     { can_be_sold => 1, archived => 0 },
39     {}, $library_id );
40 $template->param( invoice_types => $invoice_types );
41
42 my $total_paid = $input->param('paid');
43 if ( $total_paid and $total_paid ne '0.00' ) {
44     my $cash_register = Koha::Cash::Registers->find( { id => $registerid } );
45     my $payment_type  = $input->param('payment_type');
46     my $sale          = Koha::Charges::Sales->new(
47         {
48             cash_register => $cash_register,
49             staff_id      => $logged_in_user->id
50         }
51     );
52
53     my @sales = $input->multi_param('sales');
54     for my $item (@sales) {
55         $item = from_json $item;
56         $sale->add_item($item);
57     }
58
59     my $payment = $sale->purchase( { payment_type => $payment_type } );
60
61     $template->param(
62         payment_id => $payment->accountlines_id,
63         collected  => scalar $input->param('collected'),
64         change     => scalar $input->param('change')
65     );
66 }
67
68 output_html_with_http_headers( $input, $cookie, $template->output );
69
70 1;