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