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>.
26 use Try::Tiny qw( catch try );
27 use URI::Escape qw( uri_escape_utf8 );
29 use C4::Auth qw( get_template_and_user );
30 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
39 use Koha::Old::Checkouts;
41 use Koha::Patron::Categories;
42 use Koha::Account::DebitTypes;
43 use Koha::AdditionalFields;
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48 template_name => "members/maninvoice.tt",
52 borrowers => 'edit_borrowers',
53 updatecharges => 'manual_invoice'
58 my $borrowernumber = $input->param('borrowernumber');
59 my $patron = Koha::Patrons->find($borrowernumber);
61 print $input->redirect(
62 "/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
66 my $logged_in_user = Koha::Patrons->find($loggedinuser);
67 output_and_exit_if_error(
72 logged_in_user => $logged_in_user,
73 current_patron => $patron
77 my $library_id = C4::Context->userenv->{'branch'};
78 my $desc = $input->param('desc');
79 my $amount = $input->param('amount');
80 my $note = $input->param('note');
81 my $debit_type = $input->param('type');
82 my $barcode = $input->param('barcode');
91 my $add = $input->param('cud-add');
93 # Note: If the logged in user is not allowed to see this patron an invoice can be forced
94 # Here we are trusting librarians not to hack the system
95 my $desc = $input->param('desc');
96 my $amount = $input->param('amount');
97 my $note = $input->param('note');
98 my $debit_type = $input->param('type');
100 # If barcode is passed, attempt to find the associated item
103 my $olditem; # FIXME: When items and deleted_items are merged, we can remove this
106 my $item = Koha::Items->find( { barcode => $barcode } );
108 $item_id = $item->itemnumber;
111 $item = Koha::Old::Items->search( { barcode => $barcode },
112 { order_by => { -desc => 'timestamp' }, rows => 1 } );
114 $item_id = $item->next->itemnumber;
118 $template->param( error => 'itemnumber' );
123 if ( ( $debit_type eq 'LOST' ) && $item_id ) {
124 my $checkouts = Koha::Checkouts->search(
126 itemnumber => $item_id,
127 borrowernumber => $borrowernumber
133 : Koha::Old::Checkouts->search(
135 itemnumber => $item_id,
136 borrowernumber => $borrowernumber
138 { order_by => { -desc => 'returndate' }, rows => 1 }
140 $issue_id = $checkout ? $checkout->issue_id : undef;
146 my $line = $patron->account->add_debit(
149 description => $desc,
151 user_id => $logged_in_user->borrowernumber,
152 interface => 'intranet',
153 library_id => $library_id,
155 ( $olditem ? () : ( item_id => $item_id ) ),
156 issue_id => $issue_id
160 my @additional_fields;
161 my $accountline_fields = Koha::AdditionalFields->search({ tablename => 'accountlines:debit' });
162 while ( my $field = $accountline_fields->next ) {
163 my $value = $input->param('additional_field_' . $field->id);
164 if (defined $value) {
165 push @additional_fields, {
171 if (@additional_fields) {
172 $line->set_additional_fields(\@additional_fields);
175 if ( C4::Context->preference('AccountAutoReconcile') ) {
176 $patron->account->reconcile_balance;
179 if ( $add eq 'save and pay' ) {
181 '/cgi-bin/koha/members/paycollect.pl?borrowernumber=%s&pay_individual=1&debit_type_code=%s&amount=%s&amountoutstanding=%s&description=%s&itemnumber=%s&accountlines_id=%s',
182 map { uri_escape_utf8($_) } (
184 $line->debit_type_code,
186 $line->amountoutstanding,
193 print $input->redirect($url);
196 print $input->redirect(
197 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"
205 if ( ref($error) eq 'Koha::Exceptions::Object::FKConstraint' ) {
206 $template->param( error => $error->broken_fk );
209 $template->param( error => $error );
215 my $debit_types = Koha::Account::DebitTypes->search_with_library_limits(
216 { can_be_invoiced => 1, archived => 0 },
220 debit_types => $debit_types,
223 available_additional_fields => [ Koha::AdditionalFields->search({ tablename => 'accountlines:debit' })->as_list ],
225 output_html_with_http_headers $input, $cookie, $template->output;