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
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 = new CGI::Session("driver:MySQL", $sessionID, {Handle=>$dbh});
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 = new CGI::Session("driver:MySQL", $sessionID, {Handle=>$dbh});
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') || '';
114 my $year = $query->param('year');
115 my $month = $query->param('month');
116 my $day = $query->param('day');
117 my $stickyduedate = $query->param('stickyduedate');
118 my $issueconfirmed = $query->param('issueconfirmed');
119 my $cancelreserve = $query->param('cancelreserve');
120 my $organisation = $query->param('organisations');
121 my $print = $query->param('print');
126 # if ($query->param('setcookies')) {
127 # $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
128 # $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
133 my @datearr = localtime( time() );
135 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
137 ( 1900 + $datearr[5] )
138 . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
139 . sprintf( "%0.2d", ( $datearr[3] ) );
141 # check and see if we should print
142 if ( $barcode eq '' && $print eq 'maybe' ) {
146 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
148 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
151 borrowernumber => $borrowernumber
155 if ( $print eq 'yes' && $borrowernumber ne '' ) {
156 printslip( $borrowernumber );
157 $query->param( 'borrowernumber', '' );
158 $borrowernumber = '';
162 # STEP 2 : FIND BORROWER
163 # if there is a list of find borrowers....
168 my ( $count, $borrowers ) =
169 SearchMember($findborrower, 'cardnumber', 'web' );
170 my @borrowers = @$borrowers;
171 if ( $#borrowers == -1 ) {
172 $query->param( 'findborrower', '' );
173 $message = "'$findborrower'";
175 elsif ( $#borrowers == 0 ) {
176 $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
177 $query->param( 'barcode', '' );
178 $borrowernumber = $borrowers[0]->{'borrowernumber'};
181 $borrowerslist = \@borrowers;
185 # get the borrower information.....
189 if ($borrowernumber) {
190 $borrower = GetMemberDetails( $borrowernumber, 0 );
191 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
193 # Warningdate is the date that the warning starts appearing
194 my ( $today_year, $today_month, $today_day ) = Today();
195 my ( $warning_year, $warning_month, $warning_day ) = split /-/,
196 $borrower->{'dateexpiry'};
197 my ( $enrol_year, $enrol_month, $enrol_day ) = split /-/,
198 $borrower->{'dateenrolled'};
199 # Renew day is calculated by adding the enrolment period to today
200 my ( $renew_year, $renew_month, $renew_day ) =
201 Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
202 0 , $borrower->{'enrolmentperiod'}) if ($enrol_year*$enrol_month*$enrol_day>0);
203 # if the expiry date is before today
204 if ( $warning_year*$warning_month*$warning_day==0
205 || Date_to_Days( $today_year, $today_month, $today_day )
206 > Date_to_Days( $warning_year, $warning_month, $warning_day ) )
209 #borrowercard expired or nearly expired, warn the librarian
212 warndeparture => format_date($borrower->{dateexpiry}),
213 renewaldate => format_date("$renew_year-$renew_month-$renew_day")
216 # check for NotifyBorrowerDeparture
217 if ($warning_year*$warning_month*$warning_day==0
218 || (C4::Context->preference('NotifyBorrowerDeparture') &&
219 Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
220 Date_to_Days( $today_year, $today_month, $today_day )) )
222 $template->param("warndeparture" => format_date($borrower->{dateexpiry}));
226 issuecount => $issue,
237 $barcode = cuecatbarcodedecode($barcode);
238 my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
239 if ($issueconfirmed) {
240 AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
244 my ( $error, $question ) =
245 CanBookBeIssued( $borrower, $barcode, $year, $month, $day,
249 # Get the item title for more information
250 my $getmessageiteminfo = GetBiblioFromItemNumber(undef,$barcode);
252 foreach my $impossible ( keys %$error ) {
254 $impossible => $$error{$impossible},
259 foreach my $needsconfirmation ( keys %$question ) {
261 $needsconfirmation => $$question{$needsconfirmation},
262 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
263 NEEDSCONFIRMATION => 1
268 itemhomebranch => $getmessageiteminfo->{'homebranch'} ,
273 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
274 AddIssue( $borrower, $barcode, $datedue );
279 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue
280 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
282 issuecount => $issue,
286 # reload the borrower info for the sake of reseting the flags.....
287 if ($borrowernumber) {
288 $borrower = GetMemberDetails( $borrowernumber, 0 );
291 ##################################################################################
293 # show all reserves of this borrower, and the position of the reservation ....
294 if ($borrowernumber) {
297 # now we show the status of the borrower's reservations
298 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
300 my @WaitingReserveLoop;
302 foreach my $num_res (@borrowerreserv) {
304 my %getWaitingReserveInfo;
305 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
306 my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itype')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
307 my ( $transfertwhen, $transfertfrom, $transfertto ) =
308 GetTransfers( $num_res->{'itemnumber'} );
310 $getreserv{waiting} = 0;
311 $getreserv{transfered} = 0;
312 $getreserv{nottransfered} = 0;
314 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
315 $getreserv{title} = $getiteminfo->{'title'};
316 $getreserv{itemtype} = $itemtypeinfo->{'description'};
317 $getreserv{author} = $getiteminfo->{'author'};
318 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
319 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
320 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
322 # check if we have a waiting status for reservations
323 if ( $num_res->{'found'} eq 'W' ) {
324 $getreserv{color} = 'reserved';
325 $getreserv{waiting} = 1;
326 # genarate information displaying only waiting reserves
327 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
328 $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
329 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
330 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
331 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
332 if ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} ) {
333 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
337 # check transfers with the itemnumber foud in th reservation loop
338 if ($transfertwhen) {
339 $getreserv{color} = 'transfered';
340 $getreserv{transfered} = 1;
341 $getreserv{datesent} = format_date($transfertwhen);
342 $getreserv{frombranch} = GetBranchName($transfertfrom);
345 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
346 and not $transfertwhen )
348 $getreserv{nottransfered} = 1;
349 $getreserv{nottransferedby} =
350 GetBranchName( $getiteminfo->{'holdingbranch'} );
353 # if we don't have a reserv on item, we put the biblio infos and the waiting position
354 if ( $getiteminfo->{'title'} eq '' ) {
355 my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
356 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} ); # fixme - we should have item-level reserves here ?
357 $getreserv{color} = 'inwait';
358 $getreserv{title} = $getbibinfo->{'title'};
359 $getreserv{waitingposition} = $num_res->{'priority'};
360 $getreserv{nottransfered} = 0;
361 $getreserv{itemtype} = $getbibtype->{'description'};
362 $getreserv{author} = $getbibinfo->{'author'};
363 $getreserv{itemcallnumber} = '----------';
364 $getreserv{biblionumber} = $num_res->{'biblionumber'};
366 push( @reservloop, \%getreserv );
368 # if we have a reserve waiting, initiate waitingreserveloop
369 if ($getreserv{waiting} eq 1) {
370 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
375 # return result to the template
377 countreserv => scalar @reservloop,
378 reservloop => \@reservloop ,
379 WaitingReserveLoop => \@WaitingReserveLoop,
383 # make the issued books table.
384 my $todaysissues = '';
389 ## ADDED BY JF: new itemtype issuingrules counter stuff
390 my $issued_itemtypes_loop;
391 my $issued_itemtypes_count;
392 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
393 my $issued_itemtypes_remaining; # hashref with remaining
394 my $issued_itemtypes_flags; #hashref that stores flags
395 my @issued_itemtypes_count_loop;
399 # get each issue of the borrower & separate them in todayissues & previous issues
400 my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
402 # split in 2 arrays for today & previous
403 foreach my $it ( @$issueslist ) {
404 my $issuedate = $it->{'issuedate'};
405 $issuedate =~ s/-//g;
406 $issuedate = substr( $issuedate, 0, 8 );
407 ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
408 $it->{'itemnumber'}, $borrower->{'borrowernumber'}
410 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
411 ($it->{'can_renew'}, $it->{'can_renew_error'}) = CanBookBeRenewed(
412 $borrower->{'borrowernumber'},$it->{'itemnumber'}
414 my ($restype, $reserves) = CheckReserves($it->{'itemnumber'});
415 ($restype) and $it->{'can_renew'} = 0;
417 $it->{'dd'} = format_date($it->{'date_due'});
418 my $datedue = $it->{'date_due'};
420 $it->{'od'} = ($datedue < $todaysdate) ? 1 : 0 ;
421 ($it->{'author'} eq '') and $it->{'author'} = ' ';
422 $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
423 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
424 $issued_itemtypes_count->{ $it->{'itemtype'} }++;
426 if ( $todaysdate == $issuedate ) {
427 push @todaysissues, $it;
429 push @previousissues, $it;
432 @todaysissues = sort { $b->{'timestamp'} <=> $a->{'timestamp'} } @todaysissues;
433 @previousissues = sort { $b->{'date_due' } <=> $a->{'date_due' } } @previousissues;
435 foreach my $book (@todaysissues) {
436 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
439 foreach my $book (@previousissues) {
440 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
444 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
445 # FIXME: This should utilize all the issuingrules options rather than just the defaults
446 # and it should be moved to a module
447 my $dbh = C4::Context->dbh;
449 # how many of each is allowed?
450 my $issueqty_sth = $dbh->prepare( "
451 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
453 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
456 #my @issued_itemtypes_count; # huh?
457 $issueqty_sth->execute("*");
459 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
461 # subtract how many of each this borrower has
462 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
464 ( $data->{'maxissueqty'} -
465 $issued_itemtypes_count->{ $data->{'description'} } );
467 # can't have a negative number of remaining
468 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
469 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
470 unless ( ( $data->{'maxissueqty'} < 1 )
471 || ( $data->{'itemtype'} eq "*" )
472 || ( $data->{'itemtype'} eq "CIRC" ) )
474 push @issued_itemtypes_count_loop, $data;
477 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
483 my $CGIselectborrower;
484 if ($borrowerslist) {
488 . $a->{'firstname'} cmp $b->{'surname'}
493 push @values, $_->{'borrowernumber'};
494 $labels{ $_->{'borrowernumber'} } =
495 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
497 $CGIselectborrower = CGI::scrolling_list(
498 -name => 'borrowernumber',
500 -id => 'borrowernumber',
510 my $flags = $borrower->{'flags'};
513 foreach $flag ( sort keys %$flags ) {
515 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
516 if ( $flags->{$flag}->{'noissues'} ) {
521 if ( $flag eq 'GNA' ) {
522 $template->param( gna => 'true' );
524 if ( $flag eq 'LOST' ) {
525 $template->param( lost => 'true' );
527 if ( $flag eq 'DBARRED' ) {
528 $template->param( dbarred => 'true' );
530 if ( $flag eq 'CHARGES' ) {
533 chargesmsg => $flags->{'CHARGES'}->{'message'}
536 if ( $flag eq 'CREDITS' ) {
539 creditsmsg => $flags->{'CREDITS'}->{'message'}
544 if ( $flag eq 'CHARGES' ) {
548 chargesmsg => $flags->{'CHARGES'}->{'message'}
551 if ( $flag eq 'CREDITS' ) {
554 creditsmsg => $flags->{'CREDITS'}->{'message'}
557 if ( $flag eq 'ODUES' ) {
561 oduesmsg => $flags->{'ODUES'}->{'message'}
564 my $items = $flags->{$flag}->{'itemlist'};
568 # foreach my $item (@$items) {
569 # my ($iteminformation) =
570 # getiteminformation( $item->{'itemnumber'}, 0 );
571 # push @itemswaiting, $iteminformation;
574 if ( $query->param('module') ne 'returns' ) {
575 $template->param( nonreturns => 'true' );
578 if ( $flag eq 'NOTES' ) {
582 notesmsg => $flags->{'NOTES'}->{'message'}
588 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
589 my @temp = split( /\$/, $amountold );
591 my $CGIorganisations;
592 my $member_of_institution;
593 if ( C4::Context->preference("memberofinstitution") ) {
594 my $organisations = get_institutions();
597 foreach my $organisation ( keys %$organisations ) {
598 push @orgs, $organisation;
599 $org_labels{$organisation} =
600 $organisations->{$organisation}->{'surname'};
602 $member_of_institution = 1;
603 $CGIorganisations = CGI::popup_menu(
604 -id => 'organisations',
605 -name => 'organisations',
606 -labels => \%org_labels,
611 $amountold = $temp[1];
614 issued_itemtypes_count_loop => $issued_itemtypes_loop,
615 findborrower => $findborrower,
616 borrower => $borrower,
617 borrowernumber => $borrowernumber,
620 printername => $printer,
621 firstname => $borrower->{'firstname'},
622 surname => $borrower->{'surname'},
624 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
625 categorycode => $borrower->{'categorycode'},
626 address => $borrower->{'address'},
627 address2 => $borrower->{'address2'},
628 email => $borrower->{'email'},
629 emailpro => $borrower->{'emailpro'},
630 borrowernotes => $borrower->{'borrowernotes'},
631 city => $borrower->{'city'},
632 phone => $borrower->{'phone'} || $borrower->{'mobile'},
633 cardnumber => $borrower->{'cardnumber'},
634 amountold => $amountold,
636 stickyduedate => $stickyduedate,
638 CGIselectborrower => $CGIselectborrower,
639 todayissues => \@todaysissues,
640 previssues => \@previousissues,
641 inprocess => $inprocess,
642 memberofinstution => $member_of_institution,
643 CGIorganisations => $CGIorganisations,
646 # set return date if stickyduedate
647 if ($stickyduedate) {
648 my $t_year = "year" . $year;
649 my $t_month = "month" . $month;
650 my $t_day = "day" . $day;
658 #if ($branchcookie) {
659 #$cookie=[$cookie, $branchcookie, $printercookie];
663 SpecifyDueDate => C4::Context->preference("SpecifyDueDate")
665 $template->param( CircAutocompl => C4::Context->preference("CircAutocompl") );
666 output_html_with_http_headers $query, $cookie, $template->output;