Fix release notes typo
[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 ($print eq "page") {
79     $template_name = "members/moremember-print.tmpl";
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 ($print eq "slip") {
84     $template_name = "members/moremember-receipt.tmpl";
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 ($print eq "qslip") {
89     $template_name = "members/moremember-receipt.tmpl";
90     $quickslip = 1;
91     $flagsrequired =  { circulate => "circulate_remaining_permissions" };
92 } elsif ($print eq "brief") {
93     $template_name = "members/moremember-brief.tmpl";
94     $flagsrequired = { borrowers => 1 };
95 } else {
96     $template_name = "members/moremember.tmpl";
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;
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 }
189 else {
190     if ($data->{'guarantorid'}){
191             my ($guarantor) = GetMember( 'borrowernumber' =>$data->{'guarantorid'});
192                 $template->param(guarantor => 1);
193                 foreach (qw(borrowernumber cardnumber firstname surname)) {        
194                           $template->param("guarantor$_" => $guarantor->{$_});
195         }
196     }
197         if ($category_type eq 'C'){
198                 $template->param('C' => 1);
199         }
200 }
201
202 $template->param( adultborrower => 1 ) if ( $category_type eq 'A' || $category_type eq 'I' );
203
204 my %bor;
205 $bor{'borrowernumber'} = $borrowernumber;
206
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;
213     }
214     else {
215         $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
216     }
217 }
218 else {
219     $samebranch = 1;
220 }
221 my $branchdetail = GetBranchDetail( $data->{'branchcode'});
222 @{$data}{keys %$branchdetail} = values %$branchdetail; # merge in all branch columns
223
224 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
225 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
226 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
227 $template->param( lib1 => $lib1 ) if ($lib1);
228 $template->param( lib2 => $lib2 ) if ($lib2);
229
230 # If printing a page, send the account informations to the template
231 if ($print eq "page") {
232     foreach my $accountline (@$accts) {
233         $accountline->{amount} = sprintf '%.2f', $accountline->{amount};
234         $accountline->{amountoutstanding} = sprintf '%.2f', $accountline->{amountoutstanding};
235
236         if ($accountline->{accounttype} ne 'F' && $accountline->{accounttype} ne 'FU'){
237             $accountline->{printtitle} = 1;
238         }
239     }
240     $template->param( accounts => $accts );
241 }
242
243 # Show OPAC privacy preference is system preference is set
244 if ( C4::Context->preference('OPACPrivacy') ) {
245     $template->param( OPACPrivacy => 1);
246     $template->param( "privacy".$data->{'privacy'} => 1);
247 }
248
249 my @relatives = GetMemberRelatives($borrowernumber);
250 my $relatives_issues_count =
251   Koha::Database->new()->schema()->resultset('Issue')
252   ->count( { borrowernumber => \@relatives } );
253
254 my $roadtype = C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $data->{streettype} );
255 my $today       = DateTime->now( time_zone => C4::Context->tz);
256 $today->truncate(to => 'day');
257 my @borrowers_with_issues;
258 my $overdues_exist = 0;
259 my $totalprice = 0;
260
261 ### ###############################################################################
262 # BUILD HTML
263 # show all reserves of this borrower, and the position of the reservation ....
264 if ($borrowernumber) {
265     $template->param(
266         holds_count => Koha::Database->new()->schema()->resultset('Reserve')
267           ->count( { borrowernumber => $borrowernumber } ) );
268 }
269
270 # current alert subscriptions
271 my $alerts = getalert($borrowernumber);
272 foreach (@$alerts) {
273     $_->{ $_->{type} } = 1;
274     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
275 }
276
277 my $candeleteuser;
278 my $userenv = C4::Context->userenv;
279 if ( C4::Context->IsSuperLibrarian() ) {
280     $candeleteuser = 1;
281 }
282 elsif ( C4::Context->preference("IndependentBranches") ) {
283     $candeleteuser = ( $data->{'branchcode'} eq $userenv->{branch} );
284 }
285 else {
286     if ( C4::Auth::getuserflags( $userenv->{flags}, $userenv->{number} )->{borrowers} ) {
287         $candeleteuser = 1;
288     }
289     else {
290         $candeleteuser = 0;
291     }
292 }
293
294 # check to see if patron's image exists in the database
295 # basically this gives us a template var to condition the display of
296 # patronimage related interface on
297 my ($picture, $dberror) = GetPatronImage($data->{'borrowernumber'});
298 $template->param( picture => 1 ) if $picture;
299
300 my $branch=C4::Context->userenv->{'branch'};
301
302 $template->param(%$data);
303
304 if (C4::Context->preference('ExtendedPatronAttributes')) {
305     my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
306     my @classes = uniq( map {$_->{class}} @$attributes );
307     @classes = sort @classes;
308
309     my @attributes_loop;
310     for my $class (@classes) {
311         my @items;
312         for my $attr (@$attributes) {
313             push @items, $attr if $attr->{class} eq $class
314         }
315         my $lib = GetAuthorisedValueByCode( 'PA_CLASS', $class ) || $class;
316         push @attributes_loop, {
317             class => $class,
318             items => \@items,
319             lib   => $lib,
320         };
321     }
322
323     $template->param(
324         ExtendedPatronAttributes => 1,
325         attributes_loop => \@attributes_loop
326     );
327
328     my @types = C4::Members::AttributeTypes::GetAttributeTypes();
329     if (scalar(@types) == 0) {
330         $template->param(no_patron_attribute_types => 1);
331     }
332 }
333
334 if (C4::Context->preference('EnhancedMessagingPreferences')) {
335     C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
336     $template->param(messaging_form_inactive => 1);
337     $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
338     $template->param(SMSnumber     => defined $data->{'smsalertnumber'} ? $data->{'smsalertnumber'} : $data->{'mobile'});
339     $template->param(TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"));
340 }
341
342 # in template <TMPL_IF name="I"> => instutitional (A for Adult, C for children) 
343 $template->param( $data->{'categorycode'} => 1 ); 
344 $template->param(
345     detailview => 1,
346     AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
347     CANDELETEUSER    => $candeleteuser,
348     roadtype        => $roadtype,
349     borrowernumber  => $borrowernumber,
350     othernames      => $data->{'othernames'},
351     categoryname    => $data->{'description'},
352     was_renewed     => $input->param('was_renewed') ? 1 : 0,
353     branch          => $branch,
354     todaysdate      => C4::Dates->today(),
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     activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
364     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
365     SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
366     RoutingSerials => C4::Context->preference('RoutingSerials'),
367     debarments => GetDebarments({ borrowernumber => $borrowernumber }),
368     PatronsPerPage => C4::Context->preference("PatronsPerPage") || 20,
369     relatives_issues_count => $relatives_issues_count,
370     relatives_borrowernumbers => \@relatives,
371 );
372
373 output_html_with_http_headers $input, $cookie, $template->output;