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') || '';
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 $duedatespec = $query->param('duedatespec');
119 my $issueconfirmed = $query->param('issueconfirmed');
120 my $cancelreserve = $query->param('cancelreserve');
121 my $organisation = $query->param('organisations');
122 my $print = $query->param('print');
123 my $newexpiry = $query->param('dateexpiry');
128 # if ($query->param('setcookies')) {
129 # $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
130 # $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
134 my ($datedue,$invalidduedate);
136 $datedue = C4::Dates->new($duedatespec,'iso' );
137 $invalidduedate=1 unless $datedue;
140 #if (defined($year)) {
141 # $duedatespec = "$year-$month-$day";
143 # ($year, $month, $day) = ($duedatespec) ? split /-/, $duedatespec : (0,0,0);
146 my @datearr = localtime();
148 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
150 ( 1900 + $datearr[5] )
151 . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
152 . sprintf( "%0.2d", ( $datearr[3] ) );
154 # check and see if we should print
155 if ( $barcode eq '' && $print eq 'maybe' ) {
159 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
161 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
164 borrowernumber => $borrowernumber
168 if ( $print eq 'yes' && $borrowernumber ne '' ) {
169 printslip( $borrowernumber );
170 $query->param( 'borrowernumber', '' );
171 $borrowernumber = '';
175 # STEP 2 : FIND BORROWER
176 # if there is a list of find borrowers....
181 my ( $count, $borrowers ) =
182 SearchMember($findborrower, 'cardnumber', 'web' );
183 my @borrowers = @$borrowers;
184 if ( $#borrowers == -1 ) {
185 $query->param( 'findborrower', '' );
186 $message = "'$findborrower'";
188 elsif ( $#borrowers == 0 ) {
189 $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
190 $query->param( 'barcode', '' );
191 $borrowernumber = $borrowers[0]->{'borrowernumber'};
194 $borrowerslist = \@borrowers;
198 # get the borrower information.....
201 if ($borrowernumber) {
202 $borrower = GetMemberDetails( $borrowernumber, 0 );
203 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
205 # Warningdate is the date that the warning starts appearing
206 my ( $today_year, $today_month, $today_day ) = Today();
207 my ( $warning_year, $warning_month, $warning_day ) = split /-/,
208 $borrower->{'dateexpiry'};
209 my ( $enrol_year, $enrol_month, $enrol_day ) = split /-/,
210 $borrower->{'dateenrolled'};
211 # Renew day is calculated by adding the enrolment period to today
212 my ( $renew_year, $renew_month, $renew_day ) =
213 Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
214 0 , $borrower->{'enrolmentperiod'}) if ($enrol_year*$enrol_month*$enrol_day>0);
215 # if the expiry date is before today
216 if ( $warning_year*$warning_month*$warning_day==0
217 || Date_to_Days( $today_year, $today_month, $today_day )
218 > Date_to_Days( $warning_year, $warning_month, $warning_day ) )
221 #borrowercard expired or nearly expired, warn the librarian
224 warndeparture => format_date($borrower->{dateexpiry}),
225 renewaldate => format_date("$renew_year-$renew_month-$renew_day")
228 # check for NotifyBorrowerDeparture
229 if ($warning_year*$warning_month*$warning_day==0
230 || (C4::Context->preference('NotifyBorrowerDeparture') &&
231 Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
232 Date_to_Days( $today_year, $today_month, $today_day )) )
234 $template->param("warndeparture" => format_date($borrower->{dateexpiry}));
238 issuecount => $issue,
249 # $barcode = cuecatbarcodedecode($barcode);
251 if ($issueconfirmed) {
252 AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
256 my ( $error, $question ) =
257 CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
260 # Get the item title for more information
261 my $getmessageiteminfo = GetBiblioFromItemNumber(undef,$barcode);
263 foreach my $impossible ( keys %$error ) {
265 $impossible => $$error{$impossible},
270 foreach my $needsconfirmation ( keys %$question ) {
272 $needsconfirmation => $$question{$needsconfirmation},
273 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
274 NEEDSCONFIRMATION => 1
279 itemhomebranch => $getmessageiteminfo->{'homebranch'} ,
280 duedatespec => $duedatespec,
282 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
283 AddIssue( $borrower, $barcode, $datedue );
288 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue
289 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
291 issuecount => $issue,
295 # reload the borrower info for the sake of reseting the flags.....
296 if ($borrowernumber) {
297 $borrower = GetMemberDetails( $borrowernumber, 0 );
300 ##################################################################################
302 # show all reserves of this borrower, and the position of the reservation ....
303 if ($borrowernumber) {
306 # now we show the status of the borrower's reservations
307 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
309 my @WaitingReserveLoop;
311 foreach my $num_res (@borrowerreserv) {
313 my %getWaitingReserveInfo;
314 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
315 my $itemtypeinfo = getitemtypeinfo( (C4::Context->preference('item-level_itype')) ? $getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
316 my ( $transfertwhen, $transfertfrom, $transfertto ) =
317 GetTransfers( $num_res->{'itemnumber'} );
319 $getreserv{waiting} = 0;
320 $getreserv{transfered} = 0;
321 $getreserv{nottransfered} = 0;
323 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
324 $getreserv{title} = $getiteminfo->{'title'};
325 $getreserv{itemtype} = $itemtypeinfo->{'description'};
326 $getreserv{author} = $getiteminfo->{'author'};
327 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
328 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
329 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
331 # check if we have a waiting status for reservations
332 if ( $num_res->{'found'} eq 'W' ) {
333 $getreserv{color} = 'reserved';
334 $getreserv{waiting} = 1;
335 # genarate information displaying only waiting reserves
336 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
337 $getWaitingReserveInfo{biblionumber} = $getiteminfo->{'biblionumber'};
338 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
339 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
340 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
341 if ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} ) {
342 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
346 # check transfers with the itemnumber foud in th reservation loop
347 if ($transfertwhen) {
348 $getreserv{color} = 'transfered';
349 $getreserv{transfered} = 1;
350 $getreserv{datesent} = format_date($transfertwhen);
351 $getreserv{frombranch} = GetBranchName($transfertfrom);
354 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
355 and not $transfertwhen )
357 $getreserv{nottransfered} = 1;
358 $getreserv{nottransferedby} =
359 GetBranchName( $getiteminfo->{'holdingbranch'} );
362 # if we don't have a reserv on item, we put the biblio infos and the waiting position
363 if ( $getiteminfo->{'title'} eq '' ) {
364 my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
365 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} ); # fixme - we should have item-level reserves here ?
366 $getreserv{color} = 'inwait';
367 $getreserv{title} = $getbibinfo->{'title'};
368 $getreserv{waitingposition} = $num_res->{'priority'};
369 $getreserv{nottransfered} = 0;
370 $getreserv{itemtype} = $getbibtype->{'description'};
371 $getreserv{author} = $getbibinfo->{'author'};
372 $getreserv{itemcallnumber} = '----------';
373 $getreserv{biblionumber} = $num_res->{'biblionumber'};
375 push( @reservloop, \%getreserv );
377 # if we have a reserve waiting, initiate waitingreserveloop
378 if ($getreserv{waiting} eq 1) {
379 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
384 # return result to the template
386 countreserv => scalar @reservloop,
387 reservloop => \@reservloop ,
388 WaitingReserveLoop => \@WaitingReserveLoop,
392 # make the issued books table.
393 my $todaysissues = '';
398 ## ADDED BY JF: new itemtype issuingrules counter stuff
399 my $issued_itemtypes_loop;
400 my $issued_itemtypes_count;
401 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
402 my $issued_itemtypes_remaining; # hashref with remaining
403 my $issued_itemtypes_flags; #hashref that stores flags
404 my @issued_itemtypes_count_loop;
408 # get each issue of the borrower & separate them in todayissues & previous issues
409 my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
411 # split in 2 arrays for today & previous
412 foreach my $it ( @$issueslist ) {
413 my $issuedate = $it->{'issuedate'};
414 $issuedate =~ s/-//g;
415 $issuedate = substr( $issuedate, 0, 8 );
416 ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
417 $it->{'itemnumber'}, $borrower->{'borrowernumber'}
419 $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
420 ($it->{'can_renew'}, $it->{'can_renew_error'}) = CanBookBeRenewed(
421 $borrower->{'borrowernumber'},$it->{'itemnumber'}
423 my ($restype, $reserves) = CheckReserves($it->{'itemnumber'});
424 ($restype) and $it->{'can_renew'} = 0;
426 $it->{'dd'} = format_date($it->{'date_due'});
427 my $datedue = $it->{'date_due'};
429 $it->{'od'} = ($datedue < $todaysdate) ? 1 : 0 ;
430 ($it->{'author'} eq '') and $it->{'author'} = ' ';
431 $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
432 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
433 $issued_itemtypes_count->{ $it->{'itemtype'} }++;
435 if ( $todaysdate == $issuedate ) {
436 push @todaysissues, $it;
438 push @previousissues, $it;
441 @todaysissues = sort { $b->{'timestamp'} <=> $a->{'timestamp'} } @todaysissues;
442 @previousissues = sort { $b->{'date_due' } <=> $a->{'date_due' } } @previousissues;
444 foreach my $book (@todaysissues) {
445 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
448 foreach my $book (@previousissues) {
449 $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
453 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
454 # FIXME: This should utilize all the issuingrules options rather than just the defaults
455 # and it should be moved to a module
456 my $dbh = C4::Context->dbh;
458 # how many of each is allowed?
459 my $issueqty_sth = $dbh->prepare( "
460 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
462 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
465 #my @issued_itemtypes_count; # huh?
466 $issueqty_sth->execute("*");
468 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
470 # subtract how many of each this borrower has
471 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
473 ( $data->{'maxissueqty'} -
474 $issued_itemtypes_count->{ $data->{'description'} } );
476 # can't have a negative number of remaining
477 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
478 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
479 unless ( ( $data->{'maxissueqty'} < 1 )
480 || ( $data->{'itemtype'} eq "*" )
481 || ( $data->{'itemtype'} eq "CIRC" ) )
483 push @issued_itemtypes_count_loop, $data;
486 $issued_itemtypes_loop = \@issued_itemtypes_count_loop;
492 my $CGIselectborrower;
493 if ($borrowerslist) {
497 . $a->{'firstname'} cmp $b->{'surname'}
502 push @values, $_->{'borrowernumber'};
503 $labels{ $_->{'borrowernumber'} } =
504 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
506 $CGIselectborrower = CGI::scrolling_list(
507 -name => 'borrowernumber',
509 -id => 'borrowernumber',
519 my $flags = $borrower->{'flags'};
522 foreach $flag ( sort keys %$flags ) {
524 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
525 if ( $flags->{$flag}->{'noissues'} ) {
530 if ( $flag eq 'GNA' ) {
531 $template->param( gna => 'true' );
533 if ( $flag eq 'LOST' ) {
534 $template->param( lost => 'true' );
536 if ( $flag eq 'DBARRED' ) {
537 $template->param( dbarred => 'true' );
539 if ( $flag eq 'CHARGES' ) {
542 chargesmsg => $flags->{'CHARGES'}->{'message'}
545 if ( $flag eq 'CREDITS' ) {
548 creditsmsg => $flags->{'CREDITS'}->{'message'}
553 if ( $flag eq 'CHARGES' ) {
557 chargesmsg => $flags->{'CHARGES'}->{'message'}
560 if ( $flag eq 'CREDITS' ) {
563 creditsmsg => $flags->{'CREDITS'}->{'message'}
566 if ( $flag eq 'ODUES' ) {
570 oduesmsg => $flags->{'ODUES'}->{'message'}
573 my $items = $flags->{$flag}->{'itemlist'};
577 # foreach my $item (@$items) {
578 # my ($iteminformation) =
579 # getiteminformation( $item->{'itemnumber'}, 0 );
580 # push @itemswaiting, $iteminformation;
583 if ( $query->param('module') ne 'returns' ) {
584 $template->param( nonreturns => 'true' );
587 if ( $flag eq 'NOTES' ) {
591 notesmsg => $flags->{'NOTES'}->{'message'}
597 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
598 my @temp = split( /\$/, $amountold );
600 my $CGIorganisations;
601 my $member_of_institution;
602 if ( C4::Context->preference("memberofinstitution") ) {
603 my $organisations = get_institutions();
606 foreach my $organisation ( keys %$organisations ) {
607 push @orgs, $organisation;
608 $org_labels{$organisation} =
609 $organisations->{$organisation}->{'surname'};
611 $member_of_institution = 1;
612 $CGIorganisations = CGI::popup_menu(
613 -id => 'organisations',
614 -name => 'organisations',
615 -labels => \%org_labels,
620 $amountold = $temp[1];
622 my $borrowercategory = GetBorrowercategory( $borrower->{'categorycode'} );
623 my $category_type = $borrowercategory->{'category_type'};
624 ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
627 issued_itemtypes_count_loop => $issued_itemtypes_loop,
628 findborrower => $findborrower,
629 borrower => $borrower,
630 borrowernumber => $borrowernumber,
633 printername => $printer,
634 firstname => $borrower->{'firstname'},
635 surname => $borrower->{'surname'},
636 dateexpiry => format_date($newexpiry),
638 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
639 categorycode => $borrower->{'categorycode'},
640 address => $borrower->{'address'},
641 address2 => $borrower->{'address2'},
642 email => $borrower->{'email'},
643 emailpro => $borrower->{'emailpro'},
644 borrowernotes => $borrower->{'borrowernotes'},
645 city => $borrower->{'city'},
646 phone => $borrower->{'phone'} || $borrower->{'mobile'},
647 cardnumber => $borrower->{'cardnumber'},
648 amountold => $amountold,
650 stickyduedate => $stickyduedate,
652 CGIselectborrower => $CGIselectborrower,
653 todayissues => \@todaysissues,
654 previssues => \@previousissues,
655 inprocess => $inprocess,
656 memberofinstution => $member_of_institution,
657 CGIorganisations => $CGIorganisations,
662 # set return date if stickyduedate
663 if ($stickyduedate) {
665 duedatespec => $duedatespec,
669 #if ($branchcookie) {
670 #$cookie=[$cookie, $branchcookie, $printercookie];
674 SpecifyDueDate => C4::Context->preference("SpecifyDueDate"),
675 CircAutocompl => C4::Context->preference("CircAutocompl") ,
676 DHTMLformat => C4::Dates->DHTMLcalendar(),
678 output_html_with_http_headers $query, $cookie, $template->output;