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