Bug 6413 Added ability to add a note when paying or writing off a fine
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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;
27 use C4::Members;
28 use C4::Accounts;
29 use C4::Koha;
30 use C4::Branch;
31
32 my $input = CGI->new();
33
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {   template_name   => 'members/paycollect.tmpl',
36         query           => $input,
37         type            => 'intranet',
38         authnotrequired => 0,
39         flagsrequired   => { borrowers => 1, updatecharges => 1 },
40         debug           => 1,
41     }
42 );
43
44 # get borrower details
45 my $borrowernumber = $input->param('borrowernumber');
46 my $borrower       = GetMember( borrowernumber => $borrowernumber );
47 my $user           = $input->remote_user;
48
49 # get account details
50 my $branch = GetBranch( $input, GetBranches() );
51
52 my ( $total_due, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber);
53 my $total_paid = $input->param('paid');
54
55 my $individual   = $input->param('pay_individual');
56 my $writeoff     = $input->param('writeoff_individual');
57 my $select_lines = $input->param('selected');
58 my $select       = $input->param('selected_accts');
59 my $payment_note = uri_unescape $input->param('payment_note');
60 my $accountno;
61 my $accountlines_id;
62 if ( $individual || $writeoff ) {
63     if ($individual) {
64         $template->param( pay_individual => 1 );
65     } elsif ($writeoff) {
66         $template->param( writeoff_individual => 1 );
67     }
68     my $accounttype       = $input->param('accounttype');
69     $accountlines_id       = $input->param('accountlines_id');
70     my $amount            = $input->param('amount');
71     my $amountoutstanding = $input->param('amountoutstanding');
72     $accountno = $input->param('accountno');
73     my $itemnumber  = $input->param('itemnumber');
74     my $description  = $input->param('description');
75     my $title        = $input->param('title');
76     my $notify_id    = $input->param('notify_id');
77     my $notify_level = $input->param('notify_level');
78     $total_due = $amountoutstanding;
79     $template->param(
80         accounttype       => $accounttype,
81         accountlines_id    => $accountlines_id,
82         accountno         => $accountno,
83         amount            => $amount,
84         amountoutstanding => $amountoutstanding,
85         title             => $title,
86         itemnumber        => $itemnumber,
87         description       => $description,
88         notify_id         => $notify_id,
89         notify_level      => $notify_level,
90         payment_note    => $payment_note,
91     );
92 } elsif ($select_lines) {
93     $total_due = $input->param('amt');
94     $template->param(
95         selected_accts => $select_lines,
96         amt            => $total_due
97     );
98 }
99
100 if ( $total_paid and $total_paid ne '0.00' ) {
101     if ( $total_paid < 0 or $total_paid > $total_due ) {
102         $template->param(
103             error_over => 1,
104             total_due => $total_due
105         );
106     } else {
107         if ($individual) {
108             if ( $total_paid == $total_due ) {
109                 makepayment( $accountlines_id, $borrowernumber, $accountno, $total_paid, $user,
110                     $branch, $payment_note );
111             } else {
112                 makepartialpayment( $accountlines_id, $borrowernumber, $accountno, $total_paid,
113                     $user, $branch, $payment_note );
114             }
115             print $input->redirect(
116                 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber");
117         } else {
118             if ($select) {
119                 if ( $select =~ /^([\d,]*).*/ ) {
120                     $select = $1;    # ensure passing no junk
121                 }
122                 my @acc = split /,/, $select;
123                 recordpayment_selectaccts( $borrowernumber, $total_paid,
124                     \@acc );
125             } else {
126                 recordpayment( $borrowernumber, $total_paid );
127             }
128
129 # recordpayment does not return success or failure so lets redisplay the boraccount
130
131             print $input->redirect(
132 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"
133             );
134         }
135     }
136 } else {
137     $total_paid = '0.00';    #TODO not right with pay_individual
138 }
139
140 borrower_add_additional_fields($borrower);
141
142 $template->param(
143     borrowernumber => $borrowernumber,    # some templates require global
144     borrower      => $borrower,
145     total         => $total_due,
146     activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
147     RoutingSerials => C4::Context->preference('RoutingSerials'),
148 );
149
150 output_html_with_http_headers $input, $cookie, $template->output;
151
152 sub borrower_add_additional_fields {
153     my $b_ref = shift;
154
155 # some borrower info is not returned in the standard call despite being assumed
156 # in a number of templates. It should not be the business of this script but in lieu of
157 # a revised api here it is ...
158     if ( $b_ref->{category_type} eq 'C' ) {
159         my ( $catcodes, $labels ) =
160           GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
161         if ( @{$catcodes} ) {
162             if ( @{$catcodes} > 1 ) {
163                 $b_ref->{CATCODE_MULTI} = 1;
164             } elsif ( @{$catcodes} == 1 ) {
165                 $b_ref->{catcode} = $catcodes->[0];
166             }
167         }
168     } elsif ( $b_ref->{category_type} eq 'A' ) {
169         $b_ref->{adultborrower} = 1;
170     }
171     my ( $picture, $dberror ) = GetPatronImage( $b_ref->{cardnumber} );
172     if ($picture) {
173         $b_ref->{has_picture} = 1;
174     }
175
176     $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
177     return;
178 }