Bug 766: Create a plugin routine to build dropdown list
[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 $updatecharges_permissions = $input->param('writeoff_individual') ? 'writeoff' : 'remaining_permissions';
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {   template_name   => 'members/paycollect.tmpl',
37         query           => $input,
38         type            => 'intranet',
39         authnotrequired => 0,
40         flagsrequired   => { borrowers => 1, updatecharges => $updatecharges_permissions },
41         debug           => 1,
42     }
43 );
44
45 # get borrower details
46 my $borrowernumber = $input->param('borrowernumber');
47 my $borrower       = GetMember( borrowernumber => $borrowernumber );
48 my $user           = $input->remote_user;
49
50 # get account details
51 my $branch = GetBranch( $input, GetBranches() );
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                 recordpayment( $borrowernumber, $total_paid );
129             }
130
131 # recordpayment does not return success or failure so lets redisplay the boraccount
132
133             print $input->redirect(
134 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"
135             );
136         }
137     }
138 } else {
139     $total_paid = '0.00';    #TODO not right with pay_individual
140 }
141
142 borrower_add_additional_fields($borrower);
143
144 $template->param(
145     borrowernumber => $borrowernumber,    # some templates require global
146     borrower      => $borrower,
147     total         => $total_due,
148     activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
149     RoutingSerials => C4::Context->preference('RoutingSerials'),
150 );
151
152 output_html_with_http_headers $input, $cookie, $template->output;
153
154 sub borrower_add_additional_fields {
155     my $b_ref = shift;
156
157 # some borrower info is not returned in the standard call despite being assumed
158 # in a number of templates. It should not be the business of this script but in lieu of
159 # a revised api here it is ...
160     if ( $b_ref->{category_type} eq 'C' ) {
161         my ( $catcodes, $labels ) =
162           GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
163         if ( @{$catcodes} ) {
164             if ( @{$catcodes} > 1 ) {
165                 $b_ref->{CATCODE_MULTI} = 1;
166             } elsif ( @{$catcodes} == 1 ) {
167                 $b_ref->{catcode} = $catcodes->[0];
168             }
169         }
170     } elsif ( $b_ref->{category_type} eq 'A' ) {
171         $b_ref->{adultborrower} = 1;
172     }
173     my ( $picture, $dberror ) = GetPatronImage( $b_ref->{borrowernumber} );
174     if ($picture) {
175         $b_ref->{has_picture} = 1;
176     }
177
178     $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
179     return;
180 }