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