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