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