Bug 15407: Koha::Patron::Categories - replace GetborCatFromCatType
[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 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
31
32  needs html removed and to use the C4::Output more, but its tricky
33
34 =cut
35
36 use strict;
37 #use warnings; FIXME - Bug 2505
38 use CGI qw ( -utf8 );
39 use C4::Context;
40 use C4::Auth;
41 use C4::Output;
42 use C4::Members;
43 use C4::Members::Attributes;
44 use C4::Members::AttributeTypes;
45 use C4::Reserves;
46 use C4::Circulation;
47 use C4::Koha;
48 use C4::Letters;
49 use C4::Biblio;
50 use C4::Branch; # GetBranchName
51 use C4::Form::MessagingPreferences;
52 use List::MoreUtils qw/uniq/;
53 use C4::Members::Attributes qw(GetBorrowerAttributes);
54 use Koha::Patron::Debarments qw(GetDebarments);
55 use Koha::Patron::Images;
56 use Module::Load;
57 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
58     load Koha::NorwegianPatronDB, qw( NLGetSyncDataFromBorrowernumber );
59 }
60 #use Smart::Comments;
61 #use Data::Dumper;
62 use DateTime;
63 use Koha::DateUtils;
64 use Koha::Database;
65 use Koha::Patron::Categories;
66
67 use vars qw($debug);
68
69 BEGIN {
70         $debug = $ENV{DEBUG} || 0;
71 }
72
73 my $dbh = C4::Context->dbh;
74
75 my $input = CGI->new;
76 $debug or $debug = $input->param('debug') || 0;
77 my $print = $input->param('print');
78
79 my $template_name;
80 my $quickslip = 0;
81
82 my $flagsrequired;
83 if (defined $print and $print eq "page") {
84     $template_name = "members/moremember-print.tt";
85     # circ staff who process checkouts but can't edit
86     # patrons still need to be able to access print view
87     $flagsrequired = { circulate => "circulate_remaining_permissions" };
88 } elsif (defined $print and $print eq "slip") {
89     $template_name = "members/moremember-receipt.tt";
90     # circ staff who process checkouts but can't edit
91     # patrons still need to be able to print receipts
92     $flagsrequired =  { circulate => "circulate_remaining_permissions" };
93 } elsif (defined $print and $print eq "qslip") {
94     $template_name = "members/moremember-receipt.tt";
95     $quickslip = 1;
96     $flagsrequired =  { circulate => "circulate_remaining_permissions" };
97 } elsif (defined $print and $print eq "brief") {
98     $template_name = "members/moremember-brief.tt";
99     $flagsrequired = { borrowers => 1 };
100 } else {
101     $template_name = "members/moremember.tt";
102     $flagsrequired = { borrowers => 1 };
103 }
104
105 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
106     {
107         template_name   => $template_name,
108         query           => $input,
109         type            => "intranet",
110         authnotrequired => 0,
111         flagsrequired   => $flagsrequired,
112         debug           => 1,
113     }
114 );
115 my $borrowernumber = $input->param('borrowernumber');
116 my $error = $input->param('error');
117 $template->param( error => $error ) if ( $error );
118
119 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines($borrowernumber);
120 $template->param( issuecount => $issue, fines => $fines );
121
122 my $data = GetMember( 'borrowernumber' => $borrowernumber );
123
124 if ( not defined $data ) {
125     $template->param (unknowuser => 1);
126         output_html_with_http_headers $input, $cookie, $template->output;
127     exit;
128 }
129
130 my $category_type = $data->{'category_type'};
131
132 $debug and printf STDERR "dates (enrolled,expiry,birthdate) raw: (%s, %s, %s)\n", map {$data->{$_}} qw(dateenrolled dateexpiry dateofbirth);
133 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
134     my $userdate = $data->{$_};
135     unless ($userdate) {
136         $debug and warn sprintf "Empty \$data{%12s}", $_;
137         $data->{$_} = '';
138         next;
139     }
140     $template->param( $_ => dt_from_string( $userdate ) );
141 }
142 $data->{'IS_ADULT'} = ( $data->{'categorycode'} ne 'I' );
143
144 for (qw(gonenoaddress lost borrowernotes)) {
145          $data->{$_} and $template->param(flagged => 1) and last;
146 }
147
148 if ( Koha::Patrons->find( $borrowernumber )->is_debarred ) {
149     $template->param( 'userdebarred' => 1, 'flagged' => 1 );
150     my $debar = $data->{'debarred'};
151     if ( $debar ne "9999-12-31" ) {
152         $template->param( 'userdebarreddate' => output_pref( { dt => dt_from_string( $debar ), dateonly => 1 } ) );
153         $template->param( 'debarredcomment'  => $data->{debarredcomment} );
154     }
155 }
156
157 $data->{ "sex_".$data->{'sex'}."_p" } = 1 if defined $data->{sex};
158
159 if ( $category_type eq 'C') {
160     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
161     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
162     $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
163 }
164
165 my $patron = Koha::Patrons->find($data->{borrowernumber});
166 my @relatives;
167 if ( my $guarantor = $patron->guarantor ) {
168     $template->param( guarantor => $guarantor );
169     push @relatives, $guarantor->borrowernumber;
170     push @relatives, $_->borrowernumber for $patron->siblings;
171 } elsif ( $patron->contactname || $patron->contactfirstname ) {
172     $template->param(
173         guarantor => {
174             firstname => $patron->contactfirstname,
175             surname   => $patron->contactname,
176         }
177     );
178 } else {
179     my @guarantees = $patron->guarantees;
180     $template->param( guarantees => \@guarantees );
181     push @relatives, $_->borrowernumber for @guarantees;
182 }
183
184 my $relatives_issues_count =
185   Koha::Database->new()->schema()->resultset('Issue')
186   ->count( { borrowernumber => \@relatives } );
187
188 $template->param( adultborrower => 1 ) if ( $category_type eq 'A' || $category_type eq 'I' );
189
190 my %bor;
191 $bor{'borrowernumber'} = $borrowernumber;
192
193 # Converts the branchcode to the branch name
194 my $samebranch;
195 if ( C4::Context->preference("IndependentBranches") ) {
196     my $userenv = C4::Context->userenv;
197     if ( C4::Context->IsSuperLibrarian() ) {
198         $samebranch = 1;
199     }
200     else {
201         $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
202     }
203 }
204 else {
205     $samebranch = 1;
206 }
207 my $library = Koha::Libraries->find( $data->{branchcode})->unblessed;
208 @{$data}{keys %$library} = values %$library; # merge in all branch columns
209
210 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
211 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
212 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
213 $template->param( lib1 => $lib1 ) if ($lib1);
214 $template->param( lib2 => $lib2 ) if ($lib2);
215
216 # If printing a page, send the account informations to the template
217 if ($print eq "page") {
218     foreach my $accountline (@$accts) {
219         $accountline->{amount} = sprintf '%.2f', $accountline->{amount};
220         $accountline->{amountoutstanding} = sprintf '%.2f', $accountline->{amountoutstanding};
221
222         if ($accountline->{accounttype} ne 'F' && $accountline->{accounttype} ne 'FU'){
223             $accountline->{printtitle} = 1;
224         }
225     }
226     $template->param( accounts => $accts );
227 }
228
229 # Show OPAC privacy preference is system preference is set
230 if ( C4::Context->preference('OPACPrivacy') ) {
231     $template->param( OPACPrivacy => 1);
232     $template->param( "privacy".$data->{'privacy'} => 1);
233 }
234
235 my $today       = DateTime->now( time_zone => C4::Context->tz);
236 $today->truncate(to => 'day');
237 my $overdues_exist = 0;
238 my $totalprice = 0;
239
240 # Calculate and display patron's age
241 my $dateofbirth = $data->{ 'dateofbirth' };
242 my $age = GetAge($dateofbirth);
243 $template->param( age => $age );
244
245 ### ###############################################################################
246 # BUILD HTML
247 # show all reserves of this borrower, and the position of the reservation ....
248 if ($borrowernumber) {
249     $template->param(
250         holds_count => Koha::Database->new()->schema()->resultset('Reserve')
251           ->count( { borrowernumber => $borrowernumber } ) );
252 }
253
254 # current alert subscriptions
255 my $alerts = getalert($borrowernumber);
256 foreach (@$alerts) {
257     $_->{ $_->{type} } = 1;
258     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
259 }
260
261 # Add sync data to the user data
262 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
263     my $sync = NLGetSyncDataFromBorrowernumber( $borrowernumber );
264     if ( $sync ) {
265         $data->{'sync'}       = $sync->sync;
266         $data->{'syncstatus'} = $sync->syncstatus;
267         $data->{'lastsync'}   = $sync->lastsync;
268     }
269 }
270
271 # check to see if patron's image exists in the database
272 # basically this gives us a template var to condition the display of
273 # patronimage related interface on
274 my $patron_image = Koha::Patron::Images->find($data->{borrowernumber});
275 $template->param( picture => 1 ) if $patron_image;
276
277 my $branch=C4::Context->userenv->{'branch'};
278
279 $template->param(%$data);
280
281 if (C4::Context->preference('ExtendedPatronAttributes')) {
282     my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
283     my @classes = uniq( map {$_->{class}} @$attributes );
284     @classes = sort @classes;
285
286     my @attributes_loop;
287     for my $class (@classes) {
288         my @items;
289         for my $attr (@$attributes) {
290             push @items, $attr if $attr->{class} eq $class
291         }
292         my $lib = GetAuthorisedValueByCode( 'PA_CLASS', $class ) || $class;
293         push @attributes_loop, {
294             class => $class,
295             items => \@items,
296             lib   => $lib,
297         };
298     }
299
300     $template->param(
301         ExtendedPatronAttributes => 1,
302         attributes_loop => \@attributes_loop
303     );
304
305     my @types = C4::Members::AttributeTypes::GetAttributeTypes();
306     if (scalar(@types) == 0) {
307         $template->param(no_patron_attribute_types => 1);
308     }
309 }
310
311 if (C4::Context->preference('EnhancedMessagingPreferences')) {
312     C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
313     $template->param(messaging_form_inactive => 1);
314     $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
315     $template->param(SMSnumber     => defined $data->{'smsalertnumber'} ? $data->{'smsalertnumber'} : $data->{'mobile'});
316     $template->param(TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"));
317 }
318
319 # in template <TMPL_IF name="I"> => instutitional (A for Adult, C for children) 
320 $template->param( $data->{'categorycode'} => 1 ); 
321 $template->param(
322     detailview => 1,
323     borrowernumber  => $borrowernumber,
324     othernames      => $data->{'othernames'},
325     categoryname    => $data->{'description'},
326     was_renewed     => scalar $input->param('was_renewed') ? 1 : 0,
327     branch          => $branch,
328     todaysdate      => output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }),
329     totalprice      => sprintf("%.2f", $totalprice),
330     totaldue        => sprintf("%.2f", $total),
331     totaldue_raw    => $total,
332     overdues_exist  => $overdues_exist,
333     StaffMember     => ($category_type eq 'S'),
334     is_child        => ($category_type eq 'C'),
335     samebranch      => $samebranch,
336     quickslip       => $quickslip,
337     privacy_guarantor_checkouts => $data->{'privacy_guarantor_checkouts'},
338     activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
339     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
340     SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
341     RoutingSerials => C4::Context->preference('RoutingSerials'),
342     debarments => GetDebarments({ borrowernumber => $borrowernumber }),
343     PatronsPerPage => C4::Context->preference("PatronsPerPage") || 20,
344     relatives_issues_count => $relatives_issues_count,
345     relatives_borrowernumbers => \@relatives,
346 );
347
348 output_html_with_http_headers $input, $cookie, $template->output;