Bug 18871: Make patron list name a link to view contents of list
[koha.git] / members / moremember.pl
blob1ac6a3d40458aa7527dbb2834aaf18f3a976f0c1
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2014 ByWater Solutions
7 # This file is part of Koha.
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.
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.
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>.
23 =head1 moremember.pl
25 script to do a borrower enquiry/bring up borrower details etc
26 Displays all the details about a borrower
27 written 20/12/99 by chris@katipo.co.nz
28 last modified 21/1/2000 by chris@katipo.co.nz
29 modified 31/1/2001 by chris@katipo.co.nz
30 to not allow items on request to be renewed
32 needs html removed and to use the C4::Output more, but its tricky
34 =cut
36 use strict;
37 #use warnings; FIXME - Bug 2505
38 use CGI qw ( -utf8 );
39 use HTML::Entities;
40 use C4::Context;
41 use C4::Auth;
42 use C4::Output;
43 use C4::Members;
44 use C4::Members::Attributes;
45 use C4::Members::AttributeTypes;
46 use C4::Reserves;
47 use C4::Circulation;
48 use C4::Koha;
49 use C4::Letters;
50 use C4::Biblio;
51 use C4::Form::MessagingPreferences;
52 use List::MoreUtils qw/uniq/;
53 use C4::Members::Attributes qw(GetBorrowerAttributes);
54 use Koha::AuthorisedValues;
55 use Koha::CsvProfiles;
56 use Koha::Patron::Debarments qw(GetDebarments);
57 use Koha::Patron::Images;
58 use Module::Load;
59 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
60 load Koha::NorwegianPatronDB, qw( NLGetSyncDataFromBorrowernumber );
62 #use Smart::Comments;
63 #use Data::Dumper;
64 use DateTime;
65 use Koha::DateUtils;
66 use Koha::Database;
67 use Koha::Patrons;
68 use Koha::Patron::Categories;
69 use Koha::Token;
71 use vars qw($debug);
73 BEGIN {
74 $debug = $ENV{DEBUG} || 0;
77 my $dbh = C4::Context->dbh;
79 my $input = CGI->new;
80 $debug or $debug = $input->param('debug') || 0;
81 my $print = $input->param('print');
83 my $template_name;
84 my $quickslip = 0;
86 my $flagsrequired;
87 if (defined $print and $print eq "page") {
88 $template_name = "members/moremember-print.tt";
89 # circ staff who process checkouts but can't edit
90 # patrons still need to be able to access print view
91 $flagsrequired = { circulate => "circulate_remaining_permissions" };
92 } elsif (defined $print and $print eq "slip") {
93 $template_name = "members/moremember-receipt.tt";
94 # circ staff who process checkouts but can't edit
95 # patrons still need to be able to print receipts
96 $flagsrequired = { circulate => "circulate_remaining_permissions" };
97 } elsif (defined $print and $print eq "qslip") {
98 $template_name = "members/moremember-receipt.tt";
99 $quickslip = 1;
100 $flagsrequired = { circulate => "circulate_remaining_permissions" };
101 } elsif (defined $print and $print eq "brief") {
102 $template_name = "members/moremember-brief.tt";
103 $flagsrequired = { borrowers => 1 };
104 } else {
105 $template_name = "members/moremember.tt";
106 $flagsrequired = { borrowers => 1 };
109 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
111 template_name => $template_name,
112 query => $input,
113 type => "intranet",
114 authnotrequired => 0,
115 flagsrequired => $flagsrequired,
116 debug => 1,
119 my $borrowernumber = $input->param('borrowernumber');
120 $borrowernumber = HTML::Entities::encode($borrowernumber);
121 my $error = $input->param('error');
122 $template->param( error => $error ) if ( $error );
124 my $patron = Koha::Patrons->find($borrowernumber);
125 my $issues = $patron->checkouts;
126 my $balance = $patron->account->balance;
127 $template->param(
128 issuecount => $issues->count,
129 fines => $balance,
133 my $data = GetMember( 'borrowernumber' => $borrowernumber );
135 if ( not defined $data ) {
136 $template->param (unknowuser => 1);
137 output_html_with_http_headers $input, $cookie, $template->output;
138 exit;
141 my $category_type = $data->{'category_type'};
143 $debug and printf STDERR "dates (enrolled,expiry,birthdate) raw: (%s, %s, %s)\n", map {$data->{$_}} qw(dateenrolled dateexpiry dateofbirth);
144 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
145 my $userdate = $data->{$_};
146 unless ($userdate) {
147 $debug and warn sprintf "Empty \$data{%12s}", $_;
148 $data->{$_} = '';
149 next;
151 $data->{$_} = dt_from_string( $userdate );
153 $data->{'IS_ADULT'} = ( $data->{'categorycode'} ne 'I' );
155 for (qw(gonenoaddress lost borrowernotes)) {
156 $data->{$_} and $template->param(flagged => 1) and last;
159 if ( $patron->is_debarred ) {
160 $template->param(
161 userdebarred => 1,
162 flagged => 1,
163 debarments => GetDebarments({ borrowernumber => $borrowernumber }),
165 my $debar = $data->{'debarred'};
166 if ( $debar ne "9999-12-31" ) {
167 $template->param( 'userdebarreddate' => output_pref( { dt => dt_from_string( $debar ), dateonly => 1 } ) );
168 $template->param( 'debarredcomment' => $data->{debarredcomment} );
172 $data->{ "sex_".$data->{'sex'}."_p" } = 1 if defined $data->{sex};
174 if ( $category_type eq 'C') {
175 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
176 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
177 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
180 my @relatives;
181 if ( my $guarantor = $patron->guarantor ) {
182 $template->param( guarantor => $guarantor );
183 push @relatives, $guarantor->borrowernumber;
184 push @relatives, $_->borrowernumber for $patron->siblings;
185 } elsif ( $patron->contactname || $patron->contactfirstname ) {
186 $template->param(
187 guarantor => {
188 firstname => $patron->contactfirstname,
189 surname => $patron->contactname,
192 } else {
193 my @guarantees = $patron->guarantees;
194 $template->param( guarantees => \@guarantees );
195 push @relatives, $_->borrowernumber for @guarantees;
198 my $relatives_issues_count =
199 Koha::Database->new()->schema()->resultset('Issue')
200 ->count( { borrowernumber => \@relatives } );
202 $template->param( adultborrower => 1 ) if ( $category_type eq 'A' || $category_type eq 'I' );
204 my %bor;
205 $bor{'borrowernumber'} = $borrowernumber;
207 # Converts the branchcode to the branch name
208 my $samebranch;
209 if ( C4::Context->preference("IndependentBranches") ) {
210 my $userenv = C4::Context->userenv;
211 if ( C4::Context->IsSuperLibrarian() ) {
212 $samebranch = 1;
214 else {
215 $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
218 else {
219 $samebranch = 1;
221 my $library = Koha::Libraries->find( $data->{branchcode})->unblessed;
222 @{$data}{keys %$library} = values %$library; # merge in all branch columns
224 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
226 # If printing a page, send the account informations to the template
227 if ($print eq "page") {
228 foreach my $accountline (@$accts) {
229 $accountline->{amount} = sprintf '%.2f', $accountline->{amount};
230 $accountline->{amountoutstanding} = sprintf '%.2f', $accountline->{amountoutstanding};
232 if ($accountline->{accounttype} ne 'F' && $accountline->{accounttype} ne 'FU'){
233 $accountline->{printtitle} = 1;
236 $template->param( accounts => $accts );
239 # Show OPAC privacy preference is system preference is set
240 if ( C4::Context->preference('OPACPrivacy') ) {
241 $template->param( OPACPrivacy => 1);
242 $template->param( "privacy".$data->{'privacy'} => 1);
245 my $today = DateTime->now( time_zone => C4::Context->tz);
246 $today->truncate(to => 'day');
247 my $overdues_exist = 0;
248 my $totalprice = 0;
250 # Calculate and display patron's age
251 if ( $data->{dateofbirth} ) {
252 $template->param( age => Koha::Patron->new({ dateofbirth => $data->{dateofbirth} })->get_age );
255 ### ###############################################################################
256 # BUILD HTML
257 # show all reserves of this borrower, and the position of the reservation ....
258 if ($borrowernumber) {
259 $template->param(
260 holds_count => Koha::Database->new()->schema()->resultset('Reserve')
261 ->count( { borrowernumber => $borrowernumber } ) );
264 # current alert subscriptions
265 my $alerts = getalert($borrowernumber);
266 foreach (@$alerts) {
267 $_->{ $_->{type} } = 1;
268 $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
271 # Add sync data to the user data
272 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
273 my $sync = NLGetSyncDataFromBorrowernumber( $borrowernumber );
274 if ( $sync ) {
275 $data->{'sync'} = $sync->sync;
276 $data->{'syncstatus'} = $sync->syncstatus;
277 $data->{'lastsync'} = $sync->lastsync;
281 # check to see if patron's image exists in the database
282 # basically this gives us a template var to condition the display of
283 # patronimage related interface on
284 my $patron_image = Koha::Patron::Images->find($data->{borrowernumber});
285 $template->param( picture => 1 ) if $patron_image;
286 # Generate CSRF token for upload and delete image buttons
287 $template->param(
288 csrf_token => Koha::Token->new->generate_csrf({ session_id => $input->cookie('CGISESSID'),}),
292 $template->param(%$data);
294 if (C4::Context->preference('ExtendedPatronAttributes')) {
295 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
296 my @classes = uniq( map {$_->{class}} @$attributes );
297 @classes = sort @classes;
299 my @attributes_loop;
300 for my $class (@classes) {
301 my @items;
302 for my $attr (@$attributes) {
303 push @items, $attr if $attr->{class} eq $class
305 my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
306 my $lib = $av->count ? $av->next->lib : $class;
308 push @attributes_loop, {
309 class => $class,
310 items => \@items,
311 lib => $lib,
315 $template->param(
316 ExtendedPatronAttributes => 1,
317 attributes_loop => \@attributes_loop
320 my @types = C4::Members::AttributeTypes::GetAttributeTypes();
321 if (scalar(@types) == 0) {
322 $template->param(no_patron_attribute_types => 1);
326 if (C4::Context->preference('EnhancedMessagingPreferences')) {
327 C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
328 $template->param(messaging_form_inactive => 1);
329 $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
330 $template->param(SMSnumber => $data->{'smsalertnumber'});
331 $template->param(TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"));
334 if ( C4::Context->preference("ExportCircHistory") ) {
335 $template->param(csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc' }) ]);
338 # in template <TMPL_IF name="I"> => institutional (A for Adult, C for children)
339 $template->param( $data->{'categorycode'} => 1 );
341 # Display the language description instead of the code
342 # Note that this is certainly wrong
343 my ( $subtag, $region ) = split '-', $patron->lang;
344 my $translated_language = C4::Languages::language_get_description( $subtag, $subtag, 'language' );
346 $template->param(
347 patron => $patron,
348 translated_language => $translated_language,
349 detailview => 1,
350 borrowernumber => $borrowernumber,
351 othernames => $data->{'othernames'},
352 categoryname => $data->{'description'},
353 was_renewed => scalar $input->param('was_renewed') ? 1 : 0,
354 todaysdate => output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }),
355 totalprice => sprintf("%.2f", $totalprice),
356 totaldue => sprintf("%.2f", $total),
357 totaldue_raw => $total,
358 overdues_exist => $overdues_exist,
359 StaffMember => $category_type eq 'S',
360 is_child => $category_type eq 'C',
361 samebranch => $samebranch,
362 quickslip => $quickslip,
363 housebound_role => scalar $patron->housebound_role,
364 privacy_guarantor_checkouts => $data->{'privacy_guarantor_checkouts'},
365 RoutingSerials => C4::Context->preference('RoutingSerials'),
366 PatronsPerPage => C4::Context->preference("PatronsPerPage") || 20,
367 relatives_issues_count => $relatives_issues_count,
368 relatives_borrowernumbers => \@relatives,
371 output_html_with_http_headers $input, $cookie, $template->output;