Allow renewal limits to be overridden
[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 $stickyduedate  = $query->param('stickyduedate');
125 my $duedatespec    = $query->param('duedatespec');
126 my $issueconfirmed = $query->param('issueconfirmed');
127 my $cancelreserve  = $query->param('cancelreserve');
128 my $organisation   = $query->param('organisations');
129 my $print          = $query->param('print');
130 my $newexpiry      = $query->param('dateexpiry');
131 my $debt_confirmed = $query->param('debt_confirmed') || 0; # Don't show the debt error dialog twice
132
133 #set up cookie.....
134 # my $branchcookie;
135 # my $printercookie;
136 # if ($query->param('setcookies')) {
137 #     $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
138 #     $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
139 # }
140 #
141
142 my ($datedue,$invalidduedate);
143 if ($duedatespec) {
144         if ($duedatespec =~ C4::Dates->regexp('syspref')) {
145                 my $tempdate = C4::Dates->new($duedatespec);
146                 if ($tempdate and $tempdate->output('iso') gt C4::Dates->new()->output('iso')) {
147                         # i.e., it has to be later than today/now
148                         $datedue = $tempdate;
149                 } else {
150                         $invalidduedate = 1;
151                         $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
152                 }
153         } else {
154                 $invalidduedate = 1;
155                 $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
156         }
157 }
158
159 my $todaysdate = C4::Dates->new->output('iso');
160
161 # check and see if we should print
162 if ( $barcode eq '' && $print eq 'maybe' ) {
163     $print = 'yes';
164 }
165
166 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
167
168 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
169     $template->param(
170         PAYCHARGES     => 'yes',
171         borrowernumber => $borrowernumber
172     );
173 }
174
175 if ( $print eq 'yes' && $borrowernumber ne '' ) {
176     printslip( $borrowernumber );
177     $query->param( 'borrowernumber', '' );
178     $borrowernumber = '';
179 }
180
181 #
182 # STEP 2 : FIND BORROWER
183 # if there is a list of find borrowers....
184 #
185 my $borrowerslist;
186 my $message;
187 if ($findborrower) {
188     my ( $count, $borrowers ) =
189       SearchMember($findborrower, 'cardnumber', 'web' );
190     my @borrowers = @$borrowers;
191     if ( $#borrowers == -1 ) {
192         $query->param( 'findborrower', '' );
193         $message = "'$findborrower'";
194     }
195     elsif ( $#borrowers == 0 ) {
196         $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
197         $query->param( 'barcode',           '' );
198         $borrowernumber = $borrowers[0]->{'borrowernumber'};
199     }
200     else {
201         $borrowerslist = \@borrowers;
202     }
203 }
204
205 # get the borrower information.....
206 my $borrower;
207 my @lines;
208 if ($borrowernumber) {
209     $borrower = GetMemberDetails( $borrowernumber, 0 );
210     my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
211
212     # Warningdate is the date that the warning starts appearing
213     my ( $today_year,   $today_month,   $today_day )   = Today();
214     my ( $warning_year, $warning_month, $warning_day ) = split /-/,
215       $borrower->{'dateexpiry'};
216     my ( $enrol_year, $enrol_month, $enrol_day ) = split /-/,
217       $borrower->{'dateenrolled'};
218     # Renew day is calculated by adding the enrolment period to today
219     my ( $renew_year, $renew_month, $renew_day ) =
220       Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
221         0 , $borrower->{'enrolmentperiod'}) if ($enrol_year*$enrol_month*$enrol_day>0);
222     # if the expiry date is before today ie they have expired
223     if ( $warning_year*$warning_month*$warning_day==0 
224       || Date_to_Days( $today_year, $today_month, $today_day ) 
225          > Date_to_Days( $warning_year, $warning_month, $warning_day ) )
226     {
227         #borrowercard expired, no issues
228         $template->param(
229       flagged => "1",
230             noissues       => "1",
231             expired => format_date($borrower->{dateexpiry}),
232             renewaldate   => format_date("$renew_year-$renew_month-$renew_day")
233         );
234     }
235     # check for NotifyBorrowerDeparture
236   elsif ( C4::Context->preference('NotifyBorrowerDeparture') &&
237     Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
238     Date_to_Days( $today_year, $today_month, $today_day ) ) 
239   {
240     # borrower card soon to expire warn librarian
241     $template->param("warndeparture" => format_date($borrower->{dateexpiry}),
242       flagged       => "1",);
243     if ( C4::Context->preference('ReturnBeforeExpiry')){
244       $template->param("returnbeforeexpiry" => 1);
245     }
246   }
247     $template->param(
248         overduecount => $od,
249         issuecount   => $issue,
250         finetotal    => $fines
251     );
252 }
253
254 #
255 # STEP 3 : ISSUING
256 #
257 #
258 if ($barcode) {
259   # always check for blockers on issuing
260   my ( $error, $question ) =
261     CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
262   my $noerror = $invalidduedate ? 0 : 1;
263
264   delete $question->{'DEBT'} if ($debt_confirmed);
265   foreach my $impossible ( keys %$error ) {
266             $template->param(
267                 $impossible => $$error{$impossible},
268                 IMPOSSIBLE  => 1
269             );
270             $noerror = 0;
271         }
272     
273   if ($issueconfirmed && $noerror) {
274     # we have no blockers for issuing and any issues needing confirmation have been resolved
275         AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
276         $inprocess = 1;
277     }
278   elsif ($issueconfirmed){      # FIXME: Do something? Or is this to *intentionally* do nothing?
279   }
280   else {
281         my $noquestion = 1;
282 #         Get the item title for more information
283         my $getmessageiteminfo  = GetBiblioFromItemNumber(undef,$barcode);
284                 if ($noerror) {
285                         # only pass needsconfirmation to template if issuing is possible 
286                 foreach my $needsconfirmation ( keys %$question ) {
287                     $template->param(
288                         $needsconfirmation => $$question{$needsconfirmation},
289                         getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
290                         NEEDSCONFIRMATION  => 1
291                     );
292                     $noquestion = 0;
293                 }
294                         # Because of the weird conditional structure (empty elsif block),
295                         # if we reached here, $issueconfirmed must be false.
296                         # Also, since we moved inside the if ($noerror) conditional,
297                         # this old chunky conditional can be simplified:
298                     # if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
299                         if ($noquestion) {
300                                 AddIssue( $borrower, $barcode, $datedue );
301                                 $inprocess = 1;
302                         }
303             }
304                 $template->param(
305                          itemhomebranch => $getmessageiteminfo->{'homebranch'} ,                     
306                          duedatespec => $duedatespec,
307         );
308     }
309     
310 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue 
311         my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
312         $template->param(
313         issuecount   => $issue,
314         );
315 }
316
317 # reload the borrower info for the sake of reseting the flags.....
318 if ($borrowernumber) {
319     $borrower = GetMemberDetails( $borrowernumber, 0 );
320 }
321
322 ##################################################################################
323 # BUILD HTML
324 # show all reserves of this borrower, and the position of the reservation ....
325 my $borrowercategory;
326 my $category_type;
327 if ($borrowernumber) {
328
329     # new op dev
330     # now we show the status of the borrower's reservations
331     my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
332     my @reservloop;
333     my @WaitingReserveLoop;
334     
335     foreach my $num_res (@borrowerreserv) {
336         my %getreserv;
337         my %getWaitingReserveInfo;
338         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
339         my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itypes')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
340         my ( $transfertwhen, $transfertfrom, $transfertto ) =
341           GetTransfers( $num_res->{'itemnumber'} );
342
343         $getreserv{waiting}       = 0;
344         $getreserv{transfered}    = 0;
345         $getreserv{nottransfered} = 0;
346
347         $getreserv{reservedate}    = format_date( $num_res->{'reservedate'} );
348         $getreserv{title}          = $getiteminfo->{'title'};
349         $getreserv{itemtype}       = $itemtypeinfo->{'description'};
350         $getreserv{author}         = $getiteminfo->{'author'};
351         $getreserv{barcodereserv}  = $getiteminfo->{'barcode'};
352         $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
353         $getreserv{biblionumber}   = $getiteminfo->{'biblionumber'};
354         $getreserv{waitingat}    = GetBranchName( $num_res->{'branchcode'} );
355         #         check if we have a waiting status for reservations
356         if ( $num_res->{'found'} eq 'W' ) {
357             $getreserv{color}   = 'reserved';
358             $getreserv{waiting} = 1;
359 #     genarate information displaying only waiting reserves
360         $getWaitingReserveInfo{title}        = $getiteminfo->{'title'};
361         $getWaitingReserveInfo{biblionumber}   = $getiteminfo->{'biblionumber'};
362         $getWaitingReserveInfo{itemtype}    = $itemtypeinfo->{'description'};
363         $getWaitingReserveInfo{author}        = $getiteminfo->{'author'};
364         $getWaitingReserveInfo{reservedate}    = format_date( $num_res->{'reservedate'} );
365         $getWaitingReserveInfo{waitingat}    = GetBranchName( $num_res->{'branchcode'} );
366       if($num_res->{'branchcode'} eq $branch){ $getWaitingReserveInfo{waitinghere} = 1; }
367         }
368         #         check transfers with the itemnumber foud in th reservation loop
369         if ($transfertwhen) {
370             $getreserv{color}      = 'transfered';
371             $getreserv{transfered} = 1;
372             $getreserv{datesent}   = format_date($transfertwhen);
373             $getreserv{frombranch} = GetBranchName($transfertfrom);
374         }
375
376         if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
377             and not $transfertwhen )
378         {
379             $getreserv{nottransfered}   = 1;
380             $getreserv{nottransferedby} =
381               GetBranchName( $getiteminfo->{'holdingbranch'} );
382         }
383
384 #         if we don't have a reserv on item, we put the biblio infos and the waiting position
385         if ( $getiteminfo->{'title'} eq '' ) {
386             my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
387             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );  # fixme - we should have item-level reserves here ?
388             $getreserv{color}           = 'inwait';
389             $getreserv{title}           = $getbibinfo->{'title'};
390             $getreserv{nottransfered}   = 0;
391             $getreserv{itemtype}        = $getbibtype->{'description'};
392             $getreserv{author}          = $getbibinfo->{'author'};
393           $getreserv{biblionumber}    = $num_res->{'biblionumber'};
394         }
395         $getreserv{waitingposition} = $num_res->{'priority'};
396         push( @reservloop, \%getreserv );
397
398 #         if we have a reserve waiting, initiate waitingreserveloop
399         if ($getreserv{waiting} eq 1) {
400         push (@WaitingReserveLoop, \%getWaitingReserveInfo)
401         }
402       
403     }
404
405     # return result to the template
406     $template->param( 
407         countreserv => scalar @reservloop,
408         reservloop  => \@reservloop ,
409         WaitingReserveLoop  => \@WaitingReserveLoop,
410     );
411     $template->param( adultborrower => 1 ) if ( $borrower->{'category_type'} eq 'A' );
412 }
413
414 # make the issued books table.
415 my $todaysissues = '';
416 my $previssues   = '';
417 my @todaysissues;
418 my @previousissues;
419 my $allowborrow;
420 ## ADDED BY JF: new itemtype issuingrules counter stuff
421 my $issued_itemtypes_loop;
422 my $issued_itemtypes_count;
423 my $issued_itemtypes_allowed_count;    # hashref with total allowed by itemtype
424 my $issued_itemtypes_remaining;        # hashref with remaining
425 my $issued_itemtypes_flags;            #hashref that stores flags
426 my @issued_itemtypes_count_loop;
427
428 if ($borrower) {
429 # get each issue of the borrower & separate them in todayissues & previous issues
430     my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
431
432     # split in 2 arrays for today & previous
433     foreach my $it ( @$issueslist ) {
434         # set itemtype per item-level_itype syspref - FIXME this is an ugly hack
435         $it->{'itemtype'} = ( C4::Context->preference( 'item-level_itypes' ) ) ? $it->{'itype'} : $it->{'itemtype'};
436
437         ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
438             $it->{'itemnumber'}, $borrower->{'borrowernumber'}
439         );
440         $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
441         my ($can_renew, $can_renew_error) = CanBookBeRenewed( 
442             $borrower->{'borrowernumber'},$it->{'itemnumber'}
443         );
444         $it->{"renew_error_${can_renew_error}"} = 1 if defined $can_renew_error;
445         my ( $restype, $reserves ) = CheckReserves( $it->{'itemnumber'} );
446                 $it->{'can_renew'} = $can_renew;
447                 $it->{'can_confirm'} = !$can_renew && !$restype;
448                 $it->{'renew_error'} = $restype;
449
450         $it->{'dd'} = format_date($it->{'date_due'});
451         $it->{'od'} = ( $it->{'date_due'} lt $todaysdate ) ? 1 : 0 ;
452         ($it->{'author'} eq '') and $it->{'author'} = ' ';
453         $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
454         # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
455         $issued_itemtypes_count->{ $it->{'itemtype'} }++;
456
457         if ( $todaysdate eq $it->{'issuedate'} or $todaysdate eq $it->{'lastreneweddate'} ) {
458             push @todaysissues, $it;
459         } else {
460             push @previousissues, $it;
461         }
462     }
463     if ( C4::Context->preference( "todaysIssuesDefaultSortOrder" ) eq 'asc' ) {
464         @todaysissues   = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
465     }
466     else {
467         @todaysissues   = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
468     }
469     if ( C4::Context->preference( "previousIssuesDefaultSortOrder" ) eq 'asc' ){
470         @previousissues = sort { $a->{'date_due'} cmp $b->{'date_due'} } @previousissues;
471     }
472     else {
473         @previousissues = sort { $b->{'date_due'} cmp $a->{'date_due'} } @previousissues;
474     }
475     my $i = 1;
476     foreach my $book ( @todaysissues ) {
477         $book->{'togglecolor'} = ( ++$i % 2 ) ? 0 : 1 ;
478     }
479     $i = 1;
480     foreach my $book ( @previousissues ) {
481         $book->{'togglecolor'} = ( ++$i % 2 ) ? 0 : 1 ;
482     }
483 }
484
485 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
486 # FIXME: This should utilize all the issuingrules options rather than just the defaults
487 # and it should be moved to a module
488 my $dbh = C4::Context->dbh;
489
490 # how many of each is allowed?
491 my $issueqty_sth = $dbh->prepare( "
492 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
493 FROM issuingrules
494   LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
495   WHERE categorycode=?
496 " );
497 #my @issued_itemtypes_count;  # huh?
498 $issueqty_sth->execute("*");    # FIXME: Why have a WHERE clause at all with a hardcoded "*"?
499
500 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
501
502     # subtract how many of each this borrower has
503     $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };  
504     $data->{'left'}  =
505       ( $data->{'maxissueqty'} -
506           $issued_itemtypes_count->{ $data->{'description'} } );
507
508     # can't have a negative number of remaining
509     if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
510     $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
511     unless ( ( $data->{'maxissueqty'} < 1 )
512         || ( $data->{'itemtype'} eq "*" )
513         || ( $data->{'itemtype'} eq "CIRC" ) )
514     {
515         push @issued_itemtypes_count_loop, $data;
516     }
517 }
518 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
519
520 #### / JF
521
522 my @values;
523 my %labels;
524 my $CGIselectborrower;
525 if ($borrowerslist) {
526     foreach (
527         sort {(lc $a->{'surname'} cmp lc $b->{'surname'} ?lc $a->{'surname'} cmp lc $b->{'surname'}:lc $a->{'firstname'} cmp lc $b->{'firstname'})
528         } @$borrowerslist
529       )
530     {
531         push @values, $_->{'borrowernumber'};
532         $labels{ $_->{'borrowernumber'} } =
533 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'address'} ";
534     }
535     $CGIselectborrower = CGI::scrolling_list(
536         -name     => 'borrowernumber',
537         -class    => 'focus',
538         -id       => 'borrowernumber',
539         -values   => \@values,
540         -labels   => \%labels,
541         -size     => 7,
542         -tabindex => '',
543         -multiple => 0
544     );
545 }
546
547 #title
548 my $flags = $borrower->{'flags'};
549 my $flag;
550
551 foreach $flag ( sort keys %$flags ) {
552     $template->param( flagged=> 1);
553     $flags->{$flag}->{'message'} =~ s#\n#<br />#g;
554     if ( $flags->{$flag}->{'noissues'} ) {
555         $template->param(
556             flagged  => 1,
557             noissues => 'true',
558         );
559         if ( $flag eq 'GNA' ) {
560             $template->param( gna => 'true' );
561         }
562         if ( $flag eq 'LOST' ) {
563             $template->param( lost => 'true' );
564         }
565         if ( $flag eq 'DBARRED' ) {
566             $template->param( dbarred => 'true' );
567         }
568         if ( $flag eq 'CHARGES' ) {
569             $template->param(
570                 charges    => 'true',
571                 chargesmsg => $flags->{'CHARGES'}->{'message'},
572                 charges_is_blocker => 1
573             );
574         }
575         if ( $flag eq 'CREDITS' ) {
576             $template->param(
577                 credits    => 'true',
578                 creditsmsg => $flags->{'CREDITS'}->{'message'}
579             );
580         }
581     }
582     else {
583         if ( $flag eq 'CHARGES' ) {
584             $template->param(
585                 charges    => 'true',
586                 flagged    => 1,
587                 chargesmsg => $flags->{'CHARGES'}->{'message'}
588             );
589         }
590         if ( $flag eq 'CREDITS' ) {
591             $template->param(
592                 credits    => 'true',
593                 creditsmsg => $flags->{'CREDITS'}->{'message'}
594             );
595         }
596         if ( $flag eq 'ODUES' ) {
597             $template->param(
598                 odues    => 'true',
599                 flagged  => 1,
600                 oduesmsg => $flags->{'ODUES'}->{'message'}
601             );
602
603             my $items = $flags->{$flag}->{'itemlist'};
604 # useless ???
605 #             {
606 #                 my @itemswaiting;
607 #                 foreach my $item (@$items) {
608 #                     my ($iteminformation) =
609 #                         getiteminformation( $item->{'itemnumber'}, 0 );
610 #                     push @itemswaiting, $iteminformation;
611 #                 }
612 #             }
613             if ( $query->param('module') ne 'returns' ) {
614                 $template->param( nonreturns => 'true' );
615             }
616         }
617         if ( $flag eq 'NOTES' ) {
618             $template->param(
619                 notes    => 'true',
620                 flagged  => 1,
621                 notesmsg => $flags->{'NOTES'}->{'message'}
622             );
623         }
624     }
625 }
626
627 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
628 my @temp = split( /\$/, $amountold );
629
630     if ( $borrower->{'category_type'} eq 'C') {
631         my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
632         my $cnt = scalar(@$catcodes);
633         $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
634         $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
635     }
636
637 my $CGIorganisations;
638 my $member_of_institution;
639 if ( C4::Context->preference("memberofinstitution") ) {
640     my $organisations = get_institutions();
641     my @orgs;
642     my %org_labels;
643     foreach my $organisation ( keys %$organisations ) {
644         push @orgs, $organisation;
645         $org_labels{$organisation} =
646           $organisations->{$organisation}->{'surname'};
647     }
648     $member_of_institution = 1;
649     $CGIorganisations      = CGI::popup_menu(
650         -id     => 'organisations',
651         -name   => 'organisations',
652         -labels => \%org_labels,
653         -values => \@orgs,
654     );
655 }
656
657 $amountold = $temp[1];
658
659
660 $template->param(
661     issued_itemtypes_count_loop => $issued_itemtypes_loop,
662     findborrower                => $findborrower,
663     borrower                    => $borrower,
664     borrowernumber              => $borrowernumber,
665     branch                      => $branch,
666     branchname                  => GetBranchName($borrower->{'branchcode'}),
667     printer                     => $printer,
668     printername                 => $printer,
669     firstname                   => $borrower->{'firstname'},
670     surname                     => $borrower->{'surname'},
671     dateexpiry        => format_date($newexpiry),
672     expiry            => format_date($borrower->{'dateexpiry'}),
673     categorycode      => $borrower->{'categorycode'},
674     categoryname      => $borrower->{description},
675     address           => $borrower->{'address'},
676     address2          => $borrower->{'address2'},
677     email             => $borrower->{'email'},
678     emailpro          => $borrower->{'emailpro'},
679     borrowernotes     => $borrower->{'borrowernotes'},
680     city              => $borrower->{'city'},
681     zipcode           => $borrower->{'zipcode'},
682     phone             => $borrower->{'phone'} || $borrower->{'mobile'},
683     cardnumber        => $borrower->{'cardnumber'},
684     amountold         => $amountold,
685     barcode           => $barcode,
686     stickyduedate     => $stickyduedate,
687     message           => $message,
688     CGIselectborrower => $CGIselectborrower,
689     todayissues       => \@todaysissues,
690     previssues        => \@previousissues,
691     inprocess         => $inprocess,
692     memberofinstution => $member_of_institution,
693     CGIorganisations  => $CGIorganisations,
694         is_child        => ($borrower->{'category_type'} eq 'C'),
695   circview => 1,
696   
697 );
698
699 # set return date if stickyduedate
700 if ($stickyduedate) {
701     $template->param(
702         duedatespec => $duedatespec,
703     );
704 }
705
706 #if ($branchcookie) {
707 #$cookie=[$cookie, $branchcookie, $printercookie];
708 #}
709
710 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
711 $template->param( picture => 1 ) if $picture;
712
713
714 $template->param(
715     debt_confirmed            => $debt_confirmed,
716     SpecifyDueDate            => C4::Context->preference("SpecifyDueDate"),
717     CircAutocompl             => C4::Context->preference("CircAutocompl"),
718         AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
719     dateformat                => C4::Context->preference("dateformat"),
720     DHTMLcalendar_dateformat  => C4::Dates->DHTMLcalendar(),
721 );
722 output_html_with_http_headers $query, $cookie, $template->output;