fix for 2336
[koha.git] / members / moremember.pl
blob941272d8bd45fff9c572b869d71e61317a15c513
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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
21 =head1 moremember.pl
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
30 needs html removed and to use the C4::Output more, but its tricky
32 =cut
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
51 #use Smart::Comments;
52 #use Data::Dumper;
54 use vars qw($debug);
56 BEGIN {
57 $debug = $ENV{DEBUG} || 0;
60 my $dbh = C4::Context->dbh;
62 my $input = new CGI;
63 $debug or $debug = $input->param('debug') || 0;
64 my $print = $input->param('print');
65 my $override_limit = $input->param("override_limit") || 0;
66 my @failedrenews = $input->param('failedrenew');
67 my @failedreturns = $input->param('failedreturn');
68 my $error = $input->param('error');
69 my %renew_failed;
70 for my $renew (@failedrenews) { $renew_failed{$renew} = 1; }
71 my %return_failed;
72 for my $failedret (@failedreturns) { $return_failed{$failedret} = 1; }
74 my $template_name;
76 if ($print eq "page") { $template_name = "members/moremember-print.tmpl"; }
77 elsif ($print eq "slip") { $template_name = "members/moremember-receipt.tmpl"; }
78 else { $template_name = "members/moremember.tmpl"; }
80 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
82 template_name => $template_name,
83 query => $input,
84 type => "intranet",
85 authnotrequired => 0,
86 flagsrequired => { borrowers => 1 },
87 debug => 1,
90 my $borrowernumber = $input->param('borrowernumber');
92 #start the page and read in includes
93 my $data = GetMember( $borrowernumber ,'borrowernumber');
94 my $reregistration = $input->param('reregistration');
96 if ( not defined $data ) {
97 $template->param (unknowuser => 1);
98 output_html_with_http_headers $input, $cookie, $template->output;
99 exit;
102 # re-reregistration function to automatic calcul of date expiry
103 if ( $reregistration eq 'y' ) {
104 $data->{'dateexpiry'} = ExtendMemberSubscriptionTo( $borrowernumber );
107 my $category_type = $data->{'category_type'};
109 ### $category_type
111 # in template <TMPL_IF name="I"> => instutitional (A for Adult& C for children)
112 $template->param( $data->{'categorycode'} => 1 );
114 $debug and printf STDERR "dates (enrolled,expiry,birthdate) raw: (%s, %s, %s)\n", map {$data->{$_}} qw(dateenrolled dateexpiry dateofbirth);
115 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
116 my $userdate = $data->{$_};
117 unless ($userdate) {
118 $debug and warn sprintf "Empty \$data{%12s}", $_;
119 $data->{$_} = '';
120 next;
122 $userdate = C4::Dates->new($userdate,'iso')->output('syspref');
123 $data->{$_} = $userdate || '';
124 $template->param( $_ => $userdate );
126 $data->{'IS_ADULT'} = ( $data->{'categorycode'} ne 'I' );
128 for (qw(debarred gonenoaddress lost borrowernotes)) {
129 $data->{$_} and $template->param(flagged => 1) and last;
132 $data->{'ethnicity'} = fixEthnicity( $data->{'ethnicity'} );
133 $data->{ "sex_".$data->{'sex'}."_p" } = 1;
135 my $catcode;
136 if ( $category_type eq 'C') {
137 if ($data->{'guarantorid'} ne '0' ) {
138 my $data2 = GetMember( $data->{'guarantorid'} ,'borrowernumber');
139 foreach (qw(address city B_address B_city phone mobile zipcode)) {
140 $data->{$_} = $data2->{$_};
143 my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
144 my $cnt = scalar(@$catcodes);
146 $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
147 $template->param( 'catcode' => $catcodes->[0]) if $cnt == 1;
151 if ( $data->{'ethnicity'} || $data->{'ethnotes'} ) {
152 $template->param( printethnicityline => 1 );
154 if ( $category_type eq 'A' ) {
155 $template->param( isguarantee => 1 );
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.
161 my ( $count, $guarantees ) = GetGuarantees( $data->{'borrowernumber'} );
162 my @guaranteedata;
163 for ( my $i = 0 ; $i < $count ; $i++ ) {
164 push(@guaranteedata,
166 borrowernumber => $guarantees->[$i]->{'borrowernumber'},
167 cardnumber => $guarantees->[$i]->{'cardnumber'},
168 name => $guarantees->[$i]->{'firstname'} . " "
169 . $guarantees->[$i]->{'surname'}
173 $template->param( guaranteeloop => \@guaranteedata );
174 ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
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->{$_});
184 if ($category_type eq 'C'){
185 $template->param('C' => 1);
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} ) );
195 my %bor;
196 $bor{'borrowernumber'} = $borrowernumber;
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} );
205 $samebranch = 1 if ( $userenv->{flags} == 1 );
207 my $branchdetail = GetBranchDetail( $data->{'branchcode'});
208 $data->{'branchname'} = $branchdetail->{branchname};
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);
217 # current issues
219 my $issue = GetPendingIssues($borrowernumber);
220 my $count = scalar(@$issue);
221 my $roaddetails = &GetRoadTypeDetails( $data->{'streettype'} );
222 my $today = POSIX::strftime("%Y-%m-%d", localtime); # iso format
223 my @issuedata;
224 my $overdues_exist = 0;
225 my $totalprice = 0;
226 my $toggle = 0;
227 for ( my $i = 0 ; $i < $count ; $i++ ) {
228 my $datedue = $issue->[$i]{'date_due'};
229 $issue->[$i]{'date_due'} = C4::Dates->new($issue->[$i]{'date_due'},'iso')->output('syspref');
230 $issue->[$i]{'issuedate'} = C4::Dates->new($issue->[$i]{'issuedate'},'iso')->output('syspref');
231 my %row = %{ $issue->[$i] };
232 $totalprice += $issue->[$i]{'replacementprice'};
233 $row{'replacementprice'} = $issue->[$i]{'replacementprice'};
234 if ( $datedue lt $today ) {
235 $overdues_exist = 1;
236 $row{'red'} = 1; #print "<font color=red>";
238 $row{toggle} = $toggle++ % 2;
240 #find the charge for an item
241 my ( $charge, $itemtype ) =
242 GetIssuingCharges( $issue->[$i]{'itemnumber'}, $borrowernumber );
244 my $itemtypeinfo = getitemtypeinfo($itemtype);
245 $row{'itemtype_description'} = $itemtypeinfo->{description};
246 $row{'itemtype_image'} = $itemtypeinfo->{imageurl};
248 $row{'charge'} = sprintf( "%.2f", $charge );
250 my ( $renewokay,$renewerror ) = CanBookBeRenewed( $borrowernumber, $issue->[$i]{'itemnumber'}, $override_limit );
251 $row{'norenew'} = !$renewokay;
252 $row{'can_confirm'} = ( !$renewokay && $renewerror ne 'on_reserve' );
253 $row{"norenew_reason_$renewerror"} = 1 if $renewerror;
254 $row{'renew_failed'} = $renew_failed{ $issue->[$i]{'itemnumber'} };
255 $row{'return_failed'} = $return_failed{$issue->[$i]{'barcode'}};
256 push( @issuedata, \%row );
259 ### ###############################################################################
260 # BUILD HTML
261 # show all reserves of this borrower, and the position of the reservation ....
262 if ($borrowernumber) {
264 # new op dev
265 # now we show the status of the borrower's reservations
266 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
267 my @reservloop;
268 foreach my $num_res (@borrowerreserv) {
269 my %getreserv;
270 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
271 my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
272 my ( $transfertwhen, $transfertfrom, $transfertto ) =
273 GetTransfers( $num_res->{'itemnumber'} );
275 foreach (qw(waiting transfered nottransfered)) {
276 $getreserv{$_} = 0;
278 $getreserv{reservedate} = C4::Dates->new($num_res->{'reservedate'},'iso')->output('syspref');
279 foreach (qw(biblionumber title author itemcallnumber )) {
280 $getreserv{$_} = $getiteminfo->{$_};
282 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
283 $getreserv{itemtype} = $itemtypeinfo->{'description'};
285 # check if we have a waitin status for reservations
286 if ( $num_res->{'found'} eq 'W' ) {
287 $getreserv{color} = 'reserved';
288 $getreserv{waiting} = 1;
291 # check transfers with the itemnumber foud in th reservation loop
292 if ($transfertwhen) {
293 $getreserv{color} = 'transfered';
294 $getreserv{transfered} = 1;
295 $getreserv{datesent} = C4::Dates->new($transfertwhen, 'iso')->output('syspref') or die "Cannot get new($transfertwhen, 'iso') from C4::Dates";
296 $getreserv{frombranch} = GetBranchName($transfertfrom);
299 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
300 and not $transfertwhen )
302 $getreserv{nottransfered} = 1;
303 $getreserv{nottransferedby} =
304 GetBranchName( $getiteminfo->{'holdingbranch'} );
307 # if we don't have a reserv on item, we put the biblio infos and the waiting position
308 if ( $getiteminfo->{'title'} eq '' ) {
309 my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
310 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
311 $getreserv{color} = 'inwait';
312 $getreserv{title} = $getbibinfo->{'title'};
313 $getreserv{nottransfered} = 0;
314 $getreserv{itemtype} = $getbibtype->{'description'};
315 $getreserv{author} = $getbibinfo->{'author'};
316 $getreserv{biblionumber} = $num_res->{'biblionumber'};
318 $getreserv{waitingposition} = $num_res->{'priority'};
320 push( @reservloop, \%getreserv );
323 # return result to the template
324 $template->param( reservloop => \@reservloop );
327 # current alert subscriptions
328 my $alerts = getalert($borrowernumber);
329 foreach (@$alerts) {
330 $_->{ $_->{type} } = 1;
331 $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
334 # check to see if patron's image exists in the database
335 # basically this gives us a template var to condition the display of
336 # patronimage related interface on
337 my ($picture, $dberror) = GetPatronImage($data->{'cardnumber'});
338 $template->param( picture => 1 ) if $picture;
340 my $branch=C4::Context->userenv->{'branch'};
342 $template->param($data);
344 if (C4::Context->preference('ExtendedPatronAttributes')) {
345 $template->param(ExtendedPatronAttributes => 1);
346 $template->param(patron_attributes => C4::Members::Attributes::GetBorrowerAttributes($borrowernumber));
347 my @types = C4::Members::AttributeTypes::GetAttributeTypes();
348 if (scalar(@types) == 0) {
349 $template->param(no_patron_attribute_types => 1);
353 $template->param(
354 detailview => 1,
355 AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
356 DHTMLcalendar_dateformat=>C4::Dates->DHTMLcalendar(),
357 roaddetails => $roaddetails,
358 borrowernumber => $borrowernumber,
359 categoryname => $data->{'description'},
360 reregistration => $reregistration,
361 branch => $branch,
362 totalprice => sprintf( "%.2f", $totalprice ),
363 totaldue => sprintf( "%.2f", $total ),
364 issueloop => \@issuedata,
365 overdues_exist => $overdues_exist,
366 unvalidlibrarian => $unvalidlibrarian,
367 error => $error,
368 $error => 1,
369 StaffMember => ($category_type eq 'S'),
370 is_child => ($category_type eq 'C'),
371 # reserveloop => \@reservedata,
372 dateformat => C4::Context->preference("dateformat"),
375 output_html_with_http_headers $input, $cookie, $template->output;