Bug 34478: Remove check_csrf from pl files
[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 qw( uri_escape uri_unescape );
22 use CGI qw ( -utf8 );
23
24 use C4::Context;
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
27 use C4::Accounts;
28 use C4::Koha;
29
30 use Koha::Cash::Registers;
31 use Koha::Patrons;
32 use Koha::Patron::Categories;
33 use Koha::AuthorisedValues;
34 use Koha::Account;
35 use Koha::Account::Lines;
36 use Koha::AdditionalFields;
37 use Koha::DateUtils qw( output_pref );
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 $account        = $patron->account;
62 my $category       = $patron->category;
63 my $user           = $input->remote_user;
64
65 my $library_id = C4::Context->userenv->{'branch'};
66 my $total_due  = $account->outstanding_debits->total_outstanding;
67
68 my $total_paid      = $input->param('paid');
69 my $total_collected = $input->param('collected');
70
71 my $selected_lines = $input->param('selected'); # comes from pay.pl
72 my $pay_individual   = $input->param('pay_individual');
73 my $selected_accts   = $input->param('selected_accts'); # comes from paycollect.pl
74 my $payment_note = uri_unescape scalar $input->param('payment_note');
75 my $payment_type = scalar $input->param('payment_type');
76 my $accountlines_id;
77
78 my $cash_register_id = $input->param('cash_register');
79 if ( $pay_individual || $writeoff_individual ) {
80     if ($pay_individual) {
81         $template->param( pay_individual => 1 );
82     } elsif ($writeoff_individual) {
83         $template->param( writeoff_individual => 1 );
84     }
85     my $debit_type_code   = $input->param('debit_type_code');
86     $accountlines_id      = $input->param('accountlines_id');
87     my $amount            = $input->param('amount');
88     my $amountoutstanding = $input->param('amountoutstanding');
89     my $itemnumber  = $input->param('itemnumber');
90     my $description  = $input->param('description');
91     $total_due = $amountoutstanding;
92     $template->param(
93         debit_type_code    => $debit_type_code,
94         accountlines_id    => $accountlines_id,
95         amount            => $amount,
96         amountoutstanding => $amountoutstanding,
97         itemnumber        => $itemnumber,
98         individual_description => $description,
99         payment_note    => $payment_note,
100     );
101 } elsif ($selected_lines) {
102     $total_due = $input->param('amt');
103     $template->param(
104         selected_accts => $selected_lines,
105         amt            => $total_due,
106         selected_accts_notes => scalar $input->param('notes'),
107     );
108 }
109
110 my @selected_accountlines;
111 if ( $selected_accts ) {
112     if ( $selected_accts =~ /^([\d,]*).*/ ) {
113         $selected_accts = $1;    # ensure passing no junk
114     }
115     my @acc = split /,/, $selected_accts;
116
117     my $search_params = {
118         borrowernumber    => $borrowernumber,
119             amountoutstanding => { '<>' => 0 },
120             accountlines_id   => { 'in' => \@acc },
121     };
122
123     @selected_accountlines = Koha::Account::Lines->search(
124         $search_params,
125         { order_by => 'date' }
126     )->as_list;
127
128     my $sum = Koha::Account::Lines->search(
129         $search_params,
130         {
131             select => [ { sum => 'amountoutstanding' } ],
132             as     => [ 'total_amountoutstanding'],
133         }
134     );
135     $total_due = $sum->_resultset->first->get_column('total_amountoutstanding');
136 }
137
138 if ( $total_paid and $total_paid ne '0.00' ) {
139     $total_paid = $total_due if (abs($total_paid - $total_due) < 0.01) && C4::Context->preference('RoundFinesAtPayment');
140     if ( $total_paid < 0 or $total_paid > $total_due ) {
141         $template->param(
142             error_over => 1,
143             total_due => $total_due
144         );
145     } elsif ( $total_collected < $total_paid && !( $writeoff_individual || $type eq 'WRITEOFF' ) ) {
146         $template->param(
147             error_under => 1,
148             total_paid => $total_paid
149         );
150     } else {
151         my $url;
152         my $pay_result;
153         if ($pay_individual) {
154             my $line = Koha::Account::Lines->find($accountlines_id);
155             $pay_result = $account->pay(
156                 {
157                     lines        => [$line],
158                     amount       => $total_paid,
159                     library_id   => $library_id,
160                     note         => $payment_note,
161                     interface    => C4::Context->interface,
162                     payment_type => $payment_type,
163                     cash_register => $cash_register_id
164                 }
165             );
166             $payment_id = $pay_result->{payment_id};
167
168             my @additional_fields;
169             my $accountline_fields = Koha::AdditionalFields->search({ tablename => 'accountlines:credit' });
170             while ( my $field = $accountline_fields->next ) {
171                 my $value = $input->param('additional_field_' . $field->id);
172                 if (defined $value) {
173                     push @additional_fields, {
174                         id => $field->id,
175                         value => $value,
176                     };
177                 }
178             }
179             if (@additional_fields) {
180                 my $payment = Koha::Account::Lines->find($payment_id);
181                 $payment->set_additional_fields(\@additional_fields);
182             }
183
184
185             $url = "/cgi-bin/koha/members/pay.pl";
186         } else {
187             if ($selected_accts) {
188                 if ( $total_paid > $total_due ) {
189                     $template->param(
190                         error_over => 1,
191                         total_due => $total_due
192                     );
193                 } else {
194                     my $note = $input->param('selected_accts_notes');
195
196                     $pay_result = $account->pay(
197                         {
198                             type         => $type,
199                             amount       => $total_paid,
200                             library_id   => $library_id,
201                             lines        => \@selected_accountlines,
202                             note         => $note,
203                             interface    => C4::Context->interface,
204                             payment_type => $payment_type,
205                             cash_register => $cash_register_id
206                         }
207                     );
208                 }
209                 $payment_id = $pay_result->{payment_id};
210             }
211             else {
212                 my $note = $input->param('selected_accts_notes');
213                 $pay_result = $account->pay(
214                     {
215                         amount       => $total_paid,
216                         library_id   => $library_id,
217                         note         => $note,
218                         payment_type => $payment_type,
219                         interface    => C4::Context->interface,
220                         payment_type => $payment_type,
221                         cash_register => $cash_register_id
222                     }
223                 );
224                 $payment_id = $pay_result->{payment_id};
225             }
226             $payment_id = $pay_result->{payment_id};
227
228             my @additional_fields;
229             my $accountline_fields = Koha::AdditionalFields->search({ tablename => 'accountlines:credit' });
230             while ( my $field = $accountline_fields->next ) {
231                 my $value = $input->param('additional_field_' . $field->id);
232                 if (defined $value) {
233                     push @additional_fields, {
234                         id => $field->id,
235                         value => $value,
236                     };
237                 }
238             }
239             if (@additional_fields) {
240                 my $payment = Koha::Account::Lines->find($payment_id);
241                 $payment->set_additional_fields(\@additional_fields);
242             }
243
244             $url = "/cgi-bin/koha/members/boraccount.pl";
245         }
246         # It's possible renewals took place, parse any renew results
247         # and pass on
248         my @renew_result = ();
249         foreach my $ren( @{$pay_result->{renew_result}} ) {
250             my $str = "renew_result=$ren->{itemnumber},$ren->{success},";
251             my $app = $ren->{success} ?
252                 uri_escape(
253                     output_pref({ dt => $ren->{due_date}, as_due_date => 1 })
254                 ) : $ren->{error};
255                 push @renew_result, "${str}${app}";
256         }
257         my $append = scalar @renew_result ? '&' . join('&', @renew_result) : '';
258
259         $url .= "?borrowernumber=$borrowernumber&payment_id=$payment_id&change_given=${change_given}${append}";
260
261         print $input->redirect($url);
262     }
263 } else {
264     $total_paid = '0.00';    #TODO not right with pay_individual
265 }
266
267 if ( $input->param('error_over') ) {
268     $template->param( error_over => 1, total_due => scalar $input->param('amountoutstanding') );
269 }
270
271 $template->param(
272     payment_id => $payment_id,
273
274     type           => $type,
275     borrowernumber => $borrowernumber,    # some templates require global
276     patron         => $patron,
277     total          => $total_due,
278     available_additional_fields => [ Koha::AdditionalFields->search({ tablename => 'accountlines:credit' })->as_list ],
279 );
280
281 output_html_with_http_headers $input, $cookie, $template->output;