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