bug fixing on 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 ( Date_to_Days( $today_year, $today_month, $today_day ) >
207         Date_to_Days( $warning_year, $warning_month, $warning_day ) )
208     {
209
210         #borrowercard expired or nearly expired, warn the librarian
211         $template->param(
212             flagged       => "1",
213             warndeparture => format_date("$warning_year-$warning_month-$warning_day"),
214             renewaldate   => "$renew_year-$renew_month-$renew_day"
215         );
216     }
217     # check for NotifyBorrowerDeparture
218         if (C4::Context->preference('NotifyBorrowerDeparture') &&
219             Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
220             Date_to_Days( $today_year, $today_month, $today_day ) ) 
221         {
222             $template->param("warndeparture" => 1);
223         }
224     $template->param(
225         overduecount => $od,
226         issuecount   => $issue,
227         finetotal    => $fines
228     );
229 }
230
231 #
232 # STEP 3 : ISSUING
233 #
234 #
235
236 if ($barcode) {
237     $barcode = cuecatbarcodedecode($barcode);
238     my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
239     if ($issueconfirmed) {
240         AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
241         $inprocess = 1;
242     }
243     else {
244         my ( $error, $question ) =
245           CanBookBeIssued( $borrower, $barcode, $year, $month, $day,
246             $inprocess );
247         my $noerror    = 1;
248         my $noquestion = 1;
249 #         Get the item title for more information
250     my $getmessageiteminfo  = GetBiblioFromItemNumber( undef, $barcode );
251     
252         foreach my $impossible ( keys %$error ) {
253             $template->param(
254                 $impossible => $$error{$impossible},
255                 IMPOSSIBLE  => 1
256             );
257             $noerror = 0;
258         }
259         foreach my $needsconfirmation ( keys %$question ) {
260             $template->param(
261                 $needsconfirmation => $$question{$needsconfirmation},
262                 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
263                 NEEDSCONFIRMATION  => 1
264             );
265             $noquestion = 0;
266         }
267         $template->param(
268             day   => $day,
269             month => $month,
270             year  => $year
271         );
272         if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
273             AddIssue( $borrower, $barcode, $datedue );
274             $inprocess = 1;
275         }
276     }
277     
278 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue 
279         my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
280         $template->param(
281         issuecount   => $issue,
282         );
283 }
284
285 # reload the borrower info for the sake of reseting the flags.....
286 if ($borrowernumber) {
287     $borrower = GetMemberDetails( $borrowernumber, 0 );
288 }
289
290 ##################################################################################
291 # BUILD HTML
292 # show all reserves of this borrower, and the position of the reservation ....
293 if ($borrowernumber) {
294
295     # new op dev
296     # now we show the status of the borrower's reservations
297     my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
298     my @reservloop;
299     my @WaitingReserveLoop;
300     
301     foreach my $num_res (@borrowerreserv) {
302         my %getreserv;
303         my %getWaitingReserveInfo;
304         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
305         my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
306         my ( $transfertwhen, $transfertfrom, $transfertto ) =
307           GetTransfers( $num_res->{'itemnumber'} );
308
309         $getreserv{waiting}       = 0;
310         $getreserv{transfered}    = 0;
311         $getreserv{nottransfered} = 0;
312
313         $getreserv{reservedate}    = format_date( $num_res->{'reservedate'} );
314         $getreserv{biblionumber}   = $getiteminfo->{'biblionumber'};
315         $getreserv{title}          = $getiteminfo->{'title'};
316         $getreserv{itemtype}       = $itemtypeinfo->{'description'};
317         $getreserv{author}         = $getiteminfo->{'author'};
318         $getreserv{barcodereserv}  = $getiteminfo->{'barcode'};
319         $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
320
321         #         check if we have a waiting status for reservations
322         if ( $num_res->{'found'} eq 'W' ) {
323             $getreserv{color}   = 'reserved';
324             $getreserv{waiting} = 1;
325 #     genarate information displaying only waiting reserves
326         $getWaitingReserveInfo{title}        = $getiteminfo->{'title'};
327         $getWaitingReserveInfo{itemtype}    = $itemtypeinfo->{'description'};
328         $getWaitingReserveInfo{author}        = $getiteminfo->{'author'};
329         $getWaitingReserveInfo{reservedate}    = format_date( $num_res->{'reservedate'} );
330         if ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} ) {
331         $getWaitingReserveInfo{waitingat}    = GetBranchName( $num_res->{'branchcode'} );
332         }
333     
334         }
335         #         check transfers with the itemnumber foud in th reservation loop
336         if ($transfertwhen) {
337             $getreserv{color}      = 'transfered';
338             $getreserv{transfered} = 1;
339             $getreserv{datesent}   = format_date($transfertwhen);
340             $getreserv{frombranch} = GetBranchName($transfertfrom);
341         }
342
343         if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
344             and not $transfertwhen )
345         {
346             $getreserv{nottransfered}   = 1;
347             $getreserv{nottransferedby} =
348               GetBranchName( $getiteminfo->{'holdingbranch'} );
349         }
350
351 #         if we don't have a reserv on item, we put the biblio infos and the waiting position
352         if ( $getiteminfo->{'title'} eq '' ) {
353             my $getbibinfo = GetBiblioItemData( $num_res->{'biblionumber'} );
354             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
355             $getreserv{color}           = 'inwait';
356             $getreserv{title}           = $getbibinfo->{'title'};
357             $getreserv{waitingposition} = $num_res->{'priority'};
358             $getreserv{nottransfered}   = 0;
359             $getreserv{itemtype}        = $getbibtype->{'description'};
360             $getreserv{author}          = $getbibinfo->{'author'};
361             $getreserv{itemcallnumber}  = '----------';
362
363         }
364         push( @reservloop, \%getreserv );
365
366 #         if we have a reserve waiting, initiate waitingreserveloop
367         if ($getreserv{waiting} eq 1) {
368         push (@WaitingReserveLoop, \%getWaitingReserveInfo)
369         }
370       
371     }
372
373     # return result to the template
374     $template->param( 
375         countreserv => scalar @reservloop,
376         reservloop  => \@reservloop ,
377         WaitingReserveLoop  => \@WaitingReserveLoop,
378     );
379 }
380
381 # make the issued books table.
382 my $todaysissues = '';
383 my $previssues   = '';
384 my @realtodayissues;
385 my @realprevissues;
386 my $allowborrow;
387 ## ADDED BY JF: new itemtype issuingrules counter stuff
388 my $issued_itemtypes_loop;
389 my $issued_itemtypes_count;
390 my $issued_itemtypes_allowed_count;    # hashref with total allowed by itemtype
391 my $issued_itemtypes_remaining;        # hashref with remaining
392 my $issued_itemtypes_flags;            #hashref that stores flags
393
394 if ($borrower) {
395
396 # get each issue of the borrower & separate them in todayissues & previous issues
397     my @todaysissues;
398     my @previousissues;
399     my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
400
401     # split in 2 arrays for today & previous
402     my $dbh = C4::Context->dbh;
403     foreach my $it ( @$issueslist ) {
404         my $issuedate = $it->{'timestamp'};
405         $issuedate =~ s/-//g;
406         $issuedate = substr( $issuedate, 0, 8 );
407
408         # to let perl sort this correctly
409         $it->{'timestamp'} =~ s/(-|\:| )//g;
410
411         if ( $todaysdate == $issuedate ) {
412             (
413                 $it->{'charge'},
414                 $it->{'itemtype_charge'}
415               )
416               = GetIssuingCharges(
417                 $it->{'itemnumber'},
418                 $borrower->{'borrowernumber'}
419               );
420             $it->{'charge'} =
421               sprintf( "%.2f", $it->{'charge'} );
422             (
423                 $it->{'can_renew'},
424                 $it->{'can_renew_error'}
425               )
426               = CanBookBeRenewed(
427                 $borrower->{'borrowernumber'},
428                 $it->{'itemnumber'}
429               );
430             my ( $restype, $reserves ) =
431               CheckReserves( $it->{'itemnumber'} );
432             if ($restype) {
433                 $it->{'can_renew'} = 0;
434             }
435             push @todaysissues, $it;
436         }
437         else {
438             (
439                 $it->{'charge'},
440                 $it->{'itemtype_charge'}
441               )
442               = GetIssuingCharges(
443                 $it->{'itemnumber'},
444                 $borrower->{'borrowernumber'}
445               );
446             $it->{'charge'} =
447               sprintf( "%.2f", $it->{'charge'} );
448             (
449                 $it->{'can_renew'},
450                 $it->{'can_renew_error'}
451               )
452               = CanBookBeRenewed(
453                 $borrower->{'borrowernumber'},
454                 $it->{'itemnumber'}
455               );
456             my ( $restype, $reserves ) =
457               CheckReserves( $it->{'itemnumber'} );
458             if ($restype) {
459                 $it->{'can_renew'} = 0;
460             }
461             push @previousissues, $it;
462         }
463     }
464     my $od;    # overdues
465     my $i = 0;
466     my $togglecolor;
467
468     # parses today & build Template array
469     foreach my $book ( sort { $b->{'timestamp'} <=> $a->{'timestamp'} }
470         @todaysissues )
471     {
472         #warn "TIMESTAMP".$book->{'timestamp'};
473         # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
474         $issued_itemtypes_count->{ $book->{'itemtype'} }++;
475
476         my $dd      = $book->{'date_due'};
477         my $datedue = $book->{'date_due'};
478
479         #$dd=format_date($dd);
480         $datedue =~ s/-//g;
481         if ( $datedue < $todaysdate ) {
482             $od = 1;
483         }
484         else {
485             $od = 0;
486         }
487         if ( $i % 2 ) {
488             $togglecolor = 0;
489         }
490         else {
491             $togglecolor = 1;
492         }
493         $book->{'togglecolor'} = $togglecolor;
494         $book->{'od'}          = format_date($od);
495         $book->{'dd'}          = format_date($dd);
496         if ( $book->{'author'} eq '' ) {
497             $book->{'author'} = ' ';
498         }
499         push @realtodayissues, $book;
500         $i++;
501     }
502
503     # parses previous & build Template array
504     $i = 0;
505     foreach my $book ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
506         @previousissues )
507     {
508
509         # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
510         $issued_itemtypes_count->{ $book->{'itemtype'} }++;
511
512         my $dd      = format_date($book->{'date_due'});
513         my $datedue = format_date($book->{'date_due'});
514
515         #$dd=format_date($dd);
516         my $pcolor = '';
517         my $od     = '';
518         $datedue =~ s/-//g;
519         if ( $datedue < $todaysdate ) {
520             $od = 1;
521         }
522         else {
523             $od = 0;
524         }
525         if ( $i % 2 ) {
526             $togglecolor = 0;
527         }
528         else {
529             $togglecolor = 1;
530         }
531         $book->{'togglecolor'} = $togglecolor;
532         $book->{'dd'}          = $dd;
533         $book->{'od'}          = $od;
534         if ( $book->{'author'} eq '' ) {
535             $book->{'author'} = ' ';
536         }
537         push @realprevissues, $book;
538         $i++;
539     }
540 }
541
542 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
543 # FIXME: This should utilize all the issuingrules options rather than just the defaults
544 # and it should be moved to a module
545 my $dbh = C4::Context->dbh;
546
547 # how many of each is allowed?
548 my $issueqty_sth = $dbh->prepare( "
549 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
550 FROM issuingrules
551   LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
552   WHERE categorycode=?
553 " );
554 my @issued_itemtypes_count;
555 $issueqty_sth->execute("*");
556 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
557
558     # subtract how many of each this borrower has
559     $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
560     $data->{'left'}  =
561       ( $data->{'maxissueqty'} -
562           $issued_itemtypes_count->{ $data->{'description'} } );
563
564     # can't have a negative number of remaining
565     if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
566     $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
567     unless ( ( $data->{'maxissueqty'} < 1 )
568         || ( $data->{'itemtype'} eq "*" )
569         || ( $data->{'itemtype'} eq "CIRC" ) )
570     {
571         push @issued_itemtypes_count, $data;
572     }
573 }
574 $issued_itemtypes_loop = \@issued_itemtypes_count;
575
576 #### / JF
577
578 my @values;
579 my %labels;
580 my $CGIselectborrower;
581 if ($borrowerslist) {
582     foreach (
583         sort {
584                 $a->{'surname'}
585               . $a->{'firstname'} cmp $b->{'surname'}
586               . $b->{'firstname'}
587         } @$borrowerslist
588       )
589     {
590         push @values, $_->{'borrowernumber'};
591         $labels{ $_->{'borrowernumber'} } =
592 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'address'} ";
593     }
594     $CGIselectborrower = CGI::scrolling_list(
595         -name     => 'borrowernumber',
596         -values   => \@values,
597         -labels   => \%labels,
598         -size     => 7,
599         -tabindex => '',
600         -multiple => 0
601     );
602 }
603
604 #title
605 my $flags = $borrower->{'flags'};
606 my $flag;
607
608 foreach $flag ( sort keys %$flags ) {
609
610     $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
611     if ( $flags->{$flag}->{'noissues'} ) {
612         $template->param(
613             flagged  => 1,
614             noissues => 'true',
615         );
616         if ( $flag eq 'GNA' ) {
617             $template->param( gna => 'true' );
618         }
619         if ( $flag eq 'LOST' ) {
620             $template->param( lost => 'true' );
621         }
622         if ( $flag eq 'DBARRED' ) {
623             $template->param( dbarred => 'true' );
624         }
625         if ( $flag eq 'CHARGES' ) {
626             $template->param(
627                 charges    => 'true',
628                 chargesmsg => $flags->{'CHARGES'}->{'message'}
629             );
630         }
631         if ( $flag eq 'CREDITS' ) {
632             $template->param(
633                 credits    => 'true',
634                 creditsmsg => $flags->{'CREDITS'}->{'message'}
635             );
636         }
637     }
638     else {
639         if ( $flag eq 'CHARGES' ) {
640             $template->param(
641                 charges    => 'true',
642                 flagged    => 1,
643                 chargesmsg => $flags->{'CHARGES'}->{'message'}
644             );
645         }
646         if ( $flag eq 'CREDITS' ) {
647             $template->param(
648                 credits    => 'true',
649                 creditsmsg => $flags->{'CREDITS'}->{'message'}
650             );
651         }
652         if ( $flag eq 'ODUES' ) {
653             $template->param(
654                 odues    => 'true',
655                 flagged  => 1,
656                 oduesmsg => $flags->{'ODUES'}->{'message'}
657             );
658
659             my $items = $flags->{$flag}->{'itemlist'};
660 # useless ???
661 #             {
662 #                 my @itemswaiting;
663 #                 foreach my $item (@$items) {
664 #                     my ($iteminformation) =
665 #                         getiteminformation( $item->{'itemnumber'}, 0 );
666 #                     push @itemswaiting, $iteminformation;
667 #                 }
668 #             }
669             if ( $query->param('module') ne 'returns' ) {
670                 $template->param( nonreturns => 'true' );
671             }
672         }
673         if ( $flag eq 'NOTES' ) {
674             $template->param(
675                 notes    => 'true',
676                 flagged  => 1,
677                 notesmsg => $flags->{'NOTES'}->{'message'}
678             );
679         }
680     }
681 }
682
683 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
684 my @temp = split( /\$/, $amountold );
685
686 my $CGIorganisations;
687 my $member_of_institution;
688 if ( C4::Context->preference("memberofinstitution") ) {
689     my $organisations = get_institutions();
690     my @orgs;
691     my %org_labels;
692     foreach my $organisation ( keys %$organisations ) {
693         push @orgs, $organisation;
694         $org_labels{$organisation} =
695           $organisations->{$organisation}->{'surname'};
696     }
697     $member_of_institution = 1;
698     $CGIorganisations      = CGI::popup_menu(
699         -id     => 'organisations',
700         -name   => 'organisations',
701         -labels => \%org_labels,
702         -values => \@orgs,
703     );
704 }
705
706 $amountold = $temp[1];
707
708 $template->param(
709     issued_itemtypes_count_loop => $issued_itemtypes_loop,
710     findborrower                => $findborrower,
711     borrower                    => $borrower,
712     borrowernumber              => $borrowernumber,
713     branch                      => $branch,
714     printer                     => $printer,
715     printername                 => $printer,
716     firstname                   => $borrower->{'firstname'},
717     surname                     => $borrower->{'surname'},
718     expiry                      =>
719       $borrower->{'dateexpiry'},    #format_date($borrower->{'dateexpiry'}),
720     categorycode      => $borrower->{'categorycode'},
721     streetaddress     => $borrower->{'address'},
722     emailaddress      => $borrower->{'emailaddress'},
723     borrowernotes     => $borrower->{'borrowernotes'},
724     city              => $borrower->{'city'},
725     phone             => $borrower->{'phone'} || $borrower->{'mobile'},
726     cardnumber        => $borrower->{'cardnumber'},
727     amountold         => $amountold,
728     barcode           => $barcode,
729     stickyduedate     => $stickyduedate,
730     message           => $message,
731     CGIselectborrower => $CGIselectborrower,
732     todayissues       => \@realtodayissues,
733     previssues        => \@realprevissues,
734     inprocess         => $inprocess,
735     memberofinstution => $member_of_institution,
736     CGIorganisations  => $CGIorganisations,
737 );
738
739 # set return date if stickyduedate
740 if ($stickyduedate) {
741     my $t_year  = "year" . $year;
742     my $t_month = "month" . $month;
743     my $t_day   = "day" . $day;
744     $template->param(
745         $t_year  => 1,
746         $t_month => 1,
747         $t_day   => 1,
748     );
749 }
750
751 #if ($branchcookie) {
752 #$cookie=[$cookie, $branchcookie, $printercookie];
753 #}
754
755 $template->param(
756     SpecifyDueDate     => C4::Context->preference("SpecifyDueDate")
757 );
758 output_html_with_http_headers $query, $cookie, $template->output;