Bug 24786: Update borroaccount to use session register
[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
38 my $input=CGI->new;
39
40
41 my ($template, $loggedinuser, $cookie) = get_template_and_user(
42     {
43         template_name   => "members/boraccount.tt",
44         query           => $input,
45         type            => "intranet",
46         flagsrequired   => { borrowers     => 'edit_borrowers',
47                              updatecharges => 'remaining_permissions'},
48         debug           => 1,
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->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
77 if ( $action eq 'payout' ) {
78     my $payment_id        = scalar $input->param('accountlines_id');
79     my $payment           = Koha::Account::Lines->find($payment_id);
80     my $amount           = scalar $input->param('amount');
81     my $transaction_type = scalar $input->param('transaction_type');
82     $schema->txn_do(
83         sub {
84             my $payout = $payment->payout(
85                 {
86                     payout_type   => $transaction_type,
87                     branch        => $library_id,
88                     staff_id      => $logged_in_user->id,
89                     cash_register => $registerid,
90                     interface     => 'intranet',
91                     amount        => $amount
92                 }
93             );
94         }
95     );
96 }
97
98 if ( $action eq 'refund' ) {
99     my $charge_id        = scalar $input->param('accountlines_id');
100     my $charge           = Koha::Account::Lines->find($charge_id);
101     my $amount           = scalar $input->param('amount');
102     my $transaction_type = scalar $input->param('transaction_type');
103     $schema->txn_do(
104         sub {
105
106             my $refund = $charge->reduce(
107                 {
108                     reduction_type => 'REFUND',
109                     branch         => $library_id,
110                     staff_id       => $logged_in_user->id,
111                     interface      => 'intranet',
112                     amount         => $amount
113                 }
114             );
115             unless ( $transaction_type eq 'AC' ) {
116                 my $payout = $refund->payout(
117                     {
118                         payout_type   => $transaction_type,
119                         branch        => $library_id,
120                         staff_id      => $logged_in_user->id,
121                         cash_register => $registerid,
122                         interface     => 'intranet',
123                         amount        => $amount
124                     }
125                 );
126             }
127         }
128     );
129 }
130
131 if ( $action eq 'discount' ) {
132     my $charge_id        = scalar $input->param('accountlines_id');
133     my $charge           = Koha::Account::Lines->find($charge_id);
134     my $amount           = scalar $input->param('amount');
135     $schema->txn_do(
136         sub {
137
138             my $discount = $charge->reduce(
139                 {
140                     reduction_type => 'DISCOUNT',
141                     branch         => $library_id,
142                     staff_id       => $logged_in_user->id,
143                     interface      => 'intranet',
144                     amount         => $amount
145                 }
146             );
147         }
148     );
149 }
150
151 #get account details
152 my $total = $patron->account->balance;
153
154 my @accountlines = Koha::Account::Lines->search(
155     { borrowernumber => $patron->borrowernumber },
156     { order_by       => { -desc => 'accountlines_id' } }
157 );
158
159 my $totalcredit;
160 if($total <= 0){
161         $totalcredit = 1;
162 }
163
164 # Populate an arrayref with everything we need to display any
165 # renew errors that occurred based on what we were passed
166 my $renew_results_display = [];
167 foreach my $renew_result(@renew_results) {
168     my ($itemnumber, $success, $info) = split(/,/, $renew_result);
169     my $item = Koha::Items->find($itemnumber);
170     if ($success) {
171         $info = uri_unescape($info);
172     }
173     push @{$renew_results_display}, {
174         item    => $item,
175         success => $success,
176         info    => $info
177     };
178 }
179
180 $template->param(
181     patron              => $patron,
182     finesview           => 1,
183     total               => sprintf("%.2f",$total),
184     totalcredit         => $totalcredit,
185     accounts            => \@accountlines,
186     payment_id          => $payment_id,
187     change_given        => $change_given,
188     renew_results       => $renew_results_display,
189 );
190
191 output_html_with_http_headers $input, $cookie, $template->output;