Bug 21757: Clenup of moremember.pl and its templates
[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::Members::AttributeTypes;
36 use C4::Form::MessagingPreferences;
37 use List::MoreUtils qw/uniq/;
38 use C4::Members::Attributes qw(GetBorrowerAttributes);
39 use Koha::Patron::Debarments qw(GetDebarments);
40 use Koha::Patron::Messages;
41 use Koha::DateUtils;
42 use Koha::CsvProfiles;
43 use Koha::Patrons;
44 use Koha::Token;
45 use Koha::Checkouts;
46
47 use vars qw($debug);
48
49 BEGIN {
50     $debug = $ENV{DEBUG} || 0;
51 }
52
53 my $input = CGI->new;
54 $debug or $debug = $input->param('debug') || 0;
55
56 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
57     {
58         template_name   => "members/moremember.tt",
59         query           => $input,
60         type            => "intranet",
61         authnotrequired => 0,
62         flagsrequired   => { borrowers => 'edit_borrowers' },
63         debug           => 1,
64     }
65 );
66 my $borrowernumber = $input->param('borrowernumber');
67 my $error = $input->param('error');
68 $template->param( error => $error ) if ( $error );
69
70 my $patron         = Koha::Patrons->find( $borrowernumber );
71 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
72 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
73
74 my $category_type = $patron->category->category_type;
75
76 for (qw(gonenoaddress lost borrowernotes is_debarred)) {
77     $patron->$_ and $template->param(flagged => 1) and last;
78 }
79
80 if ( $patron->is_debarred ) {
81     $template->param(
82         debarments => scalar GetDebarments({ borrowernumber => $borrowernumber }),
83     );
84     if ( $patron->debarred ne "9999-12-31" ) {
85         $template->param( 'userdebarreddate' => $patron->debarred );
86     }
87 }
88
89 my @relatives;
90 if ( my $guarantor = $patron->guarantor ) {
91     $template->param( guarantor => $guarantor );
92     push @relatives, $guarantor->borrowernumber;
93     push @relatives, $_->borrowernumber for $patron->siblings;
94 } elsif ( $patron->contactname || $patron->contactfirstname ) {
95     $template->param(
96         guarantor => {
97             firstname => $patron->contactfirstname,
98             surname   => $patron->contactname,
99         }
100     );
101 } else {
102     my @guarantees = $patron->guarantees;
103     $template->param( guarantees => \@guarantees );
104     push @relatives, $_->borrowernumber for @guarantees;
105 }
106
107 my $relatives_issues_count =
108     Koha::Checkouts->count({ borrowernumber => \@relatives });
109
110 # Generate CSRF token for upload and delete image buttons
111 $template->param(
112     csrf_token => Koha::Token->new->generate_csrf({ session_id => $input->cookie('CGISESSID'),}),
113 );
114
115 if (C4::Context->preference('ExtendedPatronAttributes')) {
116     my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
117     my @classes = uniq( map {$_->{class}} @$attributes );
118     @classes = sort @classes;
119
120     my @attributes_loop;
121     for my $class (@classes) {
122         my @items;
123         for my $attr (@$attributes) {
124             push @items, $attr if $attr->{class} eq $class
125         }
126         my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
127         my $lib = $av->count ? $av->next->lib : $class;
128
129         push @attributes_loop, {
130             class => $class,
131             items => \@items,
132             lib   => $lib,
133         };
134     }
135
136     $template->param(
137         attributes_loop => \@attributes_loop
138     );
139
140     my @types = C4::Members::AttributeTypes::GetAttributeTypes();
141     if (scalar(@types) == 0) {
142         $template->param(no_patron_attribute_types => 1);
143     }
144 }
145
146 if (C4::Context->preference('EnhancedMessagingPreferences')) {
147     C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
148     $template->param(messaging_form_inactive => 1);
149 }
150
151 if ( C4::Context->preference("ExportCircHistory") ) {
152     $template->param(csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc' }) ]);
153 }
154
155 my $patron_messages = Koha::Patron::Messages->search(
156     {
157         'me.borrowernumber' => $patron->borrowernumber,
158     },
159     {
160         join => 'manager',
161         '+select' => ['manager.surname', 'manager.firstname' ],
162         '+as' => ['manager_surname', 'manager_firstname'],
163     }
164 );
165
166 if( $patron_messages->count > 0 ){
167     $template->param( patron_messages => $patron_messages );
168 }
169
170 # Display the language description instead of the code
171 # Note that this is certainly wrong
172 my ( $subtag, $region ) = split '-', $patron->lang;
173 my $translated_language = C4::Languages::language_get_description( $subtag, $subtag, 'language' );
174
175 # if the expiry date is before today ie they have expired
176 if ( $patron->is_expired || $patron->is_going_to_expire ) {
177     $template->param(
178         flagged => 1
179     );
180 }
181
182 $template->param(
183     patron          => $patron,
184     issuecount      => $patron->checkouts->count,
185     holds_count     => $patron->holds->count,
186     fines           => $patron->account->balance,
187     translated_language => $translated_language,
188     detailview      => 1,
189     was_renewed     => scalar $input->param('was_renewed') ? 1 : 0,
190     $category_type  => 1, # [% IF ( I ) %] = institutional/organisation
191     housebound_role => scalar $patron->housebound_role,
192     relatives_issues_count => $relatives_issues_count,
193     relatives_borrowernumbers => \@relatives,
194 );
195
196 output_html_with_http_headers $input, $cookie, $template->output;