Bug 14697: Enhance the return claims feature
[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 Modern::Perl;
21 use URI::Escape;
22 use CGI qw ( -utf8 );
23
24 use C4::Context;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Members;
28 use C4::Accounts;
29 use C4::Koha;
30
31 use Koha::Cash::Registers;
32 use Koha::Patrons;
33 use Koha::Patron::Categories;
34 use Koha::AuthorisedValues;
35 use Koha::Account;
36 use Koha::Token;
37
38 my $input = CGI->new();
39
40 my $payment_id          = $input->param('payment_id');
41 my $writeoff_individual = $input->param('writeoff_individual');
42 my $change_given        = $input->param('change_given');
43 my $type                = scalar $input->param('type') || 'PAYMENT';
44
45 my $updatecharges_permissions = ($writeoff_individual || $type eq 'writeoff') ? 'writeoff' : 'remaining_permissions';
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47     {   template_name   => 'members/paycollect.tt',
48         query           => $input,
49         type            => 'intranet',
50         authnotrequired => 0,
51         flagsrequired   => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
52         debug           => 1,
53     }
54 );
55
56 # get borrower details
57 my $borrowernumber = $input->param('borrowernumber');
58 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
59 my $patron         = Koha::Patrons->find( $borrowernumber );
60 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
61
62 my $borrower       = $patron->unblessed;
63 my $account        = $patron->account;
64 my $category       = $patron->category;
65 my $user           = $input->remote_user;
66
67 my $library_id = C4::Context->userenv->{'branch'};
68 my $total_due  = $account->outstanding_debits->total_outstanding;
69
70 my $total_paid = $input->param('paid');
71
72 my $select_lines = $input->param('selected');
73 my $pay_individual   = $input->param('pay_individual');
74 my $select       = $input->param('selected_accts');
75 my $payment_note = uri_unescape scalar $input->param('payment_note');
76 my $payment_type = scalar $input->param('payment_type');
77 my $accountlines_id;
78
79 my $registerid;
80 if ( C4::Context->preference('UseCashRegisters') ) {
81     $registerid = $input->param('registerid');
82     my $registers  = Koha::Cash::Registers->search(
83         { branch   => $library_id, archived => 0 },
84         { order_by => { '-asc' => 'name' } }
85     );
86
87     if ( !$registers->count ) {
88         $template->param( error_registers => 1 );
89     }
90     else {
91
92         if ( !$registerid ) {
93             my $default_register = Koha::Cash::Registers->find(
94                 { branch => $library_id, branch_default => 1 } );
95             $registerid = $default_register->id if $default_register;
96         }
97         $registerid = $registers->next->id if !$registerid;
98
99         $template->param(
100             registerid => $registerid,
101             registers  => $registers,
102         );
103     }
104 }
105
106 if ( $pay_individual || $writeoff_individual ) {
107     if ($pay_individual) {
108         $template->param( pay_individual => 1 );
109     } elsif ($writeoff_individual) {
110         $template->param( writeoff_individual => 1 );
111     }
112     my $debit_type_code   = $input->param('debit_type_code');
113     $accountlines_id      = $input->param('accountlines_id');
114     my $amount            = $input->param('amount');
115     my $amountoutstanding = $input->param('amountoutstanding');
116     my $itemnumber  = $input->param('itemnumber');
117     my $description  = $input->param('description');
118     my $title        = $input->param('title');
119     $total_due = $amountoutstanding;
120     $template->param(
121         debit_type_code    => $debit_type_code,
122         accountlines_id    => $accountlines_id,
123         amount            => $amount,
124         amountoutstanding => $amountoutstanding,
125         title             => $title,
126         itemnumber        => $itemnumber,
127         individual_description => $description,
128         payment_note    => $payment_note,
129     );
130 } elsif ($select_lines) {
131     $total_due = $input->param('amt');
132     $template->param(
133         selected_accts => $select_lines,
134         amt            => $total_due,
135         selected_accts_notes => scalar $input->param('notes'),
136     );
137 }
138
139 if ( $total_paid and $total_paid ne '0.00' ) {
140     $total_paid = $total_due if (abs($total_paid - $total_due) < 0.01) && C4::Context->preference('RoundFinesAtPayment');
141     if ( $total_paid < 0 or $total_paid > $total_due ) {
142         $template->param(
143             error_over => 1,
144             total_due => $total_due
145         );
146     } else {
147         output_and_exit( $input, $cookie, $template,  'wrong_csrf_token' )
148             unless Koha::Token->new->check_csrf( {
149                 session_id => $input->cookie('CGISESSID'),
150                 token  => scalar $input->param('csrf_token'),
151             });
152
153         if ($pay_individual) {
154             my $line = Koha::Account::Lines->find($accountlines_id);
155             $payment_id = $account->pay(
156                 {
157                     lines        => [$line],
158                     amount       => $total_paid,
159                     library_id   => $library_id,
160                     note         => $payment_note,
161                     interface    => C4::Context->interface,
162                     payment_type => $payment_type,
163                     cash_register => $registerid
164                 }
165             );
166             print $input->redirect(
167                 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber&payment_id=$payment_id&change_given=$change_given");
168         } else {
169             if ($select) {
170                 if ( $select =~ /^([\d,]*).*/ ) {
171                     $select = $1;    # ensure passing no junk
172                 }
173                 my @acc = split /,/, $select;
174                 my $note = $input->param('selected_accts_notes');
175
176                 my @lines = Koha::Account::Lines->search(
177                     {
178                         borrowernumber    => $borrowernumber,
179                         amountoutstanding => { '<>' => 0 },
180                         accountlines_id   => { 'IN' => \@acc },
181                     },
182                     { order_by => 'date' }
183                 );
184
185                 $payment_id = $account->pay(
186                     {
187                         type         => $type,
188                         amount       => $total_paid,
189                         library_id   => $library_id,
190                         lines        => \@lines,
191                         note         => $note,
192                         interface    => C4::Context->interface,
193                         payment_type => $payment_type,
194                         cash_register => $registerid
195                     }
196                   );
197             }
198             else {
199                 my $note = $input->param('selected_accts_notes');
200                 $payment_id = $account->pay(
201                     {
202                         amount       => $total_paid,
203                         library_id   => $library_id,
204                         note         => $note,
205                         payment_type => $payment_type,
206                         interface    => C4::Context->interface,
207                         payment_type => $payment_type,
208                         cash_register => $registerid
209                     }
210                 );
211             }
212
213             print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber&payment_id=$payment_id&change_given=$change_given");
214         }
215     }
216 } else {
217     $total_paid = '0.00';    #TODO not right with pay_individual
218 }
219
220 $template->param(%$borrower);
221
222 if ( $input->param('error_over') ) {
223     $template->param( error_over => 1, total_due => scalar $input->param('amountoutstanding') );
224 }
225
226 $template->param(
227     payment_id => $payment_id,
228
229     type           => $type,
230     borrowernumber => $borrowernumber,    # some templates require global
231     patron         => $patron,
232     total          => $total_due,
233
234     csrf_token => Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID') } ),
235 );
236
237 output_html_with_http_headers $input, $cookie, $template->output;