Bug 19013: Fix borrowers.guarantorid in sammple_patrons.sql
[koha.git] / members / pay.pl
blobe4d0a955d310c22b9fdf3975e57496a6c592cc92
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
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.
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.
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>.
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 qw ( -utf8 );
37 use C4::Members;
38 use C4::Accounts;
39 use C4::Stats;
40 use C4::Koha;
41 use C4::Overdues;
42 use C4::Members::Attributes qw(GetBorrowerAttributes);
43 use Koha::Patrons;
44 use Koha::Patron::Images;
46 use Koha::Patron::Categories;
47 use URI::Escape;
49 our $input = CGI->new;
51 my $updatecharges_permissions = $input->param('woall') ? 'writeoff' : 'remaining_permissions';
52 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53 { template_name => 'members/pay.tt',
54 query => $input,
55 type => 'intranet',
56 authnotrequired => 0,
57 flagsrequired => { borrowers => 1, updatecharges => $updatecharges_permissions },
58 debug => 1,
62 my @names = $input->param;
64 our $borrowernumber = $input->param('borrowernumber');
65 if ( !$borrowernumber ) {
66 $borrowernumber = $input->param('borrowernumber0');
69 # get borrower details
70 my $patron = Koha::Patrons->find( $borrowernumber );
71 my $category = $patron->category;
72 our $borrower = $patron->unblessed;
73 $borrower->{description} = $category->description;
74 $borrower->{category_type} = $category->category_type;
75 our $user = $input->remote_user;
76 $user ||= q{};
78 our $branch = C4::Context->userenv->{'branch'};
80 my $writeoff_item = $input->param('confirm_writeoff');
81 my $paycollect = $input->param('paycollect');
82 if ($paycollect) {
83 print $input->redirect(
84 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
86 my $payselected = $input->param('payselected');
87 if ($payselected) {
88 payselected(@names);
91 my $writeoff_all = $input->param('woall'); # writeoff all fines
92 if ($writeoff_all) {
93 writeoff_all(@names);
94 } elsif ($writeoff_item) {
95 my $accountlines_id = $input->param('accountlines_id');
96 my $amount = $input->param('amountoutstanding');
97 my $payment_note = $input->param("payment_note");
99 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
101 amount => $amount,
102 lines => [ scalar Koha::Account::Lines->find($accountlines_id) ],
103 type => 'writeoff',
104 note => $payment_note,
105 library_id => $branch,
110 for (@names) {
111 if (/^pay_indiv_(\d+)$/) {
112 my $line_no = $1;
113 redirect_to_paycollect( 'pay_individual', $line_no );
114 } elsif (/^wo_indiv_(\d+)$/) {
115 my $line_no = $1;
116 redirect_to_paycollect( 'writeoff_individual', $line_no );
120 $template->param(
121 finesview => 1,
122 RoutingSerials => C4::Context->preference('RoutingSerials'),
125 add_accounts_to_template();
127 output_html_with_http_headers $input, $cookie, $template->output;
129 sub add_accounts_to_template {
131 my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
132 my $accounts = [];
133 my @notify = NumberNotifyId($borrowernumber);
135 my $notify_groups = [];
136 for my $notify_id (@notify) {
137 my ( $acct_total, $accountlines, undef ) =
138 GetBorNotifyAcctRecord( $borrowernumber, $notify_id );
139 if ( @{$accountlines} ) {
140 my $totalnotify = AmountNotify( $notify_id, $borrowernumber );
141 push @{$accounts},
142 { accountlines => $accountlines,
143 notify => $notify_id,
144 total => $totalnotify,
148 borrower_add_additional_fields($borrower);
150 $template->param(%$borrower);
152 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
153 $template->param( picture => 1 ) if $patron_image;
154 $template->param(
155 accounts => $accounts,
156 borrower => $borrower,
157 categoryname => $borrower->{'description'},
158 total => $total,
160 return;
164 sub get_for_redirect {
165 my ( $name, $name_in, $money ) = @_;
166 my $s = q{&} . $name . q{=};
167 my $value = uri_escape_utf8( $input->param($name_in) );
168 if ( !defined $value ) {
169 $value = ( $money == 1 ) ? 0 : q{};
171 if ($money) {
172 $s .= sprintf '%.2f', $value;
173 } else {
174 $s .= $value;
176 return $s;
179 sub redirect_to_paycollect {
180 my ( $action, $line_no ) = @_;
181 my $redirect =
182 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
183 $redirect .= q{&};
184 $redirect .= "$action=1";
185 $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
186 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
187 $redirect .=
188 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
189 $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
190 $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
191 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
192 $redirect .= get_for_redirect( 'notify_id', "notify_id$line_no", 0 );
193 $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
194 $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
195 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( $input->param("payment_note_$line_no") );
196 $redirect .= '&remote_user=';
197 $redirect .= $user;
198 return print $input->redirect($redirect);
201 sub writeoff_all {
202 my @params = @_;
203 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
205 my $borrowernumber = $input->param('borrowernumber');
207 for (@wo_lines) {
208 if (/(\d+)/) {
209 my $value = $1;
210 my $amount = $input->param("amountoutstanding$value");
211 my $accountlines_id = $input->param("accountlines_id$value");
212 my $payment_note = $input->param("payment_note_$value");
213 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
215 amount => $amount,
216 lines => [ scalar Koha::Account::Lines->find($accountlines_id) ],
217 type => 'writeoff',
218 note => $payment_note,
219 library_id => $branch,
225 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
226 return;
229 sub borrower_add_additional_fields {
230 my $b_ref = shift;
232 # some borrower info is not returned in the standard call despite being assumed
233 # in a number of templates. It should not be the business of this script but in lieu of
234 # a revised api here it is ...
235 if ( $b_ref->{category_type} eq 'C' ) {
236 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
237 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
238 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
239 } elsif ( $b_ref->{category_type} eq 'A' || $b_ref->{category_type} eq 'I' ) {
240 $b_ref->{adultborrower} = 1;
243 if (C4::Context->preference('ExtendedPatronAttributes')) {
244 $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
245 $template->param(
246 ExtendedPatronAttributes => 1,
250 return;
253 sub payselected {
254 my @params = @_;
255 my $amt = 0;
256 my @lines_to_pay;
257 foreach (@params) {
258 if (/^incl_par_(\d+)$/) {
259 my $index = $1;
260 push @lines_to_pay, $input->param("accountlines_id$index");
261 $amt += $input->param("amountoutstanding$index");
264 $amt = '&amt=' . $amt;
265 my $sel = '&selected=' . join ',', @lines_to_pay;
266 my $notes = '&notes=' . join("%0A", map { $input->param("payment_note_$_") } @lines_to_pay );
267 my $redirect =
268 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
269 . $amt
270 . $sel
271 . $notes;
273 print $input->redirect($redirect);
274 return;