Make stickyduedate remember for session a little more permanent
[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 => 1 },
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 $barcode =~  s/^\s*|\s*$//g; # remove leading/trailing whitespace
116
117 $barcode = barcodedecode($barcode) if( $barcode && C4::Context->preference('itemBarcodeInputFilter'));
118 my $stickyduedate  = $query->param('stickyduedate') || $session->param( 'stickyduedate' );
119 my $duedatespec    = $query->param('duedatespec') || $session->param( 'stickyduedate' );
120 my $issueconfirmed = $query->param('issueconfirmed');
121 my $cancelreserve  = $query->param('cancelreserve');
122 my $organisation   = $query->param('organisations');
123 my $print          = $query->param('print');
124 my $newexpiry      = $query->param('dateexpiry');
125 my $debt_confirmed = $query->param('debt_confirmed') || 0; # Don't show the debt error dialog twice
126
127 # Check if stickyduedate is turned off
128 if ( $barcode ) {
129     # was stickyduedate loaded from session?
130     if ( $stickyduedate && ! $query->param("stickyduedate") ) {
131         $session->clear( 'stickyduedate' );
132         $stickyduedate  = $query->param('stickyduedate');
133         $duedatespec    = $query->param('duedatespec');
134     }
135 }
136
137 #set up cookie.....
138 # my $branchcookie;
139 # my $printercookie;
140 # if ($query->param('setcookies')) {
141 #     $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
142 #     $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
143 # }
144 #
145
146 my ($datedue,$invalidduedate);
147 if ($duedatespec) {
148         if ($duedatespec =~ C4::Dates->regexp('syspref')) {
149                 my $tempdate = C4::Dates->new($duedatespec);
150                 if ($tempdate and $tempdate->output('iso') gt C4::Dates->new()->output('iso')) {
151                         # i.e., it has to be later than today/now
152                         $datedue = $tempdate;
153                 } else {
154                         $invalidduedate = 1;
155                         $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
156                 }
157         } else {
158                 $invalidduedate = 1;
159                 $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
160         }
161 }
162
163 my $todaysdate = C4::Dates->new->output('iso');
164
165 # check and see if we should print
166 if ( $barcode eq '' && $print eq 'maybe' ) {
167     $print = 'yes';
168 }
169
170 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
171
172 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
173     $template->param(
174         PAYCHARGES     => 'yes',
175         borrowernumber => $borrowernumber
176     );
177 }
178
179 if ( $print eq 'yes' && $borrowernumber ne '' ) {
180     printslip( $borrowernumber );
181     $query->param( 'borrowernumber', '' );
182     $borrowernumber = '';
183 }
184
185 #
186 # STEP 2 : FIND BORROWER
187 # if there is a list of find borrowers....
188 #
189 my $borrowerslist;
190 my $message;
191 if ($findborrower) {
192     my ( $count, $borrowers ) =
193       SearchMember($findborrower, 'cardnumber', 'web' );
194     my @borrowers = @$borrowers;
195         $template->param(
196                 "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
197         );
198         if (C4::Context->preference("AddPatronLists")=~/code/){
199                 my $categories=GetBorrowercategoryList;
200                 $categories->[0]->{'first'}=1;
201                 $template->param(categories=>$categories);
202         }
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 $noerror = $invalidduedate ? 0 : 1;
275
276   delete $question->{'DEBT'} if ($debt_confirmed);
277   foreach my $impossible ( keys %$error ) {
278             if ($impossible eq "NOT_FOR_LOAN_CAN_FORCE"){
279                 $$question{$impossible}=$$error{$impossible},
280             } else {
281             $template->param(
282                 $impossible => $$error{$impossible},
283                 IMPOSSIBLE  => 1
284             );
285             }
286             $noerror = 0;
287         }
288     
289   if ($issueconfirmed && $noerror) {
290     # we have no blockers for issuing and any issues needing confirmation have been resolved
291         AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
292         $inprocess = 1;
293     }
294   elsif ($issueconfirmed){      # FIXME: Do something? Or is this to *intentionally* do nothing?
295     if (C4::Context->preference("AllowNotForLoanOverride")){
296         AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
297         $template->param(IMPOSSIBLE  => 0);
298         $inprocess = 1;
299     }
300   }
301   else {
302         my $noquestion = 1;
303 #         Get the item title for more information
304         my $getmessageiteminfo  = GetBiblioFromItemNumber(undef,$barcode);
305                 if ($noerror) {
306                         # only pass needsconfirmation to template if issuing is possible 
307                 foreach my $needsconfirmation ( keys %$question ) {
308                     $template->param(
309                         $needsconfirmation => $$question{$needsconfirmation},
310                         getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
311                         NEEDSCONFIRMATION  => 1
312                     );
313                     $noquestion = 0;
314                 }
315                         # Because of the weird conditional structure (empty elsif block),
316                         # if we reached here, $issueconfirmed must be false.
317                         # Also, since we moved inside the if ($noerror) conditional,
318                         # this old chunky conditional can be simplified:
319                     # if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
320                         if ($noquestion) {
321                                 AddIssue( $borrower, $barcode, $datedue );
322                                 $inprocess = 1;
323                         }
324             }
325                 $template->param(
326                          itemhomebranch => $getmessageiteminfo->{'homebranch'} ,                     
327                          duedatespec => $duedatespec,
328         );
329     }
330     
331 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue 
332         my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
333         $template->param(
334         issuecount   => $issue,
335         );
336 }
337
338 # reload the borrower info for the sake of reseting the flags.....
339 if ($borrowernumber) {
340     $borrower = GetMemberDetails( $borrowernumber, 0 );
341 }
342
343 ##################################################################################
344 # BUILD HTML
345 # show all reserves of this borrower, and the position of the reservation ....
346 my $borrowercategory;
347 my $category_type;
348 if ($borrowernumber) {
349
350     # new op dev
351     # now we show the status of the borrower's reservations
352     my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
353     my @reservloop;
354     my @WaitingReserveLoop;
355     
356     foreach my $num_res (@borrowerreserv) {
357         my %getreserv;
358         my %getWaitingReserveInfo;
359         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
360         my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itypes')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
361         my ( $transfertwhen, $transfertfrom, $transfertto ) =
362           GetTransfers( $num_res->{'itemnumber'} );
363
364         $getreserv{waiting}       = 0;
365         $getreserv{transfered}    = 0;
366         $getreserv{nottransfered} = 0;
367
368         $getreserv{reservedate}    = format_date( $num_res->{'reservedate'} );
369         $getreserv{title}          = $getiteminfo->{'title'};
370         $getreserv{itemtype}       = $itemtypeinfo->{'description'};
371         $getreserv{author}         = $getiteminfo->{'author'};
372         $getreserv{barcodereserv}  = $getiteminfo->{'barcode'};
373         $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
374         $getreserv{biblionumber}   = $getiteminfo->{'biblionumber'};
375         $getreserv{waitingat}    = GetBranchName( $num_res->{'branchcode'} );
376         #         check if we have a waiting status for reservations
377         if ( $num_res->{'found'} eq 'W' ) {
378             $getreserv{color}   = 'reserved';
379             $getreserv{waiting} = 1;
380 #     genarate information displaying only waiting reserves
381         $getWaitingReserveInfo{title}        = $getiteminfo->{'title'};
382         $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
383         $getWaitingReserveInfo{itemtype}     = $itemtypeinfo->{'description'};
384         $getWaitingReserveInfo{author}       = $getiteminfo->{'author'};
385         $getWaitingReserveInfo{reservedate}  = format_date( $num_res->{'reservedate'} );
386         $getWaitingReserveInfo{waitingat}    = GetBranchName( $num_res->{'branchcode'} );
387       if($num_res->{'branchcode'} eq $branch){ $getWaitingReserveInfo{waitinghere} = 1; }
388         }
389         #         check transfers with the itemnumber foud in th reservation loop
390         if ($transfertwhen) {
391             $getreserv{color}      = 'transfered';
392             $getreserv{transfered} = 1;
393             $getreserv{datesent}   = format_date($transfertwhen);
394             $getreserv{frombranch} = GetBranchName($transfertfrom);
395         }
396
397         if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
398             and not $transfertwhen )
399         {
400             $getreserv{nottransfered}   = 1;
401             $getreserv{nottransferedby} =
402               GetBranchName( $getiteminfo->{'holdingbranch'} );
403         }
404
405 #         if we don't have a reserv on item, we put the biblio infos and the waiting position
406         if ( $getiteminfo->{'title'} eq '' ) {
407             my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
408             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );  # fixme - we should have item-level reserves here ?
409             $getreserv{color}           = 'inwait';
410             $getreserv{title}           = $getbibinfo->{'title'};
411             $getreserv{nottransfered}   = 0;
412             $getreserv{itemtype}        = $getbibtype->{'description'};
413             $getreserv{author}          = $getbibinfo->{'author'};
414           $getreserv{biblionumber}    = $num_res->{'biblionumber'};
415         }
416         $getreserv{waitingposition} = $num_res->{'priority'};
417         push( @reservloop, \%getreserv );
418
419 #         if we have a reserve waiting, initiate waitingreserveloop
420         if ($getreserv{waiting} eq 1) {
421         push (@WaitingReserveLoop, \%getWaitingReserveInfo)
422         }
423       
424     }
425
426     # return result to the template
427     $template->param( 
428         countreserv => scalar @reservloop,
429         reservloop  => \@reservloop ,
430         WaitingReserveLoop  => \@WaitingReserveLoop,
431     );
432     $template->param( adultborrower => 1 ) if ( $borrower->{'category_type'} eq 'A' );
433 }
434
435 # make the issued books table.
436 my $todaysissues = '';
437 my $previssues   = '';
438 my @todaysissues;
439 my @previousissues;
440 my $allowborrow;
441 ## ADDED BY JF: new itemtype issuingrules counter stuff
442 my $issued_itemtypes_loop;
443 my $issued_itemtypes_count;
444 my $issued_itemtypes_allowed_count;    # hashref with total allowed by itemtype
445 my $issued_itemtypes_remaining;        # hashref with remaining
446 my $issued_itemtypes_flags;            #hashref that stores flags
447 my @issued_itemtypes_count_loop;
448
449 if ($borrower) {
450 # get each issue of the borrower & separate them in todayissues & previous issues
451     my ($issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
452
453     # split in 2 arrays for today & previous
454     foreach my $it ( @$issueslist ) {
455         # set itemtype per item-level_itype syspref - FIXME this is an ugly hack
456         $it->{'itemtype'} = ( C4::Context->preference( 'item-level_itypes' ) ) ? $it->{'itype'} : $it->{'itemtype'};
457
458         ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
459             $it->{'itemnumber'}, $borrower->{'borrowernumber'}
460         );
461         $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
462         my ($can_renew, $can_renew_error) = CanBookBeRenewed( 
463             $borrower->{'borrowernumber'},$it->{'itemnumber'}
464         );
465         $it->{"renew_error_${can_renew_error}"} = 1 if defined $can_renew_error;
466         my ( $restype, $reserves ) = CheckReserves( $it->{'itemnumber'} );
467                 $it->{'can_renew'} = $can_renew;
468                 $it->{'can_confirm'} = !$can_renew && !$restype;
469                 $it->{'renew_error'} = $restype;
470
471         $it->{'dd'} = format_date($it->{'date_due'});
472         $it->{'od'} = ( $it->{'date_due'} lt $todaysdate ) ? 1 : 0 ;
473         ($it->{'author'} eq '') and $it->{'author'} = ' ';
474         $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
475         # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
476         $issued_itemtypes_count->{ $it->{'itemtype'} }++;
477
478         if ( $todaysdate eq $it->{'issuedate'} or $todaysdate eq $it->{'lastreneweddate'} ) {
479             push @todaysissues, $it;
480         } else {
481             push @previousissues, $it;
482         }
483     }
484     if ( C4::Context->preference( "todaysIssuesDefaultSortOrder" ) eq 'asc' ) {
485         @todaysissues   = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
486     }
487     else {
488         @todaysissues   = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
489     }
490     if ( C4::Context->preference( "previousIssuesDefaultSortOrder" ) eq 'asc' ){
491         @previousissues = sort { $a->{'date_due'} cmp $b->{'date_due'} } @previousissues;
492     }
493     else {
494         @previousissues = sort { $b->{'date_due'} cmp $a->{'date_due'} } @previousissues;
495     }
496 }
497
498 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
499 # FIXME: This should utilize all the issuingrules options rather than just the defaults
500 # and it should be moved to a module
501 my $dbh = C4::Context->dbh;
502
503 # how many of each is allowed?
504 my $issueqty_sth = $dbh->prepare( "
505 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
506 FROM issuingrules
507   LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
508   WHERE categorycode=?
509 " );
510 #my @issued_itemtypes_count;  # huh?
511 $issueqty_sth->execute("*");    # This is a literal asterisk, not a wildcard.
512
513 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
514
515     # subtract how many of each this borrower has
516     $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };  
517     $data->{'left'}  =
518       ( $data->{'maxissueqty'} -
519           $issued_itemtypes_count->{ $data->{'description'} } );
520
521     # can't have a negative number of remaining
522     if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
523     $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
524     unless ( ( $data->{'maxissueqty'} < 1 )
525         || ( $data->{'itemtype'} eq "*" )
526         || ( $data->{'itemtype'} eq "CIRC" ) )
527     {
528         push @issued_itemtypes_count_loop, $data;
529     }
530 }
531 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
532
533 #### / JF
534
535 my @values;
536 my %labels;
537 my $CGIselectborrower;
538 if ($borrowerslist) {
539     foreach (
540         sort {(lc $a->{'surname'} cmp lc $b->{'surname'} || lc $a->{'firstname'} cmp lc $b->{'firstname'})
541         } @$borrowerslist
542       )
543     {
544         push @values, $_->{'borrowernumber'};
545         $labels{ $_->{'borrowernumber'} } =
546 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'address'} ";
547     }
548     $CGIselectborrower = CGI::scrolling_list(
549         -name     => 'borrowernumber',
550         -class    => 'focus',
551         -id       => 'borrowernumber',
552         -values   => \@values,
553         -labels   => \%labels,
554         -onclick  => "window.location = '/cgi-bin/koha/circ/circulation.pl?borrowernumber=' + this.value;",
555         -size     => 7,
556         -tabindex => '',
557         -multiple => 0
558     );
559 }
560
561 #title
562 my $flags = $borrower->{'flags'};
563 my $flag;
564
565 foreach $flag ( sort keys %$flags ) {
566     $template->param( flagged=> 1);
567     $flags->{$flag}->{'message'} =~ s#\n#<br />#g;
568     if ( $flags->{$flag}->{'noissues'} ) {
569         $template->param(
570             flagged  => 1,
571             noissues => 'true',
572         );
573         if ( $flag eq 'GNA' ) {
574             $template->param( gna => 'true' );
575         }
576         if ( $flag eq 'LOST' ) {
577             $template->param( lost => 'true' );
578         }
579         if ( $flag eq 'DBARRED' ) {
580             $template->param( dbarred => 'true' );
581         }
582         if ( $flag eq 'CHARGES' ) {
583             $template->param(
584                 charges    => 'true',
585                 chargesmsg => $flags->{'CHARGES'}->{'message'},
586                 chargesamount => $flags->{'CHARGES'}->{'amount'},
587                 charges_is_blocker => 1
588             );
589         }
590         if ( $flag eq 'CREDITS' ) {
591             $template->param(
592                 credits    => 'true',
593                 creditsmsg => $flags->{'CREDITS'}->{'message'}
594             );
595         }
596     }
597     else {
598         if ( $flag eq 'CHARGES' ) {
599             $template->param(
600                 charges    => 'true',
601                 flagged    => 1,
602                 chargesmsg => $flags->{'CHARGES'}->{'message'},
603                 chargesamount => $flags->{'CHARGES'}->{'amount'},
604             );
605         }
606         if ( $flag eq 'CREDITS' ) {
607             $template->param(
608                 credits    => 'true',
609                 creditsmsg => $flags->{'CREDITS'}->{'message'}
610             );
611         }
612         if ( $flag eq 'ODUES' ) {
613             $template->param(
614                 odues    => 'true',
615                 flagged  => 1,
616                 oduesmsg => $flags->{'ODUES'}->{'message'}
617             );
618
619             my $items = $flags->{$flag}->{'itemlist'};
620 # useless ???
621 #             {
622 #                 my @itemswaiting;
623 #                 foreach my $item (@$items) {
624 #                     my ($iteminformation) =
625 #                         getiteminformation( $item->{'itemnumber'}, 0 );
626 #                     push @itemswaiting, $iteminformation;
627 #                 }
628 #             }
629             if ( $query->param('module') ne 'returns' ) {
630                 $template->param( nonreturns => 'true' );
631             }
632         }
633         if ( $flag eq 'NOTES' ) {
634             $template->param(
635                 notes    => 'true',
636                 flagged  => 1,
637                 notesmsg => $flags->{'NOTES'}->{'message'}
638             );
639         }
640     }
641 }
642
643 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
644 my @temp = split( /\$/, $amountold );
645
646 if ( $borrower->{'category_type'} eq 'C') {
647     my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
648     my $cnt = scalar(@$catcodes);
649     $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
650     $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
651 }
652
653 my $CGIorganisations;
654 my $member_of_institution;
655 if ( C4::Context->preference("memberofinstitution") ) {
656     my $organisations = get_institutions();
657     my @orgs;
658     my %org_labels;
659     foreach my $organisation ( keys %$organisations ) {
660         push @orgs, $organisation;
661         $org_labels{$organisation} =
662           $organisations->{$organisation}->{'surname'};
663     }
664     $member_of_institution = 1;
665     $CGIorganisations      = CGI::popup_menu(
666         -id     => 'organisations',
667         -name   => 'organisations',
668         -labels => \%org_labels,
669         -values => \@orgs,
670     );
671 }
672
673 $amountold = $temp[1];
674
675 $template->param(
676     issued_itemtypes_count_loop => $issued_itemtypes_loop,
677     findborrower                => $findborrower,
678     borrower                    => $borrower,
679     borrowernumber              => $borrowernumber,
680     branch                      => $branch,
681     branchname                  => GetBranchName($borrower->{'branchcode'}),
682     printer                     => $printer,
683     printername                 => $printer,
684     firstname                   => $borrower->{'firstname'},
685     surname                     => $borrower->{'surname'},
686     dateexpiry        => format_date($newexpiry),
687     expiry            => format_date($borrower->{'dateexpiry'}),
688     categorycode      => $borrower->{'categorycode'},
689     categoryname      => $borrower->{description},
690     address           => $borrower->{'address'},
691     address2          => $borrower->{'address2'},
692     email             => $borrower->{'email'},
693     emailpro          => $borrower->{'emailpro'},
694     borrowernotes     => $borrower->{'borrowernotes'},
695     city              => $borrower->{'city'},
696     zipcode               => $borrower->{'zipcode'},
697     phone             => $borrower->{'phone'} || $borrower->{'mobile'},
698     cardnumber        => $borrower->{'cardnumber'},
699     amountold         => $amountold,
700     barcode           => $barcode,
701     stickyduedate     => $stickyduedate,
702     message           => $message,
703     CGIselectborrower => $CGIselectborrower,
704     todayissues       => \@todaysissues,
705     previssues        => \@previousissues,
706     inprocess         => $inprocess,
707     memberofinstution => $member_of_institution,
708     CGIorganisations  => $CGIorganisations,
709         is_child          => ($borrower->{'category_type'} eq 'C'),
710     circview => 1,
711 );
712
713 # save stickyduedate to session
714 if ($stickyduedate) {
715     $session->param( 'stickyduedate', $duedatespec );
716 }
717
718 #if ($branchcookie) {
719 #$cookie=[$cookie, $branchcookie, $printercookie];
720 #}
721
722 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
723 $template->param( picture => 1 ) if $picture;
724
725
726 $template->param(
727     debt_confirmed            => $debt_confirmed,
728     SpecifyDueDate            => C4::Context->preference("SpecifyDueDate"),
729     CircAutocompl             => C4::Context->preference("CircAutocompl"),
730         AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
731     dateformat                => C4::Context->preference("dateformat"),
732     DHTMLcalendar_dateformat  => C4::Dates->DHTMLcalendar(),
733 );
734 output_html_with_http_headers $query, $cookie, $template->output;