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