Rmaint fix: Remove Koha::Tickets import from 35019 rebase
[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     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
73     my $payment_id = scalar $input->param('accountlines_id');
74     my $payment    = Koha::Account::Lines->find( $payment_id );
75     $payment->void(
76         {
77             branch    => $library_id,
78             staff_id  => $logged_in_user->id,
79             interface => 'intranet',
80         }
81     );
82 }
83
84 if ( $action eq 'payout' ) {
85     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
86     my $payment_id  = scalar $input->param('accountlines_id');
87     my $payment     = Koha::Account::Lines->find($payment_id);
88     my $amount      = scalar $input->param('amount');
89     my $payout_type = scalar $input->param('payout_type');
90     if ( $payment_id eq "" ) {
91         $schema->txn_do(
92             sub {
93                 $patron->account->payout_amount(
94                      {
95                         payout_type   => $payout_type,
96                         branch        => $library_id,
97                         staff_id      => $logged_in_user->id,
98                         cash_register => $registerid,
99                         interface     => 'intranet',
100                         amount        => $amount
101                     }
102                 );
103             }
104         );
105     } else {
106         my $payment = Koha::Account::Lines->find($payment_id);
107         $schema->txn_do(
108             sub {
109                 my $payout = $payment->payout(
110                     {
111                         payout_type   => $payout_type,
112                         branch        => $library_id,
113                         staff_id      => $logged_in_user->id,
114                         cash_register => $registerid,
115                         interface     => 'intranet',
116                         amount        => $amount
117                     }
118                 );
119             }
120         );
121     }
122 }
123
124 if ( $action eq 'refund' ) {
125     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
126     my $charge_id   = scalar $input->param('accountlines_id');
127     my $charge      = Koha::Account::Lines->find($charge_id);
128     my $amount      = scalar $input->param('amount');
129     my $refund_type = scalar $input->param('refund_type');
130     $schema->txn_do(
131         sub {
132
133             my $refund = $charge->reduce(
134                 {
135                     reduction_type => 'REFUND',
136                     branch         => $library_id,
137                     staff_id       => $logged_in_user->id,
138                     interface      => 'intranet',
139                     amount         => $amount
140                 }
141             );
142             unless ( $refund_type eq 'AC' ) {
143                 my $payout = $refund->payout(
144                     {
145                         payout_type   => $refund_type,
146                         branch        => $library_id,
147                         staff_id      => $logged_in_user->id,
148                         cash_register => $registerid,
149                         interface     => 'intranet',
150                         amount        => $amount
151                     }
152                 );
153             }
154         }
155     );
156 }
157
158 if ( $action eq 'discount' ) {
159     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
160     my $charge_id        = scalar $input->param('accountlines_id');
161     my $charge           = Koha::Account::Lines->find($charge_id);
162     my $amount           = scalar $input->param('amount');
163     $schema->txn_do(
164         sub {
165
166             my $discount = $charge->reduce(
167                 {
168                     reduction_type => 'DISCOUNT',
169                     branch         => $library_id,
170                     staff_id       => $logged_in_user->id,
171                     interface      => 'intranet',
172                     amount         => $amount
173                 }
174             );
175         }
176     );
177 }
178
179 #get account details
180 my $total = $patron->account->balance;
181
182 my $accountlines = Koha::Account::Lines->search(
183     { borrowernumber => $patron->borrowernumber },
184     { order_by       => { -desc => 'accountlines_id' } }
185 );
186
187 my $totalcredit;
188 if($total <= 0){
189         $totalcredit = 1;
190 }
191
192 # Populate an arrayref with everything we need to display any
193 # renew errors that occurred based on what we were passed
194 my $renew_results_display = [];
195 foreach my $renew_result(@renew_results) {
196     my ($itemnumber, $success, $info) = split(/,/, $renew_result);
197     my $item = Koha::Items->find($itemnumber);
198     if ($success) {
199         $info = uri_unescape($info);
200     }
201     push @{$renew_results_display}, {
202         item    => $item,
203         success => $success,
204         info    => $info
205     };
206 }
207
208 my $csrf_token = Koha::Token->new->generate_csrf({
209     session_id => scalar $input->cookie('CGISESSID'),
210 });
211
212 $template->param(
213     patron              => $patron,
214     finesview           => 1,
215     total               => sprintf("%.2f",$total),
216     totalcredit         => $totalcredit,
217     accounts            => $accountlines,
218     payment_id          => $payment_id,
219     change_given        => $change_given,
220     renew_results       => $renew_results_display,
221     csrf_token          => $csrf_token,
222 );
223
224 output_html_with_http_headers $input, $cookie, $template->output;