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