Merge remote-tracking branch 'origin/new/bug_7143'
[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 C4::Context;
23 use C4::Auth;
24 use C4::Output;
25 use CGI;
26 use C4::Members;
27 use C4::Accounts;
28 use C4::Koha;
29 use C4::Branch;
30
31 my $input = CGI->new();
32
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {   template_name   => 'members/paycollect.tmpl',
35         query           => $input,
36         type            => 'intranet',
37         authnotrequired => 0,
38         flagsrequired   => { borrowers => 1, updatecharges => 1 },
39         debug           => 1,
40     }
41 );
42
43 # get borrower details
44 my $borrowernumber = $input->param('borrowernumber');
45 my $borrower       = GetMember( borrowernumber => $borrowernumber );
46 my $user           = $input->remote_user;
47
48 # get account details
49 my $branch = GetBranch( $input, GetBranches() );
50
51 my ( $total_due, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber);
52 my $total_paid = $input->param('paid');
53
54 my $individual   = $input->param('pay_individual');
55 my $writeoff     = $input->param('writeoff_individual');
56 my $select_lines = $input->param('selected');
57 my $select       = $input->param('selected_accts');
58 my $accountno;
59
60 if ( $individual || $writeoff ) {
61     if ($individual) {
62         $template->param( pay_individual => 1 );
63     } elsif ($writeoff) {
64         $template->param( writeoff_individual => 1 );
65     }
66     my $accounttype       = $input->param('accounttype');
67     my $amount            = $input->param('amount');
68     my $amountoutstanding = $input->param('amountoutstanding');
69     $accountno = $input->param('accountno');
70     my $description  = $input->param('description');
71     my $title        = $input->param('title');
72     my $notify_id    = $input->param('notify_id');
73     my $notify_level = $input->param('notify_level');
74     $total_due = $amountoutstanding;
75     $template->param(
76         accounttype       => $accounttype,
77         accountno         => $accountno,
78         amount            => $amount,
79         amountoutstanding => $amountoutstanding,
80         title             => $title,
81         description       => $description,
82         notify_id         => $notify_id,
83         notify_level      => $notify_level,
84     );
85 } elsif ($select_lines) {
86     $total_due = $input->param('amt');
87     $template->param(
88         selected_accts => $select_lines,
89         amt            => $total_due
90     );
91 }
92
93 if ( $total_paid and $total_paid ne '0.00' ) {
94     if ( $total_paid < 0 or $total_paid > $total_due ) {
95         $template->param(
96             error => sprintf( 'You must pay a value less than or equal to %f.2',
97                 $total_due )
98         );
99     } else {
100         if ($individual) {
101             if ( $total_paid == $total_due ) {
102                 makepayment( $borrowernumber, $accountno, $total_paid, $user,
103                     $branch );
104             } else {
105                 makepartialpayment( $borrowernumber, $accountno, $total_paid,
106                     $user, $branch );
107             }
108             print $input->redirect(
109                 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber");
110         } else {
111             if ($select) {
112                 if ( $select =~ /^([\d,]*).*/ ) {
113                     $select = $1;    # ensure passing no junk
114                 }
115                 my @acc = split /,/, $select;
116                 recordpayment_selectaccts( $borrowernumber, $total_paid,
117                     \@acc );
118             } else {
119                 recordpayment( $borrowernumber, $total_paid );
120             }
121
122 # recordpayment does not return success or failure so lets redisplay the boraccount
123
124             print $input->redirect(
125 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"
126             );
127         }
128     }
129 } else {
130     $total_paid = '0.00';    #TODO not right with pay_individual
131 }
132
133 borrower_add_additional_fields($borrower);
134
135 $template->param(
136
137  #borrowenumber  => $borrower->{borrowernumber}, # some templates require global
138     borrowenumber => $borrowernumber,    # some templates require global
139     borrower      => $borrower,
140     total         => $total_due
141 );
142
143 output_html_with_http_headers $input, $cookie, $template->output;
144
145 sub borrower_add_additional_fields {
146     my $b_ref = shift;
147
148 # some borrower info is not returned in the standard call despite being assumed
149 # in a number of templates. It should not be the business of this script but in lieu of
150 # a revised api here it is ...
151     if ( $b_ref->{category_type} eq 'C' ) {
152         my ( $catcodes, $labels ) =
153           GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
154         if ( @{$catcodes} ) {
155             if ( @{$catcodes} > 1 ) {
156                 $b_ref->{CATCODE_MULTI} = 1;
157             } elsif ( @{$catcodes} == 1 ) {
158                 $b_ref->{catcode} = $catcodes->[0];
159             }
160         }
161     } elsif ( $b_ref->{category_type} eq 'A' ) {
162         $b_ref->{adultborrower} = 1;
163     }
164     my ( $picture, $dberror ) = GetPatronImage( $b_ref->{cardnumber} );
165     if ($picture) {
166         $b_ref->{has_picture} = 1;
167     }
168
169     $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
170     return;
171 }