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