Bug 19482: (follow-up) Add missing filters and boolean switch to schema
[koha.git] / pos / pay.pl
blob5cc427999268ec2c08752295a1b38f63feb2b74f
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use CGI;
6 use JSON qw( from_json );
8 use C4::Auth qw/:DEFAULT get_session/;
9 use C4::Output;
10 use C4::Context;
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;
19 my $input = CGI->new();
20 my $sessionID = $input->cookie('CGISESSID');
21 my $session = get_session($sessionID);
23 my ( $template, $loggedinuser, $cookie, $user_flags ) = get_template_and_user(
25 template_name => 'pos/pay.tt',
26 query => $input,
27 type => 'intranet',
28 flagsrequired => { cash_management => 'takepayment' },
31 my $logged_in_user = Koha::Patrons->find($loggedinuser) or die "Not logged in";
33 my $library_id = C4::Context->userenv->{'branch'};
34 my $registerid = $input->param('registerid');
35 my $default_registerid = $input->param('default_register');
36 my $registers = Koha::Cash::Registers->search(
37 { branch => $library_id, archived => 0 },
38 { order_by => { '-asc' => 'name' } }
41 if ( !$registers->count ) {
42 $template->param( error_registers => 1 );
44 else {
45 if ( !$default_registerid ) {
46 my $default_register = Koha::Cash::Registers->find(
47 { branch => $library_id, branch_default => 1 } );
48 $default_registerid =
49 $default_register ? $default_register->id : $registers->next->id;
52 $template->param(
53 default_register => $default_registerid,
54 registers => $registers,
58 my $invoice_types =
59 Koha::Account::DebitTypes->search_with_library_limits( { can_be_sold => 1 },
60 {}, $library_id );
61 $template->param( invoice_types => $invoice_types );
63 my $total_paid = $input->param('paid');
64 if ( $total_paid and $total_paid ne '0.00' ) {
65 my $cash_register = Koha::Cash::Registers->find( { id => $registerid } );
66 my $payment_type = $input->param('payment_type');
67 my $sale = Koha::Charges::Sales->new(
69 cash_register => $cash_register,
70 staff_id => $logged_in_user->id
74 my @sales = $input->multi_param('sales');
75 for my $item (@sales) {
76 $item = from_json $item;
77 $sale->add_item($item);
80 my $payment = $sale->purchase( { payment_type => $payment_type } );
82 $template->param(
83 payment_id => $payment->accountlines_id,
84 collected => scalar $input->param('collected'),
85 change => scalar $input->param('change')
89 output_html_with_http_headers( $input, $cookie, $template->output );