Bug 23354: (follow-up) Make display of debit types configurable
[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 $q         = CGI->new();
20 my $sessionID = $q->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           => $q,
27         type            => 'intranet',
28         authnotrequired => 0,
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 = $q->param('registerid');
35 my $registers  = Koha::Cash::Registers->search(
36     { branch   => $library_id, archived => 0 },
37     { order_by => { '-asc' => 'name' } }
38 );
39
40 if ( !$registers->count ) {
41     $template->param( error_registers => 1 );
42 }
43 else {
44     if ( !$registerid ) {
45         my $default_register = Koha::Cash::Registers->find(
46             { branch => $library_id, branch_default => 1 } );
47         $registerid = $default_register->id if $default_register;
48     }
49     $registerid = $registers->next->id if !$registerid;
50
51     $template->param(
52         registerid => $registerid,
53         registers  => $registers,
54     );
55 }
56
57 my $invoice_types =
58   Koha::Account::DebitTypes->search_with_library_limits(
59     { can_be_sold => 1 },
60     {}, $library_id );
61 $template->param( invoice_types => $invoice_types );
62
63 my $total_paid = $q->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  = $q->param('payment_type');
67     my $sale          = Koha::Charges::Sales->new(
68         {
69             cash_register => $cash_register,
70             staff_id      => $logged_in_user->id
71         }
72     );
73
74     my @sales = $q->multi_param('sales');
75     for my $item (@sales) {
76         $item = from_json $item;
77         $sale->add_item($item);
78     }
79
80     my $payment = $sale->purchase( { payment_type => $payment_type } );
81
82     $template->param(
83         payment_id => $payment->accountlines_id,
84         collected  => scalar $q->param('collected'),
85         change     => scalar $q->param('change')
86     );
87 }
88
89 output_html_with_http_headers( $q, $cookie, $template->output );
90
91 1;