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 $dbh=C4::Context->dbh;
60 my $sessionID = $query->cookie("CGISESSID") ;
61 my $session = get_session($sessionID);
62 $session->param('branch',$branch);
63 my $branchname = GetBranchName($branch);
64 $session->param('branchname',$branchname);
67 my $printer = $query->param('printer');
69 # update our session so the userenv is updated
70 my $dbh=C4::Context->dbh;
71 my $sessionID = $query->cookie("CGISESSID") ;
72 my $session = get_session($sessionID);
73 $session->param('branchprinter',$printer);
76 if (!C4::Context->userenv && !$branch){
77 my $sessionID = $query->cookie("CGISESSID") ;
78 my $session = get_session($sessionID);
79 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
80 # no branch set we can't issue
81 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
86 my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
88 template_name => 'circ/circulation.tmpl',
92 flagsrequired => { circulate => 1 },
96 my $branches = GetBranches();
98 my $printers = GetPrinters();
100 my @failedrenews = $query->param('failedrenew');
102 for (@failedrenews) { $renew_failed[$_] = 1; }
104 my $findborrower = $query->param('findborrower');
105 $findborrower =~ s|,| |g;
106 #$findborrower =~ s|'| |g;
107 my $borrowernumber = $query->param('borrowernumber');
109 $branch = C4::Context->userenv->{'branch'};
110 $printer = C4::Context->userenv->{'branchprinter'};
113 # If Autolocated is not activated, we show the Circulation Parameters to chage settings of librarian
114 if (C4::Context->preference("AutoLocation") ne 1)
121 my $barcode = $query->param('barcode') || '';
123 $barcode = barcodedecode($barcode) if( $barcode && 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 my $debt_confirmed = $query->param('debt_confirmed') || 0; # Don't show the debt error dialog twice
139 # if ($query->param('setcookies')) {
140 # $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
141 # $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
145 my ($datedue,$invalidduedate);
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);
162 my $todaysdate = C4::Dates->new->output('iso');
164 # check and see if we should print
165 if ( $barcode eq '' && $print eq 'maybe' ) {
169 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
171 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
174 borrowernumber => $borrowernumber
178 if ( $print eq 'yes' && $borrowernumber ne '' ) {
179 printslip( $borrowernumber );
180 $query->param( 'borrowernumber', '' );
181 $borrowernumber = '';
185 # STEP 2 : FIND BORROWER
186 # if there is a list of find borrowers....
191 my ( $count, $borrowers ) =
192 SearchMember($findborrower, 'cardnumber', 'web' );
193 my @borrowers = @$borrowers;
194 if ( $#borrowers == -1 ) {
195 $query->param( 'findborrower', '' );
196 $message = "'$findborrower'";
198 elsif ( $#borrowers == 0 ) {
199 $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
200 $query->param( 'barcode', '' );
201 $borrowernumber = $borrowers[0]->{'borrowernumber'};
204 $borrowerslist = \@borrowers;
208 # get the borrower information.....
211 if ($borrowernumber) {
212 $borrower = GetMemberDetails( $borrowernumber, 0 );
213 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
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 ) )
230 #borrowercard expired, no issues
234 expired => format_date($borrower->{dateexpiry}),
235 renewaldate => format_date("$renew_year-$renew_month-$renew_day")
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 ) )
243 # borrower card soon to expire warn librarian
244 $template->param("warndeparture" => format_date($borrower->{dateexpiry}),
246 if ( C4::Context->preference('ReturnBeforeExpiry')){
247 $template->param("returnbeforeexpiry" => 1);
252 issuecount => $issue,
262 # always check for blockers on issuing
263 my ( $error, $question ) =
264 CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
265 my $noerror = $invalidduedate ? 0 : 1;
267 delete $question->{'DEBT'} if ($debt_confirmed);
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;
432 # get each issue of the borrower & separate them in todayissues & previous issues
433 my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
435 # split in 2 arrays for today & previous
436 foreach my $it ( @$issueslist ) {
437 # set itemtype per item-level_itype syspref - FIXME this is an ugly hack
438 $it->{'itemtype'} = ( C4::Context->preference( 'item-level_itypes' ) ) ? $it->{'itype'} : $it->{'itemtype'};
440 ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
441 $it->{'itemnumber'}, $borrower->{'borrowernumber'}
443 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
445 ($it->{'can_renew'}, $can_renew_error) = CanBookBeRenewed(
446 $borrower->{'borrowernumber'},$it->{'itemnumber'}
448 $it->{"renew_error_${can_renew_error}"} = 1 if defined $can_renew_error;
449 my ( $restype, $reserves ) = CheckReserves( $it->{'itemnumber'} );
450 ( $restype ) and $it->{'can_renew'} = 0;
452 $it->{'dd'} = format_date($it->{'date_due'});
453 $it->{'od'} = ( $it->{'date_due'} lt $todaysdate ) ? 1 : 0 ;
454 ($it->{'author'} eq '') and $it->{'author'} = ' ';
455 $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
456 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
457 $issued_itemtypes_count->{ $it->{'itemtype'} }++;
459 if ( $todaysdate eq $it->{'issuedate'} or $todaysdate eq $it->{'lastreneweddate'} ) {
460 push @todaysissues, $it;
462 push @previousissues, $it;
465 if ( C4::Context->preference( "todaysIssuesDefaultSortOrder" ) eq 'asc' ) {
466 @todaysissues = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
469 @todaysissues = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
471 if ( C4::Context->preference( "previousIssuesDefaultSortOrder" ) eq 'asc' ){
472 @previousissues = sort { $a->{'date_due'} cmp $b->{'date_due'} } @previousissues;
475 @previousissues = sort { $b->{'date_due'} cmp $a->{'date_due'} } @previousissues;
478 foreach my $book ( @todaysissues ) {
479 $book->{'togglecolor'} = ( ++$i % 2 ) ? 0 : 1 ;
482 foreach my $book ( @previousissues ) {
483 $book->{'togglecolor'} = ( ++$i % 2 ) ? 0 : 1 ;
487 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
488 # FIXME: This should utilize all the issuingrules options rather than just the defaults
489 # and it should be moved to a module
490 my $dbh = C4::Context->dbh;
492 # how many of each is allowed?
493 my $issueqty_sth = $dbh->prepare( "
494 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
496 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
499 #my @issued_itemtypes_count; # huh?
500 $issueqty_sth->execute("*"); # FIXME: Why have a WHERE clause at all with a hardcoded "*"?
502 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
504 # subtract how many of each this borrower has
505 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
507 ( $data->{'maxissueqty'} -
508 $issued_itemtypes_count->{ $data->{'description'} } );
510 # can't have a negative number of remaining
511 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
512 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
513 unless ( ( $data->{'maxissueqty'} < 1 )
514 || ( $data->{'itemtype'} eq "*" )
515 || ( $data->{'itemtype'} eq "CIRC" ) )
517 push @issued_itemtypes_count_loop, $data;
520 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
526 my $CGIselectborrower;
527 if ($borrowerslist) {
531 . lc $a->{'firstname'} cmp lc $b->{'surname'}
532 . lc $b->{'firstname'}
536 push @values, $_->{'borrowernumber'};
537 $labels{ $_->{'borrowernumber'} } =
538 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
540 $CGIselectborrower = CGI::scrolling_list(
541 -name => 'borrowernumber',
543 -id => 'borrowernumber',
553 my $flags = $borrower->{'flags'};
556 foreach $flag ( sort keys %$flags ) {
557 $template->param( flagged=> 1);
558 $flags->{$flag}->{'message'} =~ s#\n#<br />#g;
559 if ( $flags->{$flag}->{'noissues'} ) {
564 if ( $flag eq 'GNA' ) {
565 $template->param( gna => 'true' );
567 if ( $flag eq 'LOST' ) {
568 $template->param( lost => 'true' );
570 if ( $flag eq 'DBARRED' ) {
571 $template->param( dbarred => 'true' );
573 if ( $flag eq 'CHARGES' ) {
576 chargesmsg => $flags->{'CHARGES'}->{'message'},
577 charges_is_blocker => 1
580 if ( $flag eq 'CREDITS' ) {
583 creditsmsg => $flags->{'CREDITS'}->{'message'}
588 if ( $flag eq 'CHARGES' ) {
592 chargesmsg => $flags->{'CHARGES'}->{'message'}
595 if ( $flag eq 'CREDITS' ) {
598 creditsmsg => $flags->{'CREDITS'}->{'message'}
601 if ( $flag eq 'ODUES' ) {
605 oduesmsg => $flags->{'ODUES'}->{'message'}
608 my $items = $flags->{$flag}->{'itemlist'};
612 # foreach my $item (@$items) {
613 # my ($iteminformation) =
614 # getiteminformation( $item->{'itemnumber'}, 0 );
615 # push @itemswaiting, $iteminformation;
618 if ( $query->param('module') ne 'returns' ) {
619 $template->param( nonreturns => 'true' );
622 if ( $flag eq 'NOTES' ) {
626 notesmsg => $flags->{'NOTES'}->{'message'}
632 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
633 my @temp = split( /\$/, $amountold );
635 if ( $borrower->{'category_type'} eq 'C') {
636 my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
637 my $cnt = scalar(@$catcodes);
638 $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
639 $template->param( 'catcode' => $catcodes->[0]) if $cnt == 1;
642 my $CGIorganisations;
643 my $member_of_institution;
644 if ( C4::Context->preference("memberofinstitution") ) {
645 my $organisations = get_institutions();
648 foreach my $organisation ( keys %$organisations ) {
649 push @orgs, $organisation;
650 $org_labels{$organisation} =
651 $organisations->{$organisation}->{'surname'};
653 $member_of_institution = 1;
654 $CGIorganisations = CGI::popup_menu(
655 -id => 'organisations',
656 -name => 'organisations',
657 -labels => \%org_labels,
662 $amountold = $temp[1];
666 issued_itemtypes_count_loop => $issued_itemtypes_loop,
667 findborrower => $findborrower,
668 borrower => $borrower,
669 borrowernumber => $borrowernumber,
671 branchname => GetBranchName($borrower->{'branchcode'}),
673 printername => $printer,
674 firstname => $borrower->{'firstname'},
675 surname => $borrower->{'surname'},
676 dateexpiry => format_date($newexpiry),
677 expiry => format_date($borrower->{'dateexpiry'}),
678 categorycode => $borrower->{'categorycode'},
679 categoryname => $borrowercategory->{description},
680 address => $borrower->{'address'},
681 address2 => $borrower->{'address2'},
682 email => $borrower->{'email'},
683 emailpro => $borrower->{'emailpro'},
684 borrowernotes => $borrower->{'borrowernotes'},
685 city => $borrower->{'city'},
686 phone => $borrower->{'phone'} || $borrower->{'mobile'},
687 cardnumber => $borrower->{'cardnumber'},
688 amountold => $amountold,
690 stickyduedate => $stickyduedate,
692 CGIselectborrower => $CGIselectborrower,
693 todayissues => \@todaysissues,
694 previssues => \@previousissues,
695 inprocess => $inprocess,
696 memberofinstution => $member_of_institution,
697 CGIorganisations => $CGIorganisations,
698 is_child => ($borrower->{'category_type'} eq 'C'),
703 # set return date if stickyduedate
704 if ($stickyduedate) {
706 duedatespec => $duedatespec,
710 #if ($branchcookie) {
711 #$cookie=[$cookie, $branchcookie, $printercookie];
714 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
715 $template->param( picture => 1 ) if $picture;
719 debt_confirmed => $debt_confirmed,
720 SpecifyDueDate => C4::Context->preference("SpecifyDueDate"),
721 CircAutocompl => C4::Context->preference("CircAutocompl"),
722 dateformat => C4::Context->preference("dateformat"),
723 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
725 output_html_with_http_headers $query, $cookie, $template->output;