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