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') || '';
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');
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');
144 my ($datedue,$invalidduedate);
146 $datedue = C4::Dates->new($duedatespec);
147 $invalidduedate=1 unless $datedue;
150 #if (defined($year)) {
151 # $duedatespec = "$year-$month-$day";
153 # ($year, $month, $day) = ($duedatespec) ? split /-/, $duedatespec : (0,0,0);
156 my $todaysdate = sprintf("%-04.4d%-02.2d%-02.2d", Today());
158 # check and see if we should print
159 if ( $barcode eq '' && $print eq 'maybe' ) {
163 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
165 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
168 borrowernumber => $borrowernumber
172 if ( $print eq 'yes' && $borrowernumber ne '' ) {
173 printslip( $borrowernumber );
174 $query->param( 'borrowernumber', '' );
175 $borrowernumber = '';
179 # STEP 2 : FIND BORROWER
180 # if there is a list of find borrowers....
185 my ( $count, $borrowers ) =
186 SearchMember($findborrower, 'cardnumber', 'web' );
187 my @borrowers = @$borrowers;
188 if ( $#borrowers == -1 ) {
189 $query->param( 'findborrower', '' );
190 $message = "'$findborrower'";
192 elsif ( $#borrowers == 0 ) {
193 $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
194 $query->param( 'barcode', '' );
195 $borrowernumber = $borrowers[0]->{'borrowernumber'};
198 $borrowerslist = \@borrowers;
202 # get the borrower information.....
205 if ($borrowernumber) {
206 $borrower = GetMemberDetails( $borrowernumber, 0 );
207 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
209 # Warningdate is the date that the warning starts appearing
210 my ( $today_year, $today_month, $today_day ) = Today();
211 my ( $warning_year, $warning_month, $warning_day ) = split /-/,
212 $borrower->{'dateexpiry'};
213 my ( $enrol_year, $enrol_month, $enrol_day ) = split /-/,
214 $borrower->{'dateenrolled'};
215 # Renew day is calculated by adding the enrolment period to today
216 my ( $renew_year, $renew_month, $renew_day ) =
217 Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
218 0 , $borrower->{'enrolmentperiod'}) if ($enrol_year*$enrol_month*$enrol_day>0);
219 # if the expiry date is before today ie they have expired
220 if ( $warning_year*$warning_month*$warning_day==0
221 || Date_to_Days( $today_year, $today_month, $today_day )
222 > Date_to_Days( $warning_year, $warning_month, $warning_day ) )
224 #borrowercard expired, no issues
228 expired => format_date($borrower->{dateexpiry}),
229 renewaldate => format_date("$renew_year-$renew_month-$renew_day")
232 # check for NotifyBorrowerDeparture
233 elsif ( C4::Context->preference('NotifyBorrowerDeparture') &&
234 Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
235 Date_to_Days( $today_year, $today_month, $today_day ) )
237 # borrower card soon to expire warn librarian
238 $template->param("warndeparture" => format_date($borrower->{dateexpiry}),
240 if ( C4::Context->preference('ReturnBeforeExpiry')){
241 $template->param("returnbeforeexpiry" => 1);
246 issuecount => $issue,
256 # always check for blockers on issuing
257 my ( $error, $question ) =
258 CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
260 foreach my $impossible ( keys %$error ) {
262 $impossible => $$error{$impossible},
268 if ($issueconfirmed && $noerror) {
269 # we have no blockers for issuing and any issues needing confirmation have been resolved
270 AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
273 elsif ($issueconfirmed){
277 # Get the item title for more information
278 my $getmessageiteminfo = GetBiblioFromItemNumber(undef,$barcode);
280 # only pass needsconfirmation to template if issuing is possible
281 foreach my $needsconfirmation ( keys %$question ) {
283 $needsconfirmation => $$question{$needsconfirmation},
284 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
285 NEEDSCONFIRMATION => 1
290 # only pass needsconfirmation to template if issuing is possible
292 itemhomebranch => $getmessageiteminfo->{'homebranch'} ,
293 duedatespec => $duedatespec,
295 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
296 AddIssue( $borrower, $barcode, $datedue );
301 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue
302 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
304 issuecount => $issue,
308 # reload the borrower info for the sake of reseting the flags.....
309 if ($borrowernumber) {
310 $borrower = GetMemberDetails( $borrowernumber, 0 );
313 ##################################################################################
315 # show all reserves of this borrower, and the position of the reservation ....
316 my $borrowercategory;
318 if ($borrowernumber) {
321 # now we show the status of the borrower's reservations
322 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
324 my @WaitingReserveLoop;
326 foreach my $num_res (@borrowerreserv) {
328 my %getWaitingReserveInfo;
329 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
330 my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itypes')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
331 my ( $transfertwhen, $transfertfrom, $transfertto ) =
332 GetTransfers( $num_res->{'itemnumber'} );
334 $getreserv{waiting} = 0;
335 $getreserv{transfered} = 0;
336 $getreserv{nottransfered} = 0;
338 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
339 $getreserv{title} = $getiteminfo->{'title'};
340 $getreserv{itemtype} = $itemtypeinfo->{'description'};
341 $getreserv{author} = $getiteminfo->{'author'};
342 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
343 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
344 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
345 $getreserv{waitingat} = GetBranchName( $num_res->{'branchcode'} );
346 # check if we have a waiting status for reservations
347 if ( $num_res->{'found'} eq 'W' ) {
348 $getreserv{color} = 'reserved';
349 $getreserv{waiting} = 1;
350 # genarate information displaying only waiting reserves
351 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
352 $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
353 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
354 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
355 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
356 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
357 if($num_res->{'branchcode'} eq $branch){ $getWaitingReserveInfo{waitinghere} = 1; }
359 # check transfers with the itemnumber foud in th reservation loop
360 if ($transfertwhen) {
361 $getreserv{color} = 'transfered';
362 $getreserv{transfered} = 1;
363 $getreserv{datesent} = format_date($transfertwhen);
364 $getreserv{frombranch} = GetBranchName($transfertfrom);
367 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
368 and not $transfertwhen )
370 $getreserv{nottransfered} = 1;
371 $getreserv{nottransferedby} =
372 GetBranchName( $getiteminfo->{'holdingbranch'} );
375 # if we don't have a reserv on item, we put the biblio infos and the waiting position
376 if ( $getiteminfo->{'title'} eq '' ) {
377 my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
378 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} ); # fixme - we should have item-level reserves here ?
379 $getreserv{color} = 'inwait';
380 $getreserv{title} = $getbibinfo->{'title'};
381 $getreserv{waitingposition} = $num_res->{'priority'};
382 $getreserv{nottransfered} = 0;
383 $getreserv{itemtype} = $getbibtype->{'description'};
384 $getreserv{author} = $getbibinfo->{'author'};
385 $getreserv{biblionumber} = $num_res->{'biblionumber'};
387 push( @reservloop, \%getreserv );
389 # if we have a reserve waiting, initiate waitingreserveloop
390 if ($getreserv{waiting} eq 1) {
391 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
396 # return result to the template
398 countreserv => scalar @reservloop,
399 reservloop => \@reservloop ,
400 WaitingReserveLoop => \@WaitingReserveLoop,
402 $template->param( adultborrower => 1 ) if ( $borrower->{'category_type'} eq 'A' );
405 # make the issued books table.
406 my $todaysissues = '';
411 ## ADDED BY JF: new itemtype issuingrules counter stuff
412 my $issued_itemtypes_loop;
413 my $issued_itemtypes_count;
414 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
415 my $issued_itemtypes_remaining; # hashref with remaining
416 my $issued_itemtypes_flags; #hashref that stores flags
417 my @issued_itemtypes_count_loop;
421 # get each issue of the borrower & separate them in todayissues & previous issues
422 my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
424 # split in 2 arrays for today & previous
425 foreach my $it ( @$issueslist ) {
426 # set itemtype per item-level_itype syspref - FIXME this is an ugly hack
427 $it->{'itemtype'} = (C4::Context->preference('item-level_itypes')) ? $it->{'itype'} : $it->{'itemtype'};
429 my $issuedate = $it->{'issuedate'};
430 $issuedate =~ s/-//g;
431 $issuedate = substr( $issuedate, 0, 8 );
432 ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
433 $it->{'itemnumber'}, $borrower->{'borrowernumber'}
435 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
437 ($it->{'can_renew'}, $can_renew_error) = CanBookBeRenewed(
438 $borrower->{'borrowernumber'},$it->{'itemnumber'}
440 $it->{"renew_error_${can_renew_error}"} = 1 if defined $can_renew_error;
441 my ($restype, $reserves) = CheckReserves($it->{'itemnumber'});
442 ($restype) and $it->{'can_renew'} = 0;
444 $it->{'dd'} = format_date($it->{'date_due'});
445 my $datedue = $it->{'date_due'};
447 $it->{'od'} = ($datedue < $todaysdate) ? 1 : 0 ;
448 ($it->{'author'} eq '') and $it->{'author'} = ' ';
449 $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
450 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
451 $issued_itemtypes_count->{ $it->{'itemtype'} }++;
453 if ( $todaysdate == $issuedate ) {
454 push @todaysissues, $it;
456 push @previousissues, $it;
459 if (C4::Context->preference("todaysIssuesDefaultSortOrder") eq 'asc'){
460 @todaysissues = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
463 @todaysissues = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
465 if (C4::Context->preference("previousIssuesDefaultSortOrder") eq 'asc'){
466 @previousissues = sort { $a->{'date_due' } cmp $b->{'date_due' } } @previousissues;
469 @previousissues = sort { $b->{'date_due' } cmp $a->{'date_due' } } @previousissues;
472 foreach my $book (@todaysissues) {
473 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
476 foreach my $book (@previousissues) {
477 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
481 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
482 # FIXME: This should utilize all the issuingrules options rather than just the defaults
483 # and it should be moved to a module
484 my $dbh = C4::Context->dbh;
486 # how many of each is allowed?
487 my $issueqty_sth = $dbh->prepare( "
488 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
490 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
493 #my @issued_itemtypes_count; # huh?
494 $issueqty_sth->execute("*");
496 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
498 # subtract how many of each this borrower has
499 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
501 ( $data->{'maxissueqty'} -
502 $issued_itemtypes_count->{ $data->{'description'} } );
504 # can't have a negative number of remaining
505 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
506 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
507 unless ( ( $data->{'maxissueqty'} < 1 )
508 || ( $data->{'itemtype'} eq "*" )
509 || ( $data->{'itemtype'} eq "CIRC" ) )
511 push @issued_itemtypes_count_loop, $data;
514 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
520 my $CGIselectborrower;
521 if ($borrowerslist) {
525 . lc $a->{'firstname'} cmp lc $b->{'surname'}
526 . lc $b->{'firstname'}
530 push @values, $_->{'borrowernumber'};
531 $labels{ $_->{'borrowernumber'} } =
532 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
534 $CGIselectborrower = CGI::scrolling_list(
535 -name => 'borrowernumber',
537 -id => 'borrowernumber',
547 my $flags = $borrower->{'flags'};
550 foreach $flag ( sort keys %$flags ) {
551 $template->param( flagged=> 1);
552 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
553 if ( $flags->{$flag}->{'noissues'} ) {
558 if ( $flag eq 'GNA' ) {
559 $template->param( gna => 'true' );
561 if ( $flag eq 'LOST' ) {
562 $template->param( lost => 'true' );
564 if ( $flag eq 'DBARRED' ) {
565 $template->param( dbarred => 'true' );
567 if ( $flag eq 'CHARGES' ) {
570 chargesmsg => $flags->{'CHARGES'}->{'message'}
573 if ( $flag eq 'CREDITS' ) {
576 creditsmsg => $flags->{'CREDITS'}->{'message'}
581 if ( $flag eq 'CHARGES' ) {
585 chargesmsg => $flags->{'CHARGES'}->{'message'}
588 if ( $flag eq 'CREDITS' ) {
591 creditsmsg => $flags->{'CREDITS'}->{'message'}
594 if ( $flag eq 'ODUES' ) {
598 oduesmsg => $flags->{'ODUES'}->{'message'}
601 my $items = $flags->{$flag}->{'itemlist'};
605 # foreach my $item (@$items) {
606 # my ($iteminformation) =
607 # getiteminformation( $item->{'itemnumber'}, 0 );
608 # push @itemswaiting, $iteminformation;
611 if ( $query->param('module') ne 'returns' ) {
612 $template->param( nonreturns => 'true' );
615 if ( $flag eq 'NOTES' ) {
619 notesmsg => $flags->{'NOTES'}->{'message'}
625 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
626 my @temp = split( /\$/, $amountold );
628 my $CGIorganisations;
629 my $member_of_institution;
630 if ( C4::Context->preference("memberofinstitution") ) {
631 my $organisations = get_institutions();
634 foreach my $organisation ( keys %$organisations ) {
635 push @orgs, $organisation;
636 $org_labels{$organisation} =
637 $organisations->{$organisation}->{'surname'};
639 $member_of_institution = 1;
640 $CGIorganisations = CGI::popup_menu(
641 -id => 'organisations',
642 -name => 'organisations',
643 -labels => \%org_labels,
648 $amountold = $temp[1];
652 issued_itemtypes_count_loop => $issued_itemtypes_loop,
653 findborrower => $findborrower,
654 borrower => $borrower,
655 borrowernumber => $borrowernumber,
657 branchname => GetBranchName($borrower->{'branchcode'}),
659 printername => $printer,
660 firstname => $borrower->{'firstname'},
661 surname => $borrower->{'surname'},
662 dateexpiry => format_date($newexpiry),
663 expiry => format_date($borrower->{'dateexpiry'}),
664 categorycode => $borrower->{'categorycode'},
665 categoryname => $borrowercategory->{description},
666 address => $borrower->{'address'},
667 address2 => $borrower->{'address2'},
668 email => $borrower->{'email'},
669 emailpro => $borrower->{'emailpro'},
670 borrowernotes => $borrower->{'borrowernotes'},
671 city => $borrower->{'city'},
672 phone => $borrower->{'phone'} || $borrower->{'mobile'},
673 cardnumber => $borrower->{'cardnumber'},
674 amountold => $amountold,
676 stickyduedate => $stickyduedate,
678 CGIselectborrower => $CGIselectborrower,
679 todayissues => \@todaysissues,
680 previssues => \@previousissues,
681 inprocess => $inprocess,
682 memberofinstution => $member_of_institution,
683 CGIorganisations => $CGIorganisations,
688 # set return date if stickyduedate
689 if ($stickyduedate) {
691 duedatespec => $duedatespec,
695 #if ($branchcookie) {
696 #$cookie=[$cookie, $branchcookie, $printercookie];
699 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
700 $template->param( picture => 1 ) if $picture;
704 SpecifyDueDate => C4::Context->preference("SpecifyDueDate"),
705 CircAutocompl => C4::Context->preference("CircAutocompl") ,
706 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
708 output_html_with_http_headers $query, $cookie, $template->output;