Bug 32401: Remove x-koha-query support
[koha.git] / members / boraccount.pl
1 #!/usr/bin/perl
2
3
4 #written 11/1/2000 by chris@katipo.oc.nz
5 #script to display borrowers account details
6
7
8 # Copyright 2000-2002 Katipo Communications
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 URI::Escape qw( uri_unescape );
27
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
30 use CGI qw ( -utf8 );
31 use C4::Members;
32 use C4::Accounts;
33 use Koha::Cash::Registers;
34 use Koha::Patrons;
35 use Koha::Patron::Categories;
36 use Koha::Items;
37 use Koha::Token;
38
39 my $input=CGI->new;
40
41
42 my ($template, $loggedinuser, $cookie) = get_template_and_user(
43     {
44         template_name   => "members/boraccount.tt",
45         query           => $input,
46         type            => "intranet",
47         flagsrequired   => { borrowers     => 'edit_borrowers',
48                              updatecharges => 'remaining_permissions'},
49     }
50 );
51
52 my $schema         = Koha::Database->new->schema;
53 my $borrowernumber = $input->param('borrowernumber');
54 my $payment_id     = $input->param('payment_id');
55 my $change_given   = $input->param('change_given');
56 my $action         = $input->param('action') || '';
57 my @renew_results  = $input->multi_param('renew_result');
58
59 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
60 my $library_id = C4::Context->userenv->{'branch'};
61 my $patron = Koha::Patrons->find( $borrowernumber );
62 unless ( $patron ) {
63     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
64     exit;
65 }
66
67 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
68
69 my $registerid = $input->param('registerid');
70
71 if ( $action eq 'void' ) {
72     my $payment_id = scalar $input->param('accountlines_id');
73     my $payment    = Koha::Account::Lines->find( $payment_id );
74     $payment->void(
75         {
76             branch    => $library_id,
77             staff_id  => $logged_in_user->id,
78             interface => 'intranet',
79         }
80     );
81 }
82
83 if ( $action eq 'payout' ) {
84     my $payment_id       = scalar $input->param('accountlines_id');
85     my $amount           = scalar $input->param('amount');
86     my $payout_type = scalar $input->param('payout_type');
87     if ( $payment_id eq "" ) {
88         $schema->txn_do(
89             sub {
90                 $patron->account->payout_amount(
91                      {
92                         payout_type   => $payout_type,
93                         branch        => $library_id,
94                         staff_id      => $logged_in_user->id,
95                         cash_register => $registerid,
96                         interface     => 'intranet',
97                         amount        => $amount
98                     }
99                 );
100             }
101         );
102     } else {
103         my $payment = Koha::Account::Lines->find($payment_id);
104         $schema->txn_do(
105             sub {
106                 my $payout = $payment->payout(
107                     {
108                         payout_type   => $payout_type,
109                         branch        => $library_id,
110                         staff_id      => $logged_in_user->id,
111                         cash_register => $registerid,
112                         interface     => 'intranet',
113                         amount        => $amount
114                     }
115                 );
116             }
117         );
118     }
119 }
120
121 if ( $action eq 'refund' ) {
122     my $charge_id        = scalar $input->param('accountlines_id');
123     my $charge           = Koha::Account::Lines->find($charge_id);
124     my $amount           = scalar $input->param('amount');
125     my $refund_type = scalar $input->param('refund_type');
126     $schema->txn_do(
127         sub {
128
129             my $refund = $charge->reduce(
130                 {
131                     reduction_type => 'REFUND',
132                     branch         => $library_id,
133                     staff_id       => $logged_in_user->id,
134                     interface      => 'intranet',
135                     amount         => $amount
136                 }
137             );
138             unless ( $refund_type eq 'AC' ) {
139                 my $payout = $refund->payout(
140                     {
141                         payout_type   => $refund_type,
142                         branch        => $library_id,
143                         staff_id      => $logged_in_user->id,
144                         cash_register => $registerid,
145                         interface     => 'intranet',
146                         amount        => $amount
147                     }
148                 );
149             }
150         }
151     );
152 }
153
154 if ( $action eq 'discount' ) {
155     my $charge_id        = scalar $input->param('accountlines_id');
156     my $charge           = Koha::Account::Lines->find($charge_id);
157     my $amount           = scalar $input->param('amount');
158     $schema->txn_do(
159         sub {
160
161             my $discount = $charge->reduce(
162                 {
163                     reduction_type => 'DISCOUNT',
164                     branch         => $library_id,
165                     staff_id       => $logged_in_user->id,
166                     interface      => 'intranet',
167                     amount         => $amount
168                 }
169             );
170         }
171     );
172 }
173
174 #get account details
175 my $total = $patron->account->balance;
176
177 my $accountlines = Koha::Account::Lines->search(
178     { borrowernumber => $patron->borrowernumber },
179     { order_by       => { -desc => 'accountlines_id' } }
180 );
181
182 my $totalcredit;
183 if($total <= 0){
184         $totalcredit = 1;
185 }
186
187 # Populate an arrayref with everything we need to display any
188 # renew errors that occurred based on what we were passed
189 my $renew_results_display = [];
190 foreach my $renew_result(@renew_results) {
191     my ($itemnumber, $success, $info) = split(/,/, $renew_result);
192     my $item = Koha::Items->find($itemnumber);
193     if ($success) {
194         $info = uri_unescape($info);
195     }
196     push @{$renew_results_display}, {
197         item    => $item,
198         success => $success,
199         info    => $info
200     };
201 }
202
203 my $csrf_token = Koha::Token->new->generate_csrf({
204     session_id => scalar $input->cookie('CGISESSID'),
205 });
206
207 $template->param(
208     patron              => $patron,
209     finesview           => 1,
210     total               => sprintf("%.2f",$total),
211     totalcredit         => $totalcredit,
212     accounts            => $accountlines,
213     payment_id          => $payment_id,
214     change_given        => $change_given,
215     renew_results       => $renew_results_display,
216     csrf_token          => $csrf_token,
217 );
218
219 output_html_with_http_headers $input, $cookie, $template->output;