bug_6253: Unified member Search()
[koha.git] / circ / circulation.pl
1 #!/usr/bin/perl
2
3 # script to execute issuing of books
4
5 # Copyright 2000-2002 Katipo Communications
6 # copyright 2010 BibLibre
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 use strict;
24 #use warnings; FIXME - Bug 2505
25 use CGI;
26 use C4::Output;
27 use C4::Print;
28 use C4::Auth qw/:DEFAULT get_session/;
29 use C4::Dates qw/format_date/;
30 use C4::Branch; # GetBranches
31 use C4::Koha;   # GetPrinter
32 use C4::Circulation;
33 use C4::Members;
34 use C4::Biblio;
35 use C4::Reserves;
36 use C4::Context;
37 use CGI::Session;
38 use C4::Members::Attributes qw(GetBorrowerAttributes);
39
40 use Date::Calc qw(
41   Today
42   Add_Delta_YM
43   Add_Delta_Days
44   Date_to_Days
45 );
46 use List::MoreUtils qw/uniq/;
47
48
49 #
50 # PARAMETERS READING
51 #
52 my $query = new CGI;
53
54 my $sessionID = $query->cookie("CGISESSID") ;
55 my $session = get_session($sessionID);
56
57 # branch and printer are now defined by the userenv
58 # but first we have to check if someone has tried to change them
59
60 my $branch = $query->param('branch');
61 if ($branch){
62     # update our session so the userenv is updated
63     $session->param('branch', $branch);
64     $session->param('branchname', GetBranchName($branch));
65 }
66
67 my $printer = $query->param('printer');
68 if ($printer){
69     # update our session so the userenv is updated
70     $session->param('branchprinter', $printer);
71 }
72
73 if (!C4::Context->userenv && !$branch){
74     if ($session->param('branch') eq 'NO_LIBRARY_SET'){
75         # no branch set we can't issue
76         print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
77         exit;
78     }
79 }
80
81 my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
82     {
83         template_name   => 'circ/circulation.tmpl',
84         query           => $query,
85         type            => "intranet",
86         authnotrequired => 0,
87         flagsrequired   => { circulate => 'circulate_remaining_permissions' },
88     }
89 );
90
91 my $branches = GetBranches();
92
93 my @failedrenews = $query->param('failedrenew');    # expected to be itemnumbers 
94 my %renew_failed;
95 for (@failedrenews) { $renew_failed{$_} = 1; }
96
97 my $findborrower = $query->param('findborrower');
98 $findborrower =~ s|,| |g;
99 my $borrowernumber = $query->param('borrowernumber');
100
101 $branch  = C4::Context->userenv->{'branch'};  
102 $printer = C4::Context->userenv->{'branchprinter'};
103
104
105 # If AutoLocation is not activated, we show the Circulation Parameters to chage settings of librarian
106 if (C4::Context->preference("AutoLocation") != 1) {
107     $template->param(ManualLocation => 1);
108 }
109
110 if (C4::Context->preference("DisplayClearScreenButton")) {
111     $template->param(DisplayClearScreenButton => 1);
112 }
113
114 if (C4::Context->preference("UseTablesortForCirc")) {
115     $template->param(UseTablesortForCirc => 1);
116 }
117
118 my $barcode        = $query->param('barcode') || '';
119 $barcode =~  s/^\s*|\s*$//g; # remove leading/trailing whitespace
120
121 $barcode = barcodedecode($barcode) if( $barcode && C4::Context->preference('itemBarcodeInputFilter'));
122 my $stickyduedate  = $query->param('stickyduedate') || $session->param('stickyduedate');
123 my $duedatespec    = $query->param('duedatespec')   || $session->param('stickyduedate');
124 my $issueconfirmed = $query->param('issueconfirmed');
125 my $cancelreserve  = $query->param('cancelreserve');
126 my $organisation   = $query->param('organisations');
127 my $print          = $query->param('print');
128 my $newexpiry      = $query->param('dateexpiry');
129 my $debt_confirmed = $query->param('debt_confirmed') || 0; # Don't show the debt error dialog twice
130
131 # Check if stickyduedate is turned off
132 if ( $barcode ) {
133     # was stickyduedate loaded from session?
134     if ( $stickyduedate && ! $query->param("stickyduedate") ) {
135         $session->clear( 'stickyduedate' );
136         $stickyduedate  = $query->param('stickyduedate');
137         $duedatespec    = $query->param('duedatespec');
138     }
139 }
140
141 my ($datedue,$invalidduedate);
142
143 my $duedatespec_allow = C4::Context->preference('SpecifyDueDate');
144 if($duedatespec_allow){
145     if ($duedatespec) {
146         if ($duedatespec =~ C4::Dates->regexp('syspref')) {
147             my $tempdate = C4::Dates->new($duedatespec);
148 #           if ($tempdate and $tempdate->output('iso') gt C4::Dates->new()->output('iso')) {
149 #               # i.e., it has to be later than today/now
150                 $datedue = $tempdate;
151 #           } else {
152 #               $invalidduedate = 1;
153 #               $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
154 #           }
155         } else {
156             $invalidduedate = 1;
157             $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
158         }
159     }
160 }
161
162 my $todaysdate = C4::Dates->new->output('iso');
163
164 # check and see if we should print
165 if ( $barcode eq '' && $print eq 'maybe' ) {
166     $print = 'yes';
167 }
168
169 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
170 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
171     $template->param(
172         PAYCHARGES     => 'yes',
173         borrowernumber => $borrowernumber
174     );
175 }
176
177 if ( $print eq 'yes' && $borrowernumber ne '' ) {
178     printslip( $borrowernumber );
179     $query->param( 'borrowernumber', '' );
180     $borrowernumber = '';
181 }
182
183 #
184 # STEP 2 : FIND BORROWER
185 # if there is a list of find borrowers....
186 #
187 my $borrowerslist;
188 my $message;
189 if ($findborrower) {
190     my $borrowers = Search($findborrower, 'cardnumber');
191     my @borrowers = @$borrowers;
192     if (C4::Context->preference("AddPatronLists")) {
193         $template->param(
194             "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
195         );
196         if (C4::Context->preference("AddPatronLists")=~/code/){
197             my $categories = GetBorrowercategoryList;
198             $categories->[0]->{'first'} = 1;
199             $template->param(categories=>$categories);
200         }
201     }
202     if ( $#borrowers == -1 ) {
203         $query->param( 'findborrower', '' );
204         $message = "'$findborrower'";
205     }
206     elsif ( $#borrowers == 0 ) {
207         $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
208         $query->param( 'barcode',           '' );
209         $borrowernumber = $borrowers[0]->{'borrowernumber'};
210     }
211     else {
212         $borrowerslist = \@borrowers;
213     }
214 }
215
216 # get the borrower information.....
217 my $borrower;
218 if ($borrowernumber) {
219     $borrower = GetMemberDetails( $borrowernumber, 0 );
220     my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
221
222     # Warningdate is the date that the warning starts appearing
223     my (  $today_year,   $today_month,   $today_day) = Today();
224     my ($warning_year, $warning_month, $warning_day) = split /-/, $borrower->{'dateexpiry'};
225     my (  $enrol_year,   $enrol_month,   $enrol_day) = split /-/, $borrower->{'dateenrolled'};
226     # Renew day is calculated by adding the enrolment period to today
227     my (  $renew_year,   $renew_month,   $renew_day);
228     if ($enrol_year*$enrol_month*$enrol_day>0) {
229         (  $renew_year,   $renew_month,   $renew_day) =
230         Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
231             0 , $borrower->{'enrolmentperiod'});
232     }
233     # if the expiry date is before today ie they have expired
234     if ( !$borrower->{'dateexpiry'} || $warning_year*$warning_month*$warning_day==0
235         || Date_to_Days($today_year,     $today_month, $today_day  ) 
236          > Date_to_Days($warning_year, $warning_month, $warning_day) )
237     {
238         #borrowercard expired, no issues
239         $template->param(
240             flagged  => "1",
241             noissues => "1",
242             expired => "1",
243             renewaldate => format_date("$renew_year-$renew_month-$renew_day")
244         );
245     }
246     # check for NotifyBorrowerDeparture
247     elsif ( C4::Context->preference('NotifyBorrowerDeparture') &&
248             Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
249             Date_to_Days( $today_year, $today_month, $today_day ) ) 
250     {
251         # borrower card soon to expire warn librarian
252         $template->param("warndeparture" => format_date($borrower->{dateexpiry}),
253         flagged       => "1",);
254         if (C4::Context->preference('ReturnBeforeExpiry')){
255             $template->param("returnbeforeexpiry" => 1);
256         }
257     }
258     $template->param(
259         overduecount => $od,
260         issuecount   => $issue,
261         finetotal    => $fines
262     );
263 }
264
265 #
266 # STEP 3 : ISSUING
267 #
268 #
269 if ($barcode) {
270     # always check for blockers on issuing
271     my ( $error, $question ) =
272     CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
273     my $blocker = $invalidduedate ? 1 : 0;
274
275     delete $question->{'DEBT'} if ($debt_confirmed);
276     foreach my $impossible ( keys %$error ) {
277         $template->param(
278             $impossible => $$error{$impossible},
279             IMPOSSIBLE  => 1
280         );
281         $blocker = 1;
282     }
283     if( !$blocker ){
284         my $confirm_required = 0;
285         unless($issueconfirmed){
286             #  Get the item title for more information
287             my $getmessageiteminfo  = GetBiblioFromItemNumber(undef,$barcode);
288             $template->param( itemhomebranch => $getmessageiteminfo->{'homebranch'} );
289
290             # pass needsconfirmation to template if issuing is possible and user hasn't yet confirmed.
291             foreach my $needsconfirmation ( keys %$question ) {
292                 $template->param(
293                     $needsconfirmation => $$question{$needsconfirmation},
294                     getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
295                     getBarcodeMessageIteminfo => $getmessageiteminfo->{'barcode'},
296                     NEEDSCONFIRMATION  => 1
297                 );
298                 $confirm_required = 1;
299             }
300         }
301         unless($confirm_required) {
302             AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
303             $inprocess = 1;
304         }
305     }
306     
307     # FIXME If the issue is confirmed, we launch another time GetMemberIssuesAndFines, now display the issue count after issue 
308     my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
309     $template->param( issuecount   => $issue );
310 }
311
312 # reload the borrower info for the sake of reseting the flags.....
313 if ($borrowernumber) {
314     $borrower = GetMemberDetails( $borrowernumber, 0 );
315 }
316
317 ##################################################################################
318 # BUILD HTML
319 # show all reserves of this borrower, and the position of the reservation ....
320 if ($borrowernumber) {
321
322     # new op dev
323     # now we show the status of the borrower's reservations
324     my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
325     my @reservloop;
326     my @WaitingReserveLoop;
327     
328     foreach my $num_res (@borrowerreserv) {
329         my %getreserv;
330         my %getWaitingReserveInfo;
331         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
332         my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itypes')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
333         my ( $transfertwhen, $transfertfrom, $transfertto ) =
334           GetTransfers( $num_res->{'itemnumber'} );
335
336         $getreserv{waiting}       = 0;
337         $getreserv{transfered}    = 0;
338         $getreserv{nottransfered} = 0;
339
340         $getreserv{reservedate}    = format_date( $num_res->{'reservedate'} );
341         $getreserv{reservenumber}  = $num_res->{'reservenumber'};
342         $getreserv{title}          = $getiteminfo->{'title'};
343         $getreserv{itemtype}       = $itemtypeinfo->{'description'};
344         $getreserv{author}         = $getiteminfo->{'author'};
345         $getreserv{barcodereserv}  = $getiteminfo->{'barcode'};
346         $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
347         $getreserv{biblionumber}   = $getiteminfo->{'biblionumber'};
348         $getreserv{waitingat}      = GetBranchName( $num_res->{'branchcode'} );
349         #         check if we have a waiting status for reservations
350         if ( $num_res->{'found'} eq 'W' ) {
351             $getreserv{color}   = 'reserved';
352             $getreserv{waiting} = 1;
353 #     genarate information displaying only waiting reserves
354         $getWaitingReserveInfo{title}        = $getiteminfo->{'title'};
355         $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
356         $getWaitingReserveInfo{itemtype}     = $itemtypeinfo->{'description'};
357         $getWaitingReserveInfo{author}       = $getiteminfo->{'author'};
358         $getWaitingReserveInfo{reservedate}  = format_date( $num_res->{'reservedate'} );
359         $getWaitingReserveInfo{waitingat}    = GetBranchName( $num_res->{'branchcode'} );
360         $getWaitingReserveInfo{waitinghere}  = 1 if $num_res->{'branchcode'} eq $branch;
361         }
362         #         check transfers with the itemnumber foud in th reservation loop
363         if ($transfertwhen) {
364             $getreserv{color}      = 'transfered';
365             $getreserv{transfered} = 1;
366             $getreserv{datesent}   = format_date($transfertwhen);
367             $getreserv{frombranch} = GetBranchName($transfertfrom);
368         } elsif ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'}) {
369             $getreserv{nottransfered}   = 1;
370             $getreserv{nottransferedby} = GetBranchName( $getiteminfo->{'holdingbranch'} );
371         }
372
373 #         if we don't have a reserv on item, we put the biblio infos and the waiting position
374         if ( $getiteminfo->{'title'} eq '' ) {
375             my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
376
377             $getreserv{color}           = 'inwait';
378             $getreserv{title}           = $getbibinfo->{'title'};
379             $getreserv{nottransfered}   = 0;
380             $getreserv{itemtype}        = $itemtypeinfo->{'description'};
381             $getreserv{author}          = $getbibinfo->{'author'};
382             $getreserv{biblionumber}    = $num_res->{'biblionumber'};
383         }
384         $getreserv{waitingposition} = $num_res->{'priority'};
385         push( @reservloop, \%getreserv );
386
387 #         if we have a reserve waiting, initiate waitingreserveloop
388         if ($getreserv{waiting} == 1) {
389         push (@WaitingReserveLoop, \%getWaitingReserveInfo)
390         }
391       
392     }
393
394     # return result to the template
395     $template->param( 
396         countreserv => scalar @reservloop,
397         reservloop  => \@reservloop ,
398         WaitingReserveLoop  => \@WaitingReserveLoop,
399     );
400     $template->param( adultborrower => 1 ) if ( $borrower->{'category_type'} eq 'A' );
401 }
402
403 # make the issued books table.
404 my $todaysissues = '';
405 my $previssues   = '';
406 my @todaysissues;
407 my @previousissues;
408 my @relissues;
409 my @relprevissues;
410 my $displayrelissues;
411
412 my $totalprice = 0;
413
414 sub build_issue_data {
415     my $issueslist = shift;
416     my $relatives = shift;
417
418     # split in 2 arrays for today & previous
419     foreach my $it ( @$issueslist ) {
420         my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itypes')) ? $it->{'itype'} : $it->{'itemtype'} );
421
422         # set itemtype per item-level_itype syspref - FIXME this is an ugly hack
423         $it->{'itemtype'} = ( C4::Context->preference( 'item-level_itypes' ) ) ? $it->{'itype'} : $it->{'itemtype'};
424
425         ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
426             $it->{'itemnumber'}, $it->{'borrowernumber'}
427         );
428         $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
429         my ($can_renew, $can_renew_error) = CanBookBeRenewed( 
430             $it->{'borrowernumber'},$it->{'itemnumber'}
431         );
432         $it->{"renew_error_${can_renew_error}"} = 1 if defined $can_renew_error;
433         my ( $restype, $reserves ) = CheckReserves( $it->{'itemnumber'} );
434         $it->{'can_renew'} = $can_renew;
435         $it->{'can_confirm'} = !$can_renew && !$restype;
436         $it->{'renew_error'} = $restype;
437         $it->{'checkoutdate'} = C4::Dates->new($it->{'issuedate'},'iso')->output('syspref');
438         $it->{'issuingbranchname'} = GetBranchName($it->{'branchcode'});
439
440         $totalprice += $it->{'replacementprice'};
441         $it->{'itemtype'} = $itemtypeinfo->{'description'};
442         $it->{'itemtype_image'} = $itemtypeinfo->{'imageurl'};
443         $it->{'dd'} = format_date($it->{'date_due'});
444         $it->{'displaydate'} = format_date($it->{'issuedate'});
445         $it->{'od'} = ( $it->{'date_due'} lt $todaysdate ) ? 1 : 0 ;
446         ($it->{'author'} eq '') and $it->{'author'} = ' ';
447         $it->{'renew_failed'} = $renew_failed{$it->{'itemnumber'}};
448
449         if ( $todaysdate eq $it->{'issuedate'} or $todaysdate eq $it->{'lastreneweddate'} ) {
450             (!$relatives) ? push @todaysissues, $it : push @relissues, $it;
451         } else {
452             (!$relatives) ? push @previousissues, $it : push @relprevissues, $it;
453         }
454     }
455 }
456
457 if ($borrower) {
458
459     # Getting borrower relatives
460     my @relborrowernumbers = GetMemberRelatives($borrower->{'borrowernumber'});
461     #push @borrowernumbers, $borrower->{'borrowernumber'};
462
463     # get each issue of the borrower & separate them in todayissues & previous issues
464     my $issueslist = GetPendingIssues($borrower->{'borrowernumber'});
465     my $relissueslist = [];
466     if ( @relborrowernumbers ) {
467         $relissueslist = GetPendingIssues(@relborrowernumbers);
468     }
469
470     build_issue_data($issueslist, 0);
471     build_issue_data($relissueslist, 1);
472   
473     $displayrelissues = scalar($relissueslist);
474
475     if ( C4::Context->preference( "todaysIssuesDefaultSortOrder" ) eq 'asc' ) {
476         @todaysissues   = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
477     }
478     else {
479         @todaysissues   = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
480     }
481
482     if ( C4::Context->preference( "previousIssuesDefaultSortOrder" ) eq 'asc' ){
483         @previousissues = sort { $a->{'date_due'} cmp $b->{'date_due'} } @previousissues;
484     }
485     else {
486         @previousissues = sort { $b->{'date_due'} cmp $a->{'date_due'} } @previousissues;
487     }
488 }
489
490
491 my @values;
492 my %labels;
493 my $CGIselectborrower;
494 if ($borrowerslist) {
495     foreach (
496         sort {(lc $a->{'surname'} cmp lc $b->{'surname'} || lc $a->{'firstname'} cmp lc $b->{'firstname'})
497         } @$borrowerslist
498       )
499     {
500         push @values, $_->{'borrowernumber'};
501         $labels{ $_->{'borrowernumber'} } =
502 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'address'} ";
503     }
504     $CGIselectborrower = CGI::scrolling_list(
505         -name     => 'borrowernumber',
506         -class    => 'focus',
507         -id       => 'borrowernumber',
508         -values   => \@values,
509         -labels   => \%labels,
510         -ondblclick => 'document.forms[\'mainform\'].submit()',
511         -size     => 7,
512         -tabindex => '',
513         -multiple => 0
514     );
515 }
516
517 #title
518 my $flags = $borrower->{'flags'};
519 foreach my $flag ( sort keys %$flags ) {
520     $template->param( flagged=> 1);
521     $flags->{$flag}->{'message'} =~ s#\n#<br />#g;
522     if ( $flags->{$flag}->{'noissues'} ) {
523         $template->param(
524             flagged  => 1,
525             noissues => 'true',
526         );
527         if ( $flag eq 'GNA' ) {
528             $template->param( gna => 'true' );
529         }
530         elsif ( $flag eq 'LOST' ) {
531             $template->param( lost => 'true' );
532         }
533         elsif ( $flag eq 'DBARRED' ) {
534             $template->param( dbarred => 'true' );
535         }
536         elsif ( $flag eq 'CHARGES' ) {
537             $template->param(
538                 charges    => 'true',
539                 chargesmsg => $flags->{'CHARGES'}->{'message'},
540                 chargesamount => $flags->{'CHARGES'}->{'amount'},
541                 charges_is_blocker => 1
542             );
543         }
544         elsif ( $flag eq 'CREDITS' ) {
545             $template->param(
546                 credits    => 'true',
547                 creditsmsg => $flags->{'CREDITS'}->{'message'},
548                 creditsamount => sprintf("%.02f", -($flags->{'CREDITS'}->{'amount'})), # from patron's pov
549             );
550         }
551     }
552     else {
553         if ( $flag eq 'CHARGES' ) {
554             $template->param(
555                 charges    => 'true',
556                 flagged    => 1,
557                 chargesmsg => $flags->{'CHARGES'}->{'message'},
558                 chargesamount => $flags->{'CHARGES'}->{'amount'},
559             );
560         }
561         elsif ( $flag eq 'CREDITS' ) {
562             $template->param(
563                 credits    => 'true',
564                 creditsmsg => $flags->{'CREDITS'}->{'message'},
565                 creditsamount => sprintf("%.02f", -($flags->{'CREDITS'}->{'amount'})), # from patron's pov
566             );
567         }
568         elsif ( $flag eq 'ODUES' ) {
569             $template->param(
570                 odues    => 'true',
571                 flagged  => 1,
572                 oduesmsg => $flags->{'ODUES'}->{'message'}
573             );
574
575             my $items = $flags->{$flag}->{'itemlist'};
576             if ( ! $query->param('module') || $query->param('module') ne 'returns' ) {
577                 $template->param( nonreturns => 'true' );
578             }
579         }
580         elsif ( $flag eq 'NOTES' ) {
581             $template->param(
582                 notes    => 'true',
583                 flagged  => 1,
584                 notesmsg => $flags->{'NOTES'}->{'message'}
585             );
586         }
587     }
588 }
589
590 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
591 $amountold =~ s/^.*\$//;    # remove upto the $, if any
592
593 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
594
595 if ( $borrower->{'category_type'} eq 'C') {
596     my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
597     my $cnt = scalar(@$catcodes);
598     $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
599     $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
600 }
601
602 my $CGIorganisations;
603 my $member_of_institution;
604 if ( C4::Context->preference("memberofinstitution") ) {
605     my $organisations = get_institutions();
606     my @orgs;
607     my %org_labels;
608     foreach my $organisation ( keys %$organisations ) {
609         push @orgs, $organisation;
610         $org_labels{$organisation} = $organisations->{$organisation}->{'surname'};
611     }
612     $member_of_institution = 1;
613     $CGIorganisations      = CGI::popup_menu(
614         -id     => 'organisations',
615         -name   => 'organisations',
616         -labels => \%org_labels,
617         -values => \@orgs,
618     );
619 }
620
621 my $lib_messages_loop = GetMessages( $borrowernumber, 'L', $branch );
622 if($lib_messages_loop){ $template->param(flagged => 1 ); }
623
624 my $bor_messages_loop = GetMessages( $borrowernumber, 'B', $branch );
625 if($bor_messages_loop){ $template->param(flagged => 1 ); }
626
627 # Computes full borrower address
628 my (undef, $roadttype_hashref) = &GetRoadTypes();
629 my $address = $borrower->{'streetnumber'}.' '.$roadttype_hashref->{$borrower->{'streettype'}}.' '.$borrower->{'address'};
630
631 my $fast_cataloging = 0;
632     if (defined getframeworkinfo('FA')) {
633     $fast_cataloging = 1 
634     }
635 my $attributes = GetBorrowerAttributes($borrowernumber);
636
637 $template->param(
638     lib_messages_loop => $lib_messages_loop,
639     bor_messages_loop => $bor_messages_loop,
640     all_messages_del  => C4::Context->preference('AllowAllMessageDeletion'),
641     findborrower      => $findborrower,
642     borrower          => $borrower,
643     borrowernumber    => $borrowernumber,
644     branch            => $branch,
645     branchname        => GetBranchName($borrower->{'branchcode'}),
646     printer           => $printer,
647     printername       => $printer,
648     firstname         => $borrower->{'firstname'},
649     surname           => $borrower->{'surname'},
650     dateexpiry        => format_date($newexpiry),
651     expiry            => format_date($borrower->{'dateexpiry'}),
652     categorycode      => $borrower->{'categorycode'},
653     categoryname      => $borrower->{description},
654     address           => $address,
655     address2          => $borrower->{'address2'},
656     email             => $borrower->{'email'},
657     emailpro          => $borrower->{'emailpro'},
658     borrowernotes     => $borrower->{'borrowernotes'},
659     city              => $borrower->{'city'},
660     state              => $borrower->{'state'},
661     zipcode           => $borrower->{'zipcode'},
662     country           => $borrower->{'country'},
663     phone             => $borrower->{'phone'} || $borrower->{'mobile'},
664     cardnumber        => $borrower->{'cardnumber'},
665     amountold         => $amountold,
666     barcode           => $barcode,
667     stickyduedate     => $stickyduedate,
668     duedatespec       => $duedatespec,
669     message           => $message,
670     CGIselectborrower => $CGIselectborrower,
671     totalprice        => sprintf('%.2f', $totalprice),
672     totaldue          => sprintf('%.2f', $total),
673     todayissues       => \@todaysissues,
674     previssues        => \@previousissues,
675     relissues                   => \@relissues,
676     relprevissues               => \@relprevissues,
677     displayrelissues            => $displayrelissues,
678     inprocess         => $inprocess,
679     memberofinstution => $member_of_institution,
680     CGIorganisations  => $CGIorganisations,
681     is_child          => ($borrower->{'category_type'} eq 'C'),
682     circview => 1,
683     soundon           => C4::Context->preference("SoundOn"),
684     fast_cataloging   => $fast_cataloging,
685     extendedattributes => $attributes,
686 );
687
688 # save stickyduedate to session
689 if ($stickyduedate) {
690     $session->param( 'stickyduedate', $duedatespec );
691 }
692
693 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
694 $template->param( picture => 1 ) if $picture;
695
696 # get authorised values with type of BOR_NOTES
697
698 my $canned_notes = GetAuthorisedValues("BOR_NOTES");
699
700 $template->param(
701     debt_confirmed            => $debt_confirmed,
702     SpecifyDueDate            => $duedatespec_allow,
703     CircAutocompl             => C4::Context->preference("CircAutocompl"),
704         AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
705     dateformat                => C4::Context->preference("dateformat"),
706     DHTMLcalendar_dateformat  => C4::Dates->DHTMLcalendar(),
707     canned_bor_notes_loop     => $canned_notes,
708 );
709 output_html_with_http_headers $query, $cookie, $template->output;