Bug 18789: Use Koha::Patron->is_adult where needed
[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 Modern::Perl;
24
25 use C4::Auth;
26 use C4::Output;
27 use Koha::DateUtils;
28 use CGI qw ( -utf8 );
29 use C4::Members;
30 use C4::Accounts;
31
32 use Koha::Patrons;
33 use Koha::Patron::Categories;
34
35 my $input = new CGI;
36
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38     {   template_name   => "members/printinvoice.tt",
39         query           => $input,
40         type            => "intranet",
41         authnotrequired => 0,
42         flagsrequired => { borrowers => 'edit_borrowers', updatecharges => 'remaining_permissions' },
43         debug           => 1,
44     }
45 );
46
47 my $borrowernumber  = $input->param('borrowernumber');
48 my $action          = $input->param('action') || '';
49 my $accountlines_id = $input->param('accountlines_id');
50
51 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
52 my $patron         = Koha::Patrons->find( $borrowernumber );
53 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
54
55 my $category = $patron->category;
56
57 if ( $category->category_type eq 'C' ) {
58     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
59     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
60     $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
61 }
62
63 #get account details
64 my ( $total, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber);
65 my $totalcredit;
66 if ( $total <= 0 ) {
67     $totalcredit = 1;
68 }
69
70 my @accountrows;    # this is for the tmpl-loop
71
72 my $toggle;
73 for ( my $i = 0 ; $i < $numaccts ; $i++ ) {
74     next if ( $accts->[$i]{'accountlines_id'} ne $accountlines_id );
75
76     if ( $i % 2 ) {
77         $toggle = 0;
78     } else {
79         $toggle = 1;
80     }
81
82     $accts->[$i]{'toggle'} = $toggle;
83     $accts->[$i]{'amount'} += 0.00;
84
85     if ( $accts->[$i]{'amount'} <= 0 ) {
86         $accts->[$i]{'amountcredit'} = 1;
87     }
88
89     $accts->[$i]{'amountoutstanding'} += 0.00;
90     if ( $accts->[$i]{'amountoutstanding'} <= 0 ) {
91         $accts->[$i]{'amountoutstandingcredit'} = 1;
92     }
93
94     my %row = (
95         'date'                    => output_pref({ dt => dt_from_string( $accts->[$i]{'date'} ), dateonly => 1 }),
96         'amountcredit'            => $accts->[$i]{'amountcredit'},
97         'amountoutstandingcredit' => $accts->[$i]{'amountoutstandingcredit'},
98         'toggle'                  => $accts->[$i]{'toggle'},
99         'description'             => $accts->[$i]{'description'},
100         'itemnumber'              => $accts->[$i]{'itemnumber'},
101         'biblionumber'            => $accts->[$i]{'biblionumber'},
102         'amount'                  => sprintf( "%.2f", $accts->[$i]{'amount'} ),
103         'amountoutstanding'       => sprintf( "%.2f", $accts->[$i]{'amountoutstanding'} ),
104         'accountno'               => $accts->[$i]{'accountno'},
105         accounttype               => $accts->[$i]{accounttype},
106         'note'                    => $accts->[$i]{'note'},
107     );
108
109     if ( $accts->[$i]{'accounttype'} ne 'F' && $accts->[$i]{'accounttype'} ne 'FU' ) {
110         $row{'printtitle'} = 1;
111         $row{'title'}      = $accts->[$i]{'title'};
112     }
113
114     push( @accountrows, \%row );
115 }
116
117 $template->param(
118     patron         => $patron,
119     finesview      => 1,
120     total          => sprintf( "%.2f", $total ),
121     totalcredit    => $totalcredit,
122     accounts       => \@accountrows
123 );
124
125 output_html_with_http_headers $input, $cookie, $template->output;