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