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