3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2010,2011 PTFS-Europe Ltd
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 written 11/1/2000 by chris@katipo.oc.nz
25 part of the koha library system, script to facilitate paying off fines
42 use C4::Members::Attributes qw(GetBorrowerAttributes);
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47 { template_name => 'members/pay.tmpl',
51 flagsrequired => { borrowers => 1, updatecharges => 1 },
59 my @names = $input->param;
61 my $borrowernumber = $input->param('borrowernumber');
62 if ( !$borrowernumber ) {
63 $borrowernumber = $input->param('borrowernumber0');
66 # get borrower details
67 my $borrower = GetMember( borrowernumber => $borrowernumber );
68 my $user = $input->remote_user;
71 my $branches = GetBranches();
72 my $branch = GetBranch( $input, $branches );
74 my $writeoff_item = $input->param('confirm_writeoff');
75 my $paycollect = $input->param('paycollect');
77 print $input->redirect(
78 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
80 my $payselected = $input->param('payselected');
85 my $writeoff_all = $input->param('woall'); # writeoff all fines
88 } elsif ($writeoff_item) {
89 my $accountno = $input->param('accountno');
90 my $itemno = $input->param('itemnumber');
91 my $account_type = $input->param('accounttype');
92 my $amount = $input->param('amount');
93 writeoff( $accountno, $itemno, $account_type, $amount );
97 if (/^pay_indiv_(\d+)$/) {
99 redirect_to_paycollect( 'pay_individual', $line_no );
100 } elsif (/^wo_indiv_(\d+)$/) {
102 redirect_to_paycollect( 'writeoff_individual', $line_no );
106 $template->param( activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne '') );
108 add_accounts_to_template();
110 output_html_with_http_headers $input, $cookie, $template->output;
113 my ( $accountnum, $itemnum, $accounttype, $amount ) = @_;
115 # if no item is attached to fine, make sure to store it as a NULL
118 $writeoff_sth->execute( $accountnum, $borrowernumber );
120 my $acct = getnextacctno($borrowernumber);
121 $add_writeoff_sth->execute( $borrowernumber, $acct, $itemnum, $amount );
123 UpdateStats( $branch, 'writeoff', $amount, q{}, q{}, q{}, $borrowernumber );
128 sub add_accounts_to_template {
130 my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
132 my @notify = NumberNotifyId($borrowernumber);
134 my $notify_groups = [];
135 for my $notify_id (@notify) {
136 my ( $acct_total, $accountlines, undef ) =
137 GetBorNotifyAcctRecord( $borrowernumber, $notify_id );
138 if ( @{$accountlines} ) {
139 my $totalnotify = AmountNotify( $notify_id, $borrowernumber );
141 { accountlines => $accountlines,
142 notify => $notify_id,
143 total => $totalnotify,
147 borrower_add_additional_fields($borrower);
149 accounts => $accounts,
150 borrower => $borrower,
157 sub get_for_redirect {
158 my ( $name, $name_in, $money ) = @_;
159 my $s = q{&} . $name . q{=};
160 my $value = $input->param($name_in);
161 if ( !defined $value ) {
162 $value = ( $money == 1 ) ? 0 : q{};
165 $s .= sprintf '%.2f', $value;
172 sub redirect_to_paycollect {
173 my ( $action, $line_no ) = @_;
175 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
177 $redirect .= "$action=1";
178 $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
179 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
181 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
182 $redirect .= get_for_redirect( 'accountno', "accountno$line_no", 0 );
183 $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
184 $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
185 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
186 $redirect .= get_for_redirect( 'notify_id', "notify_id$line_no", 0 );
187 $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
188 $redirect .= '&remote_user=';
190 return print $input->redirect($redirect);
195 my @wo_lines = grep { /^accountno\d+$/ } @params;
199 my $accounttype = $input->param("accounttype$value");
201 # my $borrowernum = $input->param("borrowernumber$value");
202 my $itemno = $input->param("itemnumber$value");
203 my $amount = $input->param("amount$value");
204 my $accountno = $input->param("accountno$value");
205 writeoff( $accountno, $itemno, $accounttype, $amount );
209 $borrowernumber = $input->param('borrowernumber');
210 print $input->redirect(
211 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
215 sub borrower_add_additional_fields {
218 # some borrower info is not returned in the standard call despite being assumed
219 # in a number of templates. It should not be the business of this script but in lieu of
220 # a revised api here it is ...
221 if ( $b_ref->{category_type} eq 'C' ) {
222 my ( $catcodes, $labels ) =
223 GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
224 if ( @{$catcodes} ) {
225 if ( @{$catcodes} > 1 ) {
226 $b_ref->{CATCODE_MULTI} = 1;
227 } elsif ( @{$catcodes} == 1 ) {
228 $b_ref->{catcode} = $catcodes->[0];
231 } elsif ( $b_ref->{category_type} eq 'A' ) {
232 $b_ref->{adultborrower} = 1;
234 my ( $picture, $dberror ) = GetPatronImage( $b_ref->{cardnumber} );
236 $b_ref->{has_picture} = 1;
239 if (C4::Context->preference('ExtendedPatronAttributes')) {
240 $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
242 ExtendedPatronAttributes => 1,
246 $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
255 if (/^incl_par_(\d+)$/) {
257 push @lines_to_pay, $input->param("accountno$index");
258 $amt += $input->param("amountoutstanding$index");
261 $amt = '&amt=' . $amt;
262 my $sel = '&selected=' . join ',', @lines_to_pay;
264 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
268 print $input->redirect($redirect);
272 sub get_writeoff_sth {
274 # lets prepare these statement handles only once
278 my $dbh = C4::Context->dbh;
280 # Do we need to validate accounttype
281 my $sql = 'Update accountlines set amountoutstanding=0 '
282 . 'WHERE accountno=? and borrowernumber=?';
283 $writeoff_sth = $dbh->prepare($sql);
285 q{insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype)}
286 . q{values (?,?,?,now(),?,'Writeoff','W')};
287 $add_writeoff_sth = $dbh->prepare($insert);