3 # Please use 8-character tabs for this file (indents are every 4 characters)
5 # written 8/5/2002 by Finlay
6 # script to execute issuing of books
8 # Copyright 2000-2002 Katipo Communications
10 # This file is part of Koha.
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
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.
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
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
53 # new op dev the branch and the printer are now defined by the userenv
54 # but first we have to check if someone has tried to change them
56 my $branch = $query->param('branch');
58 # update our session so the userenv is updated
59 my $sessionID = $query->cookie("CGISESSID") ;
60 my $session = get_session($sessionID);
61 $session->param('branch',$branch);
62 my $branchname = GetBranchName($branch);
63 $session->param('branchname',$branchname);
66 my $printer = $query->param('printer');
68 # update our session so the userenv is updated
69 my $sessionID = $query->cookie("CGISESSID") ;
70 my $session = get_session($sessionID);
71 $session->param('branchprinter',$printer);
74 if (!C4::Context->userenv && !$branch){
75 my $sessionID = $query->cookie("CGISESSID") ;
76 my $session = get_session($sessionID);
77 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
78 # no branch set we can't issue
79 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
84 my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
86 template_name => 'circ/circulation.tmpl',
90 flagsrequired => { circulate => 1 },
94 my $branches = GetBranches();
95 my $printers = GetPrinters();
97 my @failedrenews = $query->param('failedrenew');
99 for (@failedrenews) { $renew_failed[$_] = 1; }
101 my $findborrower = $query->param('findborrower');
102 $findborrower =~ s|,| |g;
103 #$findborrower =~ s|'| |g;
104 my $borrowernumber = $query->param('borrowernumber');
106 $branch = C4::Context->userenv->{'branch'};
107 $printer = C4::Context->userenv->{'branchprinter'};
110 # If Autolocated is not activated, we show the Circulation Parameters to chage settings of librarian
111 if (C4::Context->preference("AutoLocation") ne 1)
118 my $barcode = $query->param('barcode') || '';
120 $barcode = barcodedecode($barcode) if( $barcode && C4::Context->preference('itemBarcodeInputFilter'));
121 my $stickyduedate = $query->param('stickyduedate');
122 my $duedatespec = $query->param('duedatespec');
123 my $issueconfirmed = $query->param('issueconfirmed');
124 my $cancelreserve = $query->param('cancelreserve');
125 my $organisation = $query->param('organisations');
126 my $print = $query->param('print');
127 my $newexpiry = $query->param('dateexpiry');
128 my $debt_confirmed = $query->param('debt_confirmed') || 0; # Don't show the debt error dialog twice
133 # if ($query->param('setcookies')) {
134 # $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
135 # $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
139 my ($datedue,$invalidduedate,$globalduedate);
141 if(C4::Context->preference('globalDueDate') && (C4::Context->preference('globalDueDate') =~ C4::Dates->regexp('syspref'))){
142 $globalduedate = C4::Dates->new(C4::Context->preference('globalDueDate'));
144 my $duedatespec_allow = C4::Context->preference('SpecifyDueDate');
145 if($duedatespec_allow){
147 if ($duedatespec =~ C4::Dates->regexp('syspref')) {
148 my $tempdate = C4::Dates->new($duedatespec);
149 if ($tempdate and $tempdate->output('iso') gt C4::Dates->new()->output('iso')) {
150 # i.e., it has to be later than today/now
151 $datedue = $tempdate;
154 $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
158 $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
161 # pass global due date to tmpl if specifyduedate is true
162 # and we have no barcode (loading circ page but not checking out)
163 if($globalduedate && ! $barcode ){
164 $duedatespec = $globalduedate->output();
169 $datedue = $globalduedate if($globalduedate);
172 my $todaysdate = C4::Dates->new->output('iso');
174 # check and see if we should print
175 if ( $barcode eq '' && $print eq 'maybe' ) {
179 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
180 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
183 borrowernumber => $borrowernumber
187 if ( $print eq 'yes' && $borrowernumber ne '' ) {
188 printslip( $borrowernumber );
189 $query->param( 'borrowernumber', '' );
190 $borrowernumber = '';
194 # STEP 2 : FIND BORROWER
195 # if there is a list of find borrowers....
200 my ( $count, $borrowers ) =
201 SearchMember($findborrower, 'cardnumber', 'web' );
202 my @borrowers = @$borrowers;
203 if ( $#borrowers == -1 ) {
204 $query->param( 'findborrower', '' );
205 $message = "'$findborrower'";
207 elsif ( $#borrowers == 0 ) {
208 $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
209 $query->param( 'barcode', '' );
210 $borrowernumber = $borrowers[0]->{'borrowernumber'};
213 $borrowerslist = \@borrowers;
217 # get the borrower information.....
220 if ($borrowernumber) {
221 $borrower = GetMemberDetails( $borrowernumber, 0 );
222 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
224 # Warningdate is the date that the warning starts appearing
225 my ( $today_year, $today_month, $today_day ) = Today();
226 my ( $warning_year, $warning_month, $warning_day ) = split /-/,
227 $borrower->{'dateexpiry'};
228 my ( $enrol_year, $enrol_month, $enrol_day ) = split /-/,
229 $borrower->{'dateenrolled'};
230 # Renew day is calculated by adding the enrolment period to today
231 my ( $renew_year, $renew_month, $renew_day ) =
232 Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
233 0 , $borrower->{'enrolmentperiod'}) if ($enrol_year*$enrol_month*$enrol_day>0);
234 # if the expiry date is before today ie they have expired
235 if ( $warning_year*$warning_month*$warning_day==0
236 || Date_to_Days( $today_year, $today_month, $today_day )
237 > Date_to_Days( $warning_year, $warning_month, $warning_day ) )
239 #borrowercard expired, no issues
243 expired => format_date($borrower->{dateexpiry}),
244 renewaldate => format_date("$renew_year-$renew_month-$renew_day")
247 # check for NotifyBorrowerDeparture
248 elsif ( C4::Context->preference('NotifyBorrowerDeparture') &&
249 Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
250 Date_to_Days( $today_year, $today_month, $today_day ) )
252 # borrower card soon to expire warn librarian
253 $template->param("warndeparture" => format_date($borrower->{dateexpiry}),
255 if ( C4::Context->preference('ReturnBeforeExpiry')){
256 $template->param("returnbeforeexpiry" => 1);
261 issuecount => $issue,
271 # always check for blockers on issuing
272 my ( $error, $question ) =
273 CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
274 my $blocker = $invalidduedate ? 1 : 0;
276 delete $question->{'DEBT'} if ($debt_confirmed);
277 foreach my $impossible ( keys %$error ) {
279 $impossible => $$error{$impossible},
285 my $confirm_required = 0;
286 unless($issueconfirmed){
287 # Get the item title for more information
288 my $getmessageiteminfo = GetBiblioFromItemNumber(undef,$barcode);
289 $template->param( itemhomebranch => $getmessageiteminfo->{'homebranch'} );
291 # pass needsconfirmation to template if issuing is possible and user hasn't yet confirmed.
292 foreach my $needsconfirmation ( keys %$question ) {
294 $needsconfirmation => $$question{$needsconfirmation},
295 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
296 NEEDSCONFIRMATION => 1
298 $confirm_required = 1;
301 unless($confirm_required) {
302 AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
304 if($globalduedate && ! $stickyduedate && $duedatespec_allow ){
305 $duedatespec = $globalduedate->output();
311 # FIXME If the issue is confirmed, we launch another time GetMemberIssuesAndFines, now display the issue count after issue
312 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
313 $template->param( issuecount => $issue );
316 # reload the borrower info for the sake of reseting the flags.....
317 if ($borrowernumber) {
318 $borrower = GetMemberDetails( $borrowernumber, 0 );
321 ##################################################################################
323 # show all reserves of this borrower, and the position of the reservation ....
324 my $borrowercategory;
326 if ($borrowernumber) {
329 # now we show the status of the borrower's reservations
330 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
332 my @WaitingReserveLoop;
334 foreach my $num_res (@borrowerreserv) {
336 my %getWaitingReserveInfo;
337 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
338 my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itypes')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
339 my ( $transfertwhen, $transfertfrom, $transfertto ) =
340 GetTransfers( $num_res->{'itemnumber'} );
342 $getreserv{waiting} = 0;
343 $getreserv{transfered} = 0;
344 $getreserv{nottransfered} = 0;
346 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
347 $getreserv{title} = $getiteminfo->{'title'};
348 $getreserv{itemtype} = $itemtypeinfo->{'description'};
349 $getreserv{author} = $getiteminfo->{'author'};
350 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
351 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
352 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
353 $getreserv{waitingat} = GetBranchName( $num_res->{'branchcode'} );
354 # check if we have a waiting status for reservations
355 if ( $num_res->{'found'} eq 'W' ) {
356 $getreserv{color} = 'reserved';
357 $getreserv{waiting} = 1;
358 # genarate information displaying only waiting reserves
359 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
360 $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
361 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
362 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
363 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
364 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
365 if($num_res->{'branchcode'} eq $branch){ $getWaitingReserveInfo{waitinghere} = 1; }
367 # check transfers with the itemnumber foud in th reservation loop
368 if ($transfertwhen) {
369 $getreserv{color} = 'transfered';
370 $getreserv{transfered} = 1;
371 $getreserv{datesent} = format_date($transfertwhen);
372 $getreserv{frombranch} = GetBranchName($transfertfrom);
375 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
376 and not $transfertwhen )
378 $getreserv{nottransfered} = 1;
379 $getreserv{nottransferedby} =
380 GetBranchName( $getiteminfo->{'holdingbranch'} );
383 # if we don't have a reserv on item, we put the biblio infos and the waiting position
384 if ( $getiteminfo->{'title'} eq '' ) {
385 my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
386 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} ); # fixme - we should have item-level reserves here ?
387 $getreserv{color} = 'inwait';
388 $getreserv{title} = $getbibinfo->{'title'};
389 $getreserv{nottransfered} = 0;
390 $getreserv{itemtype} = $getbibtype->{'description'};
391 $getreserv{author} = $getbibinfo->{'author'};
392 $getreserv{biblionumber} = $num_res->{'biblionumber'};
394 $getreserv{waitingposition} = $num_res->{'priority'};
395 push( @reservloop, \%getreserv );
397 # if we have a reserve waiting, initiate waitingreserveloop
398 if ($getreserv{waiting} eq 1) {
399 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
404 # return result to the template
406 countreserv => scalar @reservloop,
407 reservloop => \@reservloop ,
408 WaitingReserveLoop => \@WaitingReserveLoop,
410 $template->param( adultborrower => 1 ) if ( $borrower->{'category_type'} eq 'A' );
413 # make the issued books table.
414 my $todaysissues = '';
419 ## ADDED BY JF: new itemtype issuingrules counter stuff
420 my $issued_itemtypes_loop;
421 my $issued_itemtypes_count;
422 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
423 my $issued_itemtypes_remaining; # hashref with remaining
424 my $issued_itemtypes_flags; #hashref that stores flags
425 my @issued_itemtypes_count_loop;
428 # get each issue of the borrower & separate them in todayissues & previous issues
429 my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
431 # split in 2 arrays for today & previous
432 foreach my $it ( @$issueslist ) {
433 # set itemtype per item-level_itype syspref - FIXME this is an ugly hack
434 $it->{'itemtype'} = ( C4::Context->preference( 'item-level_itypes' ) ) ? $it->{'itype'} : $it->{'itemtype'};
436 ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
437 $it->{'itemnumber'}, $borrower->{'borrowernumber'}
439 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
440 my ($can_renew, $can_renew_error) = CanBookBeRenewed(
441 $borrower->{'borrowernumber'},$it->{'itemnumber'}
443 $it->{"renew_error_${can_renew_error}"} = 1 if defined $can_renew_error;
444 my ( $restype, $reserves ) = CheckReserves( $it->{'itemnumber'} );
445 $it->{'can_renew'} = $can_renew;
446 $it->{'can_confirm'} = !$can_renew && !$restype;
447 $it->{'renew_error'} = $restype;
449 $it->{'dd'} = format_date($it->{'date_due'});
450 $it->{'od'} = ( $it->{'date_due'} lt $todaysdate ) ? 1 : 0 ;
451 ($it->{'author'} eq '') and $it->{'author'} = ' ';
452 $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
453 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
454 $issued_itemtypes_count->{ $it->{'itemtype'} }++;
456 if ( $todaysdate eq $it->{'issuedate'} or $todaysdate eq $it->{'lastreneweddate'} ) {
457 push @todaysissues, $it;
459 push @previousissues, $it;
462 if ( C4::Context->preference( "todaysIssuesDefaultSortOrder" ) eq 'asc' ) {
463 @todaysissues = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
466 @todaysissues = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
468 if ( C4::Context->preference( "previousIssuesDefaultSortOrder" ) eq 'asc' ){
469 @previousissues = sort { $a->{'date_due'} cmp $b->{'date_due'} } @previousissues;
472 @previousissues = sort { $b->{'date_due'} cmp $a->{'date_due'} } @previousissues;
476 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
477 # FIXME: This should utilize all the issuingrules options rather than just the defaults
478 # and it should be moved to a module
479 my $dbh = C4::Context->dbh;
481 # how many of each is allowed?
482 my $issueqty_sth = $dbh->prepare( "
483 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
485 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
488 #my @issued_itemtypes_count; # huh?
489 $issueqty_sth->execute("*"); # FIXME: Why have a WHERE clause at all with a hardcoded "*"?
491 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
493 # subtract how many of each this borrower has
494 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
496 ( $data->{'maxissueqty'} -
497 $issued_itemtypes_count->{ $data->{'description'} } );
499 # can't have a negative number of remaining
500 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
501 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
502 unless ( ( $data->{'maxissueqty'} < 1 )
503 || ( $data->{'itemtype'} eq "*" )
504 || ( $data->{'itemtype'} eq "CIRC" ) )
506 push @issued_itemtypes_count_loop, $data;
509 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
515 my $CGIselectborrower;
516 if ($borrowerslist) {
518 sort {(lc $a->{'surname'} cmp lc $b->{'surname'} || lc $a->{'firstname'} cmp lc $b->{'firstname'})
522 push @values, $_->{'borrowernumber'};
523 $labels{ $_->{'borrowernumber'} } =
524 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
526 $CGIselectborrower = CGI::scrolling_list(
527 -name => 'borrowernumber',
529 -id => 'borrowernumber',
539 my $flags = $borrower->{'flags'};
542 foreach $flag ( sort keys %$flags ) {
543 $template->param( flagged=> 1);
544 $flags->{$flag}->{'message'} =~ s#\n#<br />#g;
545 if ( $flags->{$flag}->{'noissues'} ) {
550 if ( $flag eq 'GNA' ) {
551 $template->param( gna => 'true' );
553 if ( $flag eq 'LOST' ) {
554 $template->param( lost => 'true' );
556 if ( $flag eq 'DBARRED' ) {
557 $template->param( dbarred => 'true' );
559 if ( $flag eq 'CHARGES' ) {
562 chargesmsg => $flags->{'CHARGES'}->{'message'},
563 chargesamount => $flags->{'CHARGES'}->{'amount'},
564 charges_is_blocker => 1
567 if ( $flag eq 'CREDITS' ) {
570 creditsmsg => $flags->{'CREDITS'}->{'message'}
575 if ( $flag eq 'CHARGES' ) {
579 chargesmsg => $flags->{'CHARGES'}->{'message'},
580 chargesamount => $flags->{'CHARGES'}->{'amount'},
583 if ( $flag eq 'CREDITS' ) {
586 creditsmsg => $flags->{'CREDITS'}->{'message'}
589 if ( $flag eq 'ODUES' ) {
593 oduesmsg => $flags->{'ODUES'}->{'message'}
596 my $items = $flags->{$flag}->{'itemlist'};
600 # foreach my $item (@$items) {
601 # my ($iteminformation) =
602 # getiteminformation( $item->{'itemnumber'}, 0 );
603 # push @itemswaiting, $iteminformation;
606 if ( $query->param('module') ne 'returns' ) {
607 $template->param( nonreturns => 'true' );
610 if ( $flag eq 'NOTES' ) {
614 notesmsg => $flags->{'NOTES'}->{'message'}
620 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
621 my @temp = split( /\$/, $amountold );
623 if ( $borrower->{'category_type'} eq 'C') {
624 my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
625 my $cnt = scalar(@$catcodes);
626 $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
627 $template->param( 'catcode' => $catcodes->[0]) if $cnt == 1;
630 my $CGIorganisations;
631 my $member_of_institution;
632 if ( C4::Context->preference("memberofinstitution") ) {
633 my $organisations = get_institutions();
636 foreach my $organisation ( keys %$organisations ) {
637 push @orgs, $organisation;
638 $org_labels{$organisation} =
639 $organisations->{$organisation}->{'surname'};
641 $member_of_institution = 1;
642 $CGIorganisations = CGI::popup_menu(
643 -id => 'organisations',
644 -name => 'organisations',
645 -labels => \%org_labels,
650 $amountold = $temp[1];
653 issued_itemtypes_count_loop => $issued_itemtypes_loop,
654 findborrower => $findborrower,
655 borrower => $borrower,
656 borrowernumber => $borrowernumber,
658 branchname => GetBranchName($borrower->{'branchcode'}),
660 printername => $printer,
661 firstname => $borrower->{'firstname'},
662 surname => $borrower->{'surname'},
663 dateexpiry => format_date($newexpiry),
664 expiry => format_date($borrower->{'dateexpiry'}),
665 categorycode => $borrower->{'categorycode'},
666 categoryname => $borrower->{description},
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 zipcode => $borrower->{'zipcode'},
674 phone => $borrower->{'phone'} || $borrower->{'mobile'},
675 cardnumber => $borrower->{'cardnumber'},
676 amountold => $amountold,
678 stickyduedate => $stickyduedate,
679 duedatespec => $duedatespec,
681 CGIselectborrower => $CGIselectborrower,
682 todayissues => \@todaysissues,
683 previssues => \@previousissues,
684 inprocess => $inprocess,
685 memberofinstution => $member_of_institution,
686 CGIorganisations => $CGIorganisations,
687 is_child => ($borrower->{'category_type'} eq 'C'),
692 #if ($branchcookie) {
693 #$cookie=[$cookie, $branchcookie, $printercookie];
696 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
697 $template->param( picture => 1 ) if $picture;
701 debt_confirmed => $debt_confirmed,
702 SpecifyDueDate => $duedatespec_allow,
703 CircAutocompl => C4::Context->preference("CircAutocompl"),
704 AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
705 dateformat => C4::Context->preference("dateformat"),
706 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
708 output_html_with_http_headers $query, $cookie, $template->output;