Bug 18789: Remove adultborrower from the pay* scripts
[koha.git] / members / pay.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2010,2011 PTFS-Europe Ltd
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 =head1 pay.pl
23
24  written 11/1/2000 by chris@katipo.oc.nz
25  part of the koha library system, script to facilitate paying off fines
26
27 =cut
28
29 use Modern::Perl;
30
31 use URI::Escape;
32 use C4::Context;
33 use C4::Auth;
34 use C4::Output;
35 use CGI qw ( -utf8 );
36 use C4::Members;
37 use C4::Accounts;
38 use C4::Stats;
39 use C4::Koha;
40 use C4::Overdues;
41 use C4::Members::Attributes qw(GetBorrowerAttributes);
42 use Koha::Patrons;
43
44 use Koha::Patron::Categories;
45 use URI::Escape;
46
47 our $input = CGI->new;
48
49 my $updatecharges_permissions = $input->param('woall') ? 'writeoff' : 'remaining_permissions';
50 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
51     {   template_name   => 'members/pay.tt',
52         query           => $input,
53         type            => 'intranet',
54         authnotrequired => 0,
55         flagsrequired   => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
56         debug           => 1,
57     }
58 );
59
60 my @names = $input->param;
61
62 our $borrowernumber = $input->param('borrowernumber');
63 if ( !$borrowernumber ) {
64     $borrowernumber = $input->param('borrowernumber0');
65 }
66
67 # get borrower details
68 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
69 our $patron         = Koha::Patrons->find($borrowernumber);
70 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
71
72 our $user = $input->remote_user;
73 $user ||= q{};
74
75 our $branch = C4::Context->userenv->{'branch'};
76
77 my $writeoff_item = $input->param('confirm_writeoff');
78 my $paycollect    = $input->param('paycollect');
79 if ($paycollect) {
80     print $input->redirect(
81         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
82 }
83 my $payselected = $input->param('payselected');
84 if ($payselected) {
85     payselected(@names);
86 }
87
88 my $writeoff_all = $input->param('woall');    # writeoff all fines
89 if ($writeoff_all) {
90     writeoff_all(@names);
91 } elsif ($writeoff_item) {
92     my $accountlines_id = $input->param('accountlines_id');
93     my $amount       = $input->param('amountwrittenoff');
94     my $payment_note = $input->param("payment_note");
95
96     Koha::Account->new( { patron_id => $borrowernumber } )->pay(
97         {
98             amount     => $amount,
99             lines      => [ scalar Koha::Account::Lines->find($accountlines_id) ],
100             type       => 'writeoff',
101             note       => $payment_note,
102             library_id => $branch,
103         }
104     );
105 }
106
107 for (@names) {
108     if (/^pay_indiv_(\d+)$/) {
109         my $line_no = $1;
110         redirect_to_paycollect( 'pay_individual', $line_no );
111     } elsif (/^wo_indiv_(\d+)$/) {
112         my $line_no = $1;
113         redirect_to_paycollect( 'writeoff_individual', $line_no );
114     }
115 }
116
117 $template->param(
118     finesview => 1,
119 );
120
121 add_accounts_to_template();
122
123 output_html_with_http_headers $input, $cookie, $template->output;
124
125 sub add_accounts_to_template {
126
127     my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
128     my $account_lines = Koha::Account::Lines->search({ borrowernumber => $borrowernumber, amountoutstanding => { '!=' => 0 } }, { order_by => ['accounttype'] });
129     my @accounts;
130     while ( my $account_line = $account_lines->next ) {
131         $account_line = $account_line->unblessed;
132         if ( $account_line->{itemnumber} ) {
133             my $item = Koha::Items->find( $account_line->{itemnumber} );
134             my $biblio = $item->biblio;
135             $account_line->{biblionumber} = $biblio->biblionumber;
136             $account_line->{title}        = $biblio->title;
137         }
138         push @accounts, $account_line;
139     }
140     borrower_add_additional_fields($patron->unblessed);
141
142     $template->param(
143         patron   => $patron,
144         accounts => \@accounts,
145         total    => $total,
146     );
147     return;
148
149 }
150
151 sub get_for_redirect {
152     my ( $name, $name_in, $money ) = @_;
153     my $s     = q{&} . $name . q{=};
154     my $value;
155     if (defined $input->param($name_in)) {
156         $value = uri_escape_utf8( scalar $input->param($name_in) );
157     }
158     if ( !defined $value ) {
159         $value = ( $money == 1 ) ? 0 : q{};
160     }
161     if ($money) {
162         $s .= sprintf '%.2f', $value;
163     } else {
164         $s .= $value;
165     }
166     return $s;
167 }
168
169 sub redirect_to_paycollect {
170     my ( $action, $line_no ) = @_;
171     my $redirect =
172       "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
173     $redirect .= q{&};
174     $redirect .= "$action=1";
175     $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
176     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
177     $redirect .=
178       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
179     $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
180     $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
181     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
182     $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
183     $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
184     $redirect .= '&remote_user=';
185     $redirect .= $user;
186     return print $input->redirect($redirect);
187 }
188
189 sub writeoff_all {
190     my @params = @_;
191     my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
192
193     my $borrowernumber = $input->param('borrowernumber');
194
195     for (@wo_lines) {
196         if (/(\d+)/) {
197             my $value           = $1;
198             my $amount          = $input->param("amountoutstanding$value");
199             my $accountlines_id = $input->param("accountlines_id$value");
200             my $payment_note    = $input->param("payment_note_$value");
201             Koha::Account->new( { patron_id => $borrowernumber } )->pay(
202                 {
203                     amount => $amount,
204                     lines  => [ scalar Koha::Account::Lines->find($accountlines_id) ],
205                     type   => 'writeoff',
206                     note   => $payment_note,
207                     library_id => $branch,
208                 }
209             );
210         }
211     }
212
213     print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
214     return;
215 }
216
217 sub borrower_add_additional_fields {
218     my $b_ref = shift;
219
220 # some borrower info is not returned in the standard call despite being assumed
221 # in a number of templates. It should not be the business of this script but in lieu of
222 # a revised api here it is ...
223     if ( $b_ref->{category_type} eq 'C' ) {
224         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
225         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
226         $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
227     }
228
229     if (C4::Context->preference('ExtendedPatronAttributes')) {
230         $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
231         $template->param(
232             ExtendedPatronAttributes => 1,
233         );
234     }
235
236     return;
237 }
238
239 sub payselected {
240     my @params = @_;
241     my $amt    = 0;
242     my @lines_to_pay;
243     foreach (@params) {
244         if (/^incl_par_(\d+)$/) {
245             my $index = $1;
246             push @lines_to_pay, scalar $input->param("accountlines_id$index");
247             $amt += $input->param("amountoutstanding$index");
248         }
249     }
250     $amt = '&amt=' . $amt;
251     my $sel = '&selected=' . join ',', @lines_to_pay;
252     my $notes = '&notes=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
253     my $redirect =
254         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
255       . $amt
256       . $sel
257       . $notes;
258
259     print $input->redirect($redirect);
260     return;
261 }