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
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
57 my $branch = $query->param('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);
68 my $printer = $query->param('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);
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");
87 my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
89 template_name => 'circ/circulation.tmpl',
93 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') || '';
124 $barcode =~ s/\s*//g;
126 my $year = $query->param('year');
127 my $month = $query->param('month');
128 my $day = $query->param('day');
129 my $stickyduedate = $query->param('stickyduedate');
130 my $duedatespec = $query->param('duedatespec');
131 my $issueconfirmed = $query->param('issueconfirmed');
132 my $cancelreserve = $query->param('cancelreserve');
133 my $organisation = $query->param('organisations');
134 my $print = $query->param('print');
135 my $newexpiry = $query->param('dateexpiry');
140 # if ($query->param('setcookies')) {
141 # $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
142 # $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
146 my ($datedue,$invalidduedate);
148 $datedue = C4::Dates->new($duedatespec);
149 $invalidduedate=1 unless $datedue;
152 #if (defined($year)) {
153 # $duedatespec = "$year-$month-$day";
155 # ($year, $month, $day) = ($duedatespec) ? split /-/, $duedatespec : (0,0,0);
158 my @datearr = localtime();
160 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
162 ( 1900 + $datearr[5] )
163 . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
164 . sprintf( "%0.2d", ( $datearr[3] ) );
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,
265 # $barcode = cuecatbarcodedecode($barcode);
266 # always check for blockers on issuing
267 my ( $error, $question ) =
268 CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
270 foreach my $impossible ( keys %$error ) {
272 $impossible => $$error{$impossible},
278 if ($issueconfirmed && $noerror) {
279 # we have no blockers for issuing and any issues needing confirmation have been resolved
280 AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
283 elsif ($issueconfirmed){
287 # Get the item title for more information
288 my $getmessageiteminfo = GetBiblioFromItemNumber(undef,$barcode);
290 foreach my $needsconfirmation ( keys %$question ) {
292 $needsconfirmation => $$question{$needsconfirmation},
293 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
294 NEEDSCONFIRMATION => 1
299 itemhomebranch => $getmessageiteminfo->{'homebranch'} ,
300 duedatespec => $duedatespec,
302 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
303 AddIssue( $borrower, $barcode, $datedue );
308 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue
309 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
311 issuecount => $issue,
315 # reload the borrower info for the sake of reseting the flags.....
316 if ($borrowernumber) {
317 $borrower = GetMemberDetails( $borrowernumber, 0 );
320 ##################################################################################
322 # show all reserves of this borrower, and the position of the reservation ....
323 if ($borrowernumber) {
326 # now we show the status of the borrower's reservations
327 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
329 my @WaitingReserveLoop;
331 foreach my $num_res (@borrowerreserv) {
333 my %getWaitingReserveInfo;
334 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
335 my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itype')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
336 my ( $transfertwhen, $transfertfrom, $transfertto ) =
337 GetTransfers( $num_res->{'itemnumber'} );
339 $getreserv{waiting} = 0;
340 $getreserv{transfered} = 0;
341 $getreserv{nottransfered} = 0;
343 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
344 $getreserv{title} = $getiteminfo->{'title'};
345 $getreserv{itemtype} = $itemtypeinfo->{'description'};
346 $getreserv{author} = $getiteminfo->{'author'};
347 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
348 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
349 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
351 # check if we have a waiting status for reservations
352 if ( $num_res->{'found'} eq 'W' ) {
353 $getreserv{color} = 'reserved';
354 $getreserv{waiting} = 1;
355 # genarate information displaying only waiting reserves
356 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
357 $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
358 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
359 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
360 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
361 if ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} ) {
362 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
366 # check transfers with the itemnumber foud in th reservation loop
367 if ($transfertwhen) {
368 $getreserv{color} = 'transfered';
369 $getreserv{transfered} = 1;
370 $getreserv{datesent} = format_date($transfertwhen);
371 $getreserv{frombranch} = GetBranchName($transfertfrom);
374 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
375 and not $transfertwhen )
377 $getreserv{nottransfered} = 1;
378 $getreserv{nottransferedby} =
379 GetBranchName( $getiteminfo->{'holdingbranch'} );
382 # if we don't have a reserv on item, we put the biblio infos and the waiting position
383 if ( $getiteminfo->{'title'} eq '' ) {
384 my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
385 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} ); # fixme - we should have item-level reserves here ?
386 $getreserv{color} = 'inwait';
387 $getreserv{title} = $getbibinfo->{'title'};
388 $getreserv{waitingposition} = $num_res->{'priority'};
389 $getreserv{nottransfered} = 0;
390 $getreserv{itemtype} = $getbibtype->{'description'};
391 $getreserv{author} = $getbibinfo->{'author'};
392 $getreserv{itemcallnumber} = '----------';
393 $getreserv{biblionumber} = $num_res->{'biblionumber'};
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,
412 # make the issued books table.
413 my $todaysissues = '';
418 ## ADDED BY JF: new itemtype issuingrules counter stuff
419 my $issued_itemtypes_loop;
420 my $issued_itemtypes_count;
421 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
422 my $issued_itemtypes_remaining; # hashref with remaining
423 my $issued_itemtypes_flags; #hashref that stores flags
424 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 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'}
439 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
440 ($it->{'can_renew'}, $it->{'can_renew_error'}) = CanBookBeRenewed(
441 $borrower->{'borrowernumber'},$it->{'itemnumber'}
443 my ($restype, $reserves) = CheckReserves($it->{'itemnumber'});
444 ($restype) and $it->{'can_renew'} = 0;
446 $it->{'dd'} = format_date($it->{'date_due'});
447 my $datedue = $it->{'date_due'};
449 $it->{'od'} = ($datedue < $todaysdate) ? 1 : 0 ;
450 ($it->{'author'} eq '') and $it->{'author'} = ' ';
451 $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
452 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
453 $issued_itemtypes_count->{ $it->{'itemtype'} }++;
455 if ( $todaysdate == $issuedate ) {
456 push @todaysissues, $it;
458 push @previousissues, $it;
461 if (C4::Context->preference("todaysIssuesDefaultSortOrder") eq 'asc'){
462 @todaysissues = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
465 @todaysissues = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
467 if (C4::Context->preference("previousIssuesDefaultSortOrder") eq 'asc'){
468 @previousissues = sort { $a->{'date_due' } cmp $b->{'date_due' } } @previousissues;
471 @previousissues = sort { $b->{'date_due' } cmp $a->{'date_due' } } @previousissues;
474 foreach my $book (@todaysissues) {
475 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
478 foreach my $book (@previousissues) {
479 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
483 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
484 # FIXME: This should utilize all the issuingrules options rather than just the defaults
485 # and it should be moved to a module
486 my $dbh = C4::Context->dbh;
488 # how many of each is allowed?
489 my $issueqty_sth = $dbh->prepare( "
490 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
492 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
495 #my @issued_itemtypes_count; # huh?
496 $issueqty_sth->execute("*");
498 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
500 # subtract how many of each this borrower has
501 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
503 ( $data->{'maxissueqty'} -
504 $issued_itemtypes_count->{ $data->{'description'} } );
506 # can't have a negative number of remaining
507 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
508 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
509 unless ( ( $data->{'maxissueqty'} < 1 )
510 || ( $data->{'itemtype'} eq "*" )
511 || ( $data->{'itemtype'} eq "CIRC" ) )
513 push @issued_itemtypes_count_loop, $data;
516 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
522 my $CGIselectborrower;
523 if ($borrowerslist) {
527 . lc $a->{'firstname'} cmp lc $b->{'surname'}
528 . lc $b->{'firstname'}
532 push @values, $_->{'borrowernumber'};
533 $labels{ $_->{'borrowernumber'} } =
534 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
536 $CGIselectborrower = CGI::scrolling_list(
537 -name => 'borrowernumber',
539 -id => 'borrowernumber',
549 my $flags = $borrower->{'flags'};
552 foreach $flag ( sort keys %$flags ) {
553 $template->param( flagged=> 1);
554 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
555 if ( $flags->{$flag}->{'noissues'} ) {
560 if ( $flag eq 'GNA' ) {
561 $template->param( gna => 'true' );
563 if ( $flag eq 'LOST' ) {
564 $template->param( lost => 'true' );
566 if ( $flag eq 'DBARRED' ) {
567 $template->param( dbarred => 'true' );
569 if ( $flag eq 'CHARGES' ) {
572 chargesmsg => $flags->{'CHARGES'}->{'message'}
575 if ( $flag eq 'CREDITS' ) {
578 creditsmsg => $flags->{'CREDITS'}->{'message'}
583 if ( $flag eq 'CHARGES' ) {
587 chargesmsg => $flags->{'CHARGES'}->{'message'}
590 if ( $flag eq 'CREDITS' ) {
593 creditsmsg => $flags->{'CREDITS'}->{'message'}
596 if ( $flag eq 'ODUES' ) {
600 oduesmsg => $flags->{'ODUES'}->{'message'}
603 my $items = $flags->{$flag}->{'itemlist'};
607 # foreach my $item (@$items) {
608 # my ($iteminformation) =
609 # getiteminformation( $item->{'itemnumber'}, 0 );
610 # push @itemswaiting, $iteminformation;
613 if ( $query->param('module') ne 'returns' ) {
614 $template->param( nonreturns => 'true' );
617 if ( $flag eq 'NOTES' ) {
621 notesmsg => $flags->{'NOTES'}->{'message'}
627 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
628 my @temp = split( /\$/, $amountold );
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];
652 my $borrowercategory = GetBorrowercategory( $borrower->{'categorycode'} );
653 my $category_type = $borrowercategory->{'category_type'};
654 ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
657 issued_itemtypes_count_loop => $issued_itemtypes_loop,
658 findborrower => $findborrower,
659 borrower => $borrower,
660 borrowernumber => $borrowernumber,
663 printername => $printer,
664 firstname => $borrower->{'firstname'},
665 surname => $borrower->{'surname'},
666 dateexpiry => format_date($newexpiry),
668 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
669 categorycode => $borrower->{'categorycode'},
670 address => $borrower->{'address'},
671 address2 => $borrower->{'address2'},
672 email => $borrower->{'email'},
673 emailpro => $borrower->{'emailpro'},
674 borrowernotes => $borrower->{'borrowernotes'},
675 city => $borrower->{'city'},
676 phone => $borrower->{'phone'} || $borrower->{'mobile'},
677 cardnumber => $borrower->{'cardnumber'},
678 amountold => $amountold,
680 stickyduedate => $stickyduedate,
682 CGIselectborrower => $CGIselectborrower,
683 todayissues => \@todaysissues,
684 previssues => \@previousissues,
685 inprocess => $inprocess,
686 memberofinstution => $member_of_institution,
687 CGIorganisations => $CGIorganisations,
692 # set return date if stickyduedate
693 if ($stickyduedate) {
695 duedatespec => $duedatespec,
699 #if ($branchcookie) {
700 #$cookie=[$cookie, $branchcookie, $printercookie];
703 # grab patron's image if available
704 my $picture = GetPatronImage($borrower->{'cardnumber'});
705 $template->param( picture => $picture );
708 SpecifyDueDate => C4::Context->preference("SpecifyDueDate"),
709 CircAutocompl => C4::Context->preference("CircAutocompl") ,
710 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
712 output_html_with_http_headers $query, $cookie, $template->output;