Bug 2505 - Add commented use warnings where missing in the members/ directory
[koha.git] / members / moremember.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
21 =head1 moremember.pl
22
23  script to do a borrower enquiry/bring up borrower details etc
24  Displays all the details about a borrower
25  written 20/12/99 by chris@katipo.co.nz
26  last modified 21/1/2000 by chris@katipo.co.nz
27  modified 31/1/2001 by chris@katipo.co.nz
28    to not allow items on request to be renewed
29
30  needs html removed and to use the C4::Output more, but its tricky
31
32 =cut
33
34 use strict;
35 #use warnings; FIXME - Bug 2505
36 use CGI;
37 use C4::Context;
38 use C4::Auth;
39 use C4::Output;
40 use C4::Members;
41 use C4::Members::Attributes;
42 use C4::Members::AttributeTypes;
43 use C4::Dates;
44 use C4::Reserves;
45 use C4::Circulation;
46 use C4::Koha;
47 use C4::Letters;
48 use C4::Biblio;
49 use C4::Reserves;
50 use C4::Branch; # GetBranchName
51 use C4::Form::MessagingPreferences;
52
53 #use Smart::Comments;
54 #use Data::Dumper;
55
56 use vars qw($debug);
57
58 BEGIN {
59         $debug = $ENV{DEBUG} || 0;
60 }
61
62 my $dbh = C4::Context->dbh;
63
64 my $input = new CGI;
65 $debug or $debug = $input->param('debug') || 0;
66 my $print = $input->param('print');
67 my $override_limit = $input->param("override_limit") || 0;
68 my @failedrenews = $input->param('failedrenew');
69 my @failedreturns = $input->param('failedreturn');
70 my $error = $input->param('error');
71 my %renew_failed;
72 for my $renew (@failedrenews) { $renew_failed{$renew} = 1; }
73 my %return_failed;
74 for my $failedret (@failedreturns) { $return_failed{$failedret} = 1; }
75
76 my $template_name;
77 my $quickslip = 0;
78
79 if    ($print eq "page") { $template_name = "members/moremember-print.tmpl";   }
80 elsif ($print eq "slip") { $template_name = "members/moremember-receipt.tmpl"; }
81 elsif ($print eq "qslip") { $template_name = "members/moremember-receipt.tmpl"; $quickslip = 1; }
82 else {                     $template_name = "members/moremember.tmpl";         }
83
84 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
85     {
86         template_name   => $template_name,
87         query           => $input,
88         type            => "intranet",
89         authnotrequired => 0,
90         flagsrequired   => { borrowers => 1 },
91         debug           => 1,
92     }
93 );
94 my $borrowernumber = $input->param('borrowernumber');
95
96 #start the page and read in includes
97 my $data           = GetMember( 'borrowernumber' => $borrowernumber );
98 my $reregistration = $input->param('reregistration');
99
100 if ( not defined $data ) {
101     $template->param (unknowuser => 1);
102         output_html_with_http_headers $input, $cookie, $template->output;
103     exit;
104 }
105
106 # re-reregistration function to automatic calcul of date expiry
107 if ( $reregistration eq 'y' ) {
108         $data->{'dateexpiry'} = ExtendMemberSubscriptionTo( $borrowernumber );
109 }
110
111 my $category_type = $data->{'category_type'};
112
113 ### $category_type
114
115 # in template <TMPL_IF name="I"> => instutitional (A for Adult& C for children) 
116 $template->param( $data->{'categorycode'} => 1 ); 
117
118 $debug and printf STDERR "dates (enrolled,expiry,birthdate) raw: (%s, %s, %s)\n", map {$data->{$_}} qw(dateenrolled dateexpiry dateofbirth);
119 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
120                 my $userdate = $data->{$_};
121                 unless ($userdate) {
122                         $debug and warn sprintf "Empty \$data{%12s}", $_;
123                         $data->{$_} = '';
124                         next;
125                 }
126                 $userdate = C4::Dates->new($userdate,'iso')->output('syspref');
127                 $data->{$_} = $userdate || '';
128                 $template->param( $_ => $userdate );
129 }
130 $data->{'IS_ADULT'} = ( $data->{'categorycode'} ne 'I' );
131
132 for (qw(debarred gonenoaddress lost borrowernotes)) {
133          $data->{$_} and $template->param(flagged => 1) and last;
134 }
135
136 $data->{'ethnicity'} = fixEthnicity( $data->{'ethnicity'} );
137 $data->{ "sex_".$data->{'sex'}."_p" } = 1;
138
139 my $catcode;
140 if ( $category_type eq 'C') {
141         if ($data->{'guarantorid'} ne '0' ) {
142         my $data2 = GetMember( 'borrowernumber' => $data->{'guarantorid'} );
143         foreach (qw(address city B_address B_city phone mobile zipcode country B_country)) {
144             $data->{$_} = $data2->{$_};
145         }
146    }
147    my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
148    my $cnt = scalar(@$catcodes);
149
150    $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
151    $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
152 }
153
154
155 if ( $data->{'ethnicity'} || $data->{'ethnotes'} ) {
156     $template->param( printethnicityline => 1 );
157 }
158 if ( $category_type eq 'A' ) {
159     $template->param( isguarantee => 1 );
160
161     # FIXME
162     # It looks like the $i is only being returned to handle walking through
163     # the array, which is probably better done as a foreach loop.
164     #
165     my ( $count, $guarantees ) = GetGuarantees( $data->{'borrowernumber'} );
166     my @guaranteedata;
167     for ( my $i = 0 ; $i < $count ; $i++ ) {
168         push(@guaranteedata,
169             {
170                 borrowernumber => $guarantees->[$i]->{'borrowernumber'},
171                 cardnumber     => $guarantees->[$i]->{'cardnumber'},
172                 name           => $guarantees->[$i]->{'firstname'} . " "
173                                 . $guarantees->[$i]->{'surname'}
174             }
175         );
176     }
177     $template->param( guaranteeloop => \@guaranteedata );
178     ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
179 }
180 else {
181     if ($data->{'guarantorid'}){
182             my ($guarantor) = GetMember( 'borrowernumber' =>$data->{'guarantorid'});
183                 $template->param(guarantor => 1);
184                 foreach (qw(borrowernumber cardnumber firstname surname)) {        
185                           $template->param("guarantor$_" => $guarantor->{$_});
186         }
187     }
188         if ($category_type eq 'C'){
189                 $template->param('C' => 1);
190         }
191 }
192
193 my %bor;
194 $bor{'borrowernumber'} = $borrowernumber;
195
196 # Converts the branchcode to the branch name
197 my $samebranch;
198 if ( C4::Context->preference("IndependantBranches") ) {
199     my $userenv = C4::Context->userenv;
200     unless ( $userenv->{flags} % 2 == 1 ) {
201         $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
202     }
203     $samebranch = 1 if ( $userenv->{flags} % 2 == 1 );
204 }else{
205     $samebranch = 1;
206 }
207 my $branchdetail = GetBranchDetail( $data->{'branchcode'});
208 $data->{'branchname'} = $branchdetail->{branchname};
209
210
211 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
212 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
213 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
214 $template->param( lib1 => $lib1 ) if ($lib1);
215 $template->param( lib2 => $lib2 ) if ($lib2);
216
217 # current issues
218 #
219 my $issue = GetPendingIssues($borrowernumber);
220 my $issuecount = scalar(@$issue);
221 my $roaddetails = &GetRoadTypeDetails( $data->{'streettype'} );
222 my $today       = POSIX::strftime("%Y-%m-%d", localtime);       # iso format
223 my @issuedata;
224 my $overdues_exist = 0;
225 my $totalprice = 0;
226 for ( my $i = 0 ; $i < $issuecount ; $i++ ) {
227     my $datedue = $issue->[$i]{'date_due'};
228     my $issuedate = $issue->[$i]{'issuedate'};
229     $issue->[$i]{'date_due'}  = C4::Dates->new($issue->[$i]{'date_due'}, 'iso')->output('syspref');
230     $issue->[$i]{'issuedate'} = C4::Dates->new($issue->[$i]{'issuedate'},'iso')->output('syspref');
231     my $biblionumber = $issue->[$i]{'biblionumber'};
232     my %row = %{ $issue->[$i] };
233     $totalprice += $issue->[$i]{'replacementprice'};
234     $row{'replacementprice'} = $issue->[$i]{'replacementprice'};
235     # item lost, damaged loops
236     if ($row{'itemlost'}) {
237         my $fw = GetFrameworkCode($issue->[$i]{'biblionumber'});
238         my $category = GetAuthValCode('items.itemlost',$fw);
239         my $lostdbh = C4::Context->dbh;
240         my $sth = $lostdbh->prepare("select lib from authorised_values where category=? and authorised_value =? ");
241         $sth->execute($category, $row{'itemlost'});
242         my $loststat = $sth->fetchrow;
243         if ($loststat) {
244            $row{'itemlost'} = $loststat;
245         }
246     }
247     if ($row{'damaged'}) {
248         my $fw = GetFrameworkCode($issue->[$i]{'biblionumber'});
249         my $category = GetAuthValCode('items.damaged',$fw);
250         my $damageddbh = C4::Context->dbh;
251         my $sth = $damageddbh->prepare("select lib from authorised_values where category=? and authorised_value =? ");
252         $sth->execute($category, $row{'damaged'});
253         my $damagedstat = $sth->fetchrow;
254         if ($damagedstat) {
255            $row{'itemdamaged'} = $damagedstat;
256         }
257     }
258     # end lost, damaged
259     if ( $datedue lt $today ) {
260         $overdues_exist = 1;
261         $row{'red'} = 1;
262         }
263          if ( $issuedate eq $today ) {
264         $row{'today'} = 1; 
265          }
266
267     #find the charge for an item
268     my ( $charge, $itemtype ) =
269       GetIssuingCharges( $issue->[$i]{'itemnumber'}, $borrowernumber );
270
271     my $itemtypeinfo = getitemtypeinfo($itemtype);
272     $row{'itemtype_description'} = $itemtypeinfo->{description};
273     $row{'itemtype_image'}       = $itemtypeinfo->{imageurl};
274
275     $row{'charge'} = sprintf( "%.2f", $charge );
276
277         my ( $renewokay,$renewerror ) = CanBookBeRenewed( $borrowernumber, $issue->[$i]{'itemnumber'}, $override_limit );
278         $row{'norenew'} = !$renewokay;
279         $row{'can_confirm'} = ( !$renewokay && $renewerror ne 'on_reserve' );
280         $row{"norenew_reason_$renewerror"} = 1 if $renewerror;
281         $row{'renew_failed'}  = $renew_failed{ $issue->[$i]{'itemnumber'} };
282         $row{'return_failed'} = $return_failed{$issue->[$i]{'barcode'}};   
283     push( @issuedata, \%row );
284 }
285
286 ### ###############################################################################
287 # BUILD HTML
288 # show all reserves of this borrower, and the position of the reservation ....
289 if ($borrowernumber) {
290
291     # new op dev
292     # now we show the status of the borrower's reservations
293     my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
294     my @reservloop;
295     foreach my $num_res (@borrowerreserv) {
296         my %getreserv;
297         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
298         my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
299         my ( $transfertwhen, $transfertfrom, $transfertto ) =
300             GetTransfers( $num_res->{'itemnumber'} );
301
302         foreach (qw(waiting transfered nottransfered)) {
303             $getreserv{$_} = 0;
304         }
305         $getreserv{reservedate}  = C4::Dates->new($num_res->{'reservedate'},'iso')->output('syspref');
306         foreach (qw(biblionumber title author itemcallnumber )) {
307             $getreserv{$_} = $getiteminfo->{$_};
308         }
309         $getreserv{barcodereserv}  = $getiteminfo->{'barcode'};
310         $getreserv{itemtype}  = $itemtypeinfo->{'description'};
311
312         #               check if we have a waitin status for reservations
313         if ( $num_res->{'found'} eq 'W' ) {
314             $getreserv{color}   = 'reserved';
315             $getreserv{waiting} = 1;
316         }
317
318         #               check transfers with the itemnumber foud in th reservation loop
319         if ($transfertwhen) {
320             $getreserv{color}      = 'transfered';
321             $getreserv{transfered} = 1;
322             $getreserv{datesent}   = C4::Dates->new($transfertwhen, 'iso')->output('syspref') or die "Cannot get new($transfertwhen, 'iso') from C4::Dates";
323             $getreserv{frombranch} = GetBranchName($transfertfrom);
324         }
325
326         if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
327             and not $transfertwhen )
328         {
329             $getreserv{nottransfered}   = 1;
330             $getreserv{nottransferedby} =
331                 GetBranchName( $getiteminfo->{'holdingbranch'} );
332         }
333
334 #               if we don't have a reserv on item, we put the biblio infos and the waiting position
335         if ( $getiteminfo->{'title'} eq '' ) {
336             my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
337             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
338             $getreserv{color}           = 'inwait';
339             $getreserv{title}           = $getbibinfo->{'title'};
340             $getreserv{nottransfered}   = 0;
341             $getreserv{itemtype}        = $getbibtype->{'description'};
342             $getreserv{author}          = $getbibinfo->{'author'};
343             $getreserv{biblionumber}  = $num_res->{'biblionumber'};     
344         }
345         $getreserv{waitingposition} = $num_res->{'priority'};
346
347         push( @reservloop, \%getreserv );
348     }
349
350     # return result to the template
351     $template->param( reservloop => \@reservloop,
352         countreserv => scalar @reservloop,
353          );
354 }
355
356 # current alert subscriptions
357 my $alerts = getalert($borrowernumber);
358 foreach (@$alerts) {
359     $_->{ $_->{type} } = 1;
360     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
361 }
362
363 my $candeleteuser;
364 my $userenv = C4::Context->userenv;
365 if($userenv->{flags} % 2 == 1){
366     $candeleteuser = 1;
367 }elsif ( C4::Context->preference("IndependantBranches") ) {
368     $candeleteuser = ( $data->{'branchcode'} eq $userenv->{branch} );
369 }else{
370     if( C4::Auth::getuserflags( $userenv->{flags},$userenv->{number})->{borrowers} ) {
371         $candeleteuser = 1;
372     }else{
373         $candeleteuser = 0;
374     }
375 }
376
377 # check to see if patron's image exists in the database
378 # basically this gives us a template var to condition the display of
379 # patronimage related interface on
380 my ($picture, $dberror) = GetPatronImage($data->{'cardnumber'});
381 $template->param( picture => 1 ) if $picture;
382
383 my $branch=C4::Context->userenv->{'branch'};
384
385 $template->param($data);
386
387 if (C4::Context->preference('ExtendedPatronAttributes')) {
388     $template->param(ExtendedPatronAttributes => 1);
389     $template->param(patron_attributes => C4::Members::Attributes::GetBorrowerAttributes($borrowernumber));
390     my @types = C4::Members::AttributeTypes::GetAttributeTypes();
391     if (scalar(@types) == 0) {
392         $template->param(no_patron_attribute_types => 1);
393     }
394 }
395
396 if (C4::Context->preference('EnhancedMessagingPreferences')) {
397     C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
398     $template->param(messaging_form_inactive => 1);
399     $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
400     $template->param(SMSnumber     => defined $data->{'smsalertnumber'} ? $data->{'smsalertnumber'} : $data->{'mobile'});
401 }
402
403 $template->param(
404     detailview => 1,
405     AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
406     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
407     CANDELETEUSER    => $candeleteuser,
408     roaddetails     => $roaddetails,
409     borrowernumber  => $borrowernumber,
410     categoryname    => $data->{'description'},
411     reregistration  => $reregistration,
412     branch          => $branch,
413     totalprice      => sprintf("%.2f", $totalprice),
414     totaldue        => sprintf("%.2f", $total),
415     totaldue_raw    => $total,
416     issueloop       => \@issuedata,
417         issuecount => $issuecount,
418     overdues_exist  => $overdues_exist,
419     error           => $error,
420     $error          => 1,
421     StaffMember     => ($category_type eq 'S'),
422     is_child        => ($category_type eq 'C'),
423 #   reserveloop     => \@reservedata,
424     dateformat      => C4::Context->preference("dateformat"),
425     "dateformat_" . (C4::Context->preference("dateformat") || '') => 1,
426     samebranch     => $samebranch,
427     quickslip             => $quickslip,
428 );
429
430 output_html_with_http_headers $input, $cookie, $template->output;