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);
79 my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
81 template_name => 'circ/circulation.tmpl',
85 flagsrequired => { circulate => 1 },
88 my $branches = GetBranches();
90 my $printers = GetPrinters();
92 my @failedrenews = $query->param('failedrenew');
94 for (@failedrenews) { $renew_failed[$_] = 1; }
96 my $findborrower = $query->param('findborrower');
97 $findborrower =~ s|,| |g;
98 #$findborrower =~ s|'| |g;
99 my $borrowernumber = $query->param('borrowernumber');
101 $branch = C4::Context->userenv->{'branch'};
102 $printer = C4::Context->userenv->{'branchprinter'};
105 # If Autolocated is not activated, we show the Circulation Parameters to chage settings of librarian
106 if (C4::Context->preference("AutoLocation") ne 1)
113 my $barcode = $query->param('barcode') || '';
116 $barcode =~ s/\s*//g;
118 my $year = $query->param('year');
119 my $month = $query->param('month');
120 my $day = $query->param('day');
121 my $stickyduedate = $query->param('stickyduedate');
122 my $duedatespec = $query->param('duedatespec');
123 my $issueconfirmed = $query->param('issueconfirmed');
124 my $cancelreserve = $query->param('cancelreserve');
125 my $organisation = $query->param('organisations');
126 my $print = $query->param('print');
127 my $newexpiry = $query->param('dateexpiry');
132 # if ($query->param('setcookies')) {
133 # $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
134 # $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
138 my ($datedue,$invalidduedate);
140 $datedue = C4::Dates->new($duedatespec);
141 $invalidduedate=1 unless $datedue;
144 #if (defined($year)) {
145 # $duedatespec = "$year-$month-$day";
147 # ($year, $month, $day) = ($duedatespec) ? split /-/, $duedatespec : (0,0,0);
150 my @datearr = localtime();
152 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
154 ( 1900 + $datearr[5] )
155 . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
156 . sprintf( "%0.2d", ( $datearr[3] ) );
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}),
243 issuecount => $issue,
254 # $barcode = cuecatbarcodedecode($barcode);
256 if ($issueconfirmed) {
257 AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
261 my ( $error, $question ) =
262 CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
265 # Get the item title for more information
266 my $getmessageiteminfo = GetBiblioFromItemNumber(undef,$barcode);
268 foreach my $impossible ( keys %$error ) {
270 $impossible => $$error{$impossible},
275 foreach my $needsconfirmation ( keys %$question ) {
277 $needsconfirmation => $$question{$needsconfirmation},
278 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
279 NEEDSCONFIRMATION => 1
284 itemhomebranch => $getmessageiteminfo->{'homebranch'} ,
285 duedatespec => $duedatespec,
287 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
288 AddIssue( $borrower, $barcode, $datedue );
293 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue
294 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
296 issuecount => $issue,
300 # reload the borrower info for the sake of reseting the flags.....
301 if ($borrowernumber) {
302 $borrower = GetMemberDetails( $borrowernumber, 0 );
305 ##################################################################################
307 # show all reserves of this borrower, and the position of the reservation ....
308 if ($borrowernumber) {
311 # now we show the status of the borrower's reservations
312 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
314 my @WaitingReserveLoop;
316 foreach my $num_res (@borrowerreserv) {
318 my %getWaitingReserveInfo;
319 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
320 my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itype')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
321 my ( $transfertwhen, $transfertfrom, $transfertto ) =
322 GetTransfers( $num_res->{'itemnumber'} );
324 $getreserv{waiting} = 0;
325 $getreserv{transfered} = 0;
326 $getreserv{nottransfered} = 0;
328 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
329 $getreserv{title} = $getiteminfo->{'title'};
330 $getreserv{itemtype} = $itemtypeinfo->{'description'};
331 $getreserv{author} = $getiteminfo->{'author'};
332 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
333 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
334 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
336 # check if we have a waiting status for reservations
337 if ( $num_res->{'found'} eq 'W' ) {
338 $getreserv{color} = 'reserved';
339 $getreserv{waiting} = 1;
340 # genarate information displaying only waiting reserves
341 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
342 $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
343 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
344 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
345 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
346 if ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} ) {
347 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
351 # check transfers with the itemnumber foud in th reservation loop
352 if ($transfertwhen) {
353 $getreserv{color} = 'transfered';
354 $getreserv{transfered} = 1;
355 $getreserv{datesent} = format_date($transfertwhen);
356 $getreserv{frombranch} = GetBranchName($transfertfrom);
359 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
360 and not $transfertwhen )
362 $getreserv{nottransfered} = 1;
363 $getreserv{nottransferedby} =
364 GetBranchName( $getiteminfo->{'holdingbranch'} );
367 # if we don't have a reserv on item, we put the biblio infos and the waiting position
368 if ( $getiteminfo->{'title'} eq '' ) {
369 my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
370 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} ); # fixme - we should have item-level reserves here ?
371 $getreserv{color} = 'inwait';
372 $getreserv{title} = $getbibinfo->{'title'};
373 $getreserv{waitingposition} = $num_res->{'priority'};
374 $getreserv{nottransfered} = 0;
375 $getreserv{itemtype} = $getbibtype->{'description'};
376 $getreserv{author} = $getbibinfo->{'author'};
377 $getreserv{itemcallnumber} = '----------';
378 $getreserv{biblionumber} = $num_res->{'biblionumber'};
380 push( @reservloop, \%getreserv );
382 # if we have a reserve waiting, initiate waitingreserveloop
383 if ($getreserv{waiting} eq 1) {
384 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
389 # return result to the template
391 countreserv => scalar @reservloop,
392 reservloop => \@reservloop ,
393 WaitingReserveLoop => \@WaitingReserveLoop,
397 # make the issued books table.
398 my $todaysissues = '';
403 ## ADDED BY JF: new itemtype issuingrules counter stuff
404 my $issued_itemtypes_loop;
405 my $issued_itemtypes_count;
406 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
407 my $issued_itemtypes_remaining; # hashref with remaining
408 my $issued_itemtypes_flags; #hashref that stores flags
409 my @issued_itemtypes_count_loop;
413 # get each issue of the borrower & separate them in todayissues & previous issues
414 my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
416 # split in 2 arrays for today & previous
417 foreach my $it ( @$issueslist ) {
418 my $issuedate = $it->{'issuedate'};
419 $issuedate =~ s/-//g;
420 $issuedate = substr( $issuedate, 0, 8 );
421 ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
422 $it->{'itemnumber'}, $borrower->{'borrowernumber'}
424 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
425 ($it->{'can_renew'}, $it->{'can_renew_error'}) = CanBookBeRenewed(
426 $borrower->{'borrowernumber'},$it->{'itemnumber'}
428 my ($restype, $reserves) = CheckReserves($it->{'itemnumber'});
429 ($restype) and $it->{'can_renew'} = 0;
431 $it->{'dd'} = format_date($it->{'date_due'});
432 my $datedue = $it->{'date_due'};
434 $it->{'od'} = ($datedue < $todaysdate) ? 1 : 0 ;
435 ($it->{'author'} eq '') and $it->{'author'} = ' ';
436 $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
437 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
438 $issued_itemtypes_count->{ $it->{'itemtype'} }++;
440 if ( $todaysdate == $issuedate ) {
441 push @todaysissues, $it;
443 push @previousissues, $it;
446 if (C4::Context->preference("todaysIssuesDefaultSortOrder") eq 'asc'){
447 @todaysissues = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
450 @todaysissues = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
452 if (C4::Context->preference("previousIssuesDefaultSortOrder") eq 'asc'){
453 @previousissues = sort { $a->{'date_due' } cmp $b->{'date_due' } } @previousissues;
456 @previousissues = sort { $b->{'date_due' } cmp $a->{'date_due' } } @previousissues;
459 foreach my $book (@todaysissues) {
460 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
463 foreach my $book (@previousissues) {
464 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
468 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
469 # FIXME: This should utilize all the issuingrules options rather than just the defaults
470 # and it should be moved to a module
471 my $dbh = C4::Context->dbh;
473 # how many of each is allowed?
474 my $issueqty_sth = $dbh->prepare( "
475 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
477 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
480 #my @issued_itemtypes_count; # huh?
481 $issueqty_sth->execute("*");
483 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
485 # subtract how many of each this borrower has
486 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
488 ( $data->{'maxissueqty'} -
489 $issued_itemtypes_count->{ $data->{'description'} } );
491 # can't have a negative number of remaining
492 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
493 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
494 unless ( ( $data->{'maxissueqty'} < 1 )
495 || ( $data->{'itemtype'} eq "*" )
496 || ( $data->{'itemtype'} eq "CIRC" ) )
498 push @issued_itemtypes_count_loop, $data;
501 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
507 my $CGIselectborrower;
508 if ($borrowerslist) {
512 . lc $a->{'firstname'} cmp lc $b->{'surname'}
513 . lc $b->{'firstname'}
517 push @values, $_->{'borrowernumber'};
518 $labels{ $_->{'borrowernumber'} } =
519 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
521 $CGIselectborrower = CGI::scrolling_list(
522 -name => 'borrowernumber',
524 -id => 'borrowernumber',
534 my $flags = $borrower->{'flags'};
537 foreach $flag ( sort keys %$flags ) {
538 $template->param( flagged=> 1);
539 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
540 if ( $flags->{$flag}->{'noissues'} ) {
545 if ( $flag eq 'GNA' ) {
546 $template->param( gna => 'true' );
548 if ( $flag eq 'LOST' ) {
549 $template->param( lost => 'true' );
551 if ( $flag eq 'DBARRED' ) {
552 $template->param( dbarred => 'true' );
554 if ( $flag eq 'CHARGES' ) {
557 chargesmsg => $flags->{'CHARGES'}->{'message'}
560 if ( $flag eq 'CREDITS' ) {
563 creditsmsg => $flags->{'CREDITS'}->{'message'}
568 if ( $flag eq 'CHARGES' ) {
572 chargesmsg => $flags->{'CHARGES'}->{'message'}
575 if ( $flag eq 'CREDITS' ) {
578 creditsmsg => $flags->{'CREDITS'}->{'message'}
581 if ( $flag eq 'ODUES' ) {
585 oduesmsg => $flags->{'ODUES'}->{'message'}
588 my $items = $flags->{$flag}->{'itemlist'};
592 # foreach my $item (@$items) {
593 # my ($iteminformation) =
594 # getiteminformation( $item->{'itemnumber'}, 0 );
595 # push @itemswaiting, $iteminformation;
598 if ( $query->param('module') ne 'returns' ) {
599 $template->param( nonreturns => 'true' );
602 if ( $flag eq 'NOTES' ) {
606 notesmsg => $flags->{'NOTES'}->{'message'}
612 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
613 my @temp = split( /\$/, $amountold );
615 my $CGIorganisations;
616 my $member_of_institution;
617 if ( C4::Context->preference("memberofinstitution") ) {
618 my $organisations = get_institutions();
621 foreach my $organisation ( keys %$organisations ) {
622 push @orgs, $organisation;
623 $org_labels{$organisation} =
624 $organisations->{$organisation}->{'surname'};
626 $member_of_institution = 1;
627 $CGIorganisations = CGI::popup_menu(
628 -id => 'organisations',
629 -name => 'organisations',
630 -labels => \%org_labels,
635 $amountold = $temp[1];
637 my $borrowercategory = GetBorrowercategory( $borrower->{'categorycode'} );
638 my $category_type = $borrowercategory->{'category_type'};
639 ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
642 issued_itemtypes_count_loop => $issued_itemtypes_loop,
643 findborrower => $findborrower,
644 borrower => $borrower,
645 borrowernumber => $borrowernumber,
648 printername => $printer,
649 firstname => $borrower->{'firstname'},
650 surname => $borrower->{'surname'},
651 dateexpiry => format_date($newexpiry),
653 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
654 categorycode => $borrower->{'categorycode'},
655 address => $borrower->{'address'},
656 address2 => $borrower->{'address2'},
657 email => $borrower->{'email'},
658 emailpro => $borrower->{'emailpro'},
659 borrowernotes => $borrower->{'borrowernotes'},
660 city => $borrower->{'city'},
661 phone => $borrower->{'phone'} || $borrower->{'mobile'},
662 cardnumber => $borrower->{'cardnumber'},
663 amountold => $amountold,
665 stickyduedate => $stickyduedate,
667 CGIselectborrower => $CGIselectborrower,
668 todayissues => \@todaysissues,
669 previssues => \@previousissues,
670 inprocess => $inprocess,
671 memberofinstution => $member_of_institution,
672 CGIorganisations => $CGIorganisations,
677 # set return date if stickyduedate
678 if ($stickyduedate) {
680 duedatespec => $duedatespec,
684 #if ($branchcookie) {
685 #$cookie=[$cookie, $branchcookie, $printercookie];
689 SpecifyDueDate => C4::Context->preference("SpecifyDueDate"),
690 CircAutocompl => C4::Context->preference("CircAutocompl") ,
691 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
693 output_html_with_http_headers $query, $cookie, $template->output;