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