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