Bug 27873: Improve message consistency: checkout and patron detail pages
[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 Scalar::Util qw( looks_like_number );
38 use Koha::Patron::Attribute::Types;
39 use Koha::Patron::Debarments qw(GetDebarments);
40 use Koha::Patron::Messages;
41 use Koha::DateUtils;
42 use Koha::CsvProfiles;
43 use Koha::Holds;
44 use Koha::Patrons;
45 use Koha::Patron::Files;
46 use Koha::Token;
47 use Koha::Checkouts;
48
49 my $input = CGI->new;
50
51 my $print = $input->param('print');
52
53 my $template_name;
54
55 if (defined $print and $print eq "brief") {
56         $template_name = "members/moremember-brief.tt";
57 } else {
58         $template_name = "members/moremember.tt";
59 }
60
61 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
62     {
63         template_name   => $template_name,
64         query           => $input,
65         type            => "intranet",
66         flagsrequired   => { borrowers => 'edit_borrowers' },
67     }
68 );
69 my $borrowernumber = $input->param('borrowernumber');
70 my $error = $input->param('error');
71 $template->param( error => $error ) if ( $error );
72
73 my $patron         = Koha::Patrons->find( $borrowernumber );
74 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
75 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
76
77 my $category_type = $patron->category->category_type;
78
79 for (qw(gonenoaddress lost borrowernotes is_debarred)) {
80     $patron->$_ and $template->param(flagged => 1) and last;
81 }
82
83 if ( $patron->is_debarred ) {
84     $template->param(
85         'debarments'      => scalar GetDebarments({ borrowernumber => $borrowernumber }),
86         'userdebarred'    => $patron->debarred,
87         'debarredcomment' => $patron->debarredcomment,
88     );
89
90     if ( $patron->debarred ne "9999-12-31" ) {
91         $template->param( 'userdebarreddate' => $patron->debarred );
92     }
93 }
94
95 $template->param( flagged => 1 ) if $patron->account_locked;
96
97 my @relatives;
98 my $guarantor_relationships = $patron->guarantor_relationships;
99 my @guarantees              = $patron->guarantee_relationships->guarantees;
100 my @guarantors              = $guarantor_relationships->guarantors;
101 if (@guarantors) {
102     push( @relatives, $_->id ) for @guarantors;
103     push( @relatives, $_->id ) for $patron->siblings();
104 }
105 else {
106     push( @relatives, $_->id ) for @guarantees;
107 }
108 $template->param(
109     guarantor_relationships => $guarantor_relationships,
110     guarantees              => \@guarantees,
111 );
112
113 my $relatives_issues_count =
114     Koha::Checkouts->count({ borrowernumber => \@relatives });
115
116 # Calculate and display patron's age
117 if ( !$patron->is_valid_age ) {
118     $template->param( age_limitations => 1 );
119     $template->param( age_low => $patron->category->dateofbirthrequired );
120     $template->param( age_high => $patron->category->upperagelimit );
121 }
122
123 # Generate CSRF token for upload and delete image buttons
124 $template->param(
125     csrf_token => Koha::Token->new->generate_csrf({ session_id => $input->cookie('CGISESSID'),}),
126 );
127
128 if (C4::Context->preference('ExtendedPatronAttributes')) {
129     my @attributes = $patron->extended_attributes->as_list; # FIXME Must be improved!
130     my @classes = uniq( map {$_->type->class} @attributes );
131     @classes = sort @classes;
132
133     my @attributes_loop;
134     for my $class (@classes) {
135         my @items;
136         for my $attr (@attributes) {
137             push @items, $attr if $attr->type->class eq $class
138         }
139         my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
140         my $lib = $av->count ? $av->next->lib : $class;
141
142         push @attributes_loop, {
143             class => $class,
144             items => \@items,
145             lib   => $lib,
146         };
147     }
148
149     $template->param(
150         attributes_loop => \@attributes_loop
151     );
152
153     my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
154     my $nb_of_attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id)->count;
155     if ( $nb_of_attribute_types == 0 ) {
156         $template->param(no_patron_attribute_types => 1);
157     }
158 }
159
160 if (C4::Context->preference('EnhancedMessagingPreferences')) {
161     C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
162     $template->param(messaging_form_inactive => 1);
163 }
164
165 if ( C4::Context->preference("ExportCircHistory") ) {
166     $template->param(csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc' }) ]);
167 }
168
169 my $patron_messages = Koha::Patron::Messages->search(
170     {
171         'me.borrowernumber' => $patron->borrowernumber,
172     },
173     {
174         join => 'manager',
175         '+select' => ['manager.surname', 'manager.firstname' ],
176         '+as' => ['manager_surname', 'manager_firstname'],
177     }
178 );
179
180 if( $patron_messages->count > 0 ){
181     $template->param( patron_messages => $patron_messages );
182 }
183
184 # Display the language description instead of the code
185 # Note that this is certainly wrong
186 my ( $subtag, $region ) = split '-', $patron->lang;
187 my $translated_language = C4::Languages::language_get_description( $subtag, $subtag, 'language' );
188
189 # if the expiry date is before today ie they have expired
190 if ( $patron->is_expired || $patron->is_going_to_expire ) {
191     $template->param(
192         flagged => 1
193     );
194 }
195
196 my $holds = Koha::Holds->search( { borrowernumber => $borrowernumber } ); # FIXME must be Koha::Patron->holds
197 my $waiting_holds = $holds->waiting;
198 $template->param(
199     holds_count  => $holds->count(),
200     WaitingHolds => $waiting_holds,
201 );
202
203 my $no_issues_charge_guarantees = C4::Context->preference("NoIssuesChargeGuarantees");
204 $no_issues_charge_guarantees = undef unless looks_like_number( $no_issues_charge_guarantees );
205 if ( defined $no_issues_charge_guarantees ) {
206     my $guarantees_non_issues_charges = 0;
207     my $guarantees = $patron->guarantee_relationships->guarantees;
208     while ( my $g = $guarantees->next ) {
209         $guarantees_non_issues_charges += $g->account->non_issues_charges;
210     }
211     if ( $guarantees_non_issues_charges > $no_issues_charge_guarantees ) {
212         $template->param(
213             charges_guarantees    => 1,
214             chargesamount_guarantees => $guarantees_non_issues_charges,
215         );
216     }
217 }
218
219 if ( $patron->has_overdues ) {
220     $template->param( odues => 1 );
221 }
222 my $issues = $patron->checkouts;
223
224 my $balance = 0;
225 $balance = $patron->account->balance;
226
227 my $account = $patron->account;
228 if( ( my $owing = $account->non_issues_charges ) > 0 ) {
229     my $noissuescharge = C4::Context->preference("noissuescharge") || 5; # FIXME If noissuescharge == 0 then 5, why??
230     $template->param(
231         charges => 1,
232         chargesamount => $owing,
233     )
234 } elsif ( $balance < 0 ) {
235     $template->param(
236         credits => 1,
237         creditsamount => -$balance,
238     );
239 }
240
241 # if the expiry date is before today ie they have expired
242 if ( $patron->is_expired ) {
243     #borrowercard expired, no issues
244     $template->param(
245         expired => "1",
246     );
247 }
248 # check for NotifyBorrowerDeparture
249 elsif ( $patron->is_going_to_expire ) {
250     # borrower card soon to expire warn librarian
251     $template->param( "warndeparture" => $patron->dateexpiry ,
252                     );
253     if (C4::Context->preference('ReturnBeforeExpiry')){
254         $template->param("returnbeforeexpiry" => 1);
255     }
256 }
257
258
259 my $has_modifications = Koha::Patron::Modifications->search( { borrowernumber => $borrowernumber } )->count;
260
261 $template->param(
262     patron          => $patron,
263     issuecount      => $patron->checkouts->count,
264     holds_count     => $patron->holds->count,
265     fines           => $patron->account->balance,
266     translated_language => $translated_language,
267     detailview      => 1,
268     was_renewed     => scalar $input->param('was_renewed') ? 1 : 0,
269     $category_type  => 1, # [% IF ( I ) %] = institutional/organisation
270     housebound_role => scalar $patron->housebound_role,
271     relatives_issues_count => $relatives_issues_count,
272     relatives_borrowernumbers => \@relatives,
273     logged_in_user => $logged_in_user,
274     files => Koha::Patron::Files->new( borrowernumber => $borrowernumber ) ->GetFilesInfo(),
275     #debarments                => scalar GetDebarments({ borrowernumber => $borrowernumber }),
276     has_modifications         => $has_modifications,
277 );
278
279 output_html_with_http_headers $input, $cookie, $template->output;