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