Bug 26766: Correct selection of title during OPAC note confirmation
[koha.git] / members / maninvoice.pl
bloba178a2be380630f29ba166b3082c2dc4237d47e7
1 #!/usr/bin/perl
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>.
25 use Modern::Perl;
26 use Try::Tiny;
28 use C4::Auth;
29 use C4::Output;
30 use CGI qw ( -utf8 );
31 use C4::Members;
32 use C4::Accounts;
33 use C4::Items;
34 use Koha::Token;
36 use Koha::Patrons;
37 use Koha::Items;
38 use Koha::Old::Items;
39 use Koha::Checkouts;
40 use Koha::Old::Checkouts;
42 use Koha::Patron::Categories;
43 use Koha::Account::DebitTypes;
45 my $input = CGI->new;
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48 template_name => "members/maninvoice.tt",
49 query => $input,
50 type => "intranet",
51 flagsrequired => {
52 borrowers => 'edit_borrowers',
53 updatecharges => 'remaining_permissions'
58 my $borrowernumber = $input->param('borrowernumber');
59 my $patron = Koha::Patrons->find($borrowernumber);
60 unless ($patron) {
61 print $input->redirect(
62 "/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
63 exit;
66 my $logged_in_user = Koha::Patrons->find($loggedinuser);
67 output_and_exit_if_error(
68 $input, $cookie,
69 $template,
71 module => 'members',
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');
83 $template->param(
84 desc => $desc,
85 amount => $amount,
86 note => $note,
87 type => $debit_type,
88 barcode => $barcode
91 my $add = $input->param('add');
92 if ($add) {
93 output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
94 unless Koha::Token->new->check_csrf(
96 session_id => scalar $input->cookie('CGISESSID'),
97 token => scalar $input->param('csrf_token'),
101 # Note: If the logged in user is not allowed to see this patron an invoice can be forced
102 # Here we are trusting librarians not to hack the system
103 my $desc = $input->param('desc');
104 my $amount = $input->param('amount');
105 my $note = $input->param('note');
106 my $debit_type = $input->param('type');
108 # If barcode is passed, attempt to find the associated item
109 my $failed;
110 my $item_id;
111 my $olditem; # FIXME: When items and deleted_items are merged, we can remove this
112 my $issue_id;
113 if ($barcode) {
114 my $item = Koha::Items->find( { barcode => $barcode } );
115 if ($item) {
116 $item_id = $item->itemnumber;
118 else {
119 $item = Koha::Old::Items->search( { barcode => $barcode },
120 { order_by => { -desc => 'timestamp' }, rows => 1 } );
121 if ($item->count) {
122 $item_id = $item->next->itemnumber;
123 $olditem = 1;
125 else {
126 $template->param( error => 'itemnumber' );
127 $failed = 1;
131 if ( ( $debit_type eq 'LOST' ) && $item_id ) {
132 my $checkouts = Koha::Checkouts->search(
134 itemnumber => $item_id,
135 borrowernumber => $borrowernumber
138 my $checkout =
139 $checkouts->count
140 ? $checkouts->next
141 : Koha::Old::Checkouts->search(
143 itemnumber => $item_id,
144 borrowernumber => $borrowernumber
146 { order_by => { -desc => 'returndate' }, rows => 1 }
147 )->next;
148 $issue_id = $checkout ? $checkout->issue_id : undef;
152 unless ($failed) {
153 try {
154 $patron->account->add_debit(
156 amount => $amount,
157 description => $desc,
158 note => $note,
159 user_id => $logged_in_user->borrowernumber,
160 interface => 'intranet',
161 library_id => $library_id,
162 type => $debit_type,
163 ( $olditem ? () : ( item_id => $item_id ) ),
164 issue_id => $issue_id
168 if ( C4::Context->preference('AccountAutoReconcile') ) {
169 $patron->account->reconcile_balance;
172 if ( $add eq 'save and pay' ) {
173 print $input->redirect(
174 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber"
177 else {
178 print $input->redirect(
179 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"
183 exit;
185 catch {
186 my $error = $_;
187 if ( ref($error) eq 'Koha::Exceptions::Object::FKConstraint' ) {
188 $template->param( error => $error->broken_fk );
190 else {
191 $template->param( error => $error );
197 my @debit_types = Koha::Account::DebitTypes->search_with_library_limits(
198 { can_be_invoiced => 1, archived => 0 },
199 {}, $library_id );
201 $template->param(
202 debit_types => \@debit_types,
203 csrf_token => Koha::Token->new->generate_csrf(
204 { session_id => scalar $input->cookie('CGISESSID') }
206 patron => $patron,
207 finesview => 1,
209 output_html_with_http_headers $input, $cookie, $template->output;