Bug 15675 [QA Followup] - Close out accruing fines that are not really accruing,...
[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 C4::Branch;
32
33 my $input = CGI->new();
34
35 my $updatecharges_permissions = $input->param('writeoff_individual') ? 'writeoff' : 'remaining_permissions';
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37     {   template_name   => 'members/paycollect.tt',
38         query           => $input,
39         type            => 'intranet',
40         authnotrequired => 0,
41         flagsrequired   => { borrowers => 1, updatecharges => $updatecharges_permissions },
42         debug           => 1,
43     }
44 );
45
46 # get borrower details
47 my $borrowernumber = $input->param('borrowernumber');
48 my $borrower       = GetMember( borrowernumber => $borrowernumber );
49 my $user           = $input->remote_user;
50
51 my $branch         = C4::Context->userenv->{'branch'};
52
53 my ( $total_due, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber);
54 my $total_paid = $input->param('paid');
55
56 my $individual   = $input->param('pay_individual');
57 my $writeoff     = $input->param('writeoff_individual');
58 my $select_lines = $input->param('selected');
59 my $select       = $input->param('selected_accts');
60 my $payment_note = uri_unescape $input->param('payment_note');
61 my $accountno;
62 my $accountlines_id;
63 if ( $individual || $writeoff ) {
64     if ($individual) {
65         $template->param( pay_individual => 1 );
66     } elsif ($writeoff) {
67         $template->param( writeoff_individual => 1 );
68     }
69     my $accounttype       = $input->param('accounttype');
70     $accountlines_id       = $input->param('accountlines_id');
71     my $amount            = $input->param('amount');
72     my $amountoutstanding = $input->param('amountoutstanding');
73     $accountno = $input->param('accountno');
74     my $itemnumber  = $input->param('itemnumber');
75     my $description  = $input->param('description');
76     my $title        = $input->param('title');
77     my $notify_id    = $input->param('notify_id');
78     my $notify_level = $input->param('notify_level');
79     $total_due = $amountoutstanding;
80     $template->param(
81         accounttype       => $accounttype,
82         accountlines_id    => $accountlines_id,
83         accountno         => $accountno,
84         amount            => $amount,
85         amountoutstanding => $amountoutstanding,
86         title             => $title,
87         itemnumber        => $itemnumber,
88         description       => $description,
89         notify_id         => $notify_id,
90         notify_level      => $notify_level,
91         payment_note    => $payment_note,
92     );
93 } elsif ($select_lines) {
94     $total_due = $input->param('amt');
95     $template->param(
96         selected_accts => $select_lines,
97         amt            => $total_due,
98         selected_accts_notes => $input->param('notes'),
99     );
100 }
101
102 if ( $total_paid and $total_paid ne '0.00' ) {
103     if ( $total_paid < 0 or $total_paid > $total_due ) {
104         $template->param(
105             error_over => 1,
106             total_due => $total_due
107         );
108     } else {
109         if ($individual) {
110             if ( $total_paid == $total_due ) {
111                 makepayment( $accountlines_id, $borrowernumber, $accountno, $total_paid, $user,
112                     $branch, $payment_note );
113             } else {
114                 makepartialpayment( $accountlines_id, $borrowernumber, $accountno, $total_paid,
115                     $user, $branch, $payment_note );
116             }
117             print $input->redirect(
118                 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber");
119         } else {
120             if ($select) {
121                 if ( $select =~ /^([\d,]*).*/ ) {
122                     $select = $1;    # ensure passing no junk
123                 }
124                 my @acc = split /,/, $select;
125                 my $note = $input->param('selected_accts_notes');
126                 recordpayment_selectaccts( $borrowernumber, $total_paid, \@acc, $note );
127             } else {
128                 my $note = $input->param('selected_accts_notes');
129                 recordpayment( $borrowernumber, $total_paid, '', $note );
130             }
131
132 # recordpayment does not return success or failure so lets redisplay the boraccount
133
134             print $input->redirect(
135 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"
136             );
137         }
138     }
139 } else {
140     $total_paid = '0.00';    #TODO not right with pay_individual
141 }
142
143 borrower_add_additional_fields($borrower);
144
145 $template->param(%$borrower);
146
147 $template->param(
148     borrowernumber => $borrowernumber,    # some templates require global
149     borrower      => $borrower,
150     total         => $total_due,
151     activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
152     RoutingSerials => C4::Context->preference('RoutingSerials'),
153     ExtendedPatronAttributes => C4::Context->preference('ExtendedPatronAttributes'),
154 );
155
156 output_html_with_http_headers $input, $cookie, $template->output;
157
158 sub borrower_add_additional_fields {
159     my $b_ref = shift;
160
161 # some borrower info is not returned in the standard call despite being assumed
162 # in a number of templates. It should not be the business of this script but in lieu of
163 # a revised api here it is ...
164     if ( $b_ref->{category_type} eq 'C' ) {
165         my ( $catcodes, $labels ) =
166           GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
167         if ( @{$catcodes} ) {
168             if ( @{$catcodes} > 1 ) {
169                 $b_ref->{CATCODE_MULTI} = 1;
170             } elsif ( @{$catcodes} == 1 ) {
171                 $b_ref->{catcode} = $catcodes->[0];
172             }
173         }
174     } elsif ( $b_ref->{category_type} eq 'A' ) {
175         $b_ref->{adultborrower} = 1;
176     }
177
178     my ($picture, $dberror) = GetPatronImage($borrower->{'borrowernumber'});
179     $template->param( picture => 1 ) if $picture;
180
181     if (C4::Context->preference('ExtendedPatronAttributes')) {
182         $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
183     }
184
185     $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
186     return;
187 }