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