Bug 6554 - make Koha internally utf-8 clean
[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 my $accountlines_id;
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     $accountlines_id       = $input->param('accountlines_id');
68     my $amount            = $input->param('amount');
69     my $amountoutstanding = $input->param('amountoutstanding');
70     $accountno = $input->param('accountno');
71     my $itemnumber  = $input->param('itemnumber');
72     my $description  = $input->param('description');
73     my $title        = $input->param('title');
74     my $notify_id    = $input->param('notify_id');
75     my $notify_level = $input->param('notify_level');
76     $total_due = $amountoutstanding;
77     $template->param(
78         accounttype       => $accounttype,
79         accountlines_id    => $accountlines_id,
80         accountno         => $accountno,
81         amount            => $amount,
82         amountoutstanding => $amountoutstanding,
83         title             => $title,
84         itemnumber        => $itemnumber,
85         description       => $description,
86         notify_id         => $notify_id,
87         notify_level      => $notify_level,
88     );
89 } elsif ($select_lines) {
90     $total_due = $input->param('amt');
91     $template->param(
92         selected_accts => $select_lines,
93         amt            => $total_due
94     );
95 }
96
97 if ( $total_paid and $total_paid ne '0.00' ) {
98     if ( $total_paid < 0 or $total_paid > $total_due ) {
99         $template->param(
100             error_over => 1,
101             total_due => $total_due
102         );
103     } else {
104         if ($individual) {
105             if ( $total_paid == $total_due ) {
106                 makepayment( $accountlines_id, $borrowernumber, $accountno, $total_paid, $user,
107                     $branch );
108             } else {
109                 makepartialpayment( $accountlines_id, $borrowernumber, $accountno, $total_paid,
110                     $user, $branch );
111             }
112             print $input->redirect(
113                 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber");
114         } else {
115             if ($select) {
116                 if ( $select =~ /^([\d,]*).*/ ) {
117                     $select = $1;    # ensure passing no junk
118                 }
119                 my @acc = split /,/, $select;
120                 recordpayment_selectaccts( $borrowernumber, $total_paid,
121                     \@acc );
122             } else {
123                 recordpayment( $borrowernumber, $total_paid );
124             }
125
126 # recordpayment does not return success or failure so lets redisplay the boraccount
127
128             print $input->redirect(
129 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"
130             );
131         }
132     }
133 } else {
134     $total_paid = '0.00';    #TODO not right with pay_individual
135 }
136
137 borrower_add_additional_fields($borrower);
138
139 $template->param(
140     borrowernumber => $borrowernumber,    # some templates require global
141     borrower      => $borrower,
142     total         => $total_due,
143     activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
144     RoutingSerials => C4::Context->preference('RoutingSerials'),
145 );
146
147 output_html_with_http_headers $input, $cookie, $template->output;
148
149 sub borrower_add_additional_fields {
150     my $b_ref = shift;
151
152 # some borrower info is not returned in the standard call despite being assumed
153 # in a number of templates. It should not be the business of this script but in lieu of
154 # a revised api here it is ...
155     if ( $b_ref->{category_type} eq 'C' ) {
156         my ( $catcodes, $labels ) =
157           GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
158         if ( @{$catcodes} ) {
159             if ( @{$catcodes} > 1 ) {
160                 $b_ref->{CATCODE_MULTI} = 1;
161             } elsif ( @{$catcodes} == 1 ) {
162                 $b_ref->{catcode} = $catcodes->[0];
163             }
164         }
165     } elsif ( $b_ref->{category_type} eq 'A' ) {
166         $b_ref->{adultborrower} = 1;
167     }
168     my ( $picture, $dberror ) = GetPatronImage( $b_ref->{cardnumber} );
169     if ($picture) {
170         $b_ref->{has_picture} = 1;
171     }
172
173     $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
174     return;
175 }