Merge remote-tracking branch 'origin/new/bug_7986'
[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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 use strict;
24 use warnings;
25
26 use CGI;
27
28 use C4::Auth;
29 use C4::Output;
30 use C4::Members;
31 use C4::Branch qw(GetBranches);
32 use List::MoreUtils qw/any uniq/;
33 use Koha::DateUtils;
34
35 use C4::Dates qw/format_date/;
36 use C4::Members::Attributes qw(GetBorrowerAttributes);
37
38 my $input = CGI->new;
39
40 #get borrower details
41 my $data = undef;
42 my $borrowernumber = undef;
43 my $cardnumber = undef;
44
45 my ($template, $loggedinuser, $cookie)= get_template_and_user({template_name => "members/readingrec.tmpl",
46                                 query => $input,
47                                 type => "intranet",
48                                 authnotrequired => 0,
49                                 flagsrequired => {borrowers => 1},
50                                 debug => 1,
51                                 });
52
53 if ($input->param('cardnumber')) {
54     $cardnumber = $input->param('cardnumber');
55     $data = GetMember(cardnumber => $cardnumber);
56     $borrowernumber = $data->{'borrowernumber'}; # we must define this as it is used to retrieve other data about the patron
57 }
58 if ($input->param('borrowernumber')) {
59     $borrowernumber = $input->param('borrowernumber');
60     $data = GetMember(borrowernumber => $borrowernumber);
61 }
62
63 my $order = 'date_due desc';
64 my $limit = 0;
65 my $issues = GetAllIssues($borrowernumber,$order,$limit);
66
67 my $branches = GetBranches();
68 foreach my $issue ( @{$issues} ) {
69     $issue->{issuingbranch} = $branches->{ $issue->{branchcode} }->{branchname};
70 }
71
72 #   barcode export
73 if ( $input->param('op') eq 'export_barcodes' ) {
74     my $today = C4::Dates->new();
75     $today = $today->output('iso');
76     my @barcodes =
77       map { $_->{barcode} } grep { $_->{returndate} =~ m/^$today/o } @{$issues};
78     my $borrowercardnumber =
79       GetMember( borrowernumber => $borrowernumber )->{'cardnumber'};
80     my $delimiter = "\n";
81     binmode( STDOUT, ":encoding(UTF-8)" );
82     print $input->header(
83         -type       => 'application/octet-stream',
84         -charset    => 'utf-8',
85         -attachment => "$today-$borrowercardnumber-checkinexport.txt"
86     );
87     my $content = join $delimiter, uniq(@barcodes);
88     print $content;
89     exit;
90 }
91
92 if ( $data->{'category_type'} eq 'C') {
93     my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
94     my $cnt = scalar(@$catcodes);
95     $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
96     $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
97 }
98
99 $template->param( adultborrower => 1 ) if ( $data->{'category_type'} eq 'A' );
100 if (! $limit){
101         $limit = 'full';
102 }
103
104
105 my ($picture, $dberror) = GetPatronImage($data->{'cardnumber'});
106 $template->param( picture => 1 ) if $picture;
107
108 if (C4::Context->preference('ExtendedPatronAttributes')) {
109     my $attributes = GetBorrowerAttributes($borrowernumber);
110     $template->param(
111         ExtendedPatronAttributes => 1,
112         extendedattributes => $attributes
113     );
114 }
115
116 $template->param(
117     readingrecordview => 1,
118     title             => $data->{title},
119     initials          => $data->{initials},
120     surname           => $data->{surname},
121     othernames        => $data->{othernames},
122     borrowernumber    => $borrowernumber,
123     firstname         => $data->{firstname},
124     cardnumber        => $data->{cardnumber},
125     categorycode      => $data->{categorycode},
126     category_type     => $data->{category_type},
127     categoryname      => $data->{description},
128     address           => $data->{address},
129     address2          => $data->{address2},
130     city              => $data->{city},
131     state             => $data->{state},
132     zipcode           => $data->{zipcode},
133     country           => $data->{country},
134     phone             => $data->{phone},
135     email             => $data->{email},
136     branchcode        => $data->{branchcode},
137     is_child          => ( $data->{category_type} eq 'C' ),
138     branchname        => $branches->{ $data->{branchcode} }->{branchname},
139     loop_reading      => $issues,
140     activeBorrowerRelationship =>
141       ( C4::Context->preference('borrowerRelationship') ne '' ),
142 );
143 output_html_with_http_headers $input, $cookie, $template->output;
144