fix for 2181: Late Items in Red
[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 use Date::Manip qw(
48   Date_Cmp
49 );
50
51
52 #
53 # PARAMETERS READING
54 #
55 my $query = new CGI;
56
57 # new op dev the branch and the printer are now defined by the userenv
58 # but first we have to check if someone has tried to change them
59
60 my $branch = $query->param('branch');
61 if ($branch){
62     # update our session so the userenv is updated
63     my $dbh=C4::Context->dbh;
64     my $sessionID = $query->cookie("CGISESSID") ;
65     my $session = get_session($sessionID);
66     $session->param('branch',$branch);
67     my $branchname = GetBranchName($branch);
68     $session->param('branchname',$branchname);
69 }
70
71 my $printer = $query->param('printer');
72 if ($printer){
73     # update our session so the userenv is updated
74   my $dbh=C4::Context->dbh;
75   my $sessionID = $query->cookie("CGISESSID") ;
76   my $session = get_session($sessionID);
77   $session->param('branchprinter',$printer);
78
79 }
80 if (!C4::Context->userenv && !$branch){
81   my $sessionID = $query->cookie("CGISESSID") ;
82   my $session = get_session($sessionID);
83   if ($session->param('branch') eq 'NO_LIBRARY_SET'){
84     # no branch set we can't issue
85     print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
86     exit;
87   }
88 }
89
90 my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
91     {
92         template_name   => 'circ/circulation.tmpl',
93         query           => $query,
94         type            => "intranet",
95         authnotrequired => 0,
96         flagsrequired   => { circulate => 1 },
97     }
98 );
99 my $branches = GetBranches();
100
101 my $printers = GetPrinters();
102
103 my @failedrenews = $query->param('failedrenew');
104 my @renew_failed;
105 for (@failedrenews) { $renew_failed[$_] = 1; } 
106
107 my $findborrower = $query->param('findborrower');
108 $findborrower =~ s|,| |g;
109 #$findborrower =~ s|'| |g;
110 my $borrowernumber = $query->param('borrowernumber');
111
112 $branch  = C4::Context->userenv->{'branch'};  
113 $printer = C4::Context->userenv->{'branchprinter'};
114
115
116 # If Autolocated is not activated, we show the Circulation Parameters to chage settings of librarian
117     if (C4::Context->preference("AutoLocation") ne 1)
118         {
119             $template->param(
120             ManualLocation => 1,
121             );
122         }
123
124 my $barcode        = $query->param('barcode') || '';
125
126 $barcode = barcodedecode($barcode) if( $barcode && C4::Context->preference('itemBarcodeInputFilter'));
127 my $year           = $query->param('year');
128 my $month          = $query->param('month');
129 my $day            = $query->param('day');
130 my $stickyduedate  = $query->param('stickyduedate');
131 my $duedatespec    = $query->param('duedatespec');
132 my $issueconfirmed = $query->param('issueconfirmed');
133 my $cancelreserve  = $query->param('cancelreserve');
134 my $organisation   = $query->param('organisations');
135 my $print          = $query->param('print');
136 my $newexpiry      = $query->param('dateexpiry');
137
138 #set up cookie.....
139 # my $branchcookie;
140 # my $printercookie;
141 # if ($query->param('setcookies')) {
142 #     $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
143 #     $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
144 # }
145 #
146
147 my ($datedue,$invalidduedate);
148 if ($duedatespec) {
149         if ($duedatespec =~ C4::Dates->regexp('syspref')) {
150                 my $tempdate = C4::Dates->new($duedatespec);
151                 if ($tempdate and $tempdate->output('iso') gt C4::Dates->new()->output('iso')) {
152                         # i.e., it has to be later than today/now
153                         $datedue = $tempdate;
154                 } else {
155                         $invalidduedate = 1;
156                         $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
157                 }
158         } else {
159                 $invalidduedate = 1;
160                 $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
161         }
162 }
163
164 my $todaysdate = C4::Dates->new->output('iso');
165
166 # check and see if we should print
167 if ( $barcode eq '' && $print eq 'maybe' ) {
168     $print = 'yes';
169 }
170
171 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
172
173 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
174     $template->param(
175         PAYCHARGES     => 'yes',
176         borrowernumber => $borrowernumber
177     );
178 }
179
180 if ( $print eq 'yes' && $borrowernumber ne '' ) {
181     printslip( $borrowernumber );
182     $query->param( 'borrowernumber', '' );
183     $borrowernumber = '';
184 }
185
186 #
187 # STEP 2 : FIND BORROWER
188 # if there is a list of find borrowers....
189 #
190 my $borrowerslist;
191 my $message;
192 if ($findborrower) {
193     my ( $count, $borrowers ) =
194       SearchMember($findborrower, 'cardnumber', 'web' );
195     my @borrowers = @$borrowers;
196     if ( $#borrowers == -1 ) {
197         $query->param( 'findborrower', '' );
198         $message = "'$findborrower'";
199     }
200     elsif ( $#borrowers == 0 ) {
201         $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
202         $query->param( 'barcode',           '' );
203         $borrowernumber = $borrowers[0]->{'borrowernumber'};
204     }
205     else {
206         $borrowerslist = \@borrowers;
207     }
208 }
209
210 # get the borrower information.....
211 my $borrower;
212 my @lines;
213 if ($borrowernumber) {
214     $borrower = GetMemberDetails( $borrowernumber, 0 );
215     my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
216
217     # Warningdate is the date that the warning starts appearing
218     my ( $today_year,   $today_month,   $today_day )   = Today();
219     my ( $warning_year, $warning_month, $warning_day ) = split /-/,
220       $borrower->{'dateexpiry'};
221     my ( $enrol_year, $enrol_month, $enrol_day ) = split /-/,
222       $borrower->{'dateenrolled'};
223     # Renew day is calculated by adding the enrolment period to today
224     my ( $renew_year, $renew_month, $renew_day ) =
225       Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
226         0 , $borrower->{'enrolmentperiod'}) if ($enrol_year*$enrol_month*$enrol_day>0);
227     # if the expiry date is before today ie they have expired
228     if ( $warning_year*$warning_month*$warning_day==0 
229       || Date_to_Days( $today_year, $today_month, $today_day ) 
230          > Date_to_Days( $warning_year, $warning_month, $warning_day ) )
231     {
232         #borrowercard expired, no issues
233         $template->param(
234       flagged => "1",
235             noissues       => "1",
236             expired => format_date($borrower->{dateexpiry}),
237             renewaldate   => format_date("$renew_year-$renew_month-$renew_day")
238         );
239     }
240     # check for NotifyBorrowerDeparture
241   elsif ( C4::Context->preference('NotifyBorrowerDeparture') &&
242     Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
243     Date_to_Days( $today_year, $today_month, $today_day ) ) 
244   {
245     # borrower card soon to expire warn librarian
246     $template->param("warndeparture" => format_date($borrower->{dateexpiry}),
247       flagged       => "1",);
248     if ( C4::Context->preference('ReturnBeforeExpiry')){
249       $template->param("returnbeforeexpiry" => 1);
250     }
251   }
252     $template->param(
253         overduecount => $od,
254         issuecount   => $issue,
255         finetotal    => $fines
256     );
257 }
258
259 #
260 # STEP 3 : ISSUING
261 #
262 #
263 if ($barcode) {
264   # always check for blockers on issuing
265   my ( $error, $question ) =
266     CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
267   my $noerror = $invalidduedate ? 0 : 1;
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
433 # get each issue of the borrower & separate them in todayissues & previous issues
434     my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
435
436     # split in 2 arrays for today & previous
437     foreach my $it ( @$issueslist ) {
438         # set itemtype per item-level_itype syspref - FIXME this is an ugly hack
439         $it->{'itemtype'} = (C4::Context->preference('item-level_itypes')) ? $it->{'itype'} : $it->{'itemtype'};
440
441         my $issuedate = $it->{'issuedate'};
442         $issuedate =~ s/-//g;
443         $issuedate = substr( $issuedate, 0, 8 );
444     ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
445             $it->{'itemnumber'}, $borrower->{'borrowernumber'}
446     );
447     $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
448         my $can_renew_error;
449         ($it->{'can_renew'}, $can_renew_error) = CanBookBeRenewed( 
450             $borrower->{'borrowernumber'},$it->{'itemnumber'}
451     );
452         $it->{"renew_error_${can_renew_error}"} = 1 if defined $can_renew_error;
453     my ($restype, $reserves) = CheckReserves($it->{'itemnumber'});
454     ($restype) and $it->{'can_renew'} = 0;
455
456     $it->{'dd'} = format_date($it->{'date_due'});
457     $it->{'od'} = Date_Cmp($todaysdate, $it->{'date_due'}) == 1 ? 1 : 0 ;
458         ($it->{'author'} eq '') and $it->{'author'} = ' ';
459         $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
460         # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
461         $issued_itemtypes_count->{ $it->{'itemtype'} }++;
462
463         if ( $todaysdate == $issuedate ) {
464             push @todaysissues, $it;
465         } else {
466             push @previousissues, $it;
467         }
468     }
469   if (C4::Context->preference("todaysIssuesDefaultSortOrder") eq 'asc'){
470     @todaysissues   = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
471   }
472   else {
473     @todaysissues   = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
474   }
475   if (C4::Context->preference("previousIssuesDefaultSortOrder") eq 'asc'){
476     @previousissues = sort { $a->{'date_due' } cmp $b->{'date_due' } } @previousissues;
477   }
478   else {
479     @previousissues = sort { $b->{'date_due' } cmp $a->{'date_due' } } @previousissues;
480   }
481     my $i = 1;
482   foreach my $book (@todaysissues) {
483         $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
484     }
485     $i = 1;
486   foreach my $book (@previousissues) {
487         $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
488     }
489 }
490
491 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
492 # FIXME: This should utilize all the issuingrules options rather than just the defaults
493 # and it should be moved to a module
494 my $dbh = C4::Context->dbh;
495
496 # how many of each is allowed?
497 my $issueqty_sth = $dbh->prepare( "
498 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
499 FROM issuingrules
500   LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
501   WHERE categorycode=?
502 " );
503 #my @issued_itemtypes_count;  # huh?
504 $issueqty_sth->execute("*");    # FIXME: Why have a WHERE clause at all with a hardcoded "*"?
505
506 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
507
508     # subtract how many of each this borrower has
509     $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };  
510     $data->{'left'}  =
511       ( $data->{'maxissueqty'} -
512           $issued_itemtypes_count->{ $data->{'description'} } );
513
514     # can't have a negative number of remaining
515     if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
516     $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
517     unless ( ( $data->{'maxissueqty'} < 1 )
518         || ( $data->{'itemtype'} eq "*" )
519         || ( $data->{'itemtype'} eq "CIRC" ) )
520     {
521         push @issued_itemtypes_count_loop, $data;
522     }
523 }
524 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
525
526 #### / JF
527
528 my @values;
529 my %labels;
530 my $CGIselectborrower;
531 if ($borrowerslist) {
532     foreach (
533         sort {
534                 lc $a->{'surname'}
535               . lc $a->{'firstname'} cmp lc $b->{'surname'}
536               . lc $b->{'firstname'}
537         } @$borrowerslist
538       )
539     {
540         push @values, $_->{'borrowernumber'};
541         $labels{ $_->{'borrowernumber'} } =
542 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'address'} ";
543     }
544     $CGIselectborrower = CGI::scrolling_list(
545         -name     => 'borrowernumber',
546         -class    => 'focus',
547         -id       => 'borrowernumber',
548         -values   => \@values,
549         -labels   => \%labels,
550         -size     => 7,
551         -tabindex => '',
552         -multiple => 0
553     );
554 }
555
556 #title
557 my $flags = $borrower->{'flags'};
558 my $flag;
559
560 foreach $flag ( sort keys %$flags ) {
561     $template->param( flagged=> 1);
562     $flags->{$flag}->{'message'} =~ s#\n#<br />#g;
563     if ( $flags->{$flag}->{'noissues'} ) {
564         $template->param(
565             flagged  => 1,
566             noissues => 'true',
567         );
568         if ( $flag eq 'GNA' ) {
569             $template->param( gna => 'true' );
570         }
571         if ( $flag eq 'LOST' ) {
572             $template->param( lost => 'true' );
573         }
574         if ( $flag eq 'DBARRED' ) {
575             $template->param( dbarred => 'true' );
576         }
577         if ( $flag eq 'CHARGES' ) {
578             $template->param(
579                 charges    => 'true',
580                 chargesmsg => $flags->{'CHARGES'}->{'message'},
581                 charges_is_blocker => 1
582             );
583         }
584         if ( $flag eq 'CREDITS' ) {
585             $template->param(
586                 credits    => 'true',
587                 creditsmsg => $flags->{'CREDITS'}->{'message'}
588             );
589         }
590     }
591     else {
592         if ( $flag eq 'CHARGES' ) {
593             $template->param(
594                 charges    => 'true',
595                 flagged    => 1,
596                 chargesmsg => $flags->{'CHARGES'}->{'message'}
597             );
598         }
599         if ( $flag eq 'CREDITS' ) {
600             $template->param(
601                 credits    => 'true',
602                 creditsmsg => $flags->{'CREDITS'}->{'message'}
603             );
604         }
605         if ( $flag eq 'ODUES' ) {
606             $template->param(
607                 odues    => 'true',
608                 flagged  => 1,
609                 oduesmsg => $flags->{'ODUES'}->{'message'}
610             );
611
612             my $items = $flags->{$flag}->{'itemlist'};
613 # useless ???
614 #             {
615 #                 my @itemswaiting;
616 #                 foreach my $item (@$items) {
617 #                     my ($iteminformation) =
618 #                         getiteminformation( $item->{'itemnumber'}, 0 );
619 #                     push @itemswaiting, $iteminformation;
620 #                 }
621 #             }
622             if ( $query->param('module') ne 'returns' ) {
623                 $template->param( nonreturns => 'true' );
624             }
625         }
626         if ( $flag eq 'NOTES' ) {
627             $template->param(
628                 notes    => 'true',
629                 flagged  => 1,
630                 notesmsg => $flags->{'NOTES'}->{'message'}
631             );
632         }
633     }
634 }
635
636 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
637 my @temp = split( /\$/, $amountold );
638
639     if ( $borrower->{'category_type'} eq 'C') {
640         my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
641         my $cnt = scalar(@$catcodes);
642         $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
643         $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
644     }
645
646 my $CGIorganisations;
647 my $member_of_institution;
648 if ( C4::Context->preference("memberofinstitution") ) {
649     my $organisations = get_institutions();
650     my @orgs;
651     my %org_labels;
652     foreach my $organisation ( keys %$organisations ) {
653         push @orgs, $organisation;
654         $org_labels{$organisation} =
655           $organisations->{$organisation}->{'surname'};
656     }
657     $member_of_institution = 1;
658     $CGIorganisations      = CGI::popup_menu(
659         -id     => 'organisations',
660         -name   => 'organisations',
661         -labels => \%org_labels,
662         -values => \@orgs,
663     );
664 }
665
666 $amountold = $temp[1];
667
668
669 $template->param(
670     issued_itemtypes_count_loop => $issued_itemtypes_loop,
671     findborrower                => $findborrower,
672     borrower                    => $borrower,
673     borrowernumber              => $borrowernumber,
674     branch                      => $branch,
675     branchname                  => GetBranchName($borrower->{'branchcode'}),
676     printer                     => $printer,
677     printername                 => $printer,
678     firstname                   => $borrower->{'firstname'},
679     surname                     => $borrower->{'surname'},
680     dateexpiry        => format_date($newexpiry),
681     expiry            => format_date($borrower->{'dateexpiry'}),
682     categorycode      => $borrower->{'categorycode'},
683     categoryname      => $borrowercategory->{description},
684     address           => $borrower->{'address'},
685     address2          => $borrower->{'address2'},
686     email             => $borrower->{'email'},
687     emailpro          => $borrower->{'emailpro'},
688     borrowernotes     => $borrower->{'borrowernotes'},
689     city              => $borrower->{'city'},
690     phone             => $borrower->{'phone'} || $borrower->{'mobile'},
691     cardnumber        => $borrower->{'cardnumber'},
692     amountold         => $amountold,
693     barcode           => $barcode,
694     stickyduedate     => $stickyduedate,
695     message           => $message,
696     CGIselectborrower => $CGIselectborrower,
697     todayissues       => \@todaysissues,
698     previssues        => \@previousissues,
699     inprocess         => $inprocess,
700     memberofinstution => $member_of_institution,
701     CGIorganisations  => $CGIorganisations,
702         is_child        => ($borrower->{'category_type'} eq 'C'),
703   circview => 1,
704   
705 );
706
707 # set return date if stickyduedate
708 if ($stickyduedate) {
709     $template->param(
710         duedatespec => $duedatespec,
711     );
712 }
713
714 #if ($branchcookie) {
715 #$cookie=[$cookie, $branchcookie, $printercookie];
716 #}
717
718 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
719 $template->param( picture => 1 ) if $picture;
720
721
722 $template->param(
723     SpecifyDueDate           => C4::Context->preference("SpecifyDueDate"),
724     CircAutocompl            => C4::Context->preference("CircAutocompl"),
725         dateformat            => C4::Context->preference("dateformat"),
726     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
727 );
728 output_html_with_http_headers $query, $cookie, $template->output;