Bug 18403: Add sub output_and_exit_if_error - unknown_patron & cannot_see_patron_infos
[koha.git] / members / printfeercpt.pl
1 #!/usr/bin/perl
2
3
4 #written 3rd May 2010 by kmkale@anantcorp.com adapted from boraccount.pl by chris@katipo.oc.nz
5 #script to print fee receipts
6
7
8 # Copyright Koustubha Kale
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # Koha is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24
25 use Modern::Perl;
26
27 use C4::Auth;
28 use C4::Output;
29 use CGI qw ( -utf8 );
30 use C4::Members;
31 use C4::Accounts;
32 use Koha::DateUtils;
33 use Koha::Patrons;
34 use Koha::Patron::Categories;
35
36 my $input=new CGI;
37
38
39 my ($template, $loggedinuser, $cookie)
40     = get_template_and_user({template_name => "members/printfeercpt.tt",
41                             query => $input,
42                             type => "intranet",
43                             authnotrequired => 0,
44                             flagsrequired => {borrowers => 'edit_borrowers', updatecharges => 'remaining_permissions'},
45                             debug => 1,
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 my $category = $patron->category;
57 my $data = $patron->unblessed;
58 $data->{description} = $category->description;
59 $data->{category_type} = $category->category_type;
60
61 if ( $action eq 'print' ) {
62 #  ReversePayment( $borrowernumber, $input->param('accountno') );
63 }
64
65 if ( $data->{'category_type'} eq 'C') {
66     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
67     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
68     $template->param( 'catcode' => $patron_categories->next->categorycde )  if $patron_categories->count == 1;
69 }
70
71 #get account details
72 my ($total,$accts,$numaccts)=GetMemberAccountRecords($borrowernumber);
73 my $totalcredit;
74 if($total <= 0){
75         $totalcredit = 1;
76 }
77 my @accountrows; # this is for the tmpl-loop
78
79 my $toggle;
80 for (my $i=0;$i<$numaccts;$i++){
81     next if ( $accts->[$i]{'accountlines_id'} ne $accountlines_id );
82     if($i%2){
83             $toggle = 0;
84     } else {
85             $toggle = 1;
86     }
87     $accts->[$i]{'toggle'} = $toggle;
88     $accts->[$i]{'amount'}+=0.00;
89     if($accts->[$i]{'amount'} <= 0){
90         $accts->[$i]{'amountcredit'} = 1;
91         $accts->[$i]{'amount'}*=-1.00;
92     }
93     $accts->[$i]{'amountoutstanding'}+=0.00;
94     if($accts->[$i]{'amountoutstanding'} <= 0){
95         $accts->[$i]{'amountoutstandingcredit'} = 1;
96     }
97
98     my %row = ( 'date'         => dt_from_string( $accts->[$i]{'date'} ),
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  #   category_description => $data->{'description'},
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 output_html_with_http_headers $input, $cookie, $template->output;