Bug 18789: Send Koha::Patron object to the templates
[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 use Koha::Patron::Images;
44
45 use Koha::Patron::Categories;
46 use URI::Escape;
47
48 our $input = CGI->new;
49
50 my $updatecharges_permissions = $input->param('woall') ? 'writeoff' : 'remaining_permissions';
51 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52     {   template_name   => 'members/pay.tt',
53         query           => $input,
54         type            => 'intranet',
55         authnotrequired => 0,
56         flagsrequired   => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
57         debug           => 1,
58     }
59 );
60
61 my @names = $input->param;
62
63 our $borrowernumber = $input->param('borrowernumber');
64 if ( !$borrowernumber ) {
65     $borrowernumber = $input->param('borrowernumber0');
66 }
67
68 # get borrower details
69 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
70 our $patron         = Koha::Patrons->find($borrowernumber);
71 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
72
73 our $user = $input->remote_user;
74 $user ||= q{};
75
76 our $branch = C4::Context->userenv->{'branch'};
77
78 my $writeoff_item = $input->param('confirm_writeoff');
79 my $paycollect    = $input->param('paycollect');
80 if ($paycollect) {
81     print $input->redirect(
82         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
83 }
84 my $payselected = $input->param('payselected');
85 if ($payselected) {
86     payselected(@names);
87 }
88
89 my $writeoff_all = $input->param('woall');    # writeoff all fines
90 if ($writeoff_all) {
91     writeoff_all(@names);
92 } elsif ($writeoff_item) {
93     my $accountlines_id = $input->param('accountlines_id');
94     my $amount       = $input->param('amountwrittenoff');
95     my $payment_note = $input->param("payment_note");
96
97     Koha::Account->new( { patron_id => $borrowernumber } )->pay(
98         {
99             amount     => $amount,
100             lines      => [ scalar Koha::Account::Lines->find($accountlines_id) ],
101             type       => 'writeoff',
102             note       => $payment_note,
103             library_id => $branch,
104         }
105     );
106 }
107
108 for (@names) {
109     if (/^pay_indiv_(\d+)$/) {
110         my $line_no = $1;
111         redirect_to_paycollect( 'pay_individual', $line_no );
112     } elsif (/^wo_indiv_(\d+)$/) {
113         my $line_no = $1;
114         redirect_to_paycollect( 'writeoff_individual', $line_no );
115     }
116 }
117
118 $template->param(
119     finesview => 1,
120 );
121
122 add_accounts_to_template();
123
124 output_html_with_http_headers $input, $cookie, $template->output;
125
126 sub add_accounts_to_template {
127
128     my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
129     my $account_lines = Koha::Account::Lines->search({ borrowernumber => $borrowernumber, amountoutstanding => { '!=' => 0 } }, { order_by => ['accounttype'] });
130     my @accounts;
131     while ( my $account_line = $account_lines->next ) {
132         $account_line = $account_line->unblessed;
133         if ( $account_line->{itemnumber} ) {
134             my $item = Koha::Items->find( $account_line->{itemnumber} );
135             my $biblio = $item->biblio;
136             $account_line->{biblionumber} = $biblio->biblionumber;
137             $account_line->{title}        = $biblio->title;
138         }
139         push @accounts, $account_line;
140     }
141     borrower_add_additional_fields($patron->unblessed);
142
143     my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
144     $template->param( picture => 1 ) if $patron_image;
145     $template->param(
146         patron   => $patron,
147         accounts => \@accounts,
148         total    => $total,
149     );
150     return;
151
152 }
153
154 sub get_for_redirect {
155     my ( $name, $name_in, $money ) = @_;
156     my $s     = q{&} . $name . q{=};
157     my $value;
158     if (defined $input->param($name_in)) {
159         $value = uri_escape_utf8( scalar $input->param($name_in) );
160     }
161     if ( !defined $value ) {
162         $value = ( $money == 1 ) ? 0 : q{};
163     }
164     if ($money) {
165         $s .= sprintf '%.2f', $value;
166     } else {
167         $s .= $value;
168     }
169     return $s;
170 }
171
172 sub redirect_to_paycollect {
173     my ( $action, $line_no ) = @_;
174     my $redirect =
175       "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
176     $redirect .= q{&};
177     $redirect .= "$action=1";
178     $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
179     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
180     $redirect .=
181       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
182     $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
183     $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
184     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
185     $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
186     $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
187     $redirect .= '&remote_user=';
188     $redirect .= $user;
189     return print $input->redirect($redirect);
190 }
191
192 sub writeoff_all {
193     my @params = @_;
194     my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
195
196     my $borrowernumber = $input->param('borrowernumber');
197
198     for (@wo_lines) {
199         if (/(\d+)/) {
200             my $value           = $1;
201             my $amount          = $input->param("amountoutstanding$value");
202             my $accountlines_id = $input->param("accountlines_id$value");
203             my $payment_note    = $input->param("payment_note_$value");
204             Koha::Account->new( { patron_id => $borrowernumber } )->pay(
205                 {
206                     amount => $amount,
207                     lines  => [ scalar Koha::Account::Lines->find($accountlines_id) ],
208                     type   => 'writeoff',
209                     note   => $payment_note,
210                     library_id => $branch,
211                 }
212             );
213         }
214     }
215
216     print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
217     return;
218 }
219
220 sub borrower_add_additional_fields {
221     my $b_ref = shift;
222
223 # some borrower info is not returned in the standard call despite being assumed
224 # in a number of templates. It should not be the business of this script but in lieu of
225 # a revised api here it is ...
226     if ( $b_ref->{category_type} eq 'C' ) {
227         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
228         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
229         $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
230     } elsif ( $b_ref->{category_type} eq 'A' || $b_ref->{category_type} eq 'I' ) {
231         $b_ref->{adultborrower} = 1;
232     }
233
234     if (C4::Context->preference('ExtendedPatronAttributes')) {
235         $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
236         $template->param(
237             ExtendedPatronAttributes => 1,
238         );
239     }
240
241     return;
242 }
243
244 sub payselected {
245     my @params = @_;
246     my $amt    = 0;
247     my @lines_to_pay;
248     foreach (@params) {
249         if (/^incl_par_(\d+)$/) {
250             my $index = $1;
251             push @lines_to_pay, scalar $input->param("accountlines_id$index");
252             $amt += $input->param("amountoutstanding$index");
253         }
254     }
255     $amt = '&amt=' . $amt;
256     my $sel = '&selected=' . join ',', @lines_to_pay;
257     my $notes = '&notes=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
258     my $redirect =
259         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
260       . $amt
261       . $sel
262       . $notes;
263
264     print $input->redirect($redirect);
265     return;
266 }