Bug 28606: Remove $DEBUG and $ENV{DEBUG}
[koha.git] / members / moremember.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2014 ByWater Solutions
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22
23 =head1 moremember.pl
24
25  script to do a borrower enquiry/bring up patron details etc
26  Displays all the details about a patron
27
28 =cut
29
30 use Modern::Perl;
31 use CGI qw ( -utf8 );
32 use C4::Context;
33 use C4::Auth;
34 use C4::Output;
35 use C4::Form::MessagingPreferences;
36 use List::MoreUtils qw/uniq/;
37 use Koha::Patron::Attribute::Types;
38 use Koha::Patron::Debarments qw(GetDebarments);
39 use Koha::Patron::Messages;
40 use Koha::DateUtils;
41 use Koha::CsvProfiles;
42 use Koha::Patrons;
43 use Koha::Patron::Files;
44 use Koha::Token;
45 use Koha::Checkouts;
46
47 my $input = CGI->new;
48
49 my $print = $input->param('print');
50
51 my $template_name;
52
53 if (defined $print and $print eq "brief") {
54         $template_name = "members/moremember-brief.tt";
55 } else {
56         $template_name = "members/moremember.tt";
57 }
58
59 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
60     {
61         template_name   => $template_name,
62         query           => $input,
63         type            => "intranet",
64         flagsrequired   => { borrowers => 'edit_borrowers' },
65     }
66 );
67 my $borrowernumber = $input->param('borrowernumber');
68 my $error = $input->param('error');
69 $template->param( error => $error ) if ( $error );
70
71 my $patron         = Koha::Patrons->find( $borrowernumber );
72 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
73 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
74
75 my $category_type = $patron->category->category_type;
76
77 for (qw(gonenoaddress lost borrowernotes is_debarred)) {
78     $patron->$_ and $template->param(flagged => 1) and last;
79 }
80
81 if ( $patron->is_debarred ) {
82     $template->param(
83         debarments => scalar GetDebarments({ borrowernumber => $borrowernumber }),
84     );
85     if ( $patron->debarred ne "9999-12-31" ) {
86         $template->param( 'userdebarreddate' => $patron->debarred );
87     }
88 }
89 $template->param( flagged => 1 ) if $patron->account_locked;
90
91 my @relatives;
92 my $guarantor_relationships = $patron->guarantor_relationships;
93 my @guarantees              = $patron->guarantee_relationships->guarantees;
94 my @guarantors              = $guarantor_relationships->guarantors;
95 if (@guarantors) {
96     push( @relatives, $_->id ) for @guarantors;
97     push( @relatives, $_->id ) for $patron->siblings();
98 }
99 else {
100     push( @relatives, $_->id ) for @guarantees;
101 }
102 $template->param(
103     guarantor_relationships => $guarantor_relationships,
104     guarantees              => \@guarantees,
105 );
106
107 my $relatives_issues_count =
108     Koha::Checkouts->count({ borrowernumber => \@relatives });
109
110 # Calculate and display patron's age
111 if ( !$patron->is_valid_age ) {
112     $template->param( age_limitations => 1 );
113     $template->param( age_low => $patron->category->dateofbirthrequired );
114     $template->param( age_high => $patron->category->upperagelimit );
115 }
116
117 # Generate CSRF token for upload and delete image buttons
118 $template->param(
119     csrf_token => Koha::Token->new->generate_csrf({ session_id => $input->cookie('CGISESSID'),}),
120 );
121
122 if (C4::Context->preference('ExtendedPatronAttributes')) {
123     my @attributes = $patron->extended_attributes->as_list; # FIXME Must be improved!
124     my @classes = uniq( map {$_->type->class} @attributes );
125     @classes = sort @classes;
126
127     my @attributes_loop;
128     for my $class (@classes) {
129         my @items;
130         for my $attr (@attributes) {
131             push @items, $attr if $attr->type->class eq $class
132         }
133         my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
134         my $lib = $av->count ? $av->next->lib : $class;
135
136         push @attributes_loop, {
137             class => $class,
138             items => \@items,
139             lib   => $lib,
140         };
141     }
142
143     $template->param(
144         attributes_loop => \@attributes_loop
145     );
146
147     my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
148     my $nb_of_attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id)->count;
149     if ( $nb_of_attribute_types == 0 ) {
150         $template->param(no_patron_attribute_types => 1);
151     }
152 }
153
154 if (C4::Context->preference('EnhancedMessagingPreferences')) {
155     C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
156     $template->param(messaging_form_inactive => 1);
157 }
158
159 if ( C4::Context->preference("ExportCircHistory") ) {
160     $template->param(csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc' }) ]);
161 }
162
163 my $patron_messages = Koha::Patron::Messages->search(
164     {
165         'me.borrowernumber' => $patron->borrowernumber,
166     },
167     {
168         join => 'manager',
169         '+select' => ['manager.surname', 'manager.firstname' ],
170         '+as' => ['manager_surname', 'manager_firstname'],
171     }
172 );
173
174 if( $patron_messages->count > 0 ){
175     $template->param( patron_messages => $patron_messages );
176 }
177
178 # Display the language description instead of the code
179 # Note that this is certainly wrong
180 my ( $subtag, $region ) = split '-', $patron->lang;
181 my $translated_language = C4::Languages::language_get_description( $subtag, $subtag, 'language' );
182
183 # if the expiry date is before today ie they have expired
184 if ( $patron->is_expired || $patron->is_going_to_expire ) {
185     $template->param(
186         flagged => 1
187     );
188 }
189
190 $template->param(
191     patron          => $patron,
192     issuecount      => $patron->checkouts->count,
193     holds_count     => $patron->holds->count,
194     fines           => $patron->account->balance,
195     translated_language => $translated_language,
196     detailview      => 1,
197     was_renewed     => scalar $input->param('was_renewed') ? 1 : 0,
198     $category_type  => 1, # [% IF ( I ) %] = institutional/organisation
199     housebound_role => scalar $patron->housebound_role,
200     relatives_issues_count => $relatives_issues_count,
201     relatives_borrowernumbers => \@relatives,
202     logged_in_user => $logged_in_user,
203     files => Koha::Patron::Files->new( borrowernumber => $borrowernumber ) ->GetFilesInfo(),
204 );
205
206 output_html_with_http_headers $input, $cookie, $template->output;