fix for bug 1551: Renewing doesn't move item...
[koha.git] / circ / circulation.pl
1 #!/usr/bin/perl
2
3 # Please use 8-character tabs for this file (indents are every 4 characters)
4
5 # written 8/5/2002 by Finlay
6 # script to execute issuing of books
7
8 # Copyright 2000-2002 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use strict;
26 use CGI;
27 use C4::Output;
28 use C4::Print;
29 use C4::Auth qw/:DEFAULT get_session/;
30 use C4::Dates qw/format_date/;
31 use C4::Branch; # GetBranches
32 use C4::Koha;   # GetPrinter
33 use C4::Circulation;
34 use C4::Members;
35 use C4::Biblio;
36 use C4::Reserves;
37 use C4::Context;
38 use CGI::Session;
39
40 use Date::Calc qw(
41   Today
42   Add_Delta_YM
43   Add_Delta_Days
44   Date_to_Days
45 );
46
47
48 #
49 # PARAMETERS READING
50 #
51 my $query = new CGI;
52
53 # new op dev the branch and the printer are now defined by the userenv
54 # but first we have to check if someone has tried to change them
55
56 my $branch = $query->param('branch');
57 if ($branch){
58     # update our session so the userenv is updated
59     my $dbh=C4::Context->dbh;
60     my $sessionID = $query->cookie("CGISESSID") ;
61     my $session = get_session($sessionID);
62     $session->param('branch',$branch);
63     my $branchname = GetBranchName($branch);
64     $session->param('branchname',$branchname);
65 }
66
67 my $printer = $query->param('printer');
68 if ($printer){
69     # update our session so the userenv is updated
70   my $dbh=C4::Context->dbh;
71   my $sessionID = $query->cookie("CGISESSID") ;
72   my $session = get_session($sessionID);
73   $session->param('branchprinter',$printer);
74
75 }
76 if (!C4::Context->userenv && !$branch){
77   my $sessionID = $query->cookie("CGISESSID") ;
78   my $session = get_session($sessionID);
79   if ($session->param('branch') eq 'NO_LIBRARY_SET'){
80     # no branch set we can't issue
81     print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
82     exit;
83   }
84 }
85
86 my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
87     {
88         template_name   => 'circ/circulation.tmpl',
89         query           => $query,
90         type            => "intranet",
91         authnotrequired => 0,
92         flagsrequired   => { circulate => 1 },
93     }
94 );
95 my $branches = GetBranches();
96
97 my $printers = GetPrinters();
98
99 my @failedrenews = $query->param('failedrenew');
100 my @renew_failed;
101 for (@failedrenews) { $renew_failed[$_] = 1; } 
102
103 my $findborrower = $query->param('findborrower');
104 $findborrower =~ s|,| |g;
105 #$findborrower =~ s|'| |g;
106 my $borrowernumber = $query->param('borrowernumber');
107
108 $branch  = C4::Context->userenv->{'branch'};  
109 $printer = C4::Context->userenv->{'branchprinter'};
110
111
112 # If Autolocated is not activated, we show the Circulation Parameters to chage settings of librarian
113     if (C4::Context->preference("AutoLocation") ne 1)
114         {
115             $template->param(
116             ManualLocation => 1,
117             );
118         }
119
120 my $barcode        = $query->param('barcode') || '';
121
122 $barcode = barcodedecode($barcode) if( $barcode && C4::Context->preference('itemBarcodeInputFilter'));
123 my $year           = $query->param('year');
124 my $month          = $query->param('month');
125 my $day            = $query->param('day');
126 my $stickyduedate  = $query->param('stickyduedate');
127 my $duedatespec    = $query->param('duedatespec');
128 my $issueconfirmed = $query->param('issueconfirmed');
129 my $cancelreserve  = $query->param('cancelreserve');
130 my $organisation   = $query->param('organisations');
131 my $print          = $query->param('print');
132 my $newexpiry      = $query->param('dateexpiry');
133
134 #set up cookie.....
135 # my $branchcookie;
136 # my $printercookie;
137 # if ($query->param('setcookies')) {
138 #     $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
139 #     $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
140 # }
141 #
142
143 my ($datedue,$invalidduedate);
144 if ($duedatespec) {
145         if ($duedatespec =~ C4::Dates->regexp('syspref')) {
146                 my $tempdate = C4::Dates->new($duedatespec);
147                 if ($tempdate and $tempdate->output('iso') gt C4::Dates->new()->output('iso')) {
148                         # i.e., it has to be later than today/now
149                         $datedue = $tempdate;
150                 } else {
151                         $invalidduedate = 1;
152                         $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
153                 }
154         } else {
155                 $invalidduedate = 1;
156                 $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
157         }
158 }
159
160 my $todaysdate = C4::Dates->new->output('iso');
161
162 # check and see if we should print
163 if ( $barcode eq '' && $print eq 'maybe' ) {
164     $print = 'yes';
165 }
166
167 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
168
169 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
170     $template->param(
171         PAYCHARGES     => 'yes',
172         borrowernumber => $borrowernumber
173     );
174 }
175
176 if ( $print eq 'yes' && $borrowernumber ne '' ) {
177     printslip( $borrowernumber );
178     $query->param( 'borrowernumber', '' );
179     $borrowernumber = '';
180 }
181
182 #
183 # STEP 2 : FIND BORROWER
184 # if there is a list of find borrowers....
185 #
186 my $borrowerslist;
187 my $message;
188 if ($findborrower) {
189     my ( $count, $borrowers ) =
190       SearchMember($findborrower, 'cardnumber', 'web' );
191     my @borrowers = @$borrowers;
192     if ( $#borrowers == -1 ) {
193         $query->param( 'findborrower', '' );
194         $message = "'$findborrower'";
195     }
196     elsif ( $#borrowers == 0 ) {
197         $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
198         $query->param( 'barcode',           '' );
199         $borrowernumber = $borrowers[0]->{'borrowernumber'};
200     }
201     else {
202         $borrowerslist = \@borrowers;
203     }
204 }
205
206 # get the borrower information.....
207 my $borrower;
208 my @lines;
209 if ($borrowernumber) {
210     $borrower = GetMemberDetails( $borrowernumber, 0 );
211     my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
212
213     # Warningdate is the date that the warning starts appearing
214     my ( $today_year,   $today_month,   $today_day )   = Today();
215     my ( $warning_year, $warning_month, $warning_day ) = split /-/,
216       $borrower->{'dateexpiry'};
217     my ( $enrol_year, $enrol_month, $enrol_day ) = split /-/,
218       $borrower->{'dateenrolled'};
219     # Renew day is calculated by adding the enrolment period to today
220     my ( $renew_year, $renew_month, $renew_day ) =
221       Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
222         0 , $borrower->{'enrolmentperiod'}) if ($enrol_year*$enrol_month*$enrol_day>0);
223     # if the expiry date is before today ie they have expired
224     if ( $warning_year*$warning_month*$warning_day==0 
225       || Date_to_Days( $today_year, $today_month, $today_day ) 
226          > Date_to_Days( $warning_year, $warning_month, $warning_day ) )
227     {
228         #borrowercard expired, no issues
229         $template->param(
230       flagged => "1",
231             noissues       => "1",
232             expired => format_date($borrower->{dateexpiry}),
233             renewaldate   => format_date("$renew_year-$renew_month-$renew_day")
234         );
235     }
236     # check for NotifyBorrowerDeparture
237   elsif ( C4::Context->preference('NotifyBorrowerDeparture') &&
238     Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
239     Date_to_Days( $today_year, $today_month, $today_day ) ) 
240   {
241     # borrower card soon to expire warn librarian
242     $template->param("warndeparture" => format_date($borrower->{dateexpiry}),
243       flagged       => "1",);
244     if ( C4::Context->preference('ReturnBeforeExpiry')){
245       $template->param("returnbeforeexpiry" => 1);
246     }
247   }
248     $template->param(
249         overduecount => $od,
250         issuecount   => $issue,
251         finetotal    => $fines
252     );
253 }
254
255 #
256 # STEP 3 : ISSUING
257 #
258 #
259 if ($barcode) {
260   # always check for blockers on issuing
261   my ( $error, $question ) =
262     CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
263   my $noerror = $invalidduedate ? 0 : 1;
264   foreach my $impossible ( keys %$error ) {
265             $template->param(
266                 $impossible => $$error{$impossible},
267                 IMPOSSIBLE  => 1
268             );
269             $noerror = 0;
270         }
271     
272   if ($issueconfirmed && $noerror) {
273     # we have no blockers for issuing and any issues needing confirmation have been resolved
274         AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
275         $inprocess = 1;
276     }
277   elsif ($issueconfirmed){      # FIXME: Do something? Or is this to *intentionally* do nothing?
278   }
279   else {
280         my $noquestion = 1;
281 #         Get the item title for more information
282         my $getmessageiteminfo  = GetBiblioFromItemNumber(undef,$barcode);
283                 if ($noerror) {
284                         # only pass needsconfirmation to template if issuing is possible 
285                 foreach my $needsconfirmation ( keys %$question ) {
286                     $template->param(
287                         $needsconfirmation => $$question{$needsconfirmation},
288                         getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
289                         NEEDSCONFIRMATION  => 1
290                     );
291                     $noquestion = 0;
292                 }
293                         # Because of the weird conditional structure (empty elsif block),
294                         # if we reached here, $issueconfirmed must be false.
295                         # Also, since we moved inside the if ($noerror) conditional,
296                         # this old chunky conditional can be simplified:
297                     # if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
298                         if ($noquestion) {
299                                 AddIssue( $borrower, $barcode, $datedue );
300                                 $inprocess = 1;
301                         }
302             }
303                 $template->param(
304                          itemhomebranch => $getmessageiteminfo->{'homebranch'} ,                     
305                          duedatespec => $duedatespec,
306         );
307     }
308     
309 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue 
310         my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
311         $template->param(
312         issuecount   => $issue,
313         );
314 }
315
316 # reload the borrower info for the sake of reseting the flags.....
317 if ($borrowernumber) {
318     $borrower = GetMemberDetails( $borrowernumber, 0 );
319 }
320
321 ##################################################################################
322 # BUILD HTML
323 # show all reserves of this borrower, and the position of the reservation ....
324 my $borrowercategory;
325 my $category_type;
326 if ($borrowernumber) {
327
328     # new op dev
329     # now we show the status of the borrower's reservations
330     my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
331     my @reservloop;
332     my @WaitingReserveLoop;
333     
334     foreach my $num_res (@borrowerreserv) {
335         my %getreserv;
336         my %getWaitingReserveInfo;
337         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
338         my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itypes')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
339         my ( $transfertwhen, $transfertfrom, $transfertto ) =
340           GetTransfers( $num_res->{'itemnumber'} );
341
342         $getreserv{waiting}       = 0;
343         $getreserv{transfered}    = 0;
344         $getreserv{nottransfered} = 0;
345
346         $getreserv{reservedate}    = format_date( $num_res->{'reservedate'} );
347         $getreserv{title}          = $getiteminfo->{'title'};
348         $getreserv{itemtype}       = $itemtypeinfo->{'description'};
349         $getreserv{author}         = $getiteminfo->{'author'};
350         $getreserv{barcodereserv}  = $getiteminfo->{'barcode'};
351         $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
352         $getreserv{biblionumber}   = $getiteminfo->{'biblionumber'};
353         $getreserv{waitingat}    = GetBranchName( $num_res->{'branchcode'} );
354         #         check if we have a waiting status for reservations
355         if ( $num_res->{'found'} eq 'W' ) {
356             $getreserv{color}   = 'reserved';
357             $getreserv{waiting} = 1;
358 #     genarate information displaying only waiting reserves
359         $getWaitingReserveInfo{title}        = $getiteminfo->{'title'};
360         $getWaitingReserveInfo{biblionumber}   = $getiteminfo->{'biblionumber'};
361         $getWaitingReserveInfo{itemtype}    = $itemtypeinfo->{'description'};
362         $getWaitingReserveInfo{author}        = $getiteminfo->{'author'};
363         $getWaitingReserveInfo{reservedate}    = format_date( $num_res->{'reservedate'} );
364         $getWaitingReserveInfo{waitingat}    = GetBranchName( $num_res->{'branchcode'} );
365       if($num_res->{'branchcode'} eq $branch){ $getWaitingReserveInfo{waitinghere} = 1; }
366         }
367         #         check transfers with the itemnumber foud in th reservation loop
368         if ($transfertwhen) {
369             $getreserv{color}      = 'transfered';
370             $getreserv{transfered} = 1;
371             $getreserv{datesent}   = format_date($transfertwhen);
372             $getreserv{frombranch} = GetBranchName($transfertfrom);
373         }
374
375         if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
376             and not $transfertwhen )
377         {
378             $getreserv{nottransfered}   = 1;
379             $getreserv{nottransferedby} =
380               GetBranchName( $getiteminfo->{'holdingbranch'} );
381         }
382
383 #         if we don't have a reserv on item, we put the biblio infos and the waiting position
384         if ( $getiteminfo->{'title'} eq '' ) {
385             my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
386             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );  # fixme - we should have item-level reserves here ?
387             $getreserv{color}           = 'inwait';
388             $getreserv{title}           = $getbibinfo->{'title'};
389             $getreserv{waitingposition} = $num_res->{'priority'};
390             $getreserv{nottransfered}   = 0;
391             $getreserv{itemtype}        = $getbibtype->{'description'};
392             $getreserv{author}          = $getbibinfo->{'author'};
393           $getreserv{biblionumber}    = $num_res->{'biblionumber'};
394         }
395         push( @reservloop, \%getreserv );
396
397 #         if we have a reserve waiting, initiate waitingreserveloop
398         if ($getreserv{waiting} eq 1) {
399         push (@WaitingReserveLoop, \%getWaitingReserveInfo)
400         }
401       
402     }
403
404     # return result to the template
405     $template->param( 
406         countreserv => scalar @reservloop,
407         reservloop  => \@reservloop ,
408         WaitingReserveLoop  => \@WaitingReserveLoop,
409     );
410     $template->param( adultborrower => 1 ) if ( $borrower->{'category_type'} eq 'A' );
411 }
412
413 # make the issued books table.
414 my $todaysissues = '';
415 my $previssues   = '';
416 my @todaysissues;
417 my @previousissues;
418 my $allowborrow;
419 ## ADDED BY JF: new itemtype issuingrules counter stuff
420 my $issued_itemtypes_loop;
421 my $issued_itemtypes_count;
422 my $issued_itemtypes_allowed_count;    # hashref with total allowed by itemtype
423 my $issued_itemtypes_remaining;        # hashref with remaining
424 my $issued_itemtypes_flags;            #hashref that stores flags
425 my @issued_itemtypes_count_loop;
426
427 if ($borrower) {
428 # 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_error;
441         ($it->{'can_renew'}, $can_renew_error) = CanBookBeRenewed( 
442             $borrower->{'borrowernumber'},$it->{'itemnumber'}
443         );
444         $it->{"renew_error_${can_renew_error}"} = 1 if defined $can_renew_error;
445         my ( $restype, $reserves ) = CheckReserves( $it->{'itemnumber'} );
446         ( $restype ) and $it->{'can_renew'} = 0;
447
448         $it->{'dd'} = format_date($it->{'date_due'});
449         $it->{'od'} = ( $it->{'date_due'} lt $todaysdate ) ? 1 : 0 ;
450         ($it->{'author'} eq '') and $it->{'author'} = ' ';
451         $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
452         # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
453         $issued_itemtypes_count->{ $it->{'itemtype'} }++;
454
455         warn "$todaysdate, $it->{'issuedate'}, $it->{'lastreneweddate'}, $it->{'barcode'}";
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     my $i = 1;
475     foreach my $book ( @todaysissues ) {
476         $book->{'togglecolor'} = ( ++$i % 2 ) ? 0 : 1 ;
477     }
478     $i = 1;
479     foreach my $book ( @previousissues ) {
480         $book->{'togglecolor'} = ( ++$i % 2 ) ? 0 : 1 ;
481     }
482 }
483
484 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
485 # FIXME: This should utilize all the issuingrules options rather than just the defaults
486 # and it should be moved to a module
487 my $dbh = C4::Context->dbh;
488
489 # how many of each is allowed?
490 my $issueqty_sth = $dbh->prepare( "
491 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
492 FROM issuingrules
493   LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
494   WHERE categorycode=?
495 " );
496 #my @issued_itemtypes_count;  # huh?
497 $issueqty_sth->execute("*");    # FIXME: Why have a WHERE clause at all with a hardcoded "*"?
498
499 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
500
501     # subtract how many of each this borrower has
502     $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };  
503     $data->{'left'}  =
504       ( $data->{'maxissueqty'} -
505           $issued_itemtypes_count->{ $data->{'description'} } );
506
507     # can't have a negative number of remaining
508     if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
509     $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
510     unless ( ( $data->{'maxissueqty'} < 1 )
511         || ( $data->{'itemtype'} eq "*" )
512         || ( $data->{'itemtype'} eq "CIRC" ) )
513     {
514         push @issued_itemtypes_count_loop, $data;
515     }
516 }
517 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
518
519 #### / JF
520
521 my @values;
522 my %labels;
523 my $CGIselectborrower;
524 if ($borrowerslist) {
525     foreach (
526         sort {
527                 lc $a->{'surname'}
528               . lc $a->{'firstname'} cmp lc $b->{'surname'}
529               . lc $b->{'firstname'}
530         } @$borrowerslist
531       )
532     {
533         push @values, $_->{'borrowernumber'};
534         $labels{ $_->{'borrowernumber'} } =
535 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'address'} ";
536     }
537     $CGIselectborrower = CGI::scrolling_list(
538         -name     => 'borrowernumber',
539         -class    => 'focus',
540         -id       => 'borrowernumber',
541         -values   => \@values,
542         -labels   => \%labels,
543         -size     => 7,
544         -tabindex => '',
545         -multiple => 0
546     );
547 }
548
549 #title
550 my $flags = $borrower->{'flags'};
551 my $flag;
552
553 foreach $flag ( sort keys %$flags ) {
554     $template->param( flagged=> 1);
555     $flags->{$flag}->{'message'} =~ s#\n#<br />#g;
556     if ( $flags->{$flag}->{'noissues'} ) {
557         $template->param(
558             flagged  => 1,
559             noissues => 'true',
560         );
561         if ( $flag eq 'GNA' ) {
562             $template->param( gna => 'true' );
563         }
564         if ( $flag eq 'LOST' ) {
565             $template->param( lost => 'true' );
566         }
567         if ( $flag eq 'DBARRED' ) {
568             $template->param( dbarred => 'true' );
569         }
570         if ( $flag eq 'CHARGES' ) {
571             $template->param(
572                 charges    => 'true',
573                 chargesmsg => $flags->{'CHARGES'}->{'message'},
574                 charges_is_blocker => 1
575             );
576         }
577         if ( $flag eq 'CREDITS' ) {
578             $template->param(
579                 credits    => 'true',
580                 creditsmsg => $flags->{'CREDITS'}->{'message'}
581             );
582         }
583     }
584     else {
585         if ( $flag eq 'CHARGES' ) {
586             $template->param(
587                 charges    => 'true',
588                 flagged    => 1,
589                 chargesmsg => $flags->{'CHARGES'}->{'message'}
590             );
591         }
592         if ( $flag eq 'CREDITS' ) {
593             $template->param(
594                 credits    => 'true',
595                 creditsmsg => $flags->{'CREDITS'}->{'message'}
596             );
597         }
598         if ( $flag eq 'ODUES' ) {
599             $template->param(
600                 odues    => 'true',
601                 flagged  => 1,
602                 oduesmsg => $flags->{'ODUES'}->{'message'}
603             );
604
605             my $items = $flags->{$flag}->{'itemlist'};
606 # useless ???
607 #             {
608 #                 my @itemswaiting;
609 #                 foreach my $item (@$items) {
610 #                     my ($iteminformation) =
611 #                         getiteminformation( $item->{'itemnumber'}, 0 );
612 #                     push @itemswaiting, $iteminformation;
613 #                 }
614 #             }
615             if ( $query->param('module') ne 'returns' ) {
616                 $template->param( nonreturns => 'true' );
617             }
618         }
619         if ( $flag eq 'NOTES' ) {
620             $template->param(
621                 notes    => 'true',
622                 flagged  => 1,
623                 notesmsg => $flags->{'NOTES'}->{'message'}
624             );
625         }
626     }
627 }
628
629 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
630 my @temp = split( /\$/, $amountold );
631
632     if ( $borrower->{'category_type'} eq 'C') {
633         my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
634         my $cnt = scalar(@$catcodes);
635         $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
636         $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
637     }
638
639 my $CGIorganisations;
640 my $member_of_institution;
641 if ( C4::Context->preference("memberofinstitution") ) {
642     my $organisations = get_institutions();
643     my @orgs;
644     my %org_labels;
645     foreach my $organisation ( keys %$organisations ) {
646         push @orgs, $organisation;
647         $org_labels{$organisation} =
648           $organisations->{$organisation}->{'surname'};
649     }
650     $member_of_institution = 1;
651     $CGIorganisations      = CGI::popup_menu(
652         -id     => 'organisations',
653         -name   => 'organisations',
654         -labels => \%org_labels,
655         -values => \@orgs,
656     );
657 }
658
659 $amountold = $temp[1];
660
661
662 $template->param(
663     issued_itemtypes_count_loop => $issued_itemtypes_loop,
664     findborrower                => $findborrower,
665     borrower                    => $borrower,
666     borrowernumber              => $borrowernumber,
667     branch                      => $branch,
668     branchname                  => GetBranchName($borrower->{'branchcode'}),
669     printer                     => $printer,
670     printername                 => $printer,
671     firstname                   => $borrower->{'firstname'},
672     surname                     => $borrower->{'surname'},
673     dateexpiry        => format_date($newexpiry),
674     expiry            => format_date($borrower->{'dateexpiry'}),
675     categorycode      => $borrower->{'categorycode'},
676     categoryname      => $borrowercategory->{description},
677     address           => $borrower->{'address'},
678     address2          => $borrower->{'address2'},
679     email             => $borrower->{'email'},
680     emailpro          => $borrower->{'emailpro'},
681     borrowernotes     => $borrower->{'borrowernotes'},
682     city              => $borrower->{'city'},
683     phone             => $borrower->{'phone'} || $borrower->{'mobile'},
684     cardnumber        => $borrower->{'cardnumber'},
685     amountold         => $amountold,
686     barcode           => $barcode,
687     stickyduedate     => $stickyduedate,
688     message           => $message,
689     CGIselectborrower => $CGIselectborrower,
690     todayissues       => \@todaysissues,
691     previssues        => \@previousissues,
692     inprocess         => $inprocess,
693     memberofinstution => $member_of_institution,
694     CGIorganisations  => $CGIorganisations,
695         is_child        => ($borrower->{'category_type'} eq 'C'),
696   circview => 1,
697   
698 );
699
700 # set return date if stickyduedate
701 if ($stickyduedate) {
702     $template->param(
703         duedatespec => $duedatespec,
704     );
705 }
706
707 #if ($branchcookie) {
708 #$cookie=[$cookie, $branchcookie, $printercookie];
709 #}
710
711 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
712 $template->param( picture => 1 ) if $picture;
713
714
715 $template->param(
716     SpecifyDueDate           => C4::Context->preference("SpecifyDueDate"),
717     CircAutocompl            => C4::Context->preference("CircAutocompl"),
718         dateformat            => C4::Context->preference("dateformat"),
719     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
720 );
721 output_html_with_http_headers $query, $cookie, $template->output;