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(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 @datearr = localtime();
158 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
160 ( 1900 + $datearr[5] )
161 . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
162 . sprintf( "%0.2d", ( $datearr[3] ) );
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,
263 # always check for blockers on issuing
264 my ( $error, $question ) =
265 CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
267 foreach my $impossible ( keys %$error ) {
269 $impossible => $$error{$impossible},
275 if ($issueconfirmed && $noerror) {
276 # we have no blockers for issuing and any issues needing confirmation have been resolved
277 AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
280 elsif ($issueconfirmed){
284 # Get the item title for more information
285 my $getmessageiteminfo = GetBiblioFromItemNumber(undef,$barcode);
287 foreach my $needsconfirmation ( keys %$question ) {
289 $needsconfirmation => $$question{$needsconfirmation},
290 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
291 NEEDSCONFIRMATION => 1
296 itemhomebranch => $getmessageiteminfo->{'homebranch'} ,
297 duedatespec => $duedatespec,
299 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
300 AddIssue( $borrower, $barcode, $datedue );
305 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue
306 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
308 issuecount => $issue,
312 # reload the borrower info for the sake of reseting the flags.....
313 if ($borrowernumber) {
314 $borrower = GetMemberDetails( $borrowernumber, 0 );
317 ##################################################################################
319 # show all reserves of this borrower, and the position of the reservation ....
320 if ($borrowernumber) {
323 # now we show the status of the borrower's reservations
324 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
326 my @WaitingReserveLoop;
328 foreach my $num_res (@borrowerreserv) {
330 my %getWaitingReserveInfo;
331 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
332 my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itypes')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
333 my ( $transfertwhen, $transfertfrom, $transfertto ) =
334 GetTransfers( $num_res->{'itemnumber'} );
336 $getreserv{waiting} = 0;
337 $getreserv{transfered} = 0;
338 $getreserv{nottransfered} = 0;
340 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
341 $getreserv{title} = $getiteminfo->{'title'};
342 $getreserv{itemtype} = $itemtypeinfo->{'description'};
343 $getreserv{author} = $getiteminfo->{'author'};
344 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
345 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
346 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
348 # check if we have a waiting status for reservations
349 if ( $num_res->{'found'} eq 'W' ) {
350 $getreserv{color} = 'reserved';
351 $getreserv{waiting} = 1;
352 # genarate information displaying only waiting reserves
353 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
354 $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
355 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
356 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
357 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
358 if ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} ) {
359 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
363 # check transfers with the itemnumber foud in th reservation loop
364 if ($transfertwhen) {
365 $getreserv{color} = 'transfered';
366 $getreserv{transfered} = 1;
367 $getreserv{datesent} = format_date($transfertwhen);
368 $getreserv{frombranch} = GetBranchName($transfertfrom);
371 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
372 and not $transfertwhen )
374 $getreserv{nottransfered} = 1;
375 $getreserv{nottransferedby} =
376 GetBranchName( $getiteminfo->{'holdingbranch'} );
379 # if we don't have a reserv on item, we put the biblio infos and the waiting position
380 if ( $getiteminfo->{'title'} eq '' ) {
381 my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
382 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} ); # fixme - we should have item-level reserves here ?
383 $getreserv{color} = 'inwait';
384 $getreserv{title} = $getbibinfo->{'title'};
385 $getreserv{waitingposition} = $num_res->{'priority'};
386 $getreserv{nottransfered} = 0;
387 $getreserv{itemtype} = $getbibtype->{'description'};
388 $getreserv{author} = $getbibinfo->{'author'};
389 $getreserv{biblionumber} = $num_res->{'biblionumber'};
391 push( @reservloop, \%getreserv );
393 # if we have a reserve waiting, initiate waitingreserveloop
394 if ($getreserv{waiting} eq 1) {
395 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
400 # return result to the template
402 countreserv => scalar @reservloop,
403 reservloop => \@reservloop ,
404 WaitingReserveLoop => \@WaitingReserveLoop,
408 # make the issued books table.
409 my $todaysissues = '';
414 ## ADDED BY JF: new itemtype issuingrules counter stuff
415 my $issued_itemtypes_loop;
416 my $issued_itemtypes_count;
417 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
418 my $issued_itemtypes_remaining; # hashref with remaining
419 my $issued_itemtypes_flags; #hashref that stores flags
420 my @issued_itemtypes_count_loop;
424 # get each issue of the borrower & separate them in todayissues & previous issues
425 my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
427 # split in 2 arrays for today & previous
428 foreach my $it ( @$issueslist ) {
429 # set itemtype per item-level_itype syspref - FIXME this is an ugly hack
430 $it->{'itemtype'} = (C4::Context->preference('item-level_itypes')) ? $it->{'itype'} : $it->{'itemtype'};
432 my $issuedate = $it->{'issuedate'};
433 $issuedate =~ s/-//g;
434 $issuedate = substr( $issuedate, 0, 8 );
435 ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
436 $it->{'itemnumber'}, $borrower->{'borrowernumber'}
438 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
440 ($it->{'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 ($restype) and $it->{'can_renew'} = 0;
447 $it->{'dd'} = format_date($it->{'date_due'});
448 my $datedue = $it->{'date_due'};
450 $it->{'od'} = ($datedue < $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 == $issuedate ) {
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;
475 foreach my $book (@todaysissues) {
476 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
479 foreach my $book (@previousissues) {
480 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
484 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
485 # FIXME: This should utilize all the issuingrules options rather than just the defaults
486 # and it should be moved to a module
487 my $dbh = C4::Context->dbh;
489 # how many of each is allowed?
490 my $issueqty_sth = $dbh->prepare( "
491 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
493 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
496 #my @issued_itemtypes_count; # huh?
497 $issueqty_sth->execute("*");
499 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
501 # subtract how many of each this borrower has
502 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
504 ( $data->{'maxissueqty'} -
505 $issued_itemtypes_count->{ $data->{'description'} } );
507 # can't have a negative number of remaining
508 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
509 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
510 unless ( ( $data->{'maxissueqty'} < 1 )
511 || ( $data->{'itemtype'} eq "*" )
512 || ( $data->{'itemtype'} eq "CIRC" ) )
514 push @issued_itemtypes_count_loop, $data;
517 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
523 my $CGIselectborrower;
524 if ($borrowerslist) {
528 . lc $a->{'firstname'} cmp lc $b->{'surname'}
529 . lc $b->{'firstname'}
533 push @values, $_->{'borrowernumber'};
534 $labels{ $_->{'borrowernumber'} } =
535 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
537 $CGIselectborrower = CGI::scrolling_list(
538 -name => 'borrowernumber',
540 -id => 'borrowernumber',
550 my $flags = $borrower->{'flags'};
553 foreach $flag ( sort keys %$flags ) {
554 $template->param( flagged=> 1);
555 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
556 if ( $flags->{$flag}->{'noissues'} ) {
561 if ( $flag eq 'GNA' ) {
562 $template->param( gna => 'true' );
564 if ( $flag eq 'LOST' ) {
565 $template->param( lost => 'true' );
567 if ( $flag eq 'DBARRED' ) {
568 $template->param( dbarred => 'true' );
570 if ( $flag eq 'CHARGES' ) {
573 chargesmsg => $flags->{'CHARGES'}->{'message'}
576 if ( $flag eq 'CREDITS' ) {
579 creditsmsg => $flags->{'CREDITS'}->{'message'}
584 if ( $flag eq 'CHARGES' ) {
588 chargesmsg => $flags->{'CHARGES'}->{'message'}
591 if ( $flag eq 'CREDITS' ) {
594 creditsmsg => $flags->{'CREDITS'}->{'message'}
597 if ( $flag eq 'ODUES' ) {
601 oduesmsg => $flags->{'ODUES'}->{'message'}
604 my $items = $flags->{$flag}->{'itemlist'};
608 # foreach my $item (@$items) {
609 # my ($iteminformation) =
610 # getiteminformation( $item->{'itemnumber'}, 0 );
611 # push @itemswaiting, $iteminformation;
614 if ( $query->param('module') ne 'returns' ) {
615 $template->param( nonreturns => 'true' );
618 if ( $flag eq 'NOTES' ) {
622 notesmsg => $flags->{'NOTES'}->{'message'}
628 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
629 my @temp = split( /\$/, $amountold );
631 my $CGIorganisations;
632 my $member_of_institution;
633 if ( C4::Context->preference("memberofinstitution") ) {
634 my $organisations = get_institutions();
637 foreach my $organisation ( keys %$organisations ) {
638 push @orgs, $organisation;
639 $org_labels{$organisation} =
640 $organisations->{$organisation}->{'surname'};
642 $member_of_institution = 1;
643 $CGIorganisations = CGI::popup_menu(
644 -id => 'organisations',
645 -name => 'organisations',
646 -labels => \%org_labels,
651 $amountold = $temp[1];
653 my $borrowercategory = GetBorrowercategory( $borrower->{'categorycode'} );
654 my $category_type = $borrowercategory->{'category_type'};
655 ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
658 issued_itemtypes_count_loop => $issued_itemtypes_loop,
659 findborrower => $findborrower,
660 borrower => $borrower,
661 borrowernumber => $borrowernumber,
664 printername => $printer,
665 firstname => $borrower->{'firstname'},
666 surname => $borrower->{'surname'},
667 dateexpiry => format_date($newexpiry),
669 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
670 categorycode => $borrower->{'categorycode'},
671 address => $borrower->{'address'},
672 address2 => $borrower->{'address2'},
673 email => $borrower->{'email'},
674 emailpro => $borrower->{'emailpro'},
675 borrowernotes => $borrower->{'borrowernotes'},
676 city => $borrower->{'city'},
677 phone => $borrower->{'phone'} || $borrower->{'mobile'},
678 cardnumber => $borrower->{'cardnumber'},
679 amountold => $amountold,
681 stickyduedate => $stickyduedate,
683 CGIselectborrower => $CGIselectborrower,
684 todayissues => \@todaysissues,
685 previssues => \@previousissues,
686 inprocess => $inprocess,
687 memberofinstution => $member_of_institution,
688 CGIorganisations => $CGIorganisations,
693 # set return date if stickyduedate
694 if ($stickyduedate) {
696 duedatespec => $duedatespec,
700 #if ($branchcookie) {
701 #$cookie=[$cookie, $branchcookie, $printercookie];
704 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
705 $template->param( picture => 1 ) if $picture;
709 SpecifyDueDate => C4::Context->preference("SpecifyDueDate"),
710 CircAutocompl => C4::Context->preference("CircAutocompl") ,
711 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
713 output_html_with_http_headers $query, $cookie, $template->output;