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