Bug 36249: Fix styling of 'Send email' button
[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
35 use Koha::Patrons;
36 use Koha::Items;
37 use Koha::Old::Items;
38 use Koha::Checkouts;
39 use Koha::Old::Checkouts;
40
41 use Koha::Patron::Categories;
42 use Koha::Account::DebitTypes;
43 use Koha::AdditionalFields;
44
45 my $input = CGI->new;
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47     {
48         template_name   => "members/maninvoice.tt",
49         query           => $input,
50         type            => "intranet",
51         flagsrequired   => {
52             borrowers     => 'edit_borrowers',
53             updatecharges => 'manual_invoice'
54         }
55     }
56 );
57
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;
64 }
65
66 my $logged_in_user = Koha::Patrons->find($loggedinuser);
67 output_and_exit_if_error(
68     $input, $cookie,
69     $template,
70     {
71         module         => 'members',
72         logged_in_user => $logged_in_user,
73         current_patron => $patron
74     }
75 );
76
77 my $library_id = C4::Context->userenv->{'branch'};
78
79 my $op = $input->param('op') // q{};
80
81 my $add        = $input->param('add');
82 my $desc       = $input->param('desc');
83 my $amount     = $input->param('amount');
84 my $note       = $input->param('note');
85 my $debit_type = $input->param('type');
86 my $barcode    = $input->param('barcode');
87
88 $template->param(
89     desc    => $desc,
90     amount  => $amount,
91     note    => $note,
92     type    => $debit_type,
93     barcode => $barcode
94 );
95
96 if ( $op eq 'cud-add' ) {
97     # Note: If the logged in user is not allowed to see this patron an invoice can be forced
98     # Here we are trusting librarians not to hack the system
99     my $desc       = $input->param('desc');
100     my $amount     = $input->param('amount');
101     my $note       = $input->param('note');
102     my $debit_type = $input->param('type');
103
104     # If barcode is passed, attempt to find the associated item
105     my $failed;
106     my $item_id;
107     my $olditem; # FIXME: When items and deleted_items are merged, we can remove this
108     my $issue_id;
109     if ($barcode) {
110         my $item = Koha::Items->find( { barcode => $barcode } );
111         if ($item) {
112             $item_id = $item->itemnumber;
113         }
114         else {
115             $item = Koha::Old::Items->search( { barcode => $barcode },
116                 { order_by => { -desc => 'timestamp' }, rows => 1 } );
117             if ($item->count) {
118                 $item_id = $item->next->itemnumber;
119                 $olditem = 1;
120             }
121             else {
122                 $template->param( error => 'itemnumber' );
123                 $failed = 1;
124             }
125         }
126
127         if ( ( $debit_type eq 'LOST' ) && $item_id ) {
128             my $checkouts = Koha::Checkouts->search(
129                 {
130                     itemnumber     => $item_id,
131                     borrowernumber => $borrowernumber
132                 }
133             );
134             my $checkout =
135                 $checkouts->count
136               ? $checkouts->next
137               : Koha::Old::Checkouts->search(
138                 {
139                     itemnumber     => $item_id,
140                     borrowernumber => $borrowernumber
141                 },
142                 { order_by => { -desc => 'returndate' }, rows => 1 }
143             )->next;
144             $issue_id = $checkout ? $checkout->issue_id : undef;
145         }
146     }
147
148     unless ($failed) {
149         try {
150             my $line = $patron->account->add_debit(
151                 {
152                     amount      => $amount,
153                     description => $desc,
154                     note        => $note,
155                     user_id     => $logged_in_user->borrowernumber,
156                     interface   => 'intranet',
157                     library_id  => $library_id,
158                     type        => $debit_type,
159                     ( $olditem ? () : ( item_id => $item_id ) ),
160                     issue_id    => $issue_id
161                 }
162             );
163
164             my @additional_fields;
165             my $accountline_fields = Koha::AdditionalFields->search({ tablename => 'accountlines:debit' });
166             while ( my $field = $accountline_fields->next ) {
167                 my $value = $input->param('additional_field_' . $field->id);
168                 if (defined $value) {
169                     push @additional_fields, {
170                         id => $field->id,
171                         value => $value,
172                     };
173                 }
174             }
175             if (@additional_fields) {
176                 $line->set_additional_fields(\@additional_fields);
177             }
178
179             if ( C4::Context->preference('AccountAutoReconcile') ) {
180                 $patron->account->reconcile_balance;
181             }
182
183             if ( $add eq 'save and pay' ) {
184                 my $url = sprintf(
185                     '/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',
186                     map { uri_escape_utf8($_) } (
187                         $borrowernumber,
188                         $line->debit_type_code,
189                         $line->amount,
190                         $line->amountoutstanding,
191                         $line->description,
192                         $line->itemnumber,
193                         $line->id
194                     )
195                 );
196
197                 print $input->redirect($url);
198             }
199             else {
200                 print $input->redirect(
201                     "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"
202                 );
203             }
204
205             exit;
206         }
207         catch {
208             my $error = $_;
209             if ( ref($error) eq 'Koha::Exceptions::Object::FKConstraint' ) {
210                 $template->param( error => $error->broken_fk );
211             }
212             else {
213                 $template->param( error => $error );
214             }
215         }
216     }
217 }
218
219 my $debit_types = Koha::Account::DebitTypes->search_with_library_limits(
220   { can_be_invoiced => 1, archived => 0 },
221   {}, $library_id );
222
223 $template->param(
224   debit_types => $debit_types,
225   patron    => $patron,
226   finesview => 1,
227   available_additional_fields => [ Koha::AdditionalFields->search({ tablename => 'accountlines:debit' })->as_list ],
228   );
229 output_html_with_http_headers $input, $cookie, $template->output;