Enabling listing of category codes when "new patron" button is displayed.
[koha.git] / circ / circulation.pl
1 #!/usr/bin/perl
2
3 # written 8/5/2002 by Finlay
4 # script to execute issuing of books
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 use strict;
24 # use warnings;  # FIXME
25 use CGI;
26 use C4::Output;
27 use C4::Print;
28 use C4::Auth qw/:DEFAULT get_session/;
29 use C4::Dates qw/format_date/;
30 use C4::Branch; # GetBranches
31 use C4::Koha;   # GetPrinter
32 use C4::Circulation;
33 use C4::Members;
34 use C4::Biblio;
35 use C4::Reserves;
36 use C4::Context;
37 use CGI::Session;
38
39 use Date::Calc qw(
40   Today
41   Add_Delta_YM
42   Add_Delta_Days
43   Date_to_Days
44 );
45
46
47 #
48 # PARAMETERS READING
49 #
50 my $query = new CGI;
51
52 # new op dev the branch and the printer are now defined by the userenv
53 # but first we have to check if someone has tried to change them
54
55 my $branch = $query->param('branch');
56 if ($branch){
57     # update our session so the userenv is updated
58     my $sessionID = $query->cookie("CGISESSID") ;
59     my $session = get_session($sessionID);
60     $session->param('branch',$branch);
61     my $branchname = GetBranchName($branch);
62     $session->param('branchname',$branchname);
63 }
64
65 my $printer = $query->param('printer');
66 if ($printer){
67     # update our session so the userenv is updated
68   my $sessionID = $query->cookie("CGISESSID") ;
69   my $session = get_session($sessionID);
70   $session->param('branchprinter',$printer);
71
72 }
73 if (!C4::Context->userenv && !$branch){
74   my $sessionID = $query->cookie("CGISESSID") ;
75   my $session = get_session($sessionID);
76   if ($session->param('branch') eq 'NO_LIBRARY_SET'){
77     # no branch set we can't issue
78     print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
79     exit;
80   }
81 }
82
83 my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
84     {
85         template_name   => 'circ/circulation.tmpl',
86         query           => $query,
87         type            => "intranet",
88         authnotrequired => 0,
89         flagsrequired   => { circulate => 'circulate_remaining_permissions' },
90     }
91 );
92
93 my $branches = GetBranches();
94 my $printers = GetPrinters();
95
96 my @failedrenews = $query->param('failedrenew');
97 my @renew_failed;
98 for (@failedrenews) { $renew_failed[$_] = 1; } 
99
100 my $findborrower = $query->param('findborrower');
101 $findborrower =~ s|,| |g;
102 #$findborrower =~ s|'| |g;
103 my $borrowernumber = $query->param('borrowernumber');
104
105 $branch  = C4::Context->userenv->{'branch'};  
106 $printer = C4::Context->userenv->{'branchprinter'};
107
108
109 # If Autolocated is not activated, we show the Circulation Parameters to chage settings of librarian
110 if (C4::Context->preference("AutoLocation") ne 1) { # FIXME: string comparison to number
111     $template->param(ManualLocation => 1);
112 }
113
114 my $barcode        = $query->param('barcode') || '';
115
116 $barcode = barcodedecode($barcode) if( $barcode && C4::Context->preference('itemBarcodeInputFilter'));
117 my $stickyduedate  = $query->param('stickyduedate');
118 my $duedatespec    = $query->param('duedatespec');
119 my $issueconfirmed = $query->param('issueconfirmed');
120 my $cancelreserve  = $query->param('cancelreserve');
121 my $organisation   = $query->param('organisations');
122 my $print          = $query->param('print');
123 my $newexpiry      = $query->param('dateexpiry');
124 my $debt_confirmed = $query->param('debt_confirmed') || 0; # Don't show the debt error dialog twice
125
126 #set up cookie.....
127 # my $branchcookie;
128 # my $printercookie;
129 # if ($query->param('setcookies')) {
130 #     $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
131 #     $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
132 # }
133 #
134
135 my ($datedue,$invalidduedate,$globalduedate);
136
137 if(C4::Context->preference('globalDueDate') && (C4::Context->preference('globalDueDate') =~ C4::Dates->regexp('syspref'))){
138         $globalduedate = C4::Dates->new(C4::Context->preference('globalDueDate'));
139 }
140 my $duedatespec_allow = C4::Context->preference('SpecifyDueDate');
141 if($duedatespec_allow){
142     if ($duedatespec) {
143         if ($duedatespec =~ C4::Dates->regexp('syspref')) {
144                 my $tempdate = C4::Dates->new($duedatespec);
145                 if ($tempdate and $tempdate->output('iso') gt C4::Dates->new()->output('iso')) {
146                         # i.e., it has to be later than today/now
147                         $datedue = $tempdate;
148                 } else {
149                         $invalidduedate = 1;
150                         $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
151                 }
152         } else {
153                 $invalidduedate = 1;
154                 $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
155         }
156     } else {
157         # pass global due date to tmpl if specifyduedate is true 
158         # and we have no barcode (loading circ page but not checking out)
159         if($globalduedate &&  ! $barcode ){
160             $duedatespec = $globalduedate->output();
161             $stickyduedate = 1;
162         }
163     }
164 } else {
165     $datedue = $globalduedate if($globalduedate);
166 }
167
168 my $todaysdate = C4::Dates->new->output('iso');
169
170 # check and see if we should print
171 if ( $barcode eq '' && $print eq 'maybe' ) {
172     $print = 'yes';
173 }
174
175 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
176 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
177     $template->param(
178         PAYCHARGES     => 'yes',
179         borrowernumber => $borrowernumber
180     );
181 }
182
183 if ( $print eq 'yes' && $borrowernumber ne '' ) {
184     printslip( $borrowernumber );
185     $query->param( 'borrowernumber', '' );
186     $borrowernumber = '';
187 }
188
189 #
190 # STEP 2 : FIND BORROWER
191 # if there is a list of find borrowers....
192 #
193 my $borrowerslist;
194 my $message;
195 if ($findborrower) {
196     my ( $count, $borrowers ) =
197       SearchMember($findborrower, 'cardnumber', 'web' );
198     my @borrowers = @$borrowers;
199         $template->param(
200                 "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
201         );
202         if (C4::Context->preference("AddPatronLists")=~/code/){
203                 my $categories=GetBorrowercategoryList;
204                 $categories->[0]->{'first'}=1;
205                 $template->param(categories=>$categories);
206         }
207     if ( $#borrowers == -1 ) {
208         $query->param( 'findborrower', '' );
209         $message = "'$findborrower'";
210     }
211     elsif ( $#borrowers == 0 ) {
212         $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
213         $query->param( 'barcode',           '' );
214         $borrowernumber = $borrowers[0]->{'borrowernumber'};
215     }
216     else {
217         $borrowerslist = \@borrowers;
218     }
219 }
220
221 # get the borrower information.....
222 my $borrower;
223 my @lines;
224 if ($borrowernumber) {
225     $borrower = GetMemberDetails( $borrowernumber, 0 );
226     my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
227
228     # Warningdate is the date that the warning starts appearing
229     my ( $today_year,   $today_month,   $today_day )   = Today();
230     my ( $warning_year, $warning_month, $warning_day ) = split /-/,
231       $borrower->{'dateexpiry'};
232     my ( $enrol_year, $enrol_month, $enrol_day ) = split /-/,
233       $borrower->{'dateenrolled'};
234     # Renew day is calculated by adding the enrolment period to today
235     my ( $renew_year, $renew_month, $renew_day ) =
236       Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
237         0 , $borrower->{'enrolmentperiod'}) if ($enrol_year*$enrol_month*$enrol_day>0);
238     # if the expiry date is before today ie they have expired
239     if ( $warning_year*$warning_month*$warning_day==0 
240       || Date_to_Days( $today_year, $today_month, $today_day ) 
241          > Date_to_Days( $warning_year, $warning_month, $warning_day ) )
242     {
243         #borrowercard expired, no issues
244         $template->param(
245       flagged => "1",
246             noissues       => "1",
247             expired => format_date($borrower->{dateexpiry}),
248             renewaldate   => format_date("$renew_year-$renew_month-$renew_day")
249         );
250     }
251     # check for NotifyBorrowerDeparture
252   elsif ( C4::Context->preference('NotifyBorrowerDeparture') &&
253     Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
254     Date_to_Days( $today_year, $today_month, $today_day ) ) 
255   {
256     # borrower card soon to expire warn librarian
257     $template->param("warndeparture" => format_date($borrower->{dateexpiry}),
258       flagged       => "1",);
259     if ( C4::Context->preference('ReturnBeforeExpiry')){
260       $template->param("returnbeforeexpiry" => 1);
261     }
262   }
263     $template->param(
264         overduecount => $od,
265         issuecount   => $issue,
266         finetotal    => $fines
267     );
268 }
269
270 #
271 # STEP 3 : ISSUING
272 #
273 #
274 if ($barcode) {
275   # always check for blockers on issuing
276   my ( $error, $question ) =
277     CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
278   my $blocker = $invalidduedate ? 1 : 0;
279
280   delete $question->{'DEBT'} if ($debt_confirmed);
281   foreach my $impossible ( keys %$error ) {
282             $template->param(
283                 $impossible => $$error{$impossible},
284                 IMPOSSIBLE  => 1
285             );
286             $blocker = 1;
287         }
288     if( !$blocker ){
289         my $confirm_required = 0;
290         unless($issueconfirmed){
291             #  Get the item title for more information
292             my $getmessageiteminfo  = GetBiblioFromItemNumber(undef,$barcode);
293                     $template->param( itemhomebranch => $getmessageiteminfo->{'homebranch'} );
294
295                     # pass needsconfirmation to template if issuing is possible and user hasn't yet confirmed.
296             foreach my $needsconfirmation ( keys %$question ) {
297                 $template->param(
298                     $needsconfirmation => $$question{$needsconfirmation},
299                     getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
300                     NEEDSCONFIRMATION  => 1
301                 );
302                 $confirm_required = 1;
303             }
304                 }
305         unless($confirm_required) {
306             AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
307                         $inprocess = 1;
308             if($globalduedate && ! $stickyduedate && $duedatespec_allow ){
309                 $duedatespec = $globalduedate->output();
310                 $stickyduedate = 1;
311             }
312                 }
313     }
314     
315     # FIXME If the issue is confirmed, we launch another time GetMemberIssuesAndFines, now display the issue count after issue 
316     my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
317     $template->param( issuecount   => $issue );
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{nottransfered}   = 0;
394             $getreserv{itemtype}        = $getbibtype->{'description'};
395             $getreserv{author}          = $getbibinfo->{'author'};
396           $getreserv{biblionumber}    = $num_res->{'biblionumber'};
397         }
398         $getreserv{waitingposition} = $num_res->{'priority'};
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 ($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, $can_renew_error) = CanBookBeRenewed( 
445             $borrower->{'borrowernumber'},$it->{'itemnumber'}
446         );
447         $it->{"renew_error_${can_renew_error}"} = 1 if defined $can_renew_error;
448         my ( $restype, $reserves ) = CheckReserves( $it->{'itemnumber'} );
449                 $it->{'can_renew'} = $can_renew;
450                 $it->{'can_confirm'} = !$can_renew && !$restype;
451                 $it->{'renew_error'} = $restype;
452
453         $it->{'dd'} = format_date($it->{'date_due'});
454         $it->{'od'} = ( $it->{'date_due'} lt $todaysdate ) ? 1 : 0 ;
455         ($it->{'author'} eq '') and $it->{'author'} = ' ';
456         $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
457         # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
458         $issued_itemtypes_count->{ $it->{'itemtype'} }++;
459
460         if ( $todaysdate eq $it->{'issuedate'} or $todaysdate eq $it->{'lastreneweddate'} ) {
461             push @todaysissues, $it;
462         } else {
463             push @previousissues, $it;
464         }
465     }
466     if ( C4::Context->preference( "todaysIssuesDefaultSortOrder" ) eq 'asc' ) {
467         @todaysissues   = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
468     }
469     else {
470         @todaysissues   = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
471     }
472     if ( C4::Context->preference( "previousIssuesDefaultSortOrder" ) eq 'asc' ){
473         @previousissues = sort { $a->{'date_due'} cmp $b->{'date_due'} } @previousissues;
474     }
475     else {
476         @previousissues = sort { $b->{'date_due'} cmp $a->{'date_due'} } @previousissues;
477     }
478 }
479
480 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
481 # FIXME: This should utilize all the issuingrules options rather than just the defaults
482 # and it should be moved to a module
483 my $dbh = C4::Context->dbh;
484
485 # how many of each is allowed?
486 my $issueqty_sth = $dbh->prepare( "
487 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
488 FROM issuingrules
489   LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
490   WHERE categorycode=?
491 " );
492 #my @issued_itemtypes_count;  # huh?
493 $issueqty_sth->execute("*");    # This is a literal asterisk, not a wildcard.
494
495 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
496
497     # subtract how many of each this borrower has
498     $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };  
499     $data->{'left'}  =
500       ( $data->{'maxissueqty'} -
501           $issued_itemtypes_count->{ $data->{'description'} } );
502
503     # can't have a negative number of remaining
504     if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
505     $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
506     unless ( ( $data->{'maxissueqty'} < 1 )
507         || ( $data->{'itemtype'} eq "*" )
508         || ( $data->{'itemtype'} eq "CIRC" ) )
509     {
510         push @issued_itemtypes_count_loop, $data;
511     }
512 }
513 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
514
515 #### / JF
516
517 my @values;
518 my %labels;
519 my $CGIselectborrower;
520 if ($borrowerslist) {
521     foreach (
522         sort {(lc $a->{'surname'} cmp lc $b->{'surname'} || lc $a->{'firstname'} cmp lc $b->{'firstname'})
523         } @$borrowerslist
524       )
525     {
526         push @values, $_->{'borrowernumber'};
527         $labels{ $_->{'borrowernumber'} } =
528 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'address'} ";
529     }
530     $CGIselectborrower = CGI::scrolling_list(
531         -name     => 'borrowernumber',
532         -class    => 'focus',
533         -id       => 'borrowernumber',
534         -values   => \@values,
535         -labels   => \%labels,
536         -onclick  => "window.location = '/cgi-bin/koha/circ/circulation.pl?borrowernumber=' + this.value;",
537         -size     => 7,
538         -tabindex => '',
539         -multiple => 0
540     );
541 }
542
543 #title
544 my $flags = $borrower->{'flags'};
545 my $flag;
546
547 foreach $flag ( sort keys %$flags ) {
548     $template->param( flagged=> 1);
549     $flags->{$flag}->{'message'} =~ s#\n#<br />#g;
550     if ( $flags->{$flag}->{'noissues'} ) {
551         $template->param(
552             flagged  => 1,
553             noissues => 'true',
554         );
555         if ( $flag eq 'GNA' ) {
556             $template->param( gna => 'true' );
557         }
558         if ( $flag eq 'LOST' ) {
559             $template->param( lost => 'true' );
560         }
561         if ( $flag eq 'DBARRED' ) {
562             $template->param( dbarred => 'true' );
563         }
564         if ( $flag eq 'CHARGES' ) {
565             $template->param(
566                 charges    => 'true',
567                 chargesmsg => $flags->{'CHARGES'}->{'message'},
568                 chargesamount => $flags->{'CHARGES'}->{'amount'},
569                 charges_is_blocker => 1
570             );
571         }
572         if ( $flag eq 'CREDITS' ) {
573             $template->param(
574                 credits    => 'true',
575                 creditsmsg => $flags->{'CREDITS'}->{'message'}
576             );
577         }
578     }
579     else {
580         if ( $flag eq 'CHARGES' ) {
581             $template->param(
582                 charges    => 'true',
583                 flagged    => 1,
584                 chargesmsg => $flags->{'CHARGES'}->{'message'},
585                 chargesamount => $flags->{'CHARGES'}->{'amount'},
586             );
587         }
588         if ( $flag eq 'CREDITS' ) {
589             $template->param(
590                 credits    => 'true',
591                 creditsmsg => $flags->{'CREDITS'}->{'message'}
592             );
593         }
594         if ( $flag eq 'ODUES' ) {
595             $template->param(
596                 odues    => 'true',
597                 flagged  => 1,
598                 oduesmsg => $flags->{'ODUES'}->{'message'}
599             );
600
601             my $items = $flags->{$flag}->{'itemlist'};
602 # useless ???
603 #             {
604 #                 my @itemswaiting;
605 #                 foreach my $item (@$items) {
606 #                     my ($iteminformation) =
607 #                         getiteminformation( $item->{'itemnumber'}, 0 );
608 #                     push @itemswaiting, $iteminformation;
609 #                 }
610 #             }
611             if ( $query->param('module') ne 'returns' ) {
612                 $template->param( nonreturns => 'true' );
613             }
614         }
615         if ( $flag eq 'NOTES' ) {
616             $template->param(
617                 notes    => 'true',
618                 flagged  => 1,
619                 notesmsg => $flags->{'NOTES'}->{'message'}
620             );
621         }
622     }
623 }
624
625 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
626 my @temp = split( /\$/, $amountold );
627
628 if ( $borrower->{'category_type'} eq 'C') {
629     my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
630     my $cnt = scalar(@$catcodes);
631     $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
632     $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
633 }
634
635 my $CGIorganisations;
636 my $member_of_institution;
637 if ( C4::Context->preference("memberofinstitution") ) {
638     my $organisations = get_institutions();
639     my @orgs;
640     my %org_labels;
641     foreach my $organisation ( keys %$organisations ) {
642         push @orgs, $organisation;
643         $org_labels{$organisation} =
644           $organisations->{$organisation}->{'surname'};
645     }
646     $member_of_institution = 1;
647     $CGIorganisations      = CGI::popup_menu(
648         -id     => 'organisations',
649         -name   => 'organisations',
650         -labels => \%org_labels,
651         -values => \@orgs,
652     );
653 }
654
655 $amountold = $temp[1];
656
657 $template->param(
658     issued_itemtypes_count_loop => $issued_itemtypes_loop,
659     findborrower                => $findborrower,
660     borrower                    => $borrower,
661     borrowernumber              => $borrowernumber,
662     branch                      => $branch,
663     branchname                  => GetBranchName($borrower->{'branchcode'}),
664     printer                     => $printer,
665     printername                 => $printer,
666     firstname                   => $borrower->{'firstname'},
667     surname                     => $borrower->{'surname'},
668     dateexpiry        => format_date($newexpiry),
669     expiry            => format_date($borrower->{'dateexpiry'}),
670     categorycode      => $borrower->{'categorycode'},
671     categoryname      => $borrower->{description},
672     address           => $borrower->{'address'},
673     address2          => $borrower->{'address2'},
674     email             => $borrower->{'email'},
675     emailpro          => $borrower->{'emailpro'},
676     borrowernotes     => $borrower->{'borrowernotes'},
677     city              => $borrower->{'city'},
678     zipcode               => $borrower->{'zipcode'},
679     phone             => $borrower->{'phone'} || $borrower->{'mobile'},
680     cardnumber        => $borrower->{'cardnumber'},
681     amountold         => $amountold,
682     barcode           => $barcode,
683     stickyduedate     => $stickyduedate,
684     duedatespec       => $duedatespec,
685     message           => $message,
686     CGIselectborrower => $CGIselectborrower,
687     todayissues       => \@todaysissues,
688     previssues        => \@previousissues,
689     inprocess         => $inprocess,
690     memberofinstution => $member_of_institution,
691     CGIorganisations  => $CGIorganisations,
692         is_child          => ($borrower->{'category_type'} eq 'C'),
693     circview => 1,
694 );
695
696
697 #if ($branchcookie) {
698 #$cookie=[$cookie, $branchcookie, $printercookie];
699 #}
700
701 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
702 $template->param( picture => 1 ) if $picture;
703
704
705 $template->param(
706     debt_confirmed            => $debt_confirmed,
707     SpecifyDueDate            => $duedatespec_allow,
708     CircAutocompl             => C4::Context->preference("CircAutocompl"),
709         AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
710     dateformat                => C4::Context->preference("dateformat"),
711     DHTMLcalendar_dateformat  => C4::Dates->DHTMLcalendar(),
712 );
713 output_html_with_http_headers $query, $cookie, $template->output;