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->{'itype'} : $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'};
319 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
321 # check if we have a waiting status for reservations
322 if ( $num_res->{'found'} eq 'W' ) {
323 $getreserv{color} = 'reserved';
324 $getreserv{waiting} = 1;
325 # genarate information displaying only waiting reserves
326 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
327 $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
328 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
329 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
330 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
331 if ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} ) {
332 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
336 # check transfers with the itemnumber foud in th reservation loop
337 if ($transfertwhen) {
338 $getreserv{color} = 'transfered';
339 $getreserv{transfered} = 1;
340 $getreserv{datesent} = format_date($transfertwhen);
341 $getreserv{frombranch} = GetBranchName($transfertfrom);
344 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
345 and not $transfertwhen )
347 $getreserv{nottransfered} = 1;
348 $getreserv{nottransferedby} =
349 GetBranchName( $getiteminfo->{'holdingbranch'} );
352 # if we don't have a reserv on item, we put the biblio infos and the waiting position
353 if ( $getiteminfo->{'title'} eq '' ) {
354 my $getbibinfo = GetBiblioItemData( $num_res->{'biblionumber'} );
355 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} ); # fixme - we should have item-level reserves here ?
356 $getreserv{color} = 'inwait';
357 $getreserv{title} = $getbibinfo->{'title'};
358 $getreserv{waitingposition} = $num_res->{'priority'};
359 $getreserv{nottransfered} = 0;
360 $getreserv{itemtype} = $getbibtype->{'description'};
361 $getreserv{author} = $getbibinfo->{'author'};
362 $getreserv{itemcallnumber} = '----------';
363 $getreserv{biblionumber} = $num_res->{'biblionumber'};
365 push( @reservloop, \%getreserv );
367 # if we have a reserve waiting, initiate waitingreserveloop
368 if ($getreserv{waiting} eq 1) {
369 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
374 # return result to the template
376 countreserv => scalar @reservloop,
377 reservloop => \@reservloop ,
378 WaitingReserveLoop => \@WaitingReserveLoop,
382 # make the issued books table.
383 my $todaysissues = '';
388 ## ADDED BY JF: new itemtype issuingrules counter stuff
389 my $issued_itemtypes_loop;
390 my $issued_itemtypes_count;
391 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
392 my $issued_itemtypes_remaining; # hashref with remaining
393 my $issued_itemtypes_flags; #hashref that stores flags
394 my @issued_itemtypes_count_loop;
398 # get each issue of the borrower & separate them in todayissues & previous issues
399 my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
401 # split in 2 arrays for today & previous
402 foreach my $it ( @$issueslist ) {
403 my $issuedate = $it->{'issuedate'};
404 $issuedate =~ s/-//g;
405 $issuedate = substr( $issuedate, 0, 8 );
406 ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
407 $it->{'itemnumber'}, $borrower->{'borrowernumber'}
409 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
410 ($it->{'can_renew'}, $it->{'can_renew_error'}) = CanBookBeRenewed(
411 $borrower->{'borrowernumber'},$it->{'itemnumber'}
413 my ($restype, $reserves) = CheckReserves($it->{'itemnumber'});
414 ($restype) and $it->{'can_renew'} = 0;
416 $it->{'dd'} = format_date($it->{'date_due'});
417 my $datedue = $it->{'date_due'};
419 $it->{'od'} = ($datedue < $todaysdate) ? 1 : 0 ;
420 ($it->{'author'} eq '') and $it->{'author'} = ' ';
421 $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
422 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
423 $issued_itemtypes_count->{ $it->{'itemtype'} }++;
425 if ( $todaysdate == $issuedate ) {
426 push @todaysissues, $it;
428 push @previousissues, $it;
431 @todaysissues = sort { $b->{'timestamp'} <=> $a->{'timestamp'} } @todaysissues;
432 @previousissues = sort { $b->{'date_due' } <=> $a->{'date_due' } } @previousissues;
434 foreach my $book (@todaysissues) {
435 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
438 foreach my $book (@previousissues) {
439 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
443 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
444 # FIXME: This should utilize all the issuingrules options rather than just the defaults
445 # and it should be moved to a module
446 my $dbh = C4::Context->dbh;
448 # how many of each is allowed?
449 my $issueqty_sth = $dbh->prepare( "
450 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
452 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
455 #my @issued_itemtypes_count; # huh?
456 $issueqty_sth->execute("*");
458 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
460 # subtract how many of each this borrower has
461 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
463 ( $data->{'maxissueqty'} -
464 $issued_itemtypes_count->{ $data->{'description'} } );
466 # can't have a negative number of remaining
467 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
468 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
469 unless ( ( $data->{'maxissueqty'} < 1 )
470 || ( $data->{'itemtype'} eq "*" )
471 || ( $data->{'itemtype'} eq "CIRC" ) )
473 push @issued_itemtypes_count_loop, $data;
476 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
482 my $CGIselectborrower;
483 if ($borrowerslist) {
487 . $a->{'firstname'} cmp $b->{'surname'}
492 push @values, $_->{'borrowernumber'};
493 $labels{ $_->{'borrowernumber'} } =
494 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
496 $CGIselectborrower = CGI::scrolling_list(
497 -name => 'borrowernumber',
499 -id => 'borrowernumber',
509 my $flags = $borrower->{'flags'};
512 foreach $flag ( sort keys %$flags ) {
514 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
515 if ( $flags->{$flag}->{'noissues'} ) {
520 if ( $flag eq 'GNA' ) {
521 $template->param( gna => 'true' );
523 if ( $flag eq 'LOST' ) {
524 $template->param( lost => 'true' );
526 if ( $flag eq 'DBARRED' ) {
527 $template->param( dbarred => 'true' );
529 if ( $flag eq 'CHARGES' ) {
532 chargesmsg => $flags->{'CHARGES'}->{'message'}
535 if ( $flag eq 'CREDITS' ) {
538 creditsmsg => $flags->{'CREDITS'}->{'message'}
543 if ( $flag eq 'CHARGES' ) {
547 chargesmsg => $flags->{'CHARGES'}->{'message'}
550 if ( $flag eq 'CREDITS' ) {
553 creditsmsg => $flags->{'CREDITS'}->{'message'}
556 if ( $flag eq 'ODUES' ) {
560 oduesmsg => $flags->{'ODUES'}->{'message'}
563 my $items = $flags->{$flag}->{'itemlist'};
567 # foreach my $item (@$items) {
568 # my ($iteminformation) =
569 # getiteminformation( $item->{'itemnumber'}, 0 );
570 # push @itemswaiting, $iteminformation;
573 if ( $query->param('module') ne 'returns' ) {
574 $template->param( nonreturns => 'true' );
577 if ( $flag eq 'NOTES' ) {
581 notesmsg => $flags->{'NOTES'}->{'message'}
587 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
588 my @temp = split( /\$/, $amountold );
590 my $CGIorganisations;
591 my $member_of_institution;
592 if ( C4::Context->preference("memberofinstitution") ) {
593 my $organisations = get_institutions();
596 foreach my $organisation ( keys %$organisations ) {
597 push @orgs, $organisation;
598 $org_labels{$organisation} =
599 $organisations->{$organisation}->{'surname'};
601 $member_of_institution = 1;
602 $CGIorganisations = CGI::popup_menu(
603 -id => 'organisations',
604 -name => 'organisations',
605 -labels => \%org_labels,
610 $amountold = $temp[1];
613 issued_itemtypes_count_loop => $issued_itemtypes_loop,
614 findborrower => $findborrower,
615 borrower => $borrower,
616 borrowernumber => $borrowernumber,
619 printername => $printer,
620 firstname => $borrower->{'firstname'},
621 surname => $borrower->{'surname'},
623 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
624 categorycode => $borrower->{'categorycode'},
625 address => $borrower->{'address'},
626 address2 => $borrower->{'address2'},
627 email => $borrower->{'email'},
628 emailpro => $borrower->{'emailpro'},
629 borrowernotes => $borrower->{'borrowernotes'},
630 city => $borrower->{'city'},
631 phone => $borrower->{'phone'} || $borrower->{'mobile'},
632 cardnumber => $borrower->{'cardnumber'},
633 amountold => $amountold,
635 stickyduedate => $stickyduedate,
637 CGIselectborrower => $CGIselectborrower,
638 todayissues => \@todaysissues,
639 previssues => \@previousissues,
640 inprocess => $inprocess,
641 memberofinstution => $member_of_institution,
642 CGIorganisations => $CGIorganisations,
645 # set return date if stickyduedate
646 if ($stickyduedate) {
647 my $t_year = "year" . $year;
648 my $t_month = "month" . $month;
649 my $t_day = "day" . $day;
657 #if ($branchcookie) {
658 #$cookie=[$cookie, $branchcookie, $printercookie];
662 SpecifyDueDate => C4::Context->preference("SpecifyDueDate")
664 $template->param( CircAutocompl => C4::Context->preference("CircAutocompl") );
665 output_html_with_http_headers $query, $cookie, $template->output;