Bug 8329 - GetLostItems in C4::Items.pm has a SELECT * Provided a smaller list based...
[koha.git] / members / moremember.pl
blob25ae7e956e16201022dfef7975dbab7d653aa998
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 =head1 moremember.pl
24 script to do a borrower enquiry/bring up borrower details etc
25 Displays all the details about a borrower
26 written 20/12/99 by chris@katipo.co.nz
27 last modified 21/1/2000 by chris@katipo.co.nz
28 modified 31/1/2001 by chris@katipo.co.nz
29 to not allow items on request to be renewed
31 needs html removed and to use the C4::Output more, but its tricky
33 =cut
35 use strict;
36 #use warnings; FIXME - Bug 2505
37 use CGI;
38 use C4::Context;
39 use C4::Auth;
40 use C4::Output;
41 use C4::Members;
42 use C4::Members::Attributes;
43 use C4::Members::AttributeTypes;
44 use C4::Dates;
45 use C4::Reserves;
46 use C4::Circulation;
47 use C4::Koha;
48 use C4::Letters;
49 use C4::Biblio;
50 use C4::Reserves;
51 use C4::Branch; # GetBranchName
52 use C4::Overdues qw/CheckBorrowerDebarred/;
53 use C4::Form::MessagingPreferences;
54 use List::MoreUtils qw/uniq/;
55 use C4::Members::Attributes qw(GetBorrowerAttributes);
57 #use Smart::Comments;
58 #use Data::Dumper;
59 use DateTime;
60 use Koha::DateUtils;
62 use vars qw($debug);
64 BEGIN {
65 $debug = $ENV{DEBUG} || 0;
68 my $dbh = C4::Context->dbh;
70 my $input = CGI->new;
71 $debug or $debug = $input->param('debug') || 0;
72 my $print = $input->param('print');
73 my $override_limit = $input->param("override_limit") || 0;
74 my @failedrenews = $input->param('failedrenew');
75 my @failedreturns = $input->param('failedreturn');
76 my $error = $input->param('error');
77 my %renew_failed;
78 for my $renew (@failedrenews) { $renew_failed{$renew} = 1; }
79 my %return_failed;
80 for my $failedret (@failedreturns) { $return_failed{$failedret} = 1; }
82 my $template_name;
83 my $quickslip = 0;
85 my $flagsrequired;
86 if ($print eq "page") {
87 $template_name = "members/moremember-print.tmpl";
88 $flagsrequired = { borrowers => 1 };
89 } elsif ($print eq "slip") {
90 $template_name = "members/moremember-receipt.tmpl";
91 # circ staff who process checkouts but can't edit
92 # patrons still need to be able to print receipts
93 $flagsrequired = { circulate => "circulate_remaining_permissions" };
94 } elsif ($print eq "qslip") {
95 $template_name = "members/moremember-receipt.tmpl";
96 $quickslip = 1;
97 $flagsrequired = { circulate => "circulate_remaining_permissions" };
98 } elsif ($print eq "brief") {
99 $template_name = "members/moremember-brief.tmpl";
100 $flagsrequired = { borrowers => 1 };
101 } else {
102 $template_name = "members/moremember.tmpl";
103 $flagsrequired = { borrowers => 1 };
106 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
108 template_name => $template_name,
109 query => $input,
110 type => "intranet",
111 authnotrequired => 0,
112 flagsrequired => $flagsrequired,
113 debug => 1,
116 my $borrowernumber = $input->param('borrowernumber');
118 #start the page and read in includes
119 my $data = GetMember( 'borrowernumber' => $borrowernumber );
120 my $reregistration = $input->param('reregistration');
122 if ( not defined $data ) {
123 $template->param (unknowuser => 1);
124 output_html_with_http_headers $input, $cookie, $template->output;
125 exit;
128 # re-reregistration function to automatic calcul of date expiry
129 if ( $reregistration eq 'y' ) {
130 $data->{'dateexpiry'} = ExtendMemberSubscriptionTo( $borrowernumber );
133 my $category_type = $data->{'category_type'};
135 ### $category_type
137 $debug and printf STDERR "dates (enrolled,expiry,birthdate) raw: (%s, %s, %s)\n", map {$data->{$_}} qw(dateenrolled dateexpiry dateofbirth);
138 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
139 my $userdate = $data->{$_};
140 unless ($userdate) {
141 $debug and warn sprintf "Empty \$data{%12s}", $_;
142 $data->{$_} = '';
143 next;
145 $userdate = C4::Dates->new($userdate,'iso')->output('syspref');
146 $data->{$_} = $userdate || '';
147 $template->param( $_ => $userdate );
149 $data->{'IS_ADULT'} = ( $data->{'categorycode'} ne 'I' );
151 for (qw(gonenoaddress lost borrowernotes)) {
152 $data->{$_} and $template->param(flagged => 1) and last;
155 my $debar = CheckBorrowerDebarred($borrowernumber);
156 if ($debar) {
157 $template->param( 'userdebarred' => 1, 'flagged' => 1 );
158 if ( $debar ne "9999-12-31" ) {
159 $template->param( 'userdebarreddate' => C4::Dates::format_date($debar) );
160 $template->param( 'debarredcomment' => $data->{debarredcomment} );
164 $data->{'ethnicity'} = fixEthnicity( $data->{'ethnicity'} );
165 $data->{ "sex_".$data->{'sex'}."_p" } = 1;
167 my $catcode;
168 if ( $category_type eq 'C') {
169 my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
170 my $cnt = scalar(@$catcodes);
172 $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
173 $template->param( 'catcode' => $catcodes->[0]) if $cnt == 1;
177 if ( $data->{'ethnicity'} || $data->{'ethnotes'} ) {
178 $template->param( printethnicityline => 1 );
180 if ( $category_type eq 'A' || $category_type eq 'I') {
181 $template->param( isguarantee => 1 );
183 # FIXME
184 # It looks like the $i is only being returned to handle walking through
185 # the array, which is probably better done as a foreach loop.
187 my ( $count, $guarantees ) = GetGuarantees( $data->{'borrowernumber'} );
188 my @guaranteedata;
189 for ( my $i = 0 ; $i < $count ; $i++ ) {
190 push(@guaranteedata,
192 borrowernumber => $guarantees->[$i]->{'borrowernumber'},
193 cardnumber => $guarantees->[$i]->{'cardnumber'},
194 name => $guarantees->[$i]->{'firstname'} . " "
195 . $guarantees->[$i]->{'surname'}
199 $template->param( guaranteeloop => \@guaranteedata );
200 ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' || $category_type eq 'I' );
202 else {
203 if ($data->{'guarantorid'}){
204 my ($guarantor) = GetMember( 'borrowernumber' =>$data->{'guarantorid'});
205 $template->param(guarantor => 1);
206 foreach (qw(borrowernumber cardnumber firstname surname)) {
207 $template->param("guarantor$_" => $guarantor->{$_});
210 if ($category_type eq 'C'){
211 $template->param('C' => 1);
215 my %bor;
216 $bor{'borrowernumber'} = $borrowernumber;
218 # Converts the branchcode to the branch name
219 my $samebranch;
220 if ( C4::Context->preference("IndependantBranches") ) {
221 my $userenv = C4::Context->userenv;
222 unless ( $userenv->{flags} % 2 == 1 ) {
223 $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
225 $samebranch = 1 if ( $userenv->{flags} % 2 == 1 );
226 }else{
227 $samebranch = 1;
229 my $branchdetail = GetBranchDetail( $data->{'branchcode'});
230 @{$data}{keys %$branchdetail} = values %$branchdetail; # merge in all branch columns
232 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
233 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
234 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
235 $template->param( lib1 => $lib1 ) if ($lib1);
236 $template->param( lib2 => $lib2 ) if ($lib2);
238 # Show OPAC privacy preference is system preference is set
239 if ( C4::Context->preference('OPACPrivacy') ) {
240 $template->param( OPACPrivacy => 1);
241 $template->param( "privacy".$data->{'privacy'} => 1);
244 # current issues
246 my @borrowernumbers = GetMemberRelatives($borrowernumber);
247 my $issue = GetPendingIssues($borrowernumber);
248 my $relissue = [];
249 if ( @borrowernumbers ) {
250 $relissue = GetPendingIssues(@borrowernumbers);
252 my $roaddetails = &GetRoadTypeDetails( $data->{'streettype'} );
253 my $today = DateTime->now( time_zone => C4::Context->tz);
254 $today->truncate(to => 'day');
255 my @borrowers_with_issues;
256 my $overdues_exist = 0;
257 my $totalprice = 0;
259 my @issuedata = build_issue_data($issue);
260 my @relissuedata = build_issue_data($relissue);
263 ### ###############################################################################
264 # BUILD HTML
265 # show all reserves of this borrower, and the position of the reservation ....
266 if ($borrowernumber) {
268 # new op dev
269 # now we show the status of the borrower's reservations
270 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
271 my @reservloop;
272 foreach my $num_res (@borrowerreserv) {
273 my %getreserv;
274 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
275 my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
276 my ( $transfertwhen, $transfertfrom, $transfertto ) =
277 GetTransfers( $num_res->{'itemnumber'} );
279 foreach (qw(waiting transfered nottransfered)) {
280 $getreserv{$_} = 0;
282 $getreserv{reservedate} = C4::Dates->new($num_res->{'reservedate'},'iso')->output('syspref');
283 foreach (qw(biblionumber title author itemcallnumber )) {
284 $getreserv{$_} = $getiteminfo->{$_};
286 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
287 $getreserv{itemtype} = $itemtypeinfo->{'description'};
289 # check if we have a waitin status for reservations
290 if ( $num_res->{'found'} eq 'W' ) {
291 $getreserv{color} = 'reserved';
292 $getreserv{waiting} = 1;
295 # check transfers with the itemnumber foud in th reservation loop
296 if ($transfertwhen) {
297 $getreserv{color} = 'transfered';
298 $getreserv{transfered} = 1;
299 $getreserv{datesent} = C4::Dates->new($transfertwhen, 'iso')->output('syspref') or die "Cannot get new($transfertwhen, 'iso') from C4::Dates";
300 $getreserv{frombranch} = GetBranchName($transfertfrom);
303 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
304 and not $transfertwhen )
306 $getreserv{nottransfered} = 1;
307 $getreserv{nottransferedby} =
308 GetBranchName( $getiteminfo->{'holdingbranch'} );
311 # if we don't have a reserv on item, we put the biblio infos and the waiting position
312 if ( $getiteminfo->{'title'} eq '' ) {
313 my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
314 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
315 $getreserv{color} = 'inwait';
316 $getreserv{title} = $getbibinfo->{'title'};
317 $getreserv{nottransfered} = 0;
318 $getreserv{itemtype} = $getbibtype->{'description'};
319 $getreserv{author} = $getbibinfo->{'author'};
320 $getreserv{biblionumber} = $num_res->{'biblionumber'};
322 $getreserv{waitingposition} = $num_res->{'priority'};
323 $getreserv{suspend} = $num_res->{'suspend'};
324 $getreserv{suspend_until} = $num_res->{'suspend_until'};
326 push( @reservloop, \%getreserv );
329 # return result to the template
330 $template->param( reservloop => \@reservloop,
331 countreserv => scalar @reservloop,
335 # current alert subscriptions
336 my $alerts = getalert($borrowernumber);
337 foreach (@$alerts) {
338 $_->{ $_->{type} } = 1;
339 $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
342 my $candeleteuser;
343 my $userenv = C4::Context->userenv;
344 if($userenv->{flags} % 2 == 1){
345 $candeleteuser = 1;
346 }elsif ( C4::Context->preference("IndependantBranches") ) {
347 $candeleteuser = ( $data->{'branchcode'} eq $userenv->{branch} );
348 }else{
349 if( C4::Auth::getuserflags( $userenv->{flags},$userenv->{number})->{borrowers} ) {
350 $candeleteuser = 1;
351 }else{
352 $candeleteuser = 0;
356 # check to see if patron's image exists in the database
357 # basically this gives us a template var to condition the display of
358 # patronimage related interface on
359 my ($picture, $dberror) = GetPatronImage($data->{'cardnumber'});
360 $template->param( picture => 1 ) if $picture;
362 my $branch=C4::Context->userenv->{'branch'};
364 $template->param(%$data);
366 if (C4::Context->preference('ExtendedPatronAttributes')) {
367 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
368 my @classes = uniq( map {$_->{class}} @$attributes );
369 @classes = sort @classes;
371 my @attributes_loop;
372 for my $class (@classes) {
373 my @items;
374 for my $attr (@$attributes) {
375 push @items, $attr if $attr->{class} eq $class
377 my $lib = GetAuthorisedValueByCode( 'PA_CLASS', $class ) || $class;
378 push @attributes_loop, {
379 class => $class,
380 items => \@items,
381 lib => $lib,
385 $template->param(
386 ExtendedPatronAttributes => 1,
387 attributes_loop => \@attributes_loop
390 my @types = C4::Members::AttributeTypes::GetAttributeTypes();
391 if (scalar(@types) == 0) {
392 $template->param(no_patron_attribute_types => 1);
396 if (C4::Context->preference('EnhancedMessagingPreferences')) {
397 C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
398 $template->param(messaging_form_inactive => 1);
399 $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
400 $template->param(SMSnumber => defined $data->{'smsalertnumber'} ? $data->{'smsalertnumber'} : $data->{'mobile'});
401 $template->param(TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"));
404 # in template <TMPL_IF name="I"> => instutitional (A for Adult, C for children)
405 $template->param( $data->{'categorycode'} => 1 );
406 $template->param(
407 detailview => 1,
408 AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
409 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
410 CANDELETEUSER => $candeleteuser,
411 roaddetails => $roaddetails,
412 borrowernumber => $borrowernumber,
413 othernames => $data->{'othernames'},
414 categoryname => $data->{'description'},
415 reregistration => $reregistration,
416 branch => $branch,
417 todaysdate => C4::Dates->today(),
418 totalprice => sprintf("%.2f", $totalprice),
419 totaldue => sprintf("%.2f", $total),
420 totaldue_raw => $total,
421 issueloop => @issuedata,
422 relissueloop => @relissuedata,
423 overdues_exist => $overdues_exist,
424 error => $error,
425 StaffMember => ($category_type eq 'S'),
426 is_child => ($category_type eq 'C'),
427 # reserveloop => \@reservedata,
428 dateformat => C4::Context->preference("dateformat"),
429 "dateformat_" . (C4::Context->preference("dateformat") || '') => 1,
430 samebranch => $samebranch,
431 quickslip => $quickslip,
432 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
433 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
434 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
436 $template->param( $error => 1 ) if $error;
438 output_html_with_http_headers $input, $cookie, $template->output;
440 sub build_issue_data {
441 my $issues = shift;
443 my $localissue;
445 foreach my $issue ( @{$issues} ) {
447 # Getting borrower details
448 my $memberdetails = GetMemberDetails( $issue->{borrowernumber} );
449 $issue->{borrowername} =
450 $memberdetails->{firstname} . ' ' . $memberdetails->{surname};
451 $issue->{cardnumber} = $memberdetails->{cardnumber};
452 my $issuedate;
453 if ($issue->{issuedate} ) {
454 $issuedate = $issue->{issuedate}->clone();
457 $issue->{date_due} = output_pref( $issue->{date_due} );
458 $issue->{issuedate} = output_pref( $issue->{issuedate} ) if defined $issue->{issuedate};
459 my $biblionumber = $issue->{biblionumber};
460 my %row = %{$issue};
461 $totalprice += $issue->{replacementprice};
463 # item lost, damaged loops
464 if ( $row{'itemlost'} ) {
465 my $fw = GetFrameworkCode( $issue->{biblionumber} );
466 my $category = GetAuthValCode( 'items.itemlost', $fw );
467 my $lostdbh = C4::Context->dbh;
468 my $sth = $lostdbh->prepare(
469 "select lib from authorised_values where category=? and authorised_value =? "
471 $sth->execute( $category, $row{'itemlost'} );
472 my $loststat = $sth->fetchrow;
473 if ($loststat) {
474 $row{'itemlost'} = $loststat;
477 if ( $row{'damaged'} ) {
478 my $fw = GetFrameworkCode( $issue->{biblionumber} );
479 my $category = GetAuthValCode( 'items.damaged', $fw );
480 my $damageddbh = C4::Context->dbh;
481 my $sth = $damageddbh->prepare(
482 "select lib from authorised_values where category=? and authorised_value =? "
484 $sth->execute( $category, $row{'damaged'} );
485 my $damagedstat = $sth->fetchrow;
486 if ($damagedstat) {
487 $row{'itemdamaged'} = $damagedstat;
491 # end lost, damaged
492 if ( $issue->{overdue} ) {
493 $overdues_exist = 1;
494 $row{red} = 1;
496 if ($issuedate) {
497 $issuedate->truncate( to => 'day' );
498 if ( DateTime->compare( $issuedate, $today ) == 0 ) {
499 $row{today} = 1;
503 #find the charge for an item
504 my ( $charge, $itemtype ) =
505 GetIssuingCharges( $issue->{itemnumber}, $borrowernumber );
507 my $itemtypeinfo = getitemtypeinfo($itemtype);
508 $row{'itemtype_description'} = $itemtypeinfo->{description};
509 $row{'itemtype_image'} = $itemtypeinfo->{imageurl};
511 $row{'charge'} = sprintf( "%.2f", $charge );
513 my ( $renewokay, $renewerror ) =
514 CanBookBeRenewed( $borrowernumber, $issue->{itemnumber},
515 $override_limit );
516 $row{'norenew'} = !$renewokay;
517 $row{'can_confirm'} = ( !$renewokay && $renewerror ne 'on_reserve' );
518 $row{"norenew_reason_$renewerror"} = 1 if $renewerror;
519 $row{renew_failed} = $renew_failed{ $issue->{itemnumber} };
520 $row{return_failed} = $return_failed{ $issue->{barcode} };
521 push( @{$localissue}, \%row );
523 return $localissue;