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