Fix for bug 1014, internal server error on unknown borrower
[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::Dates;
41 use C4::Reserves;
42 use C4::Circulation;
43 use C4::Koha;
44 use C4::Letters;
45 use C4::Biblio;
46 use C4::Reserves;
47 use C4::Branch; # GetBranchName
48
49 #use Smart::Comments;
50
51 use vars qw($debug);
52
53 BEGIN {
54         $debug = $ENV{DEBUG} || 0;
55 }
56
57 my $dbh = C4::Context->dbh;
58
59 my $input = new CGI;
60 $debug or $debug = $input->param('debug') || 0;
61 my $print = $input->param('print');
62 my @failedrenews = $input->param('failedrenew');
63 my $error = $input->param('error');
64 my @renew_failed;
65 for (@failedrenews) { $renew_failed[$_] = 1; }
66
67 my $template_name;
68
69 if    ($print eq "page") { $template_name = "members/moremember-print.tmpl";   }
70 elsif ($print eq "slip") { $template_name = "members/moremember-receipt.tmpl"; }
71 else {                     $template_name = "members/moremember.tmpl";         }
72
73 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
74     {
75         template_name   => $template_name,
76         query           => $input,
77         type            => "intranet",
78         authnotrequired => 0,
79         flagsrequired   => { borrowers => 1 },
80         debug           => 1,
81     }
82 );
83 my $borrowernumber = $input->param('borrowernumber');
84
85 #start the page and read in includes
86 my $data           = GetMember( $borrowernumber ,'borrowernumber');
87 my $reregistration = $input->param('reregistration');
88
89 if ( not defined $data ) {
90     $template->param (unknowuser => 1);
91         output_html_with_http_headers $input, $cookie, $template->output;
92     exit;
93 }
94
95 # re-reregistration function to automatic calcul of date expiry
96 if ( $reregistration eq 'y' ) {
97         $data->{'dateexpiry'} = ExtendMemberSubscriptionTo( $borrowernumber );
98 }
99
100 my $borrowercategory = GetBorrowercategory( $data->{'categorycode'} );
101 my $category_type = $borrowercategory->{'category_type'};
102
103 ### $category_type
104
105 # in template <TMPL_IF name="I"> => instutitional (A for Adult& C for children) 
106 $template->param( $data->{'categorycode'} => 1 ); 
107
108 $debug and printf STDERR "dates (enrolled,expiry,birthdate) raw: (%s, %s, %s)\n", map {$data->{$_}} qw(dateenrolled dateexpiry dateofbirth);
109 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
110                 my $userdate = $data->{$_};
111                 unless ($userdate) {
112                         $debug and warn sprintf "Empty \$data{%12s}", $_;
113                         $data->{$_} = '';
114                         next;
115                 }
116                 $userdate = C4::Dates->new($userdate,'iso')->output('syspref');
117                 $data->{$_} = $userdate || '';
118                 $template->param( $_ => $userdate );
119 }
120 $data->{'IS_ADULT'} = ( $data->{'categorycode'} ne 'I' );
121
122 for (qw(debarred gonenoaddress lost borrowernotes)) {
123          $data->{$_} and $template->param(flagged => 1) and last;
124 }
125
126 $data->{'ethnicity'} = fixEthnicity( $data->{'ethnicity'} );
127
128 $data->{ "sex_".$data->{'sex'}."_p" } = 1;
129
130 if ( $category_type eq 'C' and $data->{'guarantorid'} ne '0' ) {
131
132     my $data2 = GetMember( $data->{'guarantorid'} ,'borrowernumber');
133     foreach (qw(address city B_address B_city phone mobile zipcode)) {
134         $data->{$_} = $data2->{$_};
135     }
136     my  ( $catcodes, $labels ) = 
137         GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
138     my $cnt = scalar(@$catcodes);
139     $template->param( 'CATCODE_MULTI' => 1)    if  $cnt > 1;
140     $template->param( 'IS_CHILD' => 1 );
141 }
142
143 if ( $data->{'ethnicity'} || $data->{'ethnotes'} ) {
144     $template->param( printethnicityline => 1 );
145 }
146 if ( $category_type eq 'A' ) {
147     $template->param( isguarantee => 1 );
148
149     # FIXME
150     # It looks like the $i is only being returned to handle walking through
151     # the array, which is probably better done as a foreach loop.
152     #
153     my ( $count, $guarantees ) = GetGuarantees( $data->{'borrowernumber'} );
154     my @guaranteedata;
155     for ( my $i = 0 ; $i < $count ; $i++ ) {
156         push(@guaranteedata,
157             {
158                 borrowernumber => $guarantees->[$i]->{'borrowernumber'},
159                 cardnumber     => $guarantees->[$i]->{'cardnumber'},
160                 name           => $guarantees->[$i]->{'firstname'} . " "
161                                 . $guarantees->[$i]->{'surname'}
162             }
163         );
164     }
165     $template->param( guaranteeloop => \@guaranteedata );
166     ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
167 }
168 else {
169     if ($data->{'guarantorid'}){
170             my ($guarantor) = GetMember( $data->{'guarantorid'},'biblionumber');
171                 $template->param(guarantor => 1);
172                 foreach (qw(borrowernumber cardnumber firstname surname)) {        
173                           $template->param("guarantor$_" => $guarantor->{$_});
174         }
175     }
176 }
177
178 #Independant branches management
179 my $unvalidlibrarian =
180   (      ( C4::Context->preference("IndependantBranches") )
181       && ( C4::Context->userenv->{flags} != 1 )
182       && ( $data->{'branchcode'} ne C4::Context->userenv->{branch} ) );
183
184 my %bor;
185 $bor{'borrowernumber'} = $borrowernumber;
186
187 # Converts the branchcode to the branch name
188 my $samebranch;
189 if ( C4::Context->preference("IndependantBranches") ) {
190     my $userenv = C4::Context->userenv;
191     unless ( $userenv->{flags} == 1 ) {
192         $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
193     }
194     $samebranch = 1 if ( $userenv->{flags} == 1 );
195 }
196 my $branchdetail = GetBranchDetail( $data->{'branchcode'});
197 $data->{'branchname'} = $branchdetail->{branchname};
198
199
200 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
201 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
202 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
203 ( $template->param( lib1 => $lib1 ) ) if ($lib1);
204 ( $template->param( lib2 => $lib2 ) ) if ($lib2);
205
206 # current issues
207 #
208 my ( $count, $issue ) = GetPendingIssues($borrowernumber);
209 my $roaddetails = &GetRoadTypeDetails( $data->{'streettype'} );
210 my $today       = POSIX::strftime("%Y%m%d", localtime); # iso format
211 my @issuedata;
212 my $totalprice = 0;
213 my $toggle     = 0;
214 for ( my $i = 0 ; $i < $count ; $i++ ) {
215     my $datedue = $issue->[$i]{'date_due'};
216     $issue->[$i]{'date_due'} = C4::Dates->new($issue->[$i]{'date_due'},'iso')->output('syspref');
217     my %row = %{ $issue->[$i] };
218     $totalprice += $issue->[$i]{'replacementprice'};
219     $row{'replacementprice'} = $issue->[$i]{'replacementprice'};
220     if ( $datedue < $today ) {
221         $row{'red'} = 1;    #print "<font color=red>";
222     }
223     $row{toggle} = $toggle++ % 2;
224
225     #find the charge for an item
226     my ( $charge, $itemtype ) =
227       GetIssuingCharges( $issue->[$i]{'itemnumber'}, $borrowernumber );
228
229     my $itemtypeinfo = getitemtypeinfo($itemtype);
230     $row{'itemtype_description'} = $itemtypeinfo->{description};
231     $row{'itemtype_image'}       = $itemtypeinfo->{imageurl};
232
233     $row{'charge'} = sprintf( "%.2f", $charge );
234
235     #check item is not reserved
236     my ( $restype, $reserves ) = CheckReserves( $issue->[$i]{'itemnumber'} );
237     $row{'norenew'} = ($restype) ? 1 : 0;
238         $row{'renew_failed'} = $renew_failed[$issue->[$i]{'itemnumber'}];               
239     push( @issuedata, \%row );
240 }
241
242 ##################################################################################
243 # BUILD HTML
244 # show all reserves of this borrower, and the position of the reservation ....
245 if ($borrowernumber) {
246
247     # new op dev
248     # now we show the status of the borrower's reservations
249     my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
250     my @reservloop;
251     foreach my $num_res (@borrowerreserv) {
252         my %getreserv;
253         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
254         my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
255         my ( $transfertwhen, $transfertfrom, $transfertto ) =
256             GetTransfers( $num_res->{'itemnumber'} );
257
258                 foreach (qw(waiting transfered nottransfered)) {
259                                 $getreserv{$_} = 0;
260                 }
261         $getreserv{reservedate}  = C4::Dates->new($num_res->{'reservedate'},'iso')->output('syspref');
262                 foreach (qw(biblionumber title author barcodereserv itemcallnumber )) {
263                                 $getreserv{$_} = $getiteminfo->{$_};
264                 }
265         $getreserv{itemtype}  = $itemtypeinfo->{'description'};
266
267         #               check if we have a waitin status for reservations
268         if ( $num_res->{'found'} eq 'W' ) {
269             $getreserv{color}   = 'reserved';
270             $getreserv{waiting} = 1;
271         }
272
273         #               check transfers with the itemnumber foud in th reservation loop
274         if ($transfertwhen) {
275             $getreserv{color}      = 'transfered';
276             $getreserv{transfered} = 1;
277             $getreserv{datesent}   = C4::Dates->new($transfertwhen, 'iso')->output('syspref') or die "Cannot get new($transfertwhen, 'iso') from C4::Dates";
278             $getreserv{frombranch} = GetBranchName($transfertfrom);
279         }
280
281         if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
282             and not $transfertwhen )
283         {
284             $getreserv{nottransfered}   = 1;
285             $getreserv{nottransferedby} =
286                 GetBranchName( $getiteminfo->{'holdingbranch'} );
287         }
288
289 #               if we don't have a reserv on item, we put the biblio infos and the waiting position
290         if ( $getiteminfo->{'title'} eq '' ) {
291             my $getbibinfo = GetBiblioItemData( $num_res->{'biblionumber'} );
292             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
293             $getreserv{color}           = 'inwait';
294             $getreserv{title}           = $getbibinfo->{'title'};
295             $getreserv{waitingposition} = $num_res->{'priority'};
296             $getreserv{nottransfered}   = 0;
297             $getreserv{itemtype}        = $getbibtype->{'description'};
298             $getreserv{author}          = $getbibinfo->{'author'};
299             $getreserv{itemcallnumber}  = '----------';
300             $getreserv{biblionumber}  = $num_res->{'biblionumber'};     
301         }
302
303         push( @reservloop, \%getreserv );
304     }
305
306     # return result to the template
307     $template->param( reservloop => \@reservloop );
308 }
309
310 # current alert subscriptions
311 my $alerts = getalert($borrowernumber);
312 foreach (@$alerts) {
313     $_->{ $_->{type} } = 1;
314     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
315 }
316 my $picture;
317 my $htdocs = C4::Context->config('intrahtdocs');
318 $picture = "/borrowerimages/" . $borrowernumber . ".jpg";
319 if ( -e $htdocs . "$picture" ) {
320     $template->param( picture => $picture );
321 }
322 my $branch=C4::Context->userenv->{'branch'};
323
324 $template->param($data);
325
326 $template->param(
327         detailview => 1,
328     roaddetails      => $roaddetails,
329     borrowernumber   => $borrowernumber,
330     reregistration   => $reregistration,
331     branch           => $branch,        
332     totalprice       => sprintf( "%.2f", $totalprice ),
333     totaldue         => sprintf( "%.2f", $total ),
334     issueloop        => \@issuedata,
335     unvalidlibrarian => $unvalidlibrarian,
336         error            => $error,
337         $error                  => 1,
338     StaffMember         => ($category_type eq 'S'),
339         is_child        => ($category_type eq 'C'),
340         #                reserveloop     => \@reservedata,
341 );
342
343 output_html_with_http_headers $input, $cookie, $template->output;