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