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($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
272 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
273 AddIssue( $borrower, $barcode, $datedue );
278 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue
279 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
281 issuecount => $issue,
285 # reload the borrower info for the sake of reseting the flags.....
286 if ($borrowernumber) {
287 $borrower = GetMemberDetails( $borrowernumber, 0 );
290 ##################################################################################
292 # show all reserves of this borrower, and the position of the reservation ....
293 if ($borrowernumber) {
296 # now we show the status of the borrower's reservations
297 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
299 my @WaitingReserveLoop;
301 foreach my $num_res (@borrowerreserv) {
303 my %getWaitingReserveInfo;
304 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
305 my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itype')) ? $getiteminfo->{'ccode'} : $getiteminfo->{'itemtype'} );
306 my ( $transfertwhen, $transfertfrom, $transfertto ) =
307 GetTransfers( $num_res->{'itemnumber'} );
309 $getreserv{waiting} = 0;
310 $getreserv{transfered} = 0;
311 $getreserv{nottransfered} = 0;
313 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
314 $getreserv{title} = $getiteminfo->{'title'};
315 $getreserv{itemtype} = $itemtypeinfo->{'description'};
316 $getreserv{author} = $getiteminfo->{'author'};
317 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
318 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
320 # check if we have a waiting status for reservations
321 if ( $num_res->{'found'} eq 'W' ) {
322 $getreserv{color} = 'reserved';
323 $getreserv{waiting} = 1;
324 # genarate information displaying only waiting reserves
325 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
326 $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
327 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
328 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
329 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
330 if ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} ) {
331 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
335 # check transfers with the itemnumber foud in th reservation loop
336 if ($transfertwhen) {
337 $getreserv{color} = 'transfered';
338 $getreserv{transfered} = 1;
339 $getreserv{datesent} = format_date($transfertwhen);
340 $getreserv{frombranch} = GetBranchName($transfertfrom);
343 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
344 and not $transfertwhen )
346 $getreserv{nottransfered} = 1;
347 $getreserv{nottransferedby} =
348 GetBranchName( $getiteminfo->{'holdingbranch'} );
351 # if we don't have a reserv on item, we put the biblio infos and the waiting position
352 if ( $getiteminfo->{'title'} eq '' ) {
353 my $getbibinfo = GetBiblioItemData( $num_res->{'biblionumber'} );
354 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} ); # fixme - we should have item-level reserves here ?
355 $getreserv{color} = 'inwait';
356 $getreserv{title} = $getbibinfo->{'title'};
357 $getreserv{waitingposition} = $num_res->{'priority'};
358 $getreserv{nottransfered} = 0;
359 $getreserv{itemtype} = $getbibtype->{'description'};
360 $getreserv{author} = $getbibinfo->{'author'};
361 $getreserv{itemcallnumber} = '----------';
364 push( @reservloop, \%getreserv );
366 # if we have a reserve waiting, initiate waitingreserveloop
367 if ($getreserv{waiting} eq 1) {
368 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
373 # return result to the template
375 countreserv => scalar @reservloop,
376 reservloop => \@reservloop ,
377 WaitingReserveLoop => \@WaitingReserveLoop,
381 # make the issued books table.
382 my $todaysissues = '';
387 ## ADDED BY JF: new itemtype issuingrules counter stuff
388 my $issued_itemtypes_loop;
389 my $issued_itemtypes_count;
390 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
391 my $issued_itemtypes_remaining; # hashref with remaining
392 my $issued_itemtypes_flags; #hashref that stores flags
393 my @issued_itemtypes_count_loop;
397 # get each issue of the borrower & separate them in todayissues & previous issues
398 my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
400 # split in 2 arrays for today & previous
401 foreach my $it ( @$issueslist ) {
402 my $issuedate = $it->{'issuedate'};
403 $issuedate =~ s/-//g;
404 $issuedate = substr( $issuedate, 0, 8 );
405 ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
406 $it->{'itemnumber'}, $borrower->{'borrowernumber'}
408 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
409 ($it->{'can_renew'}, $it->{'can_renew_error'}) = CanBookBeRenewed(
410 $borrower->{'borrowernumber'},$it->{'itemnumber'}
412 my ($restype, $reserves) = CheckReserves($it->{'itemnumber'});
413 ($restype) and $it->{'can_renew'} = 0;
415 $it->{'dd'} = format_date($it->{'date_due'});
416 my $datedue = $it->{'date_due'};
418 $it->{'od'} = ($datedue < $todaysdate) ? 1 : 0 ;
419 ($it->{'author'} eq '') and $it->{'author'} = ' ';
420 $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
421 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
422 $issued_itemtypes_count->{ $it->{'itemtype'} }++;
424 if ( $todaysdate == $issuedate ) {
425 push @todaysissues, $it;
427 push @previousissues, $it;
430 @todaysissues = sort { $b->{'timestamp'} <=> $a->{'timestamp'} } @todaysissues;
431 @previousissues = sort { $b->{'date_due' } <=> $a->{'date_due' } } @previousissues;
433 foreach my $book (@todaysissues) {
434 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
437 foreach my $book (@previousissues) {
438 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
442 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
443 # FIXME: This should utilize all the issuingrules options rather than just the defaults
444 # and it should be moved to a module
445 my $dbh = C4::Context->dbh;
447 # how many of each is allowed?
448 my $issueqty_sth = $dbh->prepare( "
449 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
451 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
454 #my @issued_itemtypes_count; # huh?
455 $issueqty_sth->execute("*");
457 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
459 # subtract how many of each this borrower has
460 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
462 ( $data->{'maxissueqty'} -
463 $issued_itemtypes_count->{ $data->{'description'} } );
465 # can't have a negative number of remaining
466 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
467 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
468 unless ( ( $data->{'maxissueqty'} < 1 )
469 || ( $data->{'itemtype'} eq "*" )
470 || ( $data->{'itemtype'} eq "CIRC" ) )
472 push @issued_itemtypes_count_loop, $data;
475 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
481 my $CGIselectborrower;
482 if ($borrowerslist) {
486 . $a->{'firstname'} cmp $b->{'surname'}
491 push @values, $_->{'borrowernumber'};
492 $labels{ $_->{'borrowernumber'} } =
493 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
495 $CGIselectborrower = CGI::scrolling_list(
496 -name => 'borrowernumber',
498 -id => 'borrowernumber',
508 my $flags = $borrower->{'flags'};
511 foreach $flag ( sort keys %$flags ) {
513 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
514 if ( $flags->{$flag}->{'noissues'} ) {
519 if ( $flag eq 'GNA' ) {
520 $template->param( gna => 'true' );
522 if ( $flag eq 'LOST' ) {
523 $template->param( lost => 'true' );
525 if ( $flag eq 'DBARRED' ) {
526 $template->param( dbarred => 'true' );
528 if ( $flag eq 'CHARGES' ) {
531 chargesmsg => $flags->{'CHARGES'}->{'message'}
534 if ( $flag eq 'CREDITS' ) {
537 creditsmsg => $flags->{'CREDITS'}->{'message'}
542 if ( $flag eq 'CHARGES' ) {
546 chargesmsg => $flags->{'CHARGES'}->{'message'}
549 if ( $flag eq 'CREDITS' ) {
552 creditsmsg => $flags->{'CREDITS'}->{'message'}
555 if ( $flag eq 'ODUES' ) {
559 oduesmsg => $flags->{'ODUES'}->{'message'}
562 my $items = $flags->{$flag}->{'itemlist'};
566 # foreach my $item (@$items) {
567 # my ($iteminformation) =
568 # getiteminformation( $item->{'itemnumber'}, 0 );
569 # push @itemswaiting, $iteminformation;
572 if ( $query->param('module') ne 'returns' ) {
573 $template->param( nonreturns => 'true' );
576 if ( $flag eq 'NOTES' ) {
580 notesmsg => $flags->{'NOTES'}->{'message'}
586 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
587 my @temp = split( /\$/, $amountold );
589 my $CGIorganisations;
590 my $member_of_institution;
591 if ( C4::Context->preference("memberofinstitution") ) {
592 my $organisations = get_institutions();
595 foreach my $organisation ( keys %$organisations ) {
596 push @orgs, $organisation;
597 $org_labels{$organisation} =
598 $organisations->{$organisation}->{'surname'};
600 $member_of_institution = 1;
601 $CGIorganisations = CGI::popup_menu(
602 -id => 'organisations',
603 -name => 'organisations',
604 -labels => \%org_labels,
609 $amountold = $temp[1];
612 issued_itemtypes_count_loop => $issued_itemtypes_loop,
613 findborrower => $findborrower,
614 borrower => $borrower,
615 borrowernumber => $borrowernumber,
618 printername => $printer,
619 firstname => $borrower->{'firstname'},
620 surname => $borrower->{'surname'},
622 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
623 categorycode => $borrower->{'categorycode'},
624 address => $borrower->{'address'},
625 address2 => $borrower->{'address2'},
626 email => $borrower->{'email'},
627 emailpro => $borrower->{'emailpro'},
628 borrowernotes => $borrower->{'borrowernotes'},
629 city => $borrower->{'city'},
630 phone => $borrower->{'phone'} || $borrower->{'mobile'},
631 cardnumber => $borrower->{'cardnumber'},
632 amountold => $amountold,
634 stickyduedate => $stickyduedate,
636 CGIselectborrower => $CGIselectborrower,
637 todayissues => \@todaysissues,
638 previssues => \@previousissues,
639 inprocess => $inprocess,
640 memberofinstution => $member_of_institution,
641 CGIorganisations => $CGIorganisations,
644 # set return date if stickyduedate
645 if ($stickyduedate) {
646 my $t_year = "year" . $year;
647 my $t_month = "month" . $month;
648 my $t_day = "day" . $day;
656 #if ($branchcookie) {
657 #$cookie=[$cookie, $branchcookie, $printercookie];
661 SpecifyDueDate => C4::Context->preference("SpecifyDueDate")
663 $template->param( CircAutocompl => C4::Context->preference("CircAutocompl") );
664 output_html_with_http_headers $query, $cookie, $template->output;