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}),
251 issuecount => $issue,
262 # $barcode = cuecatbarcodedecode($barcode);
264 if ($issueconfirmed) {
265 AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
269 my ( $error, $question ) =
270 CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
273 # Get the item title for more information
274 my $getmessageiteminfo = GetBiblioFromItemNumber(undef,$barcode);
276 foreach my $impossible ( keys %$error ) {
278 $impossible => $$error{$impossible},
283 foreach my $needsconfirmation ( keys %$question ) {
285 $needsconfirmation => $$question{$needsconfirmation},
286 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
287 NEEDSCONFIRMATION => 1
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 if ($borrowernumber) {
319 # now we show the status of the borrower's reservations
320 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
322 my @WaitingReserveLoop;
324 foreach my $num_res (@borrowerreserv) {
326 my %getWaitingReserveInfo;
327 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
328 my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itype')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
329 my ( $transfertwhen, $transfertfrom, $transfertto ) =
330 GetTransfers( $num_res->{'itemnumber'} );
332 $getreserv{waiting} = 0;
333 $getreserv{transfered} = 0;
334 $getreserv{nottransfered} = 0;
336 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
337 $getreserv{title} = $getiteminfo->{'title'};
338 $getreserv{itemtype} = $itemtypeinfo->{'description'};
339 $getreserv{author} = $getiteminfo->{'author'};
340 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
341 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
342 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
344 # check if we have a waiting status for reservations
345 if ( $num_res->{'found'} eq 'W' ) {
346 $getreserv{color} = 'reserved';
347 $getreserv{waiting} = 1;
348 # genarate information displaying only waiting reserves
349 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
350 $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
351 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
352 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
353 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
354 if ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} ) {
355 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
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{itemcallnumber} = '----------';
386 $getreserv{biblionumber} = $num_res->{'biblionumber'};
388 push( @reservloop, \%getreserv );
390 # if we have a reserve waiting, initiate waitingreserveloop
391 if ($getreserv{waiting} eq 1) {
392 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
397 # return result to the template
399 countreserv => scalar @reservloop,
400 reservloop => \@reservloop ,
401 WaitingReserveLoop => \@WaitingReserveLoop,
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 my $issuedate = $it->{'issuedate'};
427 $issuedate =~ s/-//g;
428 $issuedate = substr( $issuedate, 0, 8 );
429 ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
430 $it->{'itemnumber'}, $borrower->{'borrowernumber'}
432 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
433 ($it->{'can_renew'}, $it->{'can_renew_error'}) = CanBookBeRenewed(
434 $borrower->{'borrowernumber'},$it->{'itemnumber'}
436 my ($restype, $reserves) = CheckReserves($it->{'itemnumber'});
437 ($restype) and $it->{'can_renew'} = 0;
439 $it->{'dd'} = format_date($it->{'date_due'});
440 my $datedue = $it->{'date_due'};
442 $it->{'od'} = ($datedue < $todaysdate) ? 1 : 0 ;
443 ($it->{'author'} eq '') and $it->{'author'} = ' ';
444 $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
445 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
446 $issued_itemtypes_count->{ $it->{'itemtype'} }++;
448 if ( $todaysdate == $issuedate ) {
449 push @todaysissues, $it;
451 push @previousissues, $it;
454 if (C4::Context->preference("todaysIssuesDefaultSortOrder") eq 'asc'){
455 @todaysissues = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
458 @todaysissues = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
460 if (C4::Context->preference("previousIssuesDefaultSortOrder") eq 'asc'){
461 @previousissues = sort { $a->{'date_due' } cmp $b->{'date_due' } } @previousissues;
464 @previousissues = sort { $b->{'date_due' } cmp $a->{'date_due' } } @previousissues;
467 foreach my $book (@todaysissues) {
468 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
471 foreach my $book (@previousissues) {
472 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
476 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
477 # FIXME: This should utilize all the issuingrules options rather than just the defaults
478 # and it should be moved to a module
479 my $dbh = C4::Context->dbh;
481 # how many of each is allowed?
482 my $issueqty_sth = $dbh->prepare( "
483 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
485 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
488 #my @issued_itemtypes_count; # huh?
489 $issueqty_sth->execute("*");
491 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
493 # subtract how many of each this borrower has
494 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
496 ( $data->{'maxissueqty'} -
497 $issued_itemtypes_count->{ $data->{'description'} } );
499 # can't have a negative number of remaining
500 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
501 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
502 unless ( ( $data->{'maxissueqty'} < 1 )
503 || ( $data->{'itemtype'} eq "*" )
504 || ( $data->{'itemtype'} eq "CIRC" ) )
506 push @issued_itemtypes_count_loop, $data;
509 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
515 my $CGIselectborrower;
516 if ($borrowerslist) {
520 . lc $a->{'firstname'} cmp lc $b->{'surname'}
521 . lc $b->{'firstname'}
525 push @values, $_->{'borrowernumber'};
526 $labels{ $_->{'borrowernumber'} } =
527 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
529 $CGIselectborrower = CGI::scrolling_list(
530 -name => 'borrowernumber',
532 -id => 'borrowernumber',
542 my $flags = $borrower->{'flags'};
545 foreach $flag ( sort keys %$flags ) {
546 $template->param( flagged=> 1);
547 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
548 if ( $flags->{$flag}->{'noissues'} ) {
553 if ( $flag eq 'GNA' ) {
554 $template->param( gna => 'true' );
556 if ( $flag eq 'LOST' ) {
557 $template->param( lost => 'true' );
559 if ( $flag eq 'DBARRED' ) {
560 $template->param( dbarred => 'true' );
562 if ( $flag eq 'CHARGES' ) {
565 chargesmsg => $flags->{'CHARGES'}->{'message'}
568 if ( $flag eq 'CREDITS' ) {
571 creditsmsg => $flags->{'CREDITS'}->{'message'}
576 if ( $flag eq 'CHARGES' ) {
580 chargesmsg => $flags->{'CHARGES'}->{'message'}
583 if ( $flag eq 'CREDITS' ) {
586 creditsmsg => $flags->{'CREDITS'}->{'message'}
589 if ( $flag eq 'ODUES' ) {
593 oduesmsg => $flags->{'ODUES'}->{'message'}
596 my $items = $flags->{$flag}->{'itemlist'};
600 # foreach my $item (@$items) {
601 # my ($iteminformation) =
602 # getiteminformation( $item->{'itemnumber'}, 0 );
603 # push @itemswaiting, $iteminformation;
606 if ( $query->param('module') ne 'returns' ) {
607 $template->param( nonreturns => 'true' );
610 if ( $flag eq 'NOTES' ) {
614 notesmsg => $flags->{'NOTES'}->{'message'}
620 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
621 my @temp = split( /\$/, $amountold );
623 my $CGIorganisations;
624 my $member_of_institution;
625 if ( C4::Context->preference("memberofinstitution") ) {
626 my $organisations = get_institutions();
629 foreach my $organisation ( keys %$organisations ) {
630 push @orgs, $organisation;
631 $org_labels{$organisation} =
632 $organisations->{$organisation}->{'surname'};
634 $member_of_institution = 1;
635 $CGIorganisations = CGI::popup_menu(
636 -id => 'organisations',
637 -name => 'organisations',
638 -labels => \%org_labels,
643 $amountold = $temp[1];
645 my $borrowercategory = GetBorrowercategory( $borrower->{'categorycode'} );
646 my $category_type = $borrowercategory->{'category_type'};
647 ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
650 issued_itemtypes_count_loop => $issued_itemtypes_loop,
651 findborrower => $findborrower,
652 borrower => $borrower,
653 borrowernumber => $borrowernumber,
656 printername => $printer,
657 firstname => $borrower->{'firstname'},
658 surname => $borrower->{'surname'},
659 dateexpiry => format_date($newexpiry),
661 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
662 categorycode => $borrower->{'categorycode'},
663 address => $borrower->{'address'},
664 address2 => $borrower->{'address2'},
665 email => $borrower->{'email'},
666 emailpro => $borrower->{'emailpro'},
667 borrowernotes => $borrower->{'borrowernotes'},
668 city => $borrower->{'city'},
669 phone => $borrower->{'phone'} || $borrower->{'mobile'},
670 cardnumber => $borrower->{'cardnumber'},
671 amountold => $amountold,
673 stickyduedate => $stickyduedate,
675 CGIselectborrower => $CGIselectborrower,
676 todayissues => \@todaysissues,
677 previssues => \@previousissues,
678 inprocess => $inprocess,
679 memberofinstution => $member_of_institution,
680 CGIorganisations => $CGIorganisations,
685 # set return date if stickyduedate
686 if ($stickyduedate) {
688 duedatespec => $duedatespec,
692 #if ($branchcookie) {
693 #$cookie=[$cookie, $branchcookie, $printercookie];
697 SpecifyDueDate => C4::Context->preference("SpecifyDueDate"),
698 CircAutocompl => C4::Context->preference("CircAutocompl") ,
699 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
701 output_html_with_http_headers $query, $cookie, $template->output;