Bug 19230: Preventing warn when deleting course
[koha.git] / members / printinvoice.pl
1 #!/usr/bin/perl
2
3 #written 3rd May 2010 by kmkale@anantcorp.com adapted from boraccount.pl by chris@katipo.oc.nz
4 #script to print fee receipts
5
6 # Copyright Koustubha Kale
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 use strict;
24 use warnings;
25
26 use C4::Auth;
27 use C4::Output;
28 use Koha::DateUtils;
29 use CGI qw ( -utf8 );
30 use C4::Members;
31 use C4::Accounts;
32
33 use Koha::Patrons;
34 use Koha::Patron::Categories;
35
36 my $input = new CGI;
37
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39     {   template_name   => "members/printinvoice.tt",
40         query           => $input,
41         type            => "intranet",
42         authnotrequired => 0,
43         flagsrequired => { borrowers => 1, updatecharges => 'remaining_permissions' },
44         debug           => 1,
45     }
46 );
47
48 my $borrowernumber  = $input->param('borrowernumber');
49 my $action          = $input->param('action') || '';
50 my $accountlines_id = $input->param('accountlines_id');
51
52 my $patron = Koha::Patrons->find( $borrowernumber );
53 unless ( $patron ) {
54     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
55     exit;
56 }
57 my $category = $patron->category;
58 my $data = $patron->unblessed;
59 $data->{description} = $category->description;
60 $data->{category_type} = $category->category_type;
61
62 if ( $data->{'category_type'} eq 'C' ) {
63     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
64     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
65     $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
66 }
67
68 #get account details
69 my ( $total, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber);
70 my $totalcredit;
71 if ( $total <= 0 ) {
72     $totalcredit = 1;
73 }
74
75 my @accountrows;    # this is for the tmpl-loop
76
77 my $toggle;
78 for ( my $i = 0 ; $i < $numaccts ; $i++ ) {
79     next if ( $accts->[$i]{'accountlines_id'} ne $accountlines_id );
80
81     if ( $i % 2 ) {
82         $toggle = 0;
83     } else {
84         $toggle = 1;
85     }
86
87     $accts->[$i]{'toggle'} = $toggle;
88     $accts->[$i]{'amount'} += 0.00;
89
90     if ( $accts->[$i]{'amount'} <= 0 ) {
91         $accts->[$i]{'amountcredit'} = 1;
92     }
93
94     $accts->[$i]{'amountoutstanding'} += 0.00;
95     if ( $accts->[$i]{'amountoutstanding'} <= 0 ) {
96         $accts->[$i]{'amountoutstandingcredit'} = 1;
97     }
98
99     my %row = (
100         'date'                    => output_pref({ dt => dt_from_string( $accts->[$i]{'date'} ), dateonly => 1 }),
101         'amountcredit'            => $accts->[$i]{'amountcredit'},
102         'amountoutstandingcredit' => $accts->[$i]{'amountoutstandingcredit'},
103         'toggle'                  => $accts->[$i]{'toggle'},
104         'description'             => $accts->[$i]{'description'},
105         'itemnumber'              => $accts->[$i]{'itemnumber'},
106         'biblionumber'            => $accts->[$i]{'biblionumber'},
107         'amount'                  => sprintf( "%.2f", $accts->[$i]{'amount'} ),
108         'amountoutstanding'       => sprintf( "%.2f", $accts->[$i]{'amountoutstanding'} ),
109         'accountno'               => $accts->[$i]{'accountno'},
110         accounttype               => $accts->[$i]{accounttype},
111         'note'                    => $accts->[$i]{'note'},
112     );
113
114     if ( $accts->[$i]{'accounttype'} ne 'F' && $accts->[$i]{'accounttype'} ne 'FU' ) {
115         $row{'printtitle'} = 1;
116         $row{'title'}      = $accts->[$i]{'title'};
117     }
118
119     push( @accountrows, \%row );
120 }
121
122 $template->param( adultborrower => 1 ) if ( $data->{'category_type'} eq 'A' || $data->{'category_type'} eq 'I' );
123
124 $template->param( picture => 1 ) if $patron->image;
125
126 $template->param(
127     finesview      => 1,
128     firstname      => $data->{'firstname'},
129     surname        => $data->{'surname'},
130     borrowernumber => $borrowernumber,
131     cardnumber     => $data->{'cardnumber'},
132     categorycode   => $data->{'categorycode'},
133     category_type  => $data->{'category_type'},
134     categoryname   => $data->{'description'},
135     address        => $data->{'address'},
136     address2       => $data->{'address2'},
137     city           => $data->{'city'},
138     zipcode        => $data->{'zipcode'},
139     country        => $data->{'country'},
140     phone          => $data->{'phone'},
141     email          => $data->{'email'},
142     branchcode     => $data->{'branchcode'},
143     total          => sprintf( "%.2f", $total ),
144     totalcredit    => $totalcredit,
145     is_child       => ( $data->{'category_type'} eq 'C' ),
146     accounts       => \@accountrows
147 );
148
149 output_html_with_http_headers $input, $cookie, $template->output;