Bug 9727: Fix typo in variable name
[koha.git] / members / pay.pl
blob4b918aefd9e6b7cfff67b5f716801f1b9682bbf1
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2010,2011 PTFS-Europe Ltd
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 =head1 pay.pl
24 written 11/1/2000 by chris@katipo.oc.nz
25 part of the koha library system, script to facilitate paying off fines
27 =cut
29 use strict;
30 use warnings;
32 use C4::Context;
33 use C4::Auth;
34 use C4::Output;
35 use CGI;
36 use C4::Members;
37 use C4::Accounts;
38 use C4::Stats;
39 use C4::Koha;
40 use C4::Overdues;
41 use C4::Branch;
42 use C4::Members::Attributes qw(GetBorrowerAttributes);
44 our $input = CGI->new;
46 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47 { template_name => 'members/pay.tmpl',
48 query => $input,
49 type => 'intranet',
50 authnotrequired => 0,
51 flagsrequired => { borrowers => 1, updatecharges => 1 },
52 debug => 1,
56 my @names = $input->param;
58 our $borrowernumber = $input->param('borrowernumber');
59 if ( !$borrowernumber ) {
60 $borrowernumber = $input->param('borrowernumber0');
63 # get borrower details
64 our $borrower = GetMember( borrowernumber => $borrowernumber );
65 our $user = $input->remote_user;
66 $user ||= q{};
68 my $branches = GetBranches();
69 our $branch = GetBranch( $input, $branches );
71 my $writeoff_item = $input->param('confirm_writeoff');
72 my $paycollect = $input->param('paycollect');
73 if ($paycollect) {
74 print $input->redirect(
75 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
77 my $payselected = $input->param('payselected');
78 if ($payselected) {
79 payselected(@names);
82 my $writeoff_all = $input->param('woall'); # writeoff all fines
83 if ($writeoff_all) {
84 writeoff_all(@names);
85 } elsif ($writeoff_item) {
86 my $accountlines_id = $input->param('accountlines_id');
87 my $itemno = $input->param('itemnumber');
88 my $account_type = $input->param('accounttype');
89 my $amount = $input->param('amountoutstanding');
90 WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $account_type, $amount );
93 for (@names) {
94 if (/^pay_indiv_(\d+)$/) {
95 my $line_no = $1;
96 redirect_to_paycollect( 'pay_individual', $line_no );
97 } elsif (/^wo_indiv_(\d+)$/) {
98 my $line_no = $1;
99 redirect_to_paycollect( 'writeoff_individual', $line_no );
103 $template->param( activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne '') );
105 add_accounts_to_template();
107 output_html_with_http_headers $input, $cookie, $template->output;
109 sub add_accounts_to_template {
111 my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
112 my $accounts = [];
113 my @notify = NumberNotifyId($borrowernumber);
115 my $notify_groups = [];
116 for my $notify_id (@notify) {
117 my ( $acct_total, $accountlines, undef ) =
118 GetBorNotifyAcctRecord( $borrowernumber, $notify_id );
119 if ( @{$accountlines} ) {
120 my $totalnotify = AmountNotify( $notify_id, $borrowernumber );
121 push @{$accounts},
122 { accountlines => $accountlines,
123 notify => $notify_id,
124 total => $totalnotify,
128 borrower_add_additional_fields($borrower);
129 $template->param(
130 accounts => $accounts,
131 borrower => $borrower,
132 total => $total,
134 return;
138 sub get_for_redirect {
139 my ( $name, $name_in, $money ) = @_;
140 my $s = q{&} . $name . q{=};
141 my $value = $input->param($name_in);
142 if ( !defined $value ) {
143 $value = ( $money == 1 ) ? 0 : q{};
145 if ($money) {
146 $s .= sprintf '%.2f', $value;
147 } else {
148 $s .= $value;
150 return $s;
153 sub redirect_to_paycollect {
154 my ( $action, $line_no ) = @_;
155 my $redirect =
156 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
157 $redirect .= q{&};
158 $redirect .= "$action=1";
159 $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
160 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
161 $redirect .=
162 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
163 $redirect .= get_for_redirect( 'accountno', "accountno$line_no", 0 );
164 $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
165 $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
166 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
167 $redirect .= get_for_redirect( 'notify_id', "notify_id$line_no", 0 );
168 $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
169 $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
170 $redirect .= '&remote_user=';
171 $redirect .= $user;
172 return print $input->redirect($redirect);
175 sub writeoff_all {
176 my @params = @_;
177 my @wo_lines = grep { /^accountno\d+$/ } @params;
178 for (@wo_lines) {
179 if (/(\d+)/) {
180 my $value = $1;
181 my $accounttype = $input->param("accounttype$value");
183 # my $borrowernum = $input->param("borrowernumber$value");
184 my $itemno = $input->param("itemnumber$value");
185 my $amount = $input->param("amountoutstanding$value");
186 my $accountno = $input->param("accountno$value");
187 my $accountlines_id = $input->param("accountlines_id$value");
188 WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $accounttype, $amount );
192 $borrowernumber = $input->param('borrowernumber');
193 print $input->redirect(
194 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
195 return;
198 sub borrower_add_additional_fields {
199 my $b_ref = shift;
201 # some borrower info is not returned in the standard call despite being assumed
202 # in a number of templates. It should not be the business of this script but in lieu of
203 # a revised api here it is ...
204 if ( $b_ref->{category_type} eq 'C' ) {
205 my ( $catcodes, $labels ) =
206 GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
207 if ( @{$catcodes} ) {
208 if ( @{$catcodes} > 1 ) {
209 $b_ref->{CATCODE_MULTI} = 1;
210 } elsif ( @{$catcodes} == 1 ) {
211 $b_ref->{catcode} = $catcodes->[0];
214 } elsif ( $b_ref->{category_type} eq 'A' ) {
215 $b_ref->{adultborrower} = 1;
217 my ( $picture, $dberror ) = GetPatronImage( $b_ref->{cardnumber} );
218 if ($picture) {
219 $b_ref->{has_picture} = 1;
222 if (C4::Context->preference('ExtendedPatronAttributes')) {
223 $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
224 $template->param(
225 ExtendedPatronAttributes => 1,
229 $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
230 return;
233 sub payselected {
234 my @params = @_;
235 my $amt = 0;
236 my @lines_to_pay;
237 foreach (@params) {
238 if (/^incl_par_(\d+)$/) {
239 my $index = $1;
240 push @lines_to_pay, $input->param("accountno$index");
241 $amt += $input->param("amountoutstanding$index");
244 $amt = '&amt=' . $amt;
245 my $sel = '&selected=' . join ',', @lines_to_pay;
246 my $redirect =
247 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
248 . $amt
249 . $sel;
251 print $input->redirect($redirect);
252 return;