Bug 33568: Fix popup behaviour for SpineLabelShowPrintOnBibDetails
[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 # get operation
47 my $op = $input->param('op') // qw{};
48
49 my $updatecharges_permissions = ($writeoff_individual || $type eq 'WRITEOFF') ? 'writeoff' : 'remaining_permissions';
50 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
51     {   template_name   => 'members/paycollect.tt',
52         query           => $input,
53         type            => 'intranet',
54         flagsrequired   => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
55     }
56 );
57
58 # get borrower details
59 my $borrowernumber = $input->param('borrowernumber');
60 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
61 my $patron         = Koha::Patrons->find( $borrowernumber );
62 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
63
64 my $account        = $patron->account;
65 my $category       = $patron->category;
66 my $user           = $input->remote_user;
67
68 my $library_id = C4::Context->userenv->{'branch'};
69 my $total_due  = $account->outstanding_debits->total_outstanding;
70
71 my $total_paid      = $input->param('paid');
72 my $total_collected = $input->param('collected');
73
74 my $selected_lines = $input->param('selected'); # comes from pay.pl
75 my $pay_individual   = $input->param('pay_individual');
76 my $selected_accts   = $input->param('selected_accts'); # comes from paycollect.pl
77 my $payment_note = uri_unescape scalar $input->param('payment_note');
78 my $payment_type = scalar $input->param('payment_type');
79 my $accountlines_id;
80
81 my $cash_register_id = $input->param('cash_register');
82 if ( $pay_individual || $writeoff_individual ) {
83     if ($pay_individual) {
84         $template->param( pay_individual => 1 );
85     } elsif ($writeoff_individual) {
86         $template->param( writeoff_individual => 1 );
87     }
88     my $debit_type_code   = $input->param('debit_type_code');
89     $accountlines_id      = $input->param('accountlines_id');
90     my $amount            = $input->param('amount');
91     my $amountoutstanding = $input->param('amountoutstanding');
92     my $itemnumber  = $input->param('itemnumber');
93     my $description  = $input->param('description');
94     $total_due = $amountoutstanding;
95     $template->param(
96         debit_type_code    => $debit_type_code,
97         accountlines_id    => $accountlines_id,
98         amount            => $amount,
99         amountoutstanding => $amountoutstanding,
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     )->as_list;
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     $accountlines_id      = $input->param('accountlines_id');
143     $total_paid = $total_due if (abs($total_paid - $total_due) < 0.01) && C4::Context->preference('RoundFinesAtPayment');
144     if ( $total_paid < 0 or $total_paid > $total_due ) {
145         $template->param(
146             error_over => 1,
147             total_due => $total_due
148         );
149     } elsif ( $total_collected < $total_paid && !( $op eq 'cud-writeoff_individual' || $type eq 'WRITEOFF' ) ) {
150         $template->param(
151             error_under => 1,
152             total_paid => $total_paid
153         );
154     } else {
155         my $url;
156         my $pay_result;
157         if ($op eq 'cud-pay_individual') {
158             my $line = Koha::Account::Lines->find($accountlines_id);
159             $pay_result = $account->pay(
160                 {
161                     type         => $type,
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             my @additional_fields;
174             my $accountline_fields = Koha::AdditionalFields->search({ tablename => 'accountlines:credit' });
175             while ( my $field = $accountline_fields->next ) {
176                 my $value = $input->param('additional_field_' . $field->id);
177                 if (defined $value) {
178                     push @additional_fields, {
179                         id => $field->id,
180                         value => $value,
181                     };
182                 }
183             }
184             if (@additional_fields) {
185                 my $payment = Koha::Account::Lines->find($payment_id);
186                 $payment->set_additional_fields(\@additional_fields);
187             }
188
189             $url = "/cgi-bin/koha/members/pay.pl";
190         } elsif ($op eq 'cud-writeoff_individual') {
191             my $item_id         = $input->param('itemnumber');
192             my $payment_note    = $input->param("payment_note");
193
194             my $accountline = Koha::Account::Lines->find( $accountlines_id );
195             $pay_result = $account->pay(
196                 {
197                     type       => 'WRITEOFF',
198                     amount     => $total_paid,
199                     lines      => [ $accountline ],
200                     note       => $payment_note,
201                     interface  => C4::Context->interface,
202                     item_id    => $item_id,
203                     library_id => $library_id,
204                 }
205             );
206             $payment_id = $pay_result->{payment_id};
207
208             $url = "/cgi-bin/koha/members/pay.pl";
209         } elsif ( $op eq 'cud-pay' || $op eq 'cud-writeoff' ) {
210             if ($selected_accts) {
211                 if ( $total_paid > $total_due ) {
212                     $template->param(
213                         error_over => 1,
214                         total_due => $total_due
215                     );
216                 } else {
217                     my $note = $input->param('selected_accts_notes');
218
219                     $pay_result = $account->pay(
220                         {
221                             type         => $type,
222                             amount       => $total_paid,
223                             library_id   => $library_id,
224                             lines        => \@selected_accountlines,
225                             note         => $note,
226                             interface    => C4::Context->interface,
227                             payment_type => $payment_type,
228                             cash_register => $cash_register_id
229                         }
230                     );
231                 }
232                 $payment_id = $pay_result->{payment_id};
233             }
234             else {
235                 my $note = $input->param('selected_accts_notes');
236                 $pay_result = $account->pay(
237                     {
238                         amount       => $total_paid,
239                         library_id   => $library_id,
240                         note         => $note,
241                         payment_type => $payment_type,
242                         interface    => C4::Context->interface,
243                         payment_type => $payment_type,
244                         cash_register => $cash_register_id
245                     }
246                 );
247                 $payment_id = $pay_result->{payment_id};
248             }
249             $payment_id = $pay_result->{payment_id};
250
251             my @additional_fields;
252             my $accountline_fields = Koha::AdditionalFields->search({ tablename => 'accountlines:credit' });
253             while ( my $field = $accountline_fields->next ) {
254                 my $value = $input->param('additional_field_' . $field->id);
255                 if (defined $value) {
256                     push @additional_fields, {
257                         id => $field->id,
258                         value => $value,
259                     };
260                 }
261             }
262             if (@additional_fields) {
263                 my $payment = Koha::Account::Lines->find($payment_id);
264                 $payment->set_additional_fields(\@additional_fields);
265             }
266
267             $url = "/cgi-bin/koha/members/boraccount.pl";
268         }
269         # It's possible renewals took place, parse any renew results
270         # and pass on
271         my @renew_result = ();
272         foreach my $ren( @{$pay_result->{renew_result}} ) {
273             my $str = "renew_result=$ren->{itemnumber},$ren->{success},";
274             my $app = $ren->{success} ?
275                 uri_escape(
276                     output_pref({ dt => $ren->{due_date}, as_due_date => 1 })
277                 ) : $ren->{error};
278                 push @renew_result, "${str}${app}";
279         }
280         my $append = scalar @renew_result ? '&' . join('&', @renew_result) : '';
281
282         $url .= "?borrowernumber=$borrowernumber&payment_id=$payment_id&change_given=${change_given}${append}";
283
284         print $input->redirect($url);
285     }
286 } else {
287     $total_paid = '0.00';    #TODO not right with pay_individual
288 }
289
290 if ( $input->param('error_over') ) {
291     $template->param( error_over => 1, total_due => scalar $input->param('amountoutstanding') );
292 }
293
294 $template->param(
295     payment_id                  => $payment_id,
296     type                        => $type,
297     borrowernumber              => $borrowernumber,    # some templates require global
298     patron                      => $patron,
299     total                       => $total_due,
300     available_additional_fields =>
301         [ Koha::AdditionalFields->search( { tablename => 'accountlines:credit' } )->as_list ],
302 );
303
304 output_html_with_http_headers $input, $cookie, $template->output;