Bug 21008: Use patron object to get category_type
[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::Account::Lines;
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 => 'edit_borrowers', 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 $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
53 my $patron         = Koha::Patrons->find( $borrowernumber );
54 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
55
56 if ( $patron->is_child ) {
57     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
58     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
59     $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
60 }
61
62 #get account details
63 my $total = $patron->account->balance;
64 my $accountline = Koha::Account::Lines->find($accountlines_id)->unblessed;
65
66 my $totalcredit;
67 if ( $total <= 0 ) {
68     $totalcredit = 1;
69 }
70
71
72 $accountline->{'amount'} += 0.00;
73 if ( $accountline->{'amount'} <= 0 ) {
74     $accountline->{'amountcredit'} = 1;
75     $accountline->{'amount'} *= -1.00;
76 }
77 $accountline->{'amountoutstanding'} += 0.00;
78 if ( $accountline->{'amountoutstanding'} <= 0 ) {
79     $accountline->{'amountoutstandingcredit'} = 1;
80 }
81
82 my %row = (
83     'date'                    => dt_from_string( $accountline->{'date'} ),
84     'amountcredit'            => $accountline->{'amountcredit'},
85     'amountoutstandingcredit' => $accountline->{'amountoutstandingcredit'},
86     'description'             => $accountline->{'description'},
87     'amount'                  => sprintf( "%.2f", $accountline->{'amount'} ),
88     'amountoutstanding' =>
89       sprintf( "%.2f", $accountline->{'amountoutstanding'} ),
90     'accountno' => $accountline->{'accountno'},
91     accounttype => $accountline->{accounttype},
92     'note'      => $accountline->{'note'},
93 );
94
95 $template->param(
96     patron         => $patron,
97     finesview      => 1,
98     total          => sprintf( "%.2f", $total ),
99     totalcredit    => $totalcredit,
100     accounts       => [$accountline], # FIXME There is always only 1 row!
101 );
102
103 output_html_with_http_headers $input, $cookie, $template->output;