Bug 12542: Tabs inconsistency in different circ-menu.inc uses
[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 under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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;
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::Dates;
46 use C4::Reserves;
47 use C4::Circulation;
48 use C4::Koha;
49 use C4::Letters;
50 use C4::Biblio;
51 use C4::Branch; # GetBranchName
52 use C4::Form::MessagingPreferences;
53 use List::MoreUtils qw/uniq/;
54 use C4::Members::Attributes qw(GetBorrowerAttributes);
55 use Koha::Borrower::Debarments qw(GetDebarments IsDebarred);
56 #use Smart::Comments;
57 #use Data::Dumper;
58 use DateTime;
59 use Koha::DateUtils;
60 use Koha::Database;
61
62 use vars qw($debug);
63
64 BEGIN {
65         $debug = $ENV{DEBUG} || 0;
66 }
67
68 my $dbh = C4::Context->dbh;
69
70 my $input = CGI->new;
71 $debug or $debug = $input->param('debug') || 0;
72 my $print = $input->param('print');
73
74 my $template_name;
75 my $quickslip = 0;
76
77 my $flagsrequired;
78 if (defined $print and $print eq "page") {
79     $template_name = "members/moremember-print.tt";
80     # circ staff who process checkouts but can't edit
81     # patrons still need to be able to access print view
82     $flagsrequired = { circulate => "circulate_remaining_permissions" };
83 } elsif (defined $print and $print eq "slip") {
84     $template_name = "members/moremember-receipt.tt";
85     # circ staff who process checkouts but can't edit
86     # patrons still need to be able to print receipts
87     $flagsrequired =  { circulate => "circulate_remaining_permissions" };
88 } elsif (defined $print and $print eq "qslip") {
89     $template_name = "members/moremember-receipt.tt";
90     $quickslip = 1;
91     $flagsrequired =  { circulate => "circulate_remaining_permissions" };
92 } elsif (defined $print and $print eq "brief") {
93     $template_name = "members/moremember-brief.tt";
94     $flagsrequired = { borrowers => 1 };
95 } else {
96     $template_name = "members/moremember.tt";
97     $flagsrequired = { borrowers => 1 };
98 }
99
100 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
101     {
102         template_name   => $template_name,
103         query           => $input,
104         type            => "intranet",
105         authnotrequired => 0,
106         flagsrequired   => $flagsrequired,
107         debug           => 1,
108     }
109 );
110 my $borrowernumber = $input->param('borrowernumber');
111
112 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines($borrowernumber);
113 $template->param( issuecount => $issue );
114
115 my $data = GetMember( 'borrowernumber' => $borrowernumber );
116
117 if ( not defined $data ) {
118     $template->param (unknowuser => 1);
119         output_html_with_http_headers $input, $cookie, $template->output;
120     exit;
121 }
122
123 my $category_type = $data->{'category_type'};
124
125 $debug and printf STDERR "dates (enrolled,expiry,birthdate) raw: (%s, %s, %s)\n", map {$data->{$_}} qw(dateenrolled dateexpiry dateofbirth);
126 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
127                 my $userdate = $data->{$_};
128                 unless ($userdate) {
129                         $debug and warn sprintf "Empty \$data{%12s}", $_;
130                         $data->{$_} = '';
131                         next;
132                 }
133                 $userdate = C4::Dates->new($userdate,'iso')->output('syspref');
134                 $data->{$_} = $userdate || '';
135                 $template->param( $_ => $userdate );
136 }
137 $data->{'IS_ADULT'} = ( $data->{'categorycode'} ne 'I' );
138
139 for (qw(gonenoaddress lost borrowernotes)) {
140          $data->{$_} and $template->param(flagged => 1) and last;
141 }
142
143 if ( IsDebarred($borrowernumber) ) {
144     $template->param( 'userdebarred' => 1, 'flagged' => 1 );
145     my $debar = $data->{'debarred'};
146     if ( $debar ne "9999-12-31" ) {
147         $template->param( 'userdebarreddate' => C4::Dates::format_date($debar) );
148         $template->param( 'debarredcomment'  => $data->{debarredcomment} );
149     }
150 }
151
152 $data->{'ethnicity'} = fixEthnicity( $data->{'ethnicity'} );
153 $data->{ "sex_".$data->{'sex'}."_p" } = 1 if defined $data->{sex};
154
155 my $catcode;
156 if ( $category_type eq 'C') {
157    my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
158    my $cnt = scalar(@$catcodes);
159
160    $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
161    $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
162 }
163
164
165 if ( $data->{'ethnicity'} || $data->{'ethnotes'} ) {
166     $template->param( printethnicityline => 1 );
167 }
168 if ( $category_type eq 'A' || $category_type eq 'I') {
169     $template->param( isguarantee => 1 );
170
171     # FIXME
172     # It looks like the $i is only being returned to handle walking through
173     # the array, which is probably better done as a foreach loop.
174     #
175     my ( $count, $guarantees ) = GetGuarantees( $data->{'borrowernumber'} );
176     my @guaranteedata;
177     for ( my $i = 0 ; $i < $count ; $i++ ) {
178         push(@guaranteedata,
179             {
180                 borrowernumber => $guarantees->[$i]->{'borrowernumber'},
181                 cardnumber     => $guarantees->[$i]->{'cardnumber'},
182                 name           => $guarantees->[$i]->{'firstname'} . " "
183                                 . $guarantees->[$i]->{'surname'}
184             }
185         );
186     }
187     $template->param( guaranteeloop => \@guaranteedata );
188     ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' || $category_type eq 'I' );
189 }
190 else {
191     if ($data->{'guarantorid'}){
192             my ($guarantor) = GetMember( 'borrowernumber' =>$data->{'guarantorid'});
193                 $template->param(guarantor => 1);
194                 foreach (qw(borrowernumber cardnumber firstname surname)) {        
195                           $template->param("guarantor$_" => $guarantor->{$_});
196         }
197     }
198         if ($category_type eq 'C'){
199                 $template->param('C' => 1);
200         }
201 }
202
203 my %bor;
204 $bor{'borrowernumber'} = $borrowernumber;
205
206 # Converts the branchcode to the branch name
207 my $samebranch;
208 if ( C4::Context->preference("IndependentBranches") ) {
209     my $userenv = C4::Context->userenv;
210     if ( C4::Context->IsSuperLibrarian() ) {
211         $samebranch = 1;
212     }
213     else {
214         $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
215     }
216 }
217 else {
218     $samebranch = 1;
219 }
220 my $branchdetail = GetBranchDetail( $data->{'branchcode'});
221 @{$data}{keys %$branchdetail} = values %$branchdetail; # merge in all branch columns
222
223 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
224 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
225 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
226 $template->param( lib1 => $lib1 ) if ($lib1);
227 $template->param( lib2 => $lib2 ) if ($lib2);
228
229 # If printing a page, send the account informations to the template
230 if ($print eq "page") {
231     foreach my $accountline (@$accts) {
232         $accountline->{amount} = sprintf '%.2f', $accountline->{amount};
233         $accountline->{amountoutstanding} = sprintf '%.2f', $accountline->{amountoutstanding};
234
235         if ($accountline->{accounttype} ne 'F' && $accountline->{accounttype} ne 'FU'){
236             $accountline->{printtitle} = 1;
237         }
238     }
239     $template->param( accounts => $accts );
240 }
241
242 # Show OPAC privacy preference is system preference is set
243 if ( C4::Context->preference('OPACPrivacy') ) {
244     $template->param( OPACPrivacy => 1);
245     $template->param( "privacy".$data->{'privacy'} => 1);
246 }
247
248 my @relatives = GetMemberRelatives($borrowernumber);
249 my $relatives_issues_count =
250   Koha::Database->new()->schema()->resultset('Issue')
251   ->count( { borrowernumber => \@relatives } );
252
253 my $roadtype = C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $data->{streettype} );
254 my $today       = DateTime->now( time_zone => C4::Context->tz);
255 $today->truncate(to => 'day');
256 my @borrowers_with_issues;
257 my $overdues_exist = 0;
258 my $totalprice = 0;
259
260 ### ###############################################################################
261 # BUILD HTML
262 # show all reserves of this borrower, and the position of the reservation ....
263 if ($borrowernumber) {
264     $template->param(
265         holds_count => Koha::Database->new()->schema()->resultset('Reserve')
266           ->count( { borrowernumber => $borrowernumber } ) );
267 }
268
269 # current alert subscriptions
270 my $alerts = getalert($borrowernumber);
271 foreach (@$alerts) {
272     $_->{ $_->{type} } = 1;
273     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
274 }
275
276 my $candeleteuser;
277 my $userenv = C4::Context->userenv;
278 if ( C4::Context->IsSuperLibrarian() ) {
279     $candeleteuser = 1;
280 }
281 elsif ( C4::Context->preference("IndependentBranches") ) {
282     $candeleteuser = ( $data->{'branchcode'} eq $userenv->{branch} );
283 }
284 else {
285     if ( C4::Auth::getuserflags( $userenv->{flags}, $userenv->{number} )->{borrowers} ) {
286         $candeleteuser = 1;
287     }
288     else {
289         $candeleteuser = 0;
290     }
291 }
292
293 # check to see if patron's image exists in the database
294 # basically this gives us a template var to condition the display of
295 # patronimage related interface on
296 my ($picture, $dberror) = GetPatronImage($data->{'borrowernumber'});
297 $template->param( picture => 1 ) if $picture;
298
299 my $branch=C4::Context->userenv->{'branch'};
300
301 $template->param(%$data);
302
303 if (C4::Context->preference('ExtendedPatronAttributes')) {
304     my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
305     my @classes = uniq( map {$_->{class}} @$attributes );
306     @classes = sort @classes;
307
308     my @attributes_loop;
309     for my $class (@classes) {
310         my @items;
311         for my $attr (@$attributes) {
312             push @items, $attr if $attr->{class} eq $class
313         }
314         my $lib = GetAuthorisedValueByCode( 'PA_CLASS', $class ) || $class;
315         push @attributes_loop, {
316             class => $class,
317             items => \@items,
318             lib   => $lib,
319         };
320     }
321
322     $template->param(
323         ExtendedPatronAttributes => 1,
324         attributes_loop => \@attributes_loop
325     );
326
327     my @types = C4::Members::AttributeTypes::GetAttributeTypes();
328     if (scalar(@types) == 0) {
329         $template->param(no_patron_attribute_types => 1);
330     }
331 }
332
333 if (C4::Context->preference('EnhancedMessagingPreferences')) {
334     C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
335     $template->param(messaging_form_inactive => 1);
336     $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
337     $template->param(SMSnumber     => defined $data->{'smsalertnumber'} ? $data->{'smsalertnumber'} : $data->{'mobile'});
338     $template->param(TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"));
339 }
340
341 # Computes full borrower address
342 my $address = $data->{'streetnumber'} . " $roadtype " . $data->{'address'};
343
344 # in template <TMPL_IF name="I"> => instutitional (A for Adult, C for children) 
345 $template->param( $data->{'categorycode'} => 1 ); 
346 $template->param(
347     detailview => 1,
348     AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
349     CANDELETEUSER    => $candeleteuser,
350     roadtype        => $roadtype,
351     borrowernumber  => $borrowernumber,
352     othernames      => $data->{'othernames'},
353     categoryname    => $data->{'description'},
354     was_renewed     => $input->param('was_renewed') ? 1 : 0,
355     branch          => $branch,
356     todaysdate      => C4::Dates->today(),
357     totalprice      => sprintf("%.2f", $totalprice),
358     totaldue        => sprintf("%.2f", $total),
359     totaldue_raw    => $total,
360     overdues_exist  => $overdues_exist,
361     StaffMember     => ($category_type eq 'S'),
362     is_child        => ($category_type eq 'C'),
363     samebranch     => $samebranch,
364     quickslip             => $quickslip,
365     activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
366     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
367     SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
368     RoutingSerials => C4::Context->preference('RoutingSerials'),
369     debarments => GetDebarments({ borrowernumber => $borrowernumber }),
370     PatronsPerPage => C4::Context->preference("PatronsPerPage") || 20,
371     relatives_issues_count => $relatives_issues_count,
372     relatives_borrowernumbers => \@relatives,
373     address => $address
374 );
375
376 output_html_with_http_headers $input, $cookie, $template->output;