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();
93 my $findborrower = $query->param('findborrower');
94 $findborrower =~ s|,| |g;
95 #$findborrower =~ s|'| |g;
96 my $borrowernumber = $query->param('borrowernumber');
98 $branch = C4::Context->userenv->{'branch'};
99 $printer = C4::Context->userenv->{'branchprinter'};
102 # If Autolocated is not activated, we show the Circulation Parameters to chage settings of librarian
103 if (C4::Context->preference("AutoLocation") ne 1)
110 my $barcode = $query->param('barcode') || '';
111 my $year = $query->param('year');
112 my $month = $query->param('month');
113 my $day = $query->param('day');
114 my $stickyduedate = $query->param('stickyduedate');
115 my $issueconfirmed = $query->param('issueconfirmed');
116 my $cancelreserve = $query->param('cancelreserve');
117 my $organisation = $query->param('organisations');
118 my $print = $query->param('print');
123 # if ($query->param('setcookies')) {
124 # $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
125 # $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
130 my @datearr = localtime( time() );
132 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
134 ( 1900 + $datearr[5] )
135 . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
136 . sprintf( "%0.2d", ( $datearr[3] ) );
138 # check and see if we should print
139 if ( $barcode eq '' && $print eq 'maybe' ) {
143 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
145 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
148 borrowernumber => $borrowernumber
152 if ( $print eq 'yes' && $borrowernumber ne '' ) {
153 printslip( $borrowernumber );
154 $query->param( 'borrowernumber', '' );
155 $borrowernumber = '';
159 # STEP 2 : FIND BORROWER
160 # if there is a list of find borrowers....
165 my ( $count, $borrowers ) =
166 SearchMember($findborrower, 'cardnumber', 'web' );
167 my @borrowers = @$borrowers;
168 if ( $#borrowers == -1 ) {
169 $query->param( 'findborrower', '' );
170 $message = "'$findborrower'";
172 elsif ( $#borrowers == 0 ) {
173 $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
174 $query->param( 'barcode', '' );
175 $borrowernumber = $borrowers[0]->{'borrowernumber'};
178 $borrowerslist = \@borrowers;
182 # get the borrower information.....
186 if ($borrowernumber) {
187 $borrower = GetMemberDetails( $borrowernumber, 0 );
188 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
190 # Warningdate is the date that the warning starts appearing
191 my ( $today_year, $today_month, $today_day ) = Today();
192 my ( $warning_year, $warning_month, $warning_day ) = split /-/,
193 $borrower->{'dateexpiry'};
194 my ( $enrol_year, $enrol_month, $enrol_day ) = split /-/,
195 $borrower->{'dateenrolled'};
196 # Renew day is calculated by adding the enrolment period to today
197 my ( $renew_year, $renew_month, $renew_day ) =
198 Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
199 0 , $borrower->{'enrolmentperiod'}) if ($enrol_year*$enrol_month*$enrol_day>0);
200 # if the expiry date is before today
201 if ( $warning_year*$warning_month*$warning_day==0
202 || Date_to_Days( $today_year, $today_month, $today_day )
203 > Date_to_Days( $warning_year, $warning_month, $warning_day ) )
206 #borrowercard expired or nearly expired, warn the librarian
209 warndeparture => format_date($borrower->{dateexpiry}),
210 renewaldate => format_date("$renew_year-$renew_month-$renew_day")
213 # check for NotifyBorrowerDeparture
214 if ($warning_year*$warning_month*$warning_day==0
215 || (C4::Context->preference('NotifyBorrowerDeparture') &&
216 Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
217 Date_to_Days( $today_year, $today_month, $today_day )) )
219 $template->param("warndeparture" => format_date($borrower->{dateexpiry}));
223 issuecount => $issue,
234 $barcode = cuecatbarcodedecode($barcode);
235 my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
236 if ($issueconfirmed) {
237 AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
241 my ( $error, $question ) =
242 CanBookBeIssued( $borrower, $barcode, $year, $month, $day,
246 # Get the item title for more information
247 my $getmessageiteminfo = GetBiblioFromItemNumber($barcode);
249 foreach my $impossible ( keys %$error ) {
251 $impossible => $$error{$impossible},
256 foreach my $needsconfirmation ( keys %$question ) {
258 $needsconfirmation => $$question{$needsconfirmation},
259 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
260 NEEDSCONFIRMATION => 1
269 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
270 AddIssue( $borrower, $barcode, $datedue );
275 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue
276 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
278 issuecount => $issue,
282 # reload the borrower info for the sake of reseting the flags.....
283 if ($borrowernumber) {
284 $borrower = GetMemberDetails( $borrowernumber, 0 );
287 ##################################################################################
289 # show all reserves of this borrower, and the position of the reservation ....
290 if ($borrowernumber) {
293 # now we show the status of the borrower's reservations
294 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
296 my @WaitingReserveLoop;
298 foreach my $num_res (@borrowerreserv) {
300 my %getWaitingReserveInfo;
301 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
302 my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itype')) ? $getiteminfo->{'ccode'} : $getiteminfo->{'itemtype'} );
303 my ( $transfertwhen, $transfertfrom, $transfertto ) =
304 GetTransfers( $num_res->{'itemnumber'} );
306 $getreserv{waiting} = 0;
307 $getreserv{transfered} = 0;
308 $getreserv{nottransfered} = 0;
310 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
311 $getreserv{title} = $getiteminfo->{'title'};
312 $getreserv{itemtype} = $itemtypeinfo->{'description'};
313 $getreserv{author} = $getiteminfo->{'author'};
314 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
315 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
317 # check if we have a waiting status for reservations
318 if ( $num_res->{'found'} eq 'W' ) {
319 $getreserv{color} = 'reserved';
320 $getreserv{waiting} = 1;
321 # genarate information displaying only waiting reserves
322 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
323 $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
324 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
325 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
326 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
327 if ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} ) {
328 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
332 # check transfers with the itemnumber foud in th reservation loop
333 if ($transfertwhen) {
334 $getreserv{color} = 'transfered';
335 $getreserv{transfered} = 1;
336 $getreserv{datesent} = format_date($transfertwhen);
337 $getreserv{frombranch} = GetBranchName($transfertfrom);
340 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
341 and not $transfertwhen )
343 $getreserv{nottransfered} = 1;
344 $getreserv{nottransferedby} =
345 GetBranchName( $getiteminfo->{'holdingbranch'} );
348 # if we don't have a reserv on item, we put the biblio infos and the waiting position
349 if ( $getiteminfo->{'title'} eq '' ) {
350 my $getbibinfo = GetBiblioItemData( $num_res->{'biblionumber'} );
351 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} ); # fixme - we should have item-level reserves here ?
352 $getreserv{color} = 'inwait';
353 $getreserv{title} = $getbibinfo->{'title'};
354 $getreserv{waitingposition} = $num_res->{'priority'};
355 $getreserv{nottransfered} = 0;
356 $getreserv{itemtype} = $getbibtype->{'description'};
357 $getreserv{author} = $getbibinfo->{'author'};
358 $getreserv{itemcallnumber} = '----------';
361 push( @reservloop, \%getreserv );
363 # if we have a reserve waiting, initiate waitingreserveloop
364 if ($getreserv{waiting} eq 1) {
365 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
370 # return result to the template
372 countreserv => scalar @reservloop,
373 reservloop => \@reservloop ,
374 WaitingReserveLoop => \@WaitingReserveLoop,
378 # make the issued books table.
379 my $todaysissues = '';
384 ## ADDED BY JF: new itemtype issuingrules counter stuff
385 my $issued_itemtypes_loop;
386 my $issued_itemtypes_count;
387 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
388 my $issued_itemtypes_remaining; # hashref with remaining
389 my $issued_itemtypes_flags; #hashref that stores flags
393 # get each issue of the borrower & separate them in todayissues & previous issues
394 my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
396 # split in 2 arrays for today & previous
397 foreach my $it ( @$issueslist ) {
398 my $issuedate = $it->{'issuedate'};
399 $issuedate =~ s/-//g;
400 $issuedate = substr( $issuedate, 0, 8 );
401 ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
402 $it->{'itemnumber'}, $borrower->{'borrowernumber'}
404 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
405 ($it->{'can_renew'}, $it->{'can_renew_error'}) = CanBookBeRenewed(
406 $borrower->{'borrowernumber'},$it->{'itemnumber'}
408 my ($restype, $reserves) = CheckReserves($it->{'itemnumber'});
409 ($restype) and $it->{'can_renew'} = 0;
411 $it->{'dd'} = format_date($it->{'date_due'});
412 my $datedue = $it->{'date_due'};
414 $it->{'od'} = ($datedue < $todaysdate) ? 1 : 0 ;
415 ($it->{'author'} eq '') and $it->{'author'} = ' ';
417 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
418 $issued_itemtypes_count->{ $it->{'itemtype'} }++;
420 if ( $todaysdate == $issuedate ) {
421 push @todaysissues, $it;
423 push @previousissues, $it;
426 @todaysissues = sort { $b->{'timestamp'} <=> $a->{'timestamp'} } @todaysissues;
427 @previousissues = sort { $b->{'date_due' } <=> $a->{'date_due' } } @previousissues;
429 foreach my $book (@todaysissues) {
430 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
433 foreach my $book (@previousissues) {
434 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
438 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
439 # FIXME: This should utilize all the issuingrules options rather than just the defaults
440 # and it should be moved to a module
441 my $dbh = C4::Context->dbh;
443 # how many of each is allowed?
444 my $issueqty_sth = $dbh->prepare( "
445 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
447 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
450 #my @issued_itemtypes_count; # huh?
451 $issueqty_sth->execute("*");
452 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
454 # subtract how many of each this borrower has
455 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
457 ( $data->{'maxissueqty'} -
458 $issued_itemtypes_count->{ $data->{'description'} } );
460 # can't have a negative number of remaining
461 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
462 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
463 unless ( ( $data->{'maxissueqty'} < 1 )
464 || ( $data->{'itemtype'} eq "*" )
465 || ( $data->{'itemtype'} eq "CIRC" ) )
467 push @issued_itemtypes_count, $data;
470 $issued_itemtypes_loop = \@issued_itemtypes_count;
476 my $CGIselectborrower;
477 if ($borrowerslist) {
481 . $a->{'firstname'} cmp $b->{'surname'}
486 push @values, $_->{'borrowernumber'};
487 $labels{ $_->{'borrowernumber'} } =
488 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
490 $CGIselectborrower = CGI::scrolling_list(
491 -name => 'borrowernumber',
492 -id => 'borrowernumber',
502 my $flags = $borrower->{'flags'};
505 foreach $flag ( sort keys %$flags ) {
507 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
508 if ( $flags->{$flag}->{'noissues'} ) {
513 if ( $flag eq 'GNA' ) {
514 $template->param( gna => 'true' );
516 if ( $flag eq 'LOST' ) {
517 $template->param( lost => 'true' );
519 if ( $flag eq 'DBARRED' ) {
520 $template->param( dbarred => 'true' );
522 if ( $flag eq 'CHARGES' ) {
525 chargesmsg => $flags->{'CHARGES'}->{'message'}
528 if ( $flag eq 'CREDITS' ) {
531 creditsmsg => $flags->{'CREDITS'}->{'message'}
536 if ( $flag eq 'CHARGES' ) {
540 chargesmsg => $flags->{'CHARGES'}->{'message'}
543 if ( $flag eq 'CREDITS' ) {
546 creditsmsg => $flags->{'CREDITS'}->{'message'}
549 if ( $flag eq 'ODUES' ) {
553 oduesmsg => $flags->{'ODUES'}->{'message'}
556 my $items = $flags->{$flag}->{'itemlist'};
560 # foreach my $item (@$items) {
561 # my ($iteminformation) =
562 # getiteminformation( $item->{'itemnumber'}, 0 );
563 # push @itemswaiting, $iteminformation;
566 if ( $query->param('module') ne 'returns' ) {
567 $template->param( nonreturns => 'true' );
570 if ( $flag eq 'NOTES' ) {
574 notesmsg => $flags->{'NOTES'}->{'message'}
580 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
581 my @temp = split( /\$/, $amountold );
583 my $CGIorganisations;
584 my $member_of_institution;
585 if ( C4::Context->preference("memberofinstitution") ) {
586 my $organisations = get_institutions();
589 foreach my $organisation ( keys %$organisations ) {
590 push @orgs, $organisation;
591 $org_labels{$organisation} =
592 $organisations->{$organisation}->{'surname'};
594 $member_of_institution = 1;
595 $CGIorganisations = CGI::popup_menu(
596 -id => 'organisations',
597 -name => 'organisations',
598 -labels => \%org_labels,
603 $amountold = $temp[1];
606 issued_itemtypes_count_loop => $issued_itemtypes_loop,
607 findborrower => $findborrower,
608 borrower => $borrower,
609 borrowernumber => $borrowernumber,
612 printername => $printer,
613 firstname => $borrower->{'firstname'},
614 surname => $borrower->{'surname'},
616 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
617 categorycode => $borrower->{'categorycode'},
618 streetaddress => $borrower->{'address'},
619 email => $borrower->{'email'},
620 emailpro => $borrower->{'emailpro'},
621 borrowernotes => $borrower->{'borrowernotes'},
622 city => $borrower->{'city'},
623 phone => $borrower->{'phone'} || $borrower->{'mobile'},
624 cardnumber => $borrower->{'cardnumber'},
625 amountold => $amountold,
627 stickyduedate => $stickyduedate,
629 CGIselectborrower => $CGIselectborrower,
630 todayissues => \@todaysissues,
631 previssues => \@previousissues,
632 inprocess => $inprocess,
633 memberofinstution => $member_of_institution,
634 CGIorganisations => $CGIorganisations,
637 # set return date if stickyduedate
638 if ($stickyduedate) {
639 my $t_year = "year" . $year;
640 my $t_month = "month" . $month;
641 my $t_day = "day" . $day;
649 #if ($branchcookie) {
650 #$cookie=[$cookie, $branchcookie, $printercookie];
654 SpecifyDueDate => C4::Context->preference("SpecifyDueDate")
656 $template->param( CircAutocompl => C4::Context->preference("CircAutocompl") );
657 output_html_with_http_headers $query, $cookie, $template->output;