Bug 23710: (follow-up) Add tests for new features in Koha::REST::V!::Holds::add and...
[koha.git] / members / paycollect.pl
blob0974997885817ac0c8b595302fc8bcc3dfefe27e
1 #!/usr/bin/perl
2 # Copyright 2009,2010 PTFS Inc.
3 # Copyright 2011 PTFS-Europe Ltd
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
21 use URI::Escape;
22 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Members;
28 use C4::Accounts;
29 use C4::Koha;
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;
38 my $input = CGI->new();
40 my $payment_id = $input->param('payment_id');
41 my $writeoff_individual = $input->param('writeoff_individual');
42 my $type = scalar $input->param('type') || 'payment';
44 my $updatecharges_permissions = ($writeoff_individual || $type eq 'writeoff') ? 'writeoff' : 'remaining_permissions';
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
46 { template_name => 'members/paycollect.tt',
47 query => $input,
48 type => 'intranet',
49 authnotrequired => 0,
50 flagsrequired => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
51 debug => 1,
55 # get borrower details
56 my $borrowernumber = $input->param('borrowernumber');
57 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
58 my $patron = Koha::Patrons->find( $borrowernumber );
59 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
61 my $borrower = $patron->unblessed;
62 my $account = $patron->account;
63 my $category = $patron->category;
64 my $user = $input->remote_user;
66 my $library_id = C4::Context->userenv->{'branch'};
67 my $total_due = $account->outstanding_debits->total_outstanding;
69 my $total_paid = $input->param('paid');
71 my $select_lines = $input->param('selected');
72 my $pay_individual = $input->param('pay_individual');
73 my $select = $input->param('selected_accts');
74 my $payment_note = uri_unescape scalar $input->param('payment_note');
75 my $payment_type = scalar $input->param('payment_type');
76 my $accountlines_id;
78 my $registerid;
79 if ( C4::Context->preference('UseCashRegisters') ) {
80 $registerid = $input->param('registerid');
81 my $registers = Koha::Cash::Registers->search(
82 { branch => $library_id, archived => 0 },
83 { order_by => { '-asc' => 'name' } }
86 if ( !$registers->count ) {
87 $template->param( error_registers => 1 );
89 else {
91 if ( !$registerid ) {
92 my $default_register = Koha::Cash::Registers->find(
93 { branch => $library_id, branch_default => 1 } );
94 $registerid = $default_register->id if $default_register;
96 $registerid = $registers->next->id if !$registerid;
98 $template->param(
99 registerid => $registerid,
100 registers => $registers,
105 if ( $pay_individual || $writeoff_individual ) {
106 if ($pay_individual) {
107 $template->param( pay_individual => 1 );
108 } elsif ($writeoff_individual) {
109 $template->param( writeoff_individual => 1 );
111 my $accounttype = $input->param('accounttype');
112 $accountlines_id = $input->param('accountlines_id');
113 my $amount = $input->param('amount');
114 my $amountoutstanding = $input->param('amountoutstanding');
115 my $itemnumber = $input->param('itemnumber');
116 my $description = $input->param('description');
117 my $title = $input->param('title');
118 $total_due = $amountoutstanding;
119 $template->param(
120 accounttype => $accounttype,
121 accountlines_id => $accountlines_id,
122 amount => $amount,
123 amountoutstanding => $amountoutstanding,
124 title => $title,
125 itemnumber => $itemnumber,
126 individual_description => $description,
127 payment_note => $payment_note,
129 } elsif ($select_lines) {
130 $total_due = $input->param('amt');
131 $template->param(
132 selected_accts => $select_lines,
133 amt => $total_due,
134 selected_accts_notes => scalar $input->param('notes'),
138 if ( $total_paid and $total_paid ne '0.00' ) {
139 if ( $total_paid < 0 or $total_paid > $total_due ) {
140 $template->param(
141 error_over => 1,
142 total_due => $total_due
144 } else {
145 output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
146 unless Koha::Token->new->check_csrf( {
147 session_id => $input->cookie('CGISESSID'),
148 token => scalar $input->param('csrf_token'),
151 if ($pay_individual) {
152 my $line = Koha::Account::Lines->find($accountlines_id);
153 $payment_id = $account->pay(
155 lines => [$line],
156 amount => $total_paid,
157 library_id => $library_id,
158 note => $payment_note,
159 interface => C4::Context->interface,
160 payment_type => $payment_type,
161 cash_register => $registerid
164 print $input->redirect(
165 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber&payment_id=$payment_id");
166 } else {
167 if ($select) {
168 if ( $select =~ /^([\d,]*).*/ ) {
169 $select = $1; # ensure passing no junk
171 my @acc = split /,/, $select;
172 my $note = $input->param('selected_accts_notes');
174 my @lines = Koha::Account::Lines->search(
176 borrowernumber => $borrowernumber,
177 amountoutstanding => { '<>' => 0 },
178 accountlines_id => { 'IN' => \@acc },
180 { order_by => 'date' }
183 $payment_id = $account->pay(
185 type => $type,
186 amount => $total_paid,
187 library_id => $library_id,
188 lines => \@lines,
189 note => $note,
190 interface => C4::Context->interface,
191 payment_type => $payment_type,
192 cash_register => $registerid
196 else {
197 my $note = $input->param('selected_accts_notes');
198 $payment_id = $account->pay(
200 amount => $total_paid,
201 library_id => $library_id,
202 note => $note,
203 payment_type => $payment_type,
204 interface => C4::Context->interface,
205 payment_type => $payment_type,
206 cash_register => $registerid
211 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber&payment_id=$payment_id");
214 } else {
215 $total_paid = '0.00'; #TODO not right with pay_individual
218 $template->param(%$borrower);
220 if ( $input->param('error_over') ) {
221 $template->param( error_over => 1, total_due => scalar $input->param('amountoutstanding') );
224 $template->param(
225 payment_id => $payment_id,
227 type => $type,
228 borrowernumber => $borrowernumber, # some templates require global
229 patron => $patron,
230 total => $total_due,
232 csrf_token => Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID') } ),
235 output_html_with_http_headers $input, $cookie, $template->output;