Bug 20946: Use K::Account->outstanding_debits in pay.pl and paycollect.pl
[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 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::Members::Attributes qw(GetBorrowerAttributes);
29 use C4::Accounts;
30 use C4::Koha;
31
32 use Koha::Patrons;
33 use Koha::Patron::Categories;
34 use Koha::AuthorisedValues;
35 use Koha::Account;
36 use Koha::Token;
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 => 'edit_borrowers', updatecharges => $updatecharges_permissions },
47         debug           => 1,
48     }
49 );
50
51 # get borrower details
52 my $borrowernumber = $input->param('borrowernumber');
53 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
54 my $patron         = Koha::Patrons->find( $borrowernumber );
55 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
56
57 my $borrower       = $patron->unblessed;
58 my $category       = $patron->category;
59 my $user           = $input->remote_user;
60
61 my $branch         = C4::Context->userenv->{'branch'};
62 my ( $total_due ) = Koha::Account->new( { patron_id => $borrowernumber } )->outstanding_debits;
63 my $total_paid = $input->param('paid');
64
65 my $individual   = $input->param('pay_individual');
66 my $writeoff     = $input->param('writeoff_individual');
67 my $select_lines = $input->param('selected');
68 my $select       = $input->param('selected_accts');
69 my $payment_note = uri_unescape scalar $input->param('payment_note');
70 my $payment_type = scalar $input->param('payment_type');
71 my $accountlines_id;
72
73 if ( $individual || $writeoff ) {
74     if ($individual) {
75         $template->param( pay_individual => 1 );
76     } elsif ($writeoff) {
77         $template->param( writeoff_individual => 1 );
78     }
79     my $accounttype       = $input->param('accounttype');
80     $accountlines_id       = $input->param('accountlines_id');
81     my $amount            = $input->param('amount');
82     my $amountoutstanding = $input->param('amountoutstanding');
83     my $itemnumber  = $input->param('itemnumber');
84     my $description  = $input->param('description');
85     my $title        = $input->param('title');
86     $total_due = $amountoutstanding;
87     $template->param(
88         accounttype       => $accounttype,
89         accountlines_id    => $accountlines_id,
90         amount            => $amount,
91         amountoutstanding => $amountoutstanding,
92         title             => $title,
93         itemnumber        => $itemnumber,
94         individual_description => $description,
95         payment_note    => $payment_note,
96     );
97 } elsif ($select_lines) {
98     $total_due = $input->param('amt');
99     $template->param(
100         selected_accts => $select_lines,
101         amt            => $total_due,
102         selected_accts_notes => scalar $input->param('notes'),
103     );
104 }
105
106 if ( $total_paid and $total_paid ne '0.00' ) {
107     if ( $total_paid < 0 or $total_paid > $total_due ) {
108         $template->param(
109             error_over => 1,
110             total_due => $total_due
111         );
112     } else {
113         die "Wrong CSRF token"
114             unless Koha::Token->new->check_csrf( {
115                 session_id => $input->cookie('CGISESSID'),
116                 token  => scalar $input->param('csrf_token'),
117             });
118
119         if ($individual) {
120             my $line = Koha::Account::Lines->find($accountlines_id);
121             Koha::Account->new( { patron_id => $borrowernumber } )->pay(
122                 {
123                     lines        => [$line],
124                     amount       => $total_paid,
125                     library_id   => $branch,
126                     note         => $payment_note,
127                     payment_type => $payment_type,
128                 }
129             );
130             print $input->redirect(
131                 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber");
132         } else {
133             if ($select) {
134                 if ( $select =~ /^([\d,]*).*/ ) {
135                     $select = $1;    # ensure passing no junk
136                 }
137                 my @acc = split /,/, $select;
138                 my $note = $input->param('selected_accts_notes');
139
140                 my @lines = Koha::Account::Lines->search(
141                     {
142                         borrowernumber    => $borrowernumber,
143                         amountoutstanding => { '<>' => 0 },
144                         accountlines_id   => { 'IN' => \@acc },
145                     },
146                     { order_by => 'date' }
147                 );
148
149                 Koha::Account->new(
150                     {
151                         patron_id => $borrowernumber,
152                     }
153                   )->pay(
154                     {
155                         amount       => $total_paid,
156                         lines        => \@lines,
157                         note         => $note,
158                         payment_type => $payment_type,
159                     }
160                   );
161             }
162             else {
163                 my $note = $input->param('selected_accts_notes');
164                 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
165                     {
166                         amount       => $total_paid,
167                         note         => $note,
168                         payment_type => $payment_type,
169                     }
170                 );
171             }
172
173             print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
174         }
175     }
176 } else {
177     $total_paid = '0.00';    #TODO not right with pay_individual
178 }
179
180 borrower_add_additional_fields($borrower, $template);
181
182 $template->param(%$borrower);
183
184 if ( $input->param('error_over') ) {
185     $template->param( error_over => 1, total_due => scalar $input->param('amountoutstanding') );
186 }
187
188 $template->param(
189     borrowernumber => $borrowernumber,    # some templates require global
190     patron        => $patron,
191     total         => $total_due,
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->categorycode )  if $patron_categories->count == 1;
209     }
210
211     if (C4::Context->preference('ExtendedPatronAttributes')) {
212         $b_ref->{extendedattributes} = GetBorrowerAttributes($b_ref->{borrowernumber});
213     }
214
215     return;
216 }