Bug 18403: Add sub output_and_exit_if_error - unknown_patron & cannot_see_patron_infos
[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 my $data = $patron->unblessed;
57 $data->{description} = $category->description;
58 $data->{category_type} = $category->category_type;
59
60 if ( $data->{'category_type'} eq 'C' ) {
61     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
62     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
63     $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
64 }
65
66 #get account details
67 my ( $total, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber);
68 my $totalcredit;
69 if ( $total <= 0 ) {
70     $totalcredit = 1;
71 }
72
73 my @accountrows;    # this is for the tmpl-loop
74
75 my $toggle;
76 for ( my $i = 0 ; $i < $numaccts ; $i++ ) {
77     next if ( $accts->[$i]{'accountlines_id'} ne $accountlines_id );
78
79     if ( $i % 2 ) {
80         $toggle = 0;
81     } else {
82         $toggle = 1;
83     }
84
85     $accts->[$i]{'toggle'} = $toggle;
86     $accts->[$i]{'amount'} += 0.00;
87
88     if ( $accts->[$i]{'amount'} <= 0 ) {
89         $accts->[$i]{'amountcredit'} = 1;
90     }
91
92     $accts->[$i]{'amountoutstanding'} += 0.00;
93     if ( $accts->[$i]{'amountoutstanding'} <= 0 ) {
94         $accts->[$i]{'amountoutstandingcredit'} = 1;
95     }
96
97     my %row = (
98         'date'                    => output_pref({ dt => dt_from_string( $accts->[$i]{'date'} ), dateonly => 1 }),
99         'amountcredit'            => $accts->[$i]{'amountcredit'},
100         'amountoutstandingcredit' => $accts->[$i]{'amountoutstandingcredit'},
101         'toggle'                  => $accts->[$i]{'toggle'},
102         'description'             => $accts->[$i]{'description'},
103         'itemnumber'              => $accts->[$i]{'itemnumber'},
104         'biblionumber'            => $accts->[$i]{'biblionumber'},
105         'amount'                  => sprintf( "%.2f", $accts->[$i]{'amount'} ),
106         'amountoutstanding'       => sprintf( "%.2f", $accts->[$i]{'amountoutstanding'} ),
107         'accountno'               => $accts->[$i]{'accountno'},
108         accounttype               => $accts->[$i]{accounttype},
109         'note'                    => $accts->[$i]{'note'},
110     );
111
112     if ( $accts->[$i]{'accounttype'} ne 'F' && $accts->[$i]{'accounttype'} ne 'FU' ) {
113         $row{'printtitle'} = 1;
114         $row{'title'}      = $accts->[$i]{'title'};
115     }
116
117     push( @accountrows, \%row );
118 }
119
120 $template->param( adultborrower => 1 ) if ( $data->{'category_type'} eq 'A' || $data->{'category_type'} eq 'I' );
121
122 $template->param( picture => 1 ) if $patron->image;
123
124 $template->param(
125     finesview      => 1,
126     firstname      => $data->{'firstname'},
127     surname        => $data->{'surname'},
128     borrowernumber => $borrowernumber,
129     cardnumber     => $data->{'cardnumber'},
130     categorycode   => $data->{'categorycode'},
131     category_type  => $data->{'category_type'},
132     categoryname   => $data->{'description'},
133     address        => $data->{'address'},
134     address2       => $data->{'address2'},
135     city           => $data->{'city'},
136     zipcode        => $data->{'zipcode'},
137     country        => $data->{'country'},
138     phone          => $data->{'phone'},
139     email          => $data->{'email'},
140     branchcode     => $data->{'branchcode'},
141     total          => sprintf( "%.2f", $total ),
142     totalcredit    => $totalcredit,
143     is_child       => ( $data->{'category_type'} eq 'C' ),
144     accounts       => \@accountrows
145 );
146
147 output_html_with_http_headers $input, $cookie, $template->output;