First installment: Patron card generation feature
[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_itypes')) ? $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         # set itemtype per item-level_itype syspref - FIXME this is an ugly hack
431         $it->{'itemtype'} = (C4::Context->preference('item-level_itypes')) ? $it->{'itype'} : $it->{'itemtype'};
432
433         my $issuedate = $it->{'issuedate'};
434         $issuedate =~ s/-//g;
435         $issuedate = substr( $issuedate, 0, 8 );
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         my $datedue = $it->{'date_due'};
450         $datedue =~ s/-//g;
451                 $it->{'od'} = ($datedue < $todaysdate) ? 1 : 0 ;
452         ($it->{'author'} eq '') and $it->{'author'} = ' ';
453         $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
454         # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
455         $issued_itemtypes_count->{ $it->{'itemtype'} }++;
456
457         if ( $todaysdate == $issuedate ) {
458             push @todaysissues, $it;
459         } else {
460             push @previousissues, $it;
461         }
462     }
463         if (C4::Context->preference("todaysIssuesDefaultSortOrder") eq 'asc'){
464                 @todaysissues   = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
465         }
466         else {
467                 @todaysissues   = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
468         }
469         if (C4::Context->preference("previousIssuesDefaultSortOrder") eq 'asc'){
470                 @previousissues = sort { $a->{'date_due' } cmp $b->{'date_due' } } @previousissues;
471         }
472         else {
473                 @previousissues = sort { $b->{'date_due' } cmp $a->{'date_due' } } @previousissues;
474         }
475     my $i = 1;
476         foreach my $book (@todaysissues) {
477         $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
478     }
479     $i = 1;
480         foreach my $book (@previousissues) {
481         $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
482     }
483 }
484
485 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
486 # FIXME: This should utilize all the issuingrules options rather than just the defaults
487 # and it should be moved to a module
488 my $dbh = C4::Context->dbh;
489
490 # how many of each is allowed?
491 my $issueqty_sth = $dbh->prepare( "
492 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
493 FROM issuingrules
494   LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
495   WHERE categorycode=?
496 " );
497 #my @issued_itemtypes_count;  # huh?
498 $issueqty_sth->execute("*");
499
500 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
501
502     # subtract how many of each this borrower has
503     $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };  
504     $data->{'left'}  =
505       ( $data->{'maxissueqty'} -
506           $issued_itemtypes_count->{ $data->{'description'} } );
507
508     # can't have a negative number of remaining
509     if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
510     $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
511     unless ( ( $data->{'maxissueqty'} < 1 )
512         || ( $data->{'itemtype'} eq "*" )
513         || ( $data->{'itemtype'} eq "CIRC" ) )
514     {
515         push @issued_itemtypes_count_loop, $data;
516     }
517 }
518 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
519
520 #### / JF
521
522 my @values;
523 my %labels;
524 my $CGIselectborrower;
525 if ($borrowerslist) {
526     foreach (
527         sort {
528                 lc $a->{'surname'}
529               . lc $a->{'firstname'} cmp lc $b->{'surname'}
530               . lc $b->{'firstname'}
531         } @$borrowerslist
532       )
533     {
534         push @values, $_->{'borrowernumber'};
535         $labels{ $_->{'borrowernumber'} } =
536 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'address'} ";
537     }
538     $CGIselectborrower = CGI::scrolling_list(
539         -name     => 'borrowernumber',
540                 -class     => 'focus',
541                 -id          => 'borrowernumber',
542         -values   => \@values,
543         -labels   => \%labels,
544         -size     => 7,
545         -tabindex => '',
546         -multiple => 0
547     );
548 }
549
550 #title
551 my $flags = $borrower->{'flags'};
552 my $flag;
553
554 foreach $flag ( sort keys %$flags ) {
555     $template->param( flagged=> 1);
556     $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
557     if ( $flags->{$flag}->{'noissues'} ) {
558         $template->param(
559             flagged  => 1,
560             noissues => 'true',
561         );
562         if ( $flag eq 'GNA' ) {
563             $template->param( gna => 'true' );
564         }
565         if ( $flag eq 'LOST' ) {
566             $template->param( lost => 'true' );
567         }
568         if ( $flag eq 'DBARRED' ) {
569             $template->param( dbarred => 'true' );
570         }
571         if ( $flag eq 'CHARGES' ) {
572             $template->param(
573                 charges    => 'true',
574                 chargesmsg => $flags->{'CHARGES'}->{'message'}
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 my $CGIorganisations;
633 my $member_of_institution;
634 if ( C4::Context->preference("memberofinstitution") ) {
635     my $organisations = get_institutions();
636     my @orgs;
637     my %org_labels;
638     foreach my $organisation ( keys %$organisations ) {
639         push @orgs, $organisation;
640         $org_labels{$organisation} =
641           $organisations->{$organisation}->{'surname'};
642     }
643     $member_of_institution = 1;
644     $CGIorganisations      = CGI::popup_menu(
645         -id     => 'organisations',
646         -name   => 'organisations',
647         -labels => \%org_labels,
648         -values => \@orgs,
649     );
650 }
651
652 $amountold = $temp[1];
653
654 my $borrowercategory = GetBorrowercategory( $borrower->{'categorycode'} );
655 my $category_type = $borrowercategory->{'category_type'};
656 ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
657
658 $template->param(
659     issued_itemtypes_count_loop => $issued_itemtypes_loop,
660     findborrower                => $findborrower,
661     borrower                    => $borrower,
662     borrowernumber              => $borrowernumber,
663     branch                      => $branch,
664     printer                     => $printer,
665     printername                 => $printer,
666     firstname                   => $borrower->{'firstname'},
667     surname                     => $borrower->{'surname'},
668         dateexpiry => format_date($newexpiry),
669     expiry                      =>
670       $borrower->{'dateexpiry'},    #format_date($borrower->{'dateexpiry'}),
671     categorycode      => $borrower->{'categorycode'},
672     address     => $borrower->{'address'},
673     address2     => $borrower->{'address2'},
674     email      => $borrower->{'email'},
675         emailpro           => $borrower->{'emailpro'},
676     borrowernotes     => $borrower->{'borrowernotes'},
677     city              => $borrower->{'city'},
678     phone             => $borrower->{'phone'} || $borrower->{'mobile'},
679     cardnumber        => $borrower->{'cardnumber'},
680     amountold         => $amountold,
681     barcode           => $barcode,
682     stickyduedate     => $stickyduedate,
683     message           => $message,
684     CGIselectborrower => $CGIselectborrower,
685     todayissues       => \@todaysissues,
686     previssues        => \@previousissues,
687     inprocess         => $inprocess,
688     memberofinstution => $member_of_institution,
689     CGIorganisations  => $CGIorganisations,
690         circview => 1,
691         
692 );
693
694 # set return date if stickyduedate
695 if ($stickyduedate) {
696     $template->param(
697         duedatespec => $duedatespec,
698     );
699 }
700
701 #if ($branchcookie) {
702 #$cookie=[$cookie, $branchcookie, $printercookie];
703 #}
704
705 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
706 $template->param( picture => 1 ) if $picture;
707
708
709 $template->param(
710     SpecifyDueDate           => C4::Context->preference("SpecifyDueDate"),
711     CircAutocompl            => C4::Context->preference("CircAutocompl") ,
712     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
713 );
714 output_html_with_http_headers $query, $cookie, $template->output;