Bug 21801: Make paycollect.pl pass library_id when calling ->pay
[koha-equinox.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 Modern::Perl;
21 use URI::Escape;
22 use CGI qw ( -utf8 );
23
24 use C4::Context;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Members;
28 use C4::Accounts;
29 use C4::Koha;
30
31 use Koha::Patrons;
32 use Koha::Patron::Categories;
33 use Koha::AuthorisedValues;
34 use Koha::Account;
35 use Koha::Token;
36
37 my $input = CGI->new();
38
39 my $writeoff_individual       = $input->param('writeoff_individual');
40 my $type                      = scalar $input->param('type') || 'payment';
41
42 my $updatecharges_permissions = ($writeoff_individual || $type eq 'writeoff') ? 'writeoff' : 'remaining_permissions';
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {   template_name   => 'members/paycollect.tt',
45         query           => $input,
46         type            => 'intranet',
47         authnotrequired => 0,
48         flagsrequired   => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
49         debug           => 1,
50     }
51 );
52
53 # get borrower details
54 my $borrowernumber = $input->param('borrowernumber');
55 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
56 my $patron         = Koha::Patrons->find( $borrowernumber );
57 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
58
59 my $borrower       = $patron->unblessed;
60 my $category       = $patron->category;
61 my $user           = $input->remote_user;
62
63 my $branch    = C4::Context->userenv->{'branch'};
64 my $total_due = $patron->account->outstanding_debits->total_outstanding;
65
66 my $total_paid = $input->param('paid');
67
68 my $select_lines = $input->param('selected');
69 my $pay_individual   = $input->param('pay_individual');
70 my $select       = $input->param('selected_accts');
71 my $payment_note = uri_unescape scalar $input->param('payment_note');
72 my $payment_type = scalar $input->param('payment_type');
73 my $accountlines_id;
74
75 if ( $pay_individual || $writeoff_individual ) {
76     if ($pay_individual) {
77         $template->param( pay_individual => 1 );
78     } elsif ($writeoff_individual) {
79         $template->param( writeoff_individual => 1 );
80     }
81     my $accounttype       = $input->param('accounttype');
82     $accountlines_id       = $input->param('accountlines_id');
83     my $amount            = $input->param('amount');
84     my $amountoutstanding = $input->param('amountoutstanding');
85     my $itemnumber  = $input->param('itemnumber');
86     my $description  = $input->param('description');
87     my $title        = $input->param('title');
88     $total_due = $amountoutstanding;
89     $template->param(
90         accounttype       => $accounttype,
91         accountlines_id    => $accountlines_id,
92         amount            => $amount,
93         amountoutstanding => $amountoutstanding,
94         title             => $title,
95         itemnumber        => $itemnumber,
96         individual_description => $description,
97         payment_note    => $payment_note,
98     );
99 } elsif ($select_lines) {
100     $total_due = $input->param('amt');
101     $template->param(
102         selected_accts => $select_lines,
103         amt            => $total_due,
104         selected_accts_notes => scalar $input->param('notes'),
105     );
106 }
107
108 if ( $total_paid and $total_paid ne '0.00' ) {
109     if ( $total_paid < 0 or $total_paid > $total_due ) {
110         $template->param(
111             error_over => 1,
112             total_due => $total_due
113         );
114     } else {
115         output_and_exit( $input, $cookie, $template,  'wrong_csrf_token' )
116             unless Koha::Token->new->check_csrf( {
117                 session_id => $input->cookie('CGISESSID'),
118                 token  => scalar $input->param('csrf_token'),
119             });
120
121         if ($pay_individual) {
122             my $line = Koha::Account::Lines->find($accountlines_id);
123             Koha::Account->new( { patron_id => $borrowernumber } )->pay(
124                 {
125                     lines        => [$line],
126                     amount       => $total_paid,
127                     library_id   => $branch,
128                     note         => $payment_note,
129                     interface    => C4::Context->interface,
130                     payment_type => $payment_type,
131                 }
132             );
133             print $input->redirect(
134                 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber");
135         } else {
136             if ($select) {
137                 if ( $select =~ /^([\d,]*).*/ ) {
138                     $select = $1;    # ensure passing no junk
139                 }
140                 my @acc = split /,/, $select;
141                 my $note = $input->param('selected_accts_notes');
142
143                 my @lines = Koha::Account::Lines->search(
144                     {
145                         borrowernumber    => $borrowernumber,
146                         amountoutstanding => { '<>' => 0 },
147                         accountlines_id   => { 'IN' => \@acc },
148                     },
149                     { order_by => 'date' }
150                 );
151
152                 Koha::Account->new(
153                     {
154                         patron_id => $borrowernumber,
155                     }
156                   )->pay(
157                     {
158                         type         => $type,
159                         amount       => $total_paid,
160                         library_id   => $branch,
161                         lines        => \@lines,
162                         note         => $note,
163                         interface    => C4::Context->interface,
164                         payment_type => $payment_type,
165                     }
166                   );
167             }
168             else {
169                 my $note = $input->param('selected_accts_notes');
170                 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
171                     {
172                         amount       => $total_paid,
173                         library_id   => $branch,
174                         note         => $note,
175                         payment_type => $payment_type,
176                         interface    => C4::Context->interface
177                     }
178                 );
179             }
180
181             print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
182         }
183     }
184 } else {
185     $total_paid = '0.00';    #TODO not right with pay_individual
186 }
187
188 $template->param(%$borrower);
189
190 if ( $input->param('error_over') ) {
191     $template->param( error_over => 1, total_due => scalar $input->param('amountoutstanding') );
192 }
193
194 $template->param(
195     type           => $type,
196     borrowernumber => $borrowernumber,    # some templates require global
197     patron        => $patron,
198     total         => $total_due,
199
200     csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
201 );
202
203 output_html_with_http_headers $input, $cookie, $template->output;