Bug 32401: Remove x-koha-query support
[koha.git] / members / maninvoice.pl
1 #!/usr/bin/perl
2
3 #written 11/1/2000 by chris@katipo.oc.nz
4 #script to display borrowers account details
5
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2010 BibLibre
8 # Copyright 2019 PTFS Europe
9 #
10 # This file is part of Koha.
11 #
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.
16 #
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.
21 #
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>.
24
25 use Modern::Perl;
26 use Try::Tiny qw( catch try );
27 use URI::Escape qw( uri_escape_utf8 );
28
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 );
31 use CGI qw ( -utf8 );
32 use C4::Members;
33 use C4::Accounts;
34 use Koha::Token;
35
36 use Koha::Patrons;
37 use Koha::Items;
38 use Koha::Old::Items;
39 use Koha::Checkouts;
40 use Koha::Old::Checkouts;
41
42 use Koha::Patron::Categories;
43 use Koha::Account::DebitTypes;
44 use Koha::AdditionalFields;
45
46 my $input = CGI->new;
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48     {
49         template_name   => "members/maninvoice.tt",
50         query           => $input,
51         type            => "intranet",
52         flagsrequired   => {
53             borrowers     => 'edit_borrowers',
54             updatecharges => 'manual_invoice'
55         }
56     }
57 );
58
59 my $borrowernumber = $input->param('borrowernumber');
60 my $patron         = Koha::Patrons->find($borrowernumber);
61 unless ($patron) {
62     print $input->redirect(
63         "/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
64     exit;
65 }
66
67 my $logged_in_user = Koha::Patrons->find($loggedinuser);
68 output_and_exit_if_error(
69     $input, $cookie,
70     $template,
71     {
72         module         => 'members',
73         logged_in_user => $logged_in_user,
74         current_patron => $patron
75     }
76 );
77
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');
84 $template->param(
85     desc    => $desc,
86     amount  => $amount,
87     note    => $note,
88     type    => $debit_type,
89     barcode => $barcode
90 );
91
92 my $add = $input->param('add');
93 if ($add) {
94     output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
95       unless Koha::Token->new->check_csrf(
96         {
97             session_id => scalar $input->cookie('CGISESSID'),
98             token      => scalar $input->param('csrf_token'),
99         }
100       );
101
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');
108
109     # If barcode is passed, attempt to find the associated item
110     my $failed;
111     my $item_id;
112     my $olditem; # FIXME: When items and deleted_items are merged, we can remove this
113     my $issue_id;
114     if ($barcode) {
115         my $item = Koha::Items->find( { barcode => $barcode } );
116         if ($item) {
117             $item_id = $item->itemnumber;
118         }
119         else {
120             $item = Koha::Old::Items->search( { barcode => $barcode },
121                 { order_by => { -desc => 'timestamp' }, rows => 1 } );
122             if ($item->count) {
123                 $item_id = $item->next->itemnumber;
124                 $olditem = 1;
125             }
126             else {
127                 $template->param( error => 'itemnumber' );
128                 $failed = 1;
129             }
130         }
131
132         if ( ( $debit_type eq 'LOST' ) && $item_id ) {
133             my $checkouts = Koha::Checkouts->search(
134                 {
135                     itemnumber     => $item_id,
136                     borrowernumber => $borrowernumber
137                 }
138             );
139             my $checkout =
140                 $checkouts->count
141               ? $checkouts->next
142               : Koha::Old::Checkouts->search(
143                 {
144                     itemnumber     => $item_id,
145                     borrowernumber => $borrowernumber
146                 },
147                 { order_by => { -desc => 'returndate' }, rows => 1 }
148             )->next;
149             $issue_id = $checkout ? $checkout->issue_id : undef;
150         }
151     }
152
153     unless ($failed) {
154         try {
155             my $line = $patron->account->add_debit(
156                 {
157                     amount      => $amount,
158                     description => $desc,
159                     note        => $note,
160                     user_id     => $logged_in_user->borrowernumber,
161                     interface   => 'intranet',
162                     library_id  => $library_id,
163                     type        => $debit_type,
164                     ( $olditem ? () : ( item_id => $item_id ) ),
165                     issue_id    => $issue_id
166                 }
167             );
168
169             my @additional_fields;
170             my $accountline_fields = Koha::AdditionalFields->search({ tablename => 'accountlines:debit' });
171             while ( my $field = $accountline_fields->next ) {
172                 my $value = $input->param('additional_field_' . $field->id);
173                 if (defined $value) {
174                     push @additional_fields, {
175                         id => $field->id,
176                         value => $value,
177                     };
178                 }
179             }
180             if (@additional_fields) {
181                 $line->set_additional_fields(\@additional_fields);
182             }
183
184             if ( C4::Context->preference('AccountAutoReconcile') ) {
185                 $patron->account->reconcile_balance;
186             }
187
188             if ( $add eq 'save and pay' ) {
189                 my $url = sprintf(
190                     '/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',
191                     map { uri_escape_utf8($_) } (
192                         $borrowernumber,
193                         $line->debit_type_code,
194                         $line->amount,
195                         $line->amountoutstanding,
196                         $line->description,
197                         $line->itemnumber,
198                         $line->id
199                     )
200                 );
201
202                 print $input->redirect($url);
203             }
204             else {
205                 print $input->redirect(
206                     "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"
207                 );
208             }
209
210             exit;
211         }
212         catch {
213             my $error = $_;
214             if ( ref($error) eq 'Koha::Exceptions::Object::FKConstraint' ) {
215                 $template->param( error => $error->broken_fk );
216             }
217             else {
218                 $template->param( error => $error );
219             }
220         }
221     }
222 }
223
224 my $debit_types = Koha::Account::DebitTypes->search_with_library_limits(
225   { can_be_invoiced => 1, archived => 0 },
226   {}, $library_id );
227
228 $template->param(
229   debit_types => $debit_types,
230   csrf_token  => Koha::Token->new->generate_csrf(
231       { session_id => scalar $input->cookie('CGISESSID') }
232   ),
233   patron    => $patron,
234   finesview => 1,
235   available_additional_fields => [ Koha::AdditionalFields->search({ tablename => 'accountlines:debit' })->as_list ],
236   );
237 output_html_with_http_headers $input, $cookie, $template->output;