3 #written 11/1/2000 by chris@katipo.oc.nz
4 #script to display borrowers account details
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2010 BibLibre
8 # Copyright 2019 PTFS Europe
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
17 # Koha is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with Koha; if not, see <http://www.gnu.org/licenses>.
40 use Koha::Old::Checkouts;
42 use Koha::Patron::Categories;
43 use Koha::Account::DebitTypes;
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48 template_name => "members/maninvoice.tt",
53 borrowers => 'edit_borrowers',
54 updatecharges => 'remaining_permissions'
59 my $borrowernumber = $input->param('borrowernumber');
60 my $patron = Koha::Patrons->find($borrowernumber);
62 print $input->redirect(
63 "/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
67 my $logged_in_user = Koha::Patrons->find($loggedinuser);
68 output_and_exit_if_error(
73 logged_in_user => $logged_in_user,
74 current_patron => $patron
78 my $library_id = C4::Context->userenv->{'branch'};
79 my $desc = $input->param('desc');
80 my $amount = $input->param('amount');
81 my $note = $input->param('note');
82 my $debit_type = $input->param('type');
83 my $barcode = $input->param('barcode');
92 my $add = $input->param('add');
94 output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
95 unless Koha::Token->new->check_csrf(
97 session_id => scalar $input->cookie('CGISESSID'),
98 token => scalar $input->param('csrf_token'),
102 # Note: If the logged in user is not allowed to see this patron an invoice can be forced
103 # Here we are trusting librarians not to hack the system
104 my $desc = $input->param('desc');
105 my $amount = $input->param('amount');
106 my $note = $input->param('note');
107 my $debit_type = $input->param('type');
109 # If barcode is passed, attempt to find the associated item
114 my $item = Koha::Items->find( { barcode => $barcode } );
116 $item_id = $item->itemnumber;
119 $item = Koha::Old::Items->find( { barcode => $barcode } );
121 $item_id = $item->itemnumber;
124 $template->param( error => 'itemnumber' );
129 if ( ( $debit_type eq 'LOST' ) && $item_id ) {
130 my $checkouts = Koha::Checkouts->search(
132 itemnumber => $item_id,
133 borrowernumber => $borrowernumber
139 : Koha::Old::Checkouts->search(
141 itemnumber => $item_id,
142 borrowernumber => $borrowernumber
144 { order_by => { -desc => 'returndate' }, rows => 1 }
146 $issue_id = $checkout ? $checkout->issue_id : undef;
152 $patron->account->add_debit(
155 description => $desc,
157 user_id => $logged_in_user->borrowernumber,
158 interface => 'intranet',
159 library_id => $library_id,
162 issue_id => $issue_id
166 if ( C4::Context->preference('AccountAutoReconcile') ) {
167 $patron->account->reconcile_balance;
170 if ( $add eq 'save and pay' ) {
171 print $input->redirect(
172 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber"
176 print $input->redirect(
177 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"
185 if ( ref($error) eq 'Koha::Exceptions::Object::FKConstraint' ) {
186 $template->param( error => $error->broken_fk );
189 $template->param( error => $error );
195 my @debit_types = Koha::Account::DebitTypes->search_with_library_limits(
196 { can_be_invoiced => 1, archived => 0 },
200 debit_types => \@debit_types,
201 csrf_token => Koha::Token->new->generate_csrf(
202 { session_id => scalar $input->cookie('CGISESSID') }
207 output_html_with_http_headers $input, $cookie, $template->output;