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