Bug 12497: Fix search history non-accessible when OPAC was private
[koha.git] / members / paycollect.pl
1 #!/usr/bin/perl
2 # Copyright 2009,2010 PTFS Inc.
3 # Copyright 2011 PTFS-Europe Ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22 use URI::Escape;
23 use C4::Context;
24 use C4::Auth;
25 use C4::Output;
26 use CGI qw ( -utf8 );
27 use C4::Members;
28 use C4::Members::Attributes qw(GetBorrowerAttributes);
29 use C4::Accounts;
30 use C4::Koha;
31 use Koha::Patron::Images;
32 use Koha::Patrons;
33 use Koha::Account;
34 use Koha::Token;
35
36 use Koha::Patron::Categories;
37
38 my $input = CGI->new();
39
40 my $updatecharges_permissions = $input->param('writeoff_individual') ? 'writeoff' : 'remaining_permissions';
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {   template_name   => 'members/paycollect.tt',
43         query           => $input,
44         type            => 'intranet',
45         authnotrequired => 0,
46         flagsrequired   => { borrowers => 1, updatecharges => $updatecharges_permissions },
47         debug           => 1,
48     }
49 );
50
51 # get borrower details
52 my $borrowernumber = $input->param('borrowernumber');
53 my $patron         = Koha::Patrons->find( $borrowernumber );
54 unless ( $patron ) {
55     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
56     exit;
57 }
58 my $borrower       = $patron->unblessed;
59 my $category       = $patron->category;
60 $borrower->{description} = $category->description;
61 $borrower->{category_type} = $category->category_type;
62 my $user           = $input->remote_user;
63
64 my $branch         = C4::Context->userenv->{'branch'};
65
66 my ( $total_due, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber);
67 my $total_paid = $input->param('paid');
68
69 my $individual   = $input->param('pay_individual');
70 my $writeoff     = $input->param('writeoff_individual');
71 my $select_lines = $input->param('selected');
72 my $select       = $input->param('selected_accts');
73 my $payment_note = uri_unescape scalar $input->param('payment_note');
74 my $accountlines_id;
75
76 if ( $individual || $writeoff ) {
77     if ($individual) {
78         $template->param( pay_individual => 1 );
79     } elsif ($writeoff) {
80         $template->param( writeoff_individual => 1 );
81     }
82     my $accounttype       = $input->param('accounttype');
83     $accountlines_id       = $input->param('accountlines_id');
84     my $amount            = $input->param('amount');
85     my $amountoutstanding = $input->param('amountoutstanding');
86     my $itemnumber  = $input->param('itemnumber');
87     my $description  = $input->param('description');
88     my $title        = $input->param('title');
89     my $notify_id    = $input->param('notify_id');
90     my $notify_level = $input->param('notify_level');
91     $total_due = $amountoutstanding;
92     $template->param(
93         accounttype       => $accounttype,
94         accountlines_id    => $accountlines_id,
95         amount            => $amount,
96         amountoutstanding => $amountoutstanding,
97         title             => $title,
98         itemnumber        => $itemnumber,
99         individual_description => $description,
100         notify_id         => $notify_id,
101         notify_level      => $notify_level,
102         payment_note    => $payment_note,
103     );
104 } elsif ($select_lines) {
105     $total_due = $input->param('amt');
106     $template->param(
107         selected_accts => $select_lines,
108         amt            => $total_due,
109         selected_accts_notes => scalar $input->param('notes'),
110     );
111 }
112
113 if ( $total_paid and $total_paid ne '0.00' ) {
114     if ( $total_paid < 0 or $total_paid > $total_due ) {
115         $template->param(
116             error_over => 1,
117             total_due => $total_due
118         );
119     } else {
120         die "Wrong CSRF token"
121             unless Koha::Token->new->check_csrf( {
122                 session_id => $input->cookie('CGISESSID'),
123                 token  => scalar $input->param('csrf_token'),
124             });
125
126         if ($individual) {
127             my $line = Koha::Account::Lines->find($accountlines_id);
128             Koha::Account->new( { patron_id => $borrowernumber } )->pay(
129                 {
130                     lines      => [$line],
131                     amount     => $total_paid,
132                     library_id => $branch,
133                     note       => $payment_note
134                 }
135             );
136             print $input->redirect(
137                 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber");
138         } else {
139             if ($select) {
140                 if ( $select =~ /^([\d,]*).*/ ) {
141                     $select = $1;    # ensure passing no junk
142                 }
143                 my @acc = split /,/, $select;
144                 my $note = $input->param('selected_accts_notes');
145
146                 my @lines = Koha::Account::Lines->search(
147                     {
148                         borrowernumber    => $borrowernumber,
149                         amountoutstanding => { '<>' => 0 },
150                         accountlines_id   => { 'IN' => \@acc },
151                     },
152                     { order_by => 'date' }
153                 );
154
155                 Koha::Account->new(
156                     {
157                         patron_id => $borrowernumber,
158                     }
159                   )->pay(
160                     {
161                         amount => $total_paid,
162                         lines  => \@lines,
163                         note   => $note,
164                     }
165                   );
166             }
167             else {
168                 my $note = $input->param('selected_accts_notes');
169                 Koha::Account->new( { patron_id => $borrowernumber } )
170                   ->pay( { amount => $total_paid, note => $note } );
171             }
172
173             print $input->redirect(
174 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"
175             );
176         }
177     }
178 } else {
179     $total_paid = '0.00';    #TODO not right with pay_individual
180 }
181
182 borrower_add_additional_fields($borrower, $template);
183
184 $template->param(%$borrower);
185
186 $template->param(
187     borrowernumber => $borrowernumber,    # some templates require global
188     borrower      => $borrower,
189     categoryname  => $borrower->{description},
190     total         => $total_due,
191     RoutingSerials => C4::Context->preference('RoutingSerials'),
192     ExtendedPatronAttributes => C4::Context->preference('ExtendedPatronAttributes'),
193
194     csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
195 );
196
197 output_html_with_http_headers $input, $cookie, $template->output;
198
199 sub borrower_add_additional_fields {
200     my ( $b_ref, $template ) = @_;
201
202 # some borrower info is not returned in the standard call despite being assumed
203 # in a number of templates. It should not be the business of this script but in lieu of
204 # a revised api here it is ...
205     if ( $b_ref->{category_type} eq 'C' ) {
206         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
207         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
208         $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
209     } elsif ( $b_ref->{category_type} eq 'A' || $b_ref->{category_type} eq 'I' ) {
210         $b_ref->{adultborrower} = 1;
211     }
212
213     my $patron_image = Koha::Patron::Images->find($b_ref->{borrowernumber});
214     $template->param( picture => 1 ) if $patron_image;
215
216     if (C4::Context->preference('ExtendedPatronAttributes')) {
217         $b_ref->{extendedattributes} = GetBorrowerAttributes($b_ref->{borrowernumber});
218     }
219
220     return;
221 }