Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / members / paycollect.pl
1 #!/usr/bin/perl
2 # Copyright 2009,2010 PTFS Inc.
3 # Copyright 2011 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 use URI::Escape;
22 use CGI qw ( -utf8 );
23
24 use C4::Context;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Members;
28 use C4::Accounts;
29 use C4::Koha;
30
31 use Koha::Cash::Registers;
32 use Koha::Patrons;
33 use Koha::Patron::Categories;
34 use Koha::AuthorisedValues;
35 use Koha::Account;
36 use Koha::Token;
37 use Koha::DateUtils;
38
39 my $input = CGI->new();
40
41 my $payment_id          = $input->param('payment_id');
42 my $writeoff_individual = $input->param('writeoff_individual');
43 my $change_given        = $input->param('change_given');
44 my $type                = scalar $input->param('type') || 'PAYMENT';
45
46 my $updatecharges_permissions = ($writeoff_individual || $type eq 'WRITEOFF') ? 'writeoff' : 'remaining_permissions';
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48     {   template_name   => 'members/paycollect.tt',
49         query           => $input,
50         type            => 'intranet',
51         flagsrequired   => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
52     }
53 );
54
55 # get borrower details
56 my $borrowernumber = $input->param('borrowernumber');
57 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
58 my $patron         = Koha::Patrons->find( $borrowernumber );
59 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
60
61 my $borrower       = $patron->unblessed;
62 my $account        = $patron->account;
63 my $category       = $patron->category;
64 my $user           = $input->remote_user;
65
66 my $library_id = C4::Context->userenv->{'branch'};
67 my $total_due  = $account->outstanding_debits->total_outstanding;
68
69 my $total_paid      = $input->param('paid');
70 my $total_collected = $input->param('collected');
71
72 my $selected_lines = $input->param('selected'); # comes from pay.pl
73 my $pay_individual   = $input->param('pay_individual');
74 my $selected_accts   = $input->param('selected_accts'); # comes from paycollect.pl
75 my $payment_note = uri_unescape scalar $input->param('payment_note');
76 my $payment_type = scalar $input->param('payment_type');
77 my $accountlines_id;
78
79 my $cash_register_id = $input->param('cash_register');
80 if ( $pay_individual || $writeoff_individual ) {
81     if ($pay_individual) {
82         $template->param( pay_individual => 1 );
83     } elsif ($writeoff_individual) {
84         $template->param( writeoff_individual => 1 );
85     }
86     my $debit_type_code   = $input->param('debit_type_code');
87     $accountlines_id      = $input->param('accountlines_id');
88     my $amount            = $input->param('amount');
89     my $amountoutstanding = $input->param('amountoutstanding');
90     my $itemnumber  = $input->param('itemnumber');
91     my $description  = $input->param('description');
92     my $title        = $input->param('title');
93     $total_due = $amountoutstanding;
94     $template->param(
95         debit_type_code    => $debit_type_code,
96         accountlines_id    => $accountlines_id,
97         amount            => $amount,
98         amountoutstanding => $amountoutstanding,
99         title             => $title,
100         itemnumber        => $itemnumber,
101         individual_description => $description,
102         payment_note    => $payment_note,
103     );
104 } elsif ($selected_lines) {
105     $total_due = $input->param('amt');
106     $template->param(
107         selected_accts => $selected_lines,
108         amt            => $total_due,
109         selected_accts_notes => scalar $input->param('notes'),
110     );
111 }
112
113 my @selected_accountlines;
114 if ( $selected_accts ) {
115     if ( $selected_accts =~ /^([\d,]*).*/ ) {
116         $selected_accts = $1;    # ensure passing no junk
117     }
118     my @acc = split /,/, $selected_accts;
119
120     my $search_params = {
121         borrowernumber    => $borrowernumber,
122             amountoutstanding => { '<>' => 0 },
123             accountlines_id   => { 'in' => \@acc },
124     };
125
126     @selected_accountlines = Koha::Account::Lines->search(
127         $search_params,
128         { order_by => 'date' }
129     );
130
131     my $sum = Koha::Account::Lines->search(
132         $search_params,
133         {
134             select => [ { sum => 'amountoutstanding' } ],
135             as     => [ 'total_amountoutstanding'],
136         }
137     );
138     $total_due = $sum->_resultset->first->get_column('total_amountoutstanding');
139 }
140
141 if ( $total_paid and $total_paid ne '0.00' ) {
142     $total_paid = $total_due if (abs($total_paid - $total_due) < 0.01) && C4::Context->preference('RoundFinesAtPayment');
143     if ( $total_paid < 0 or $total_paid > $total_due ) {
144         $template->param(
145             error_over => 1,
146             total_due => $total_due
147         );
148     } elsif ( $total_collected < $total_paid && !( $writeoff_individual || $type eq 'WRITEOFF' ) ) {
149         $template->param(
150             error_under => 1,
151             total_paid => $total_paid
152         );
153     } else {
154         output_and_exit( $input, $cookie, $template,  'wrong_csrf_token' )
155             unless Koha::Token->new->check_csrf( {
156                 session_id => $input->cookie('CGISESSID'),
157                 token  => scalar $input->param('csrf_token'),
158             });
159
160         my $url;
161         my $pay_result;
162         if ($pay_individual) {
163             my $line = Koha::Account::Lines->find($accountlines_id);
164             $pay_result = $account->pay(
165                 {
166                     lines        => [$line],
167                     amount       => $total_paid,
168                     library_id   => $library_id,
169                     note         => $payment_note,
170                     interface    => C4::Context->interface,
171                     payment_type => $payment_type,
172                     cash_register => $cash_register_id
173                 }
174             );
175             $payment_id = $pay_result->{payment_id};
176
177             $url = "/cgi-bin/koha/members/pay.pl";
178         } else {
179             if ($selected_accts) {
180                 if ( $total_paid > $total_due ) {
181                     $template->param(
182                         error_over => 1,
183                         total_due => $total_due
184                     );
185                 } else {
186                     my $note = $input->param('selected_accts_notes');
187
188                     $pay_result = $account->pay(
189                         {
190                             type         => $type,
191                             amount       => $total_paid,
192                             library_id   => $library_id,
193                             lines        => \@selected_accountlines,
194                             note         => $note,
195                             interface    => C4::Context->interface,
196                             payment_type => $payment_type,
197                             cash_register => $cash_register_id
198                         }
199                     );
200                 }
201                 $payment_id = $pay_result->{payment_id};
202             }
203             else {
204                 my $note = $input->param('selected_accts_notes');
205                 $pay_result = $account->pay(
206                     {
207                         amount       => $total_paid,
208                         library_id   => $library_id,
209                         note         => $note,
210                         payment_type => $payment_type,
211                         interface    => C4::Context->interface,
212                         payment_type => $payment_type,
213                         cash_register => $cash_register_id
214                     }
215                 );
216                 $payment_id = $pay_result->{payment_id};
217             }
218             $payment_id = $pay_result->{payment_id};
219
220             $url = "/cgi-bin/koha/members/boraccount.pl";
221         }
222         # It's possible renewals took place, parse any renew results
223         # and pass on
224         my @renew_result = ();
225         foreach my $ren( @{$pay_result->{renew_result}} ) {
226             my $str = "renew_result=$ren->{itemnumber},$ren->{success},";
227             my $app = $ren->{success} ?
228                 uri_escape(
229                     output_pref({ dt => $ren->{due_date}, as_due_date => 1 })
230                 ) : $ren->{error};
231                 push @renew_result, "${str}${app}";
232         }
233         my $append = scalar @renew_result ? '&' . join('&', @renew_result) : '';
234
235         $url .= "?borrowernumber=$borrowernumber&payment_id=$payment_id&change_given=${change_given}${append}";
236
237         print $input->redirect($url);
238     }
239 } else {
240     $total_paid = '0.00';    #TODO not right with pay_individual
241 }
242
243 $template->param(%$borrower);
244
245 if ( $input->param('error_over') ) {
246     $template->param( error_over => 1, total_due => scalar $input->param('amountoutstanding') );
247 }
248
249 $template->param(
250     payment_id => $payment_id,
251
252     type           => $type,
253     borrowernumber => $borrowernumber,    # some templates require global
254     patron         => $patron,
255     total          => $total_due,
256
257     csrf_token => Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID') } ),
258 );
259
260 output_html_with_http_headers $input, $cookie, $template->output;