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
57 # new op dev the branch and the printer are now defined by the userenv
58 # but first we have to check if someone has tried to change them
60 my $branch = $query->param('branch');
62 # update our session so the userenv is updated
63 my $dbh=C4::Context->dbh;
64 my $sessionID = $query->cookie("CGISESSID") ;
65 my $session = get_session($sessionID);
66 $session->param('branch',$branch);
67 my $branchname = GetBranchName($branch);
68 $session->param('branchname',$branchname);
71 my $printer = $query->param('printer');
73 # update our session so the userenv is updated
74 my $dbh=C4::Context->dbh;
75 my $sessionID = $query->cookie("CGISESSID") ;
76 my $session = get_session($sessionID);
77 $session->param('branchprinter',$printer);
80 if (!C4::Context->userenv && !$branch){
81 my $sessionID = $query->cookie("CGISESSID") ;
82 my $session = get_session($sessionID);
83 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
84 # no branch set we can't issue
85 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
90 my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
92 template_name => 'circ/circulation.tmpl',
96 flagsrequired => { circulate => 1 },
99 my $branches = GetBranches();
101 my $printers = GetPrinters();
103 my @failedrenews = $query->param('failedrenew');
105 for (@failedrenews) { $renew_failed[$_] = 1; }
107 my $findborrower = $query->param('findborrower');
108 $findborrower =~ s|,| |g;
109 #$findborrower =~ s|'| |g;
110 my $borrowernumber = $query->param('borrowernumber');
112 $branch = C4::Context->userenv->{'branch'};
113 $printer = C4::Context->userenv->{'branchprinter'};
116 # If Autolocated is not activated, we show the Circulation Parameters to chage settings of librarian
117 if (C4::Context->preference("AutoLocation") ne 1)
124 my $barcode = $query->param('barcode') || '';
126 $barcode = barcodedecode($barcode) if( $barcode && C4::Context->preference('itemBarcodeInputFilter'));
127 my $year = $query->param('year');
128 my $month = $query->param('month');
129 my $day = $query->param('day');
130 my $stickyduedate = $query->param('stickyduedate');
131 my $duedatespec = $query->param('duedatespec');
132 my $issueconfirmed = $query->param('issueconfirmed');
133 my $cancelreserve = $query->param('cancelreserve');
134 my $organisation = $query->param('organisations');
135 my $print = $query->param('print');
136 my $newexpiry = $query->param('dateexpiry');
141 # if ($query->param('setcookies')) {
142 # $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
143 # $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
147 my ($datedue,$invalidduedate);
149 if ($duedatespec =~ C4::Dates->regexp('syspref')) {
150 my $tempdate = C4::Dates->new($duedatespec);
151 if ($tempdate and $tempdate->output('iso') gt C4::Dates->new()->output('iso')) {
152 # i.e., it has to be later than today/now
153 $datedue = $tempdate;
156 $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
160 $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
164 my $todaysdate = C4::Dates->new->output('iso');
166 # check and see if we should print
167 if ( $barcode eq '' && $print eq 'maybe' ) {
171 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
173 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
176 borrowernumber => $borrowernumber
180 if ( $print eq 'yes' && $borrowernumber ne '' ) {
181 printslip( $borrowernumber );
182 $query->param( 'borrowernumber', '' );
183 $borrowernumber = '';
187 # STEP 2 : FIND BORROWER
188 # if there is a list of find borrowers....
193 my ( $count, $borrowers ) =
194 SearchMember($findborrower, 'cardnumber', 'web' );
195 my @borrowers = @$borrowers;
196 if ( $#borrowers == -1 ) {
197 $query->param( 'findborrower', '' );
198 $message = "'$findborrower'";
200 elsif ( $#borrowers == 0 ) {
201 $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
202 $query->param( 'barcode', '' );
203 $borrowernumber = $borrowers[0]->{'borrowernumber'};
206 $borrowerslist = \@borrowers;
210 # get the borrower information.....
213 if ($borrowernumber) {
214 $borrower = GetMemberDetails( $borrowernumber, 0 );
215 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
217 # Warningdate is the date that the warning starts appearing
218 my ( $today_year, $today_month, $today_day ) = Today();
219 my ( $warning_year, $warning_month, $warning_day ) = split /-/,
220 $borrower->{'dateexpiry'};
221 my ( $enrol_year, $enrol_month, $enrol_day ) = split /-/,
222 $borrower->{'dateenrolled'};
223 # Renew day is calculated by adding the enrolment period to today
224 my ( $renew_year, $renew_month, $renew_day ) =
225 Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
226 0 , $borrower->{'enrolmentperiod'}) if ($enrol_year*$enrol_month*$enrol_day>0);
227 # if the expiry date is before today ie they have expired
228 if ( $warning_year*$warning_month*$warning_day==0
229 || Date_to_Days( $today_year, $today_month, $today_day )
230 > Date_to_Days( $warning_year, $warning_month, $warning_day ) )
232 #borrowercard expired, no issues
236 expired => format_date($borrower->{dateexpiry}),
237 renewaldate => format_date("$renew_year-$renew_month-$renew_day")
240 # check for NotifyBorrowerDeparture
241 elsif ( C4::Context->preference('NotifyBorrowerDeparture') &&
242 Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
243 Date_to_Days( $today_year, $today_month, $today_day ) )
245 # borrower card soon to expire warn librarian
246 $template->param("warndeparture" => format_date($borrower->{dateexpiry}),
248 if ( C4::Context->preference('ReturnBeforeExpiry')){
249 $template->param("returnbeforeexpiry" => 1);
254 issuecount => $issue,
264 # always check for blockers on issuing
265 my ( $error, $question ) =
266 CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
267 my $noerror = $invalidduedate ? 0 : 1;
268 foreach my $impossible ( keys %$error ) {
270 $impossible => $$error{$impossible},
276 if ($issueconfirmed && $noerror) {
277 # we have no blockers for issuing and any issues needing confirmation have been resolved
278 AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
281 elsif ($issueconfirmed){ # FIXME: Do something? Or is this to *intentionally* do nothing?
285 # Get the item title for more information
286 my $getmessageiteminfo = GetBiblioFromItemNumber(undef,$barcode);
288 # only pass needsconfirmation to template if issuing is possible
289 foreach my $needsconfirmation ( keys %$question ) {
291 $needsconfirmation => $$question{$needsconfirmation},
292 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
293 NEEDSCONFIRMATION => 1
297 # Because of the weird conditional structure (empty elsif block),
298 # if we reached here, $issueconfirmed must be false.
299 # Also, since we moved inside the if ($noerror) conditional,
300 # this old chunky conditional can be simplified:
301 # if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
303 AddIssue( $borrower, $barcode, $datedue );
308 itemhomebranch => $getmessageiteminfo->{'homebranch'} ,
309 duedatespec => $duedatespec,
313 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue
314 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
316 issuecount => $issue,
320 # reload the borrower info for the sake of reseting the flags.....
321 if ($borrowernumber) {
322 $borrower = GetMemberDetails( $borrowernumber, 0 );
325 ##################################################################################
327 # show all reserves of this borrower, and the position of the reservation ....
328 my $borrowercategory;
330 if ($borrowernumber) {
333 # now we show the status of the borrower's reservations
334 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
336 my @WaitingReserveLoop;
338 foreach my $num_res (@borrowerreserv) {
340 my %getWaitingReserveInfo;
341 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
342 my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itypes')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
343 my ( $transfertwhen, $transfertfrom, $transfertto ) =
344 GetTransfers( $num_res->{'itemnumber'} );
346 $getreserv{waiting} = 0;
347 $getreserv{transfered} = 0;
348 $getreserv{nottransfered} = 0;
350 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
351 $getreserv{title} = $getiteminfo->{'title'};
352 $getreserv{itemtype} = $itemtypeinfo->{'description'};
353 $getreserv{author} = $getiteminfo->{'author'};
354 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
355 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
356 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
357 $getreserv{waitingat} = GetBranchName( $num_res->{'branchcode'} );
358 # check if we have a waiting status for reservations
359 if ( $num_res->{'found'} eq 'W' ) {
360 $getreserv{color} = 'reserved';
361 $getreserv{waiting} = 1;
362 # genarate information displaying only waiting reserves
363 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
364 $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
365 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
366 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
367 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
368 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
369 if($num_res->{'branchcode'} eq $branch){ $getWaitingReserveInfo{waitinghere} = 1; }
371 # check transfers with the itemnumber foud in th reservation loop
372 if ($transfertwhen) {
373 $getreserv{color} = 'transfered';
374 $getreserv{transfered} = 1;
375 $getreserv{datesent} = format_date($transfertwhen);
376 $getreserv{frombranch} = GetBranchName($transfertfrom);
379 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
380 and not $transfertwhen )
382 $getreserv{nottransfered} = 1;
383 $getreserv{nottransferedby} =
384 GetBranchName( $getiteminfo->{'holdingbranch'} );
387 # if we don't have a reserv on item, we put the biblio infos and the waiting position
388 if ( $getiteminfo->{'title'} eq '' ) {
389 my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
390 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} ); # fixme - we should have item-level reserves here ?
391 $getreserv{color} = 'inwait';
392 $getreserv{title} = $getbibinfo->{'title'};
393 $getreserv{waitingposition} = $num_res->{'priority'};
394 $getreserv{nottransfered} = 0;
395 $getreserv{itemtype} = $getbibtype->{'description'};
396 $getreserv{author} = $getbibinfo->{'author'};
397 $getreserv{biblionumber} = $num_res->{'biblionumber'};
399 push( @reservloop, \%getreserv );
401 # if we have a reserve waiting, initiate waitingreserveloop
402 if ($getreserv{waiting} eq 1) {
403 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
408 # return result to the template
410 countreserv => scalar @reservloop,
411 reservloop => \@reservloop ,
412 WaitingReserveLoop => \@WaitingReserveLoop,
414 $template->param( adultborrower => 1 ) if ( $borrower->{'category_type'} eq 'A' );
417 # make the issued books table.
418 my $todaysissues = '';
423 ## ADDED BY JF: new itemtype issuingrules counter stuff
424 my $issued_itemtypes_loop;
425 my $issued_itemtypes_count;
426 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
427 my $issued_itemtypes_remaining; # hashref with remaining
428 my $issued_itemtypes_flags; #hashref that stores flags
429 my @issued_itemtypes_count_loop;
433 # get each issue of the borrower & separate them in todayissues & previous issues
434 my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
436 # split in 2 arrays for today & previous
437 foreach my $it ( @$issueslist ) {
438 # set itemtype per item-level_itype syspref - FIXME this is an ugly hack
439 $it->{'itemtype'} = (C4::Context->preference('item-level_itypes')) ? $it->{'itype'} : $it->{'itemtype'};
441 my $issuedate = $it->{'issuedate'};
442 $issuedate =~ s/-//g;
443 $issuedate = substr( $issuedate, 0, 8 );
444 ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
445 $it->{'itemnumber'}, $borrower->{'borrowernumber'}
447 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
449 ($it->{'can_renew'}, $can_renew_error) = CanBookBeRenewed(
450 $borrower->{'borrowernumber'},$it->{'itemnumber'}
452 $it->{"renew_error_${can_renew_error}"} = 1 if defined $can_renew_error;
453 my ($restype, $reserves) = CheckReserves($it->{'itemnumber'});
454 ($restype) and $it->{'can_renew'} = 0;
456 $it->{'dd'} = format_date($it->{'date_due'});
457 $it->{'od'} = Date_Cmp($todaysdate, $it->{'date_due'}) == 1 ? 1 : 0 ;
458 ($it->{'author'} eq '') and $it->{'author'} = ' ';
459 $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
460 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
461 $issued_itemtypes_count->{ $it->{'itemtype'} }++;
463 if ( $todaysdate == $issuedate ) {
464 push @todaysissues, $it;
466 push @previousissues, $it;
469 if (C4::Context->preference("todaysIssuesDefaultSortOrder") eq 'asc'){
470 @todaysissues = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
473 @todaysissues = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
475 if (C4::Context->preference("previousIssuesDefaultSortOrder") eq 'asc'){
476 @previousissues = sort { $a->{'date_due' } cmp $b->{'date_due' } } @previousissues;
479 @previousissues = sort { $b->{'date_due' } cmp $a->{'date_due' } } @previousissues;
482 foreach my $book (@todaysissues) {
483 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
486 foreach my $book (@previousissues) {
487 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
491 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
492 # FIXME: This should utilize all the issuingrules options rather than just the defaults
493 # and it should be moved to a module
494 my $dbh = C4::Context->dbh;
496 # how many of each is allowed?
497 my $issueqty_sth = $dbh->prepare( "
498 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
500 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
503 #my @issued_itemtypes_count; # huh?
504 $issueqty_sth->execute("*"); # FIXME: Why have a WHERE clause at all with a hardcoded "*"?
506 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
508 # subtract how many of each this borrower has
509 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
511 ( $data->{'maxissueqty'} -
512 $issued_itemtypes_count->{ $data->{'description'} } );
514 # can't have a negative number of remaining
515 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
516 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
517 unless ( ( $data->{'maxissueqty'} < 1 )
518 || ( $data->{'itemtype'} eq "*" )
519 || ( $data->{'itemtype'} eq "CIRC" ) )
521 push @issued_itemtypes_count_loop, $data;
524 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
530 my $CGIselectborrower;
531 if ($borrowerslist) {
535 . lc $a->{'firstname'} cmp lc $b->{'surname'}
536 . lc $b->{'firstname'}
540 push @values, $_->{'borrowernumber'};
541 $labels{ $_->{'borrowernumber'} } =
542 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
544 $CGIselectborrower = CGI::scrolling_list(
545 -name => 'borrowernumber',
547 -id => 'borrowernumber',
557 my $flags = $borrower->{'flags'};
560 foreach $flag ( sort keys %$flags ) {
561 $template->param( flagged=> 1);
562 $flags->{$flag}->{'message'} =~ s#\n#<br />#g;
563 if ( $flags->{$flag}->{'noissues'} ) {
568 if ( $flag eq 'GNA' ) {
569 $template->param( gna => 'true' );
571 if ( $flag eq 'LOST' ) {
572 $template->param( lost => 'true' );
574 if ( $flag eq 'DBARRED' ) {
575 $template->param( dbarred => 'true' );
577 if ( $flag eq 'CHARGES' ) {
580 chargesmsg => $flags->{'CHARGES'}->{'message'},
581 charges_is_blocker => 1
584 if ( $flag eq 'CREDITS' ) {
587 creditsmsg => $flags->{'CREDITS'}->{'message'}
592 if ( $flag eq 'CHARGES' ) {
596 chargesmsg => $flags->{'CHARGES'}->{'message'}
599 if ( $flag eq 'CREDITS' ) {
602 creditsmsg => $flags->{'CREDITS'}->{'message'}
605 if ( $flag eq 'ODUES' ) {
609 oduesmsg => $flags->{'ODUES'}->{'message'}
612 my $items = $flags->{$flag}->{'itemlist'};
616 # foreach my $item (@$items) {
617 # my ($iteminformation) =
618 # getiteminformation( $item->{'itemnumber'}, 0 );
619 # push @itemswaiting, $iteminformation;
622 if ( $query->param('module') ne 'returns' ) {
623 $template->param( nonreturns => 'true' );
626 if ( $flag eq 'NOTES' ) {
630 notesmsg => $flags->{'NOTES'}->{'message'}
636 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
637 my @temp = split( /\$/, $amountold );
639 if ( $borrower->{'category_type'} eq 'C') {
640 my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
641 my $cnt = scalar(@$catcodes);
642 $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
643 $template->param( 'catcode' => $catcodes->[0]) if $cnt == 1;
646 my $CGIorganisations;
647 my $member_of_institution;
648 if ( C4::Context->preference("memberofinstitution") ) {
649 my $organisations = get_institutions();
652 foreach my $organisation ( keys %$organisations ) {
653 push @orgs, $organisation;
654 $org_labels{$organisation} =
655 $organisations->{$organisation}->{'surname'};
657 $member_of_institution = 1;
658 $CGIorganisations = CGI::popup_menu(
659 -id => 'organisations',
660 -name => 'organisations',
661 -labels => \%org_labels,
666 $amountold = $temp[1];
670 issued_itemtypes_count_loop => $issued_itemtypes_loop,
671 findborrower => $findborrower,
672 borrower => $borrower,
673 borrowernumber => $borrowernumber,
675 branchname => GetBranchName($borrower->{'branchcode'}),
677 printername => $printer,
678 firstname => $borrower->{'firstname'},
679 surname => $borrower->{'surname'},
680 dateexpiry => format_date($newexpiry),
681 expiry => format_date($borrower->{'dateexpiry'}),
682 categorycode => $borrower->{'categorycode'},
683 categoryname => $borrowercategory->{description},
684 address => $borrower->{'address'},
685 address2 => $borrower->{'address2'},
686 email => $borrower->{'email'},
687 emailpro => $borrower->{'emailpro'},
688 borrowernotes => $borrower->{'borrowernotes'},
689 city => $borrower->{'city'},
690 phone => $borrower->{'phone'} || $borrower->{'mobile'},
691 cardnumber => $borrower->{'cardnumber'},
692 amountold => $amountold,
694 stickyduedate => $stickyduedate,
696 CGIselectborrower => $CGIselectborrower,
697 todayissues => \@todaysissues,
698 previssues => \@previousissues,
699 inprocess => $inprocess,
700 memberofinstution => $member_of_institution,
701 CGIorganisations => $CGIorganisations,
702 is_child => ($borrower->{'category_type'} eq 'C'),
707 # set return date if stickyduedate
708 if ($stickyduedate) {
710 duedatespec => $duedatespec,
714 #if ($branchcookie) {
715 #$cookie=[$cookie, $branchcookie, $printercookie];
718 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
719 $template->param( picture => 1 ) if $picture;
723 SpecifyDueDate => C4::Context->preference("SpecifyDueDate"),
724 CircAutocompl => C4::Context->preference("CircAutocompl"),
725 dateformat => C4::Context->preference("dateformat"),
726 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
728 output_html_with_http_headers $query, $cookie, $template->output;