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