Bug 10508: add unit tests for C4::Branch
[koha.git] / members / pay.pl
blob4efffc306ea1ddbe23af03126a5957f8f75ef859
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 URI::Escape;
33 use C4::Context;
34 use C4::Auth;
35 use C4::Output;
36 use CGI;
37 use C4::Members;
38 use C4::Accounts;
39 use C4::Stats;
40 use C4::Koha;
41 use C4::Overdues;
42 use C4::Branch;
43 use C4::Members::Attributes qw(GetBorrowerAttributes);
45 our $input = CGI->new;
47 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48 { template_name => 'members/pay.tmpl',
49 query => $input,
50 type => 'intranet',
51 authnotrequired => 0,
52 flagsrequired => { borrowers => 1, updatecharges => 1 },
53 debug => 1,
57 my @names = $input->param;
59 our $borrowernumber = $input->param('borrowernumber');
60 if ( !$borrowernumber ) {
61 $borrowernumber = $input->param('borrowernumber0');
64 # get borrower details
65 our $borrower = GetMember( borrowernumber => $borrowernumber );
66 our $user = $input->remote_user;
67 $user ||= q{};
69 my $branches = GetBranches();
70 our $branch = GetBranch( $input, $branches );
72 my $writeoff_item = $input->param('confirm_writeoff');
73 my $paycollect = $input->param('paycollect');
74 if ($paycollect) {
75 print $input->redirect(
76 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
78 my $payselected = $input->param('payselected');
79 if ($payselected) {
80 payselected(@names);
83 my $writeoff_all = $input->param('woall'); # writeoff all fines
84 if ($writeoff_all) {
85 writeoff_all(@names);
86 } elsif ($writeoff_item) {
87 my $accountlines_id = $input->param('accountlines_id');
88 my $itemno = $input->param('itemnumber');
89 my $account_type = $input->param('accounttype');
90 my $amount = $input->param('amountoutstanding');
91 my $payment_note = $input->param("payment_note");
92 WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $account_type, $amount, $branch, $payment_note );
95 for (@names) {
96 if (/^pay_indiv_(\d+)$/) {
97 my $line_no = $1;
98 redirect_to_paycollect( 'pay_individual', $line_no );
99 } elsif (/^wo_indiv_(\d+)$/) {
100 my $line_no = $1;
101 redirect_to_paycollect( 'writeoff_individual', $line_no );
105 $template->param(
106 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
107 RoutingSerials => C4::Context->preference('RoutingSerials'),
110 add_accounts_to_template();
112 output_html_with_http_headers $input, $cookie, $template->output;
114 sub add_accounts_to_template {
116 my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
117 my $accounts = [];
118 my @notify = NumberNotifyId($borrowernumber);
120 my $notify_groups = [];
121 for my $notify_id (@notify) {
122 my ( $acct_total, $accountlines, undef ) =
123 GetBorNotifyAcctRecord( $borrowernumber, $notify_id );
124 if ( @{$accountlines} ) {
125 my $totalnotify = AmountNotify( $notify_id, $borrowernumber );
126 push @{$accounts},
127 { accountlines => $accountlines,
128 notify => $notify_id,
129 total => $totalnotify,
133 borrower_add_additional_fields($borrower);
134 $template->param(
135 accounts => $accounts,
136 borrower => $borrower,
137 total => $total,
139 return;
143 sub get_for_redirect {
144 my ( $name, $name_in, $money ) = @_;
145 my $s = q{&} . $name . q{=};
146 my $value = $input->param($name_in);
147 if ( !defined $value ) {
148 $value = ( $money == 1 ) ? 0 : q{};
150 if ($money) {
151 $s .= sprintf '%.2f', $value;
152 } else {
153 $s .= $value;
155 return $s;
158 sub redirect_to_paycollect {
159 my ( $action, $line_no ) = @_;
160 my $redirect =
161 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
162 $redirect .= q{&};
163 $redirect .= "$action=1";
164 $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
165 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
166 $redirect .=
167 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
168 $redirect .= get_for_redirect( 'accountno', "accountno$line_no", 0 );
169 $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
170 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
171 $redirect .= get_for_redirect( 'notify_id', "notify_id$line_no", 0 );
172 $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
173 $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
174 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape( $input->param("payment_note_$line_no") );
175 $redirect .= '&remote_user=';
176 $redirect .= $user;
177 return print $input->redirect($redirect);
180 sub writeoff_all {
181 my @params = @_;
182 my @wo_lines = grep { /^accountno\d+$/ } @params;
183 for (@wo_lines) {
184 if (/(\d+)/) {
185 my $value = $1;
186 my $accounttype = $input->param("accounttype$value");
188 # my $borrowernum = $input->param("borrowernumber$value");
189 my $itemno = $input->param("itemnumber$value");
190 my $amount = $input->param("amountoutstanding$value");
191 my $accountno = $input->param("accountno$value");
192 my $accountlines_id = $input->param("accountlines_id$value");
193 my $payment_note = $input->param("payment_note_$value");
194 WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $accounttype, $amount, $branch, $payment_note );
198 $borrowernumber = $input->param('borrowernumber');
199 print $input->redirect(
200 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
201 return;
204 sub borrower_add_additional_fields {
205 my $b_ref = shift;
207 # some borrower info is not returned in the standard call despite being assumed
208 # in a number of templates. It should not be the business of this script but in lieu of
209 # a revised api here it is ...
210 if ( $b_ref->{category_type} eq 'C' ) {
211 my ( $catcodes, $labels ) =
212 GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
213 if ( @{$catcodes} ) {
214 if ( @{$catcodes} > 1 ) {
215 $b_ref->{CATCODE_MULTI} = 1;
216 } elsif ( @{$catcodes} == 1 ) {
217 $b_ref->{catcode} = $catcodes->[0];
220 } elsif ( $b_ref->{category_type} eq 'A' ) {
221 $b_ref->{adultborrower} = 1;
223 my ( $picture, $dberror ) = GetPatronImage( $b_ref->{cardnumber} );
224 if ($picture) {
225 $b_ref->{has_picture} = 1;
228 if (C4::Context->preference('ExtendedPatronAttributes')) {
229 $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
230 $template->param(
231 ExtendedPatronAttributes => 1,
235 $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
236 return;
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, $input->param("accountno$index");
247 $amt += $input->param("amountoutstanding$index");
250 $amt = '&amt=' . $amt;
251 my $sel = '&selected=' . join ',', @lines_to_pay;
252 my $notes = '&notes=' . join("%0A", map { $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;
259 print $input->redirect($redirect);
260 return;