Bug 18403: Add sub output_and_exit_if_error - unknown_patron & cannot_see_patron_infos
[koha.git] / members / readingrec.pl
1 #!/usr/bin/perl
2
3 # written 27/01/2000
4 # script to display borrowers reading record
5
6 # Copyright 2000-2002 Katipo Communications
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 CGI qw ( -utf8 );
26
27 use C4::Auth;
28 use C4::Output;
29 use C4::Members;
30 use List::MoreUtils qw/any uniq/;
31 use Koha::DateUtils;
32 use C4::Members::Attributes qw(GetBorrowerAttributes);
33
34 use Koha::Patrons;
35 use Koha::Patron::Categories;
36
37 my $input = CGI->new;
38
39 #get borrower details
40 my $data = undef;
41 my $borrowernumber = undef;
42 my $cardnumber = undef;
43
44 my ($template, $loggedinuser, $cookie)= get_template_and_user({template_name => "members/readingrec.tt",
45                                 query => $input,
46                                 type => "intranet",
47                                 authnotrequired => 0,
48                 flagsrequired => {borrowers => 'edit_borrowers'},
49                                 debug => 1,
50                                 });
51
52 my $op = $input->param('op') || '';
53 my $patron;
54 if ($input->param('cardnumber')) {
55     $cardnumber = $input->param('cardnumber');
56     $patron = Koha::Patrons->find( { cardnumber => $cardnumber } );
57 }
58 if ($input->param('borrowernumber')) {
59     $borrowernumber = $input->param('borrowernumber');
60     $patron = Koha::Patrons->find( $borrowernumber );
61 }
62
63 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
64 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
65
66 $data = $patron->unblessed;
67 $borrowernumber = $patron->borrowernumber;
68
69 my $order = 'date_due desc';
70 my $limit = 0;
71 my $issues = ();
72 # Do not request the old issues of anonymous patron
73 if ( $borrowernumber eq C4::Context->preference('AnonymousPatron') ){
74     # use of 'eq' in the above comparison is intentional -- the
75     # system preference value could be blank
76     $template->param( is_anonymous => 1 );
77 } else {
78     $issues = GetAllIssues($borrowernumber,$order,$limit);
79 }
80
81 #   barcode export
82 if ( $op eq 'export_barcodes' ) {
83     # FIXME This should be moved out of this script
84     if ( $data->{'privacy'} < 2) {
85         my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
86         my @barcodes =
87           map { $_->{barcode} } grep { $_->{returndate} =~ m/^$today/o } @{$issues};
88         my $borrowercardnumber = $data->{cardnumber};
89         my $delimiter = "\n";
90         binmode( STDOUT, ":encoding(UTF-8)" );
91         print $input->header(
92             -type       => 'application/octet-stream',
93             -charset    => 'utf-8',
94             -attachment => "$today-$borrowercardnumber-checkinexport.txt"
95         );
96
97         my $content = join $delimiter, uniq(@barcodes);
98         print $content;
99         exit;
100     }
101 }
102
103 if ( $data->{'category_type'} eq 'C') {
104     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
105     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
106     $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
107 }
108
109 $template->param( adultborrower => 1 ) if ( $data->{'category_type'} eq 'A' || $data->{'category_type'} eq 'I' );
110 if (! $limit){
111         $limit = 'full';
112 }
113
114 $template->param( picture => 1 ) if $patron->image;
115
116 if (C4::Context->preference('ExtendedPatronAttributes')) {
117     my $attributes = GetBorrowerAttributes($borrowernumber);
118     $template->param(
119         ExtendedPatronAttributes => 1,
120         extendedattributes => $attributes
121     );
122 }
123
124 $template->param(%$data);
125
126 $template->param(
127     readingrecordview => 1,
128     borrowernumber    => $borrowernumber,
129     privacy           => $data->{'privacy'},
130     categoryname      => $data->{description},
131     is_child          => ( $data->{category_type} eq 'C' ),
132     loop_reading      => $issues,
133 );
134 output_html_with_http_headers $input, $cookie, $template->output;
135