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>.
31 use Koha::Cash::Registers;
33 use Koha::Patron::Categories;
34 use Koha::AuthorisedValues;
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',
50 flagsrequired => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
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');
78 my $registerid = $input->param('registerid');
80 $registerid = Koha::Cash::Registers->find(
81 { branch => $library_id, branch_default => 1 },
84 my $registers = Koha::Cash::Registers->search(
85 { branch => $library_id, archived => 0 },
86 { order_by => { '-asc' => 'name' } }
89 registerid => $registerid,
90 registers => $registers,
93 if ( $pay_individual || $writeoff_individual ) {
94 if ($pay_individual) {
95 $template->param( pay_individual => 1 );
96 } elsif ($writeoff_individual) {
97 $template->param( writeoff_individual => 1 );
99 my $accounttype = $input->param('accounttype');
100 $accountlines_id = $input->param('accountlines_id');
101 my $amount = $input->param('amount');
102 my $amountoutstanding = $input->param('amountoutstanding');
103 my $itemnumber = $input->param('itemnumber');
104 my $description = $input->param('description');
105 my $title = $input->param('title');
106 $total_due = $amountoutstanding;
108 accounttype => $accounttype,
109 accountlines_id => $accountlines_id,
111 amountoutstanding => $amountoutstanding,
113 itemnumber => $itemnumber,
114 individual_description => $description,
115 payment_note => $payment_note,
117 } elsif ($select_lines) {
118 $total_due = $input->param('amt');
120 selected_accts => $select_lines,
122 selected_accts_notes => scalar $input->param('notes'),
126 if ( $total_paid and $total_paid ne '0.00' ) {
127 if ( $total_paid < 0 or $total_paid > $total_due ) {
130 total_due => $total_due
133 output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
134 unless Koha::Token->new->check_csrf( {
135 session_id => $input->cookie('CGISESSID'),
136 token => scalar $input->param('csrf_token'),
139 if ($pay_individual) {
140 my $line = Koha::Account::Lines->find($accountlines_id);
141 $payment_id = $account->pay(
144 amount => $total_paid,
145 library_id => $library_id,
146 note => $payment_note,
147 interface => C4::Context->interface,
148 payment_type => $payment_type,
149 cash_register => $registerid
152 print $input->redirect(
153 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber&payment_id=$payment_id");
156 if ( $select =~ /^([\d,]*).*/ ) {
157 $select = $1; # ensure passing no junk
159 my @acc = split /,/, $select;
160 my $note = $input->param('selected_accts_notes');
162 my @lines = Koha::Account::Lines->search(
164 borrowernumber => $borrowernumber,
165 amountoutstanding => { '<>' => 0 },
166 accountlines_id => { 'IN' => \@acc },
168 { order_by => 'date' }
171 $payment_id = $account->pay(
174 amount => $total_paid,
175 library_id => $library_id,
178 interface => C4::Context->interface,
179 payment_type => $payment_type,
180 cash_register => $registerid
185 my $note = $input->param('selected_accts_notes');
186 $payment_id = $account->pay(
188 amount => $total_paid,
189 library_id => $library_id,
191 payment_type => $payment_type,
192 interface => C4::Context->interface,
193 payment_type => $payment_type,
194 cash_register => $registerid
199 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber&payment_id=$payment_id");
203 $total_paid = '0.00'; #TODO not right with pay_individual
206 $template->param(%$borrower);
208 if ( $input->param('error_over') ) {
209 $template->param( error_over => 1, total_due => scalar $input->param('amountoutstanding') );
213 payment_id => $payment_id,
216 borrowernumber => $borrowernumber, # some templates require global
220 csrf_token => Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID') } ),
223 output_html_with_http_headers $input, $cookie, $template->output;