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 = $query->param('inprocess');
144 if ( $barcode eq '' ) {
150 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
153 borrowernumber => $borrowernumber
157 if ( $print eq 'yes' && $borrowernumber ne '' ) {
158 printslip( $borrowernumber );
159 $query->param( 'borrowernumber', '' );
160 $borrowernumber = '';
164 # STEP 2 : FIND BORROWER
165 # if there is a list of find borrowers....
170 my ( $count, $borrowers ) =
171 SearchMember($findborrower, 'cardnumber', 'web' );
172 my @borrowers = @$borrowers;
173 if ( $#borrowers == -1 ) {
174 $query->param( 'findborrower', '' );
175 $message = "'$findborrower'";
177 elsif ( $#borrowers == 0 ) {
178 $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
179 $query->param( 'barcode', '' );
180 $borrowernumber = $borrowers[0]->{'borrowernumber'};
183 $borrowerslist = \@borrowers;
187 # get the borrower information.....
191 if ($borrowernumber) {
192 $borrower = GetMemberDetails( $borrowernumber, 0 );
193 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
195 # Warningdate is the date that the warning starts appearing
196 my ( $today_year, $today_month, $today_day ) = Today();
197 my ( $warning_year, $warning_month, $warning_day ) = split /-/,
198 $borrower->{'dateexpiry'};
199 my ( $enrol_year, $enrol_month, $enrol_day ) = split /-/,
200 $borrower->{'dateenrolled'};
201 # Renew day is calculated by adding the enrolment period to today
202 my ( $renew_year, $renew_month, $renew_day ) =
203 Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
204 0 , $borrower->{'enrolmentperiod'}) if ($enrol_year*$enrol_month*$enrol_day>0);
205 # if the expiry date is before today
206 if ( $warning_year*$warning_month*$warning_day==0
207 || Date_to_Days( $today_year, $today_month, $today_day )
208 > Date_to_Days( $warning_year, $warning_month, $warning_day ) )
211 #borrowercard expired or nearly expired, warn the librarian
214 warndeparture => "1",
215 renewaldate => "$renew_year-$renew_month-$renew_day"
218 # check for NotifyBorrowerDeparture
219 if ($warning_year*$warning_month*$warning_day==0
220 || (C4::Context->preference('NotifyBorrowerDeparture') &&
221 Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
222 Date_to_Days( $today_year, $today_month, $today_day )) )
224 $template->param("warndeparture" => 1);
228 issuecount => $issue,
239 $barcode = cuecatbarcodedecode($barcode);
240 my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
241 if ($issueconfirmed) {
242 AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
246 my ( $error, $question ) =
247 CanBookBeIssued( $borrower, $barcode, $year, $month, $day,
251 # Get the item title for more information
252 my $getmessageiteminfo = GetBiblioFromItemNumber( undef, $barcode );
254 foreach my $impossible ( keys %$error ) {
256 $impossible => $$error{$impossible},
261 foreach my $needsconfirmation ( keys %$question ) {
263 $needsconfirmation => $$question{$needsconfirmation},
264 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
265 NEEDSCONFIRMATION => 1
274 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
275 AddIssue( $borrower, $barcode, $datedue );
280 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue
281 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
283 issuecount => $issue,
287 # reload the borrower info for the sake of reseting the flags.....
288 if ($borrowernumber) {
289 $borrower = GetMemberDetails( $borrowernumber, 0 );
292 ##################################################################################
294 # show all reserves of this borrower, and the position of the reservation ....
295 if ($borrowernumber) {
298 # now we show the status of the borrower's reservations
299 my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
301 my @WaitingReserveLoop;
303 foreach my $num_res (@borrowerreserv) {
305 my %getWaitingReserveInfo;
306 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
307 my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
308 my ( $transfertwhen, $transfertfrom, $transfertto ) =
309 GetTransfers( $num_res->{'itemnumber'} );
311 $getreserv{waiting} = 0;
312 $getreserv{transfered} = 0;
313 $getreserv{nottransfered} = 0;
315 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
316 $getreserv{title} = $getiteminfo->{'title'};
317 $getreserv{itemtype} = $itemtypeinfo->{'description'};
318 $getreserv{author} = $getiteminfo->{'author'};
319 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
320 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
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 = GetBiblioItemData( $num_res->{'biblionumber'} );
356 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
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} = '----------';
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
398 # get each issue of the borrower & separate them in todayissues & previous issues
401 my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
403 # split in 2 arrays for today & previous
404 my $dbh = C4::Context->dbh;
405 foreach my $it ( @$issueslist ) {
406 my $issuedate = $it->{'issuedate'};
407 $issuedate =~ s/-//g;
408 $issuedate = substr( $issuedate, 0, 8 );
409 if ( $todaysdate == $issuedate ) {
412 $it->{'itemtype_charge'}
416 $borrower->{'borrowernumber'}
419 sprintf( "%.2f", $it->{'charge'} );
422 $it->{'can_renew_error'}
425 $borrower->{'borrowernumber'},
428 my ( $restype, $reserves ) =
429 CheckReserves( $it->{'itemnumber'} );
431 $it->{'can_renew'} = 0;
433 push @todaysissues, $it;
438 $it->{'itemtype_charge'}
442 $borrower->{'borrowernumber'}
445 sprintf( "%.2f", $it->{'charge'} );
448 $it->{'can_renew_error'}
451 $borrower->{'borrowernumber'},
454 my ( $restype, $reserves ) =
455 CheckReserves( $it->{'itemnumber'} );
457 $it->{'can_renew'} = 0;
459 push @previousissues, $it;
466 # parses today & build Template array
467 foreach my $book ( sort { $b->{'timestamp'} <=> $a->{'timestamp'} }
470 #warn "TIMESTAMP".$book->{'timestamp'};
471 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
472 $issued_itemtypes_count->{ $book->{'itemtype'} }++;
474 my $dd = $book->{'date_due'};
475 my $datedue = $book->{'date_due'};
477 #$dd=format_date($dd);
479 if ( $datedue < $todaysdate ) {
491 $book->{'togglecolor'} = $togglecolor;
492 $book->{'od'} = format_date($od);
493 $book->{'dd'} = format_date($dd);
494 if ( $book->{'author'} eq '' ) {
495 $book->{'author'} = ' ';
497 push @realtodayissues, $book;
501 # parses previous & build Template array
503 foreach my $book ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
507 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
508 $issued_itemtypes_count->{ $book->{'itemtype'} }++;
510 my $dd = format_date($book->{'date_due'});
511 my $datedue = format_date($book->{'date_due'});
513 #$dd=format_date($dd);
517 if ( $datedue < $todaysdate ) {
529 $book->{'togglecolor'} = $togglecolor;
532 if ( $book->{'author'} eq '' ) {
533 $book->{'author'} = ' ';
535 push @realprevissues, $book;
540 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
541 # FIXME: This should utilize all the issuingrules options rather than just the defaults
542 # and it should be moved to a module
543 my $dbh = C4::Context->dbh;
545 # how many of each is allowed?
546 my $issueqty_sth = $dbh->prepare( "
547 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
549 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
552 my @issued_itemtypes_count;
553 $issueqty_sth->execute("*");
554 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
556 # subtract how many of each this borrower has
557 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
559 ( $data->{'maxissueqty'} -
560 $issued_itemtypes_count->{ $data->{'description'} } );
562 # can't have a negative number of remaining
563 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
564 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
565 unless ( ( $data->{'maxissueqty'} < 1 )
566 || ( $data->{'itemtype'} eq "*" )
567 || ( $data->{'itemtype'} eq "CIRC" ) )
569 push @issued_itemtypes_count, $data;
572 $issued_itemtypes_loop = \@issued_itemtypes_count;
578 my $CGIselectborrower;
579 if ($borrowerslist) {
583 . $a->{'firstname'} cmp $b->{'surname'}
588 push @values, $_->{'borrowernumber'};
589 $labels{ $_->{'borrowernumber'} } =
590 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
592 $CGIselectborrower = CGI::scrolling_list(
593 -name => 'borrowernumber',
594 -id => 'borrowernumber',
604 my $flags = $borrower->{'flags'};
607 foreach $flag ( sort keys %$flags ) {
609 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
610 if ( $flags->{$flag}->{'noissues'} ) {
615 if ( $flag eq 'GNA' ) {
616 $template->param( gna => 'true' );
618 if ( $flag eq 'LOST' ) {
619 $template->param( lost => 'true' );
621 if ( $flag eq 'DBARRED' ) {
622 $template->param( dbarred => 'true' );
624 if ( $flag eq 'CHARGES' ) {
627 chargesmsg => $flags->{'CHARGES'}->{'message'}
630 if ( $flag eq 'CREDITS' ) {
633 creditsmsg => $flags->{'CREDITS'}->{'message'}
638 if ( $flag eq 'CHARGES' ) {
642 chargesmsg => $flags->{'CHARGES'}->{'message'}
645 if ( $flag eq 'CREDITS' ) {
648 creditsmsg => $flags->{'CREDITS'}->{'message'}
651 if ( $flag eq 'ODUES' ) {
655 oduesmsg => $flags->{'ODUES'}->{'message'}
658 my $items = $flags->{$flag}->{'itemlist'};
662 # foreach my $item (@$items) {
663 # my ($iteminformation) =
664 # getiteminformation( $item->{'itemnumber'}, 0 );
665 # push @itemswaiting, $iteminformation;
668 if ( $query->param('module') ne 'returns' ) {
669 $template->param( nonreturns => 'true' );
672 if ( $flag eq 'NOTES' ) {
676 notesmsg => $flags->{'NOTES'}->{'message'}
682 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
683 my @temp = split( /\$/, $amountold );
685 my $CGIorganisations;
686 my $member_of_institution;
687 if ( C4::Context->preference("memberofinstitution") ) {
688 my $organisations = get_institutions();
691 foreach my $organisation ( keys %$organisations ) {
692 push @orgs, $organisation;
693 $org_labels{$organisation} =
694 $organisations->{$organisation}->{'surname'};
696 $member_of_institution = 1;
697 $CGIorganisations = CGI::popup_menu(
698 -id => 'organisations',
699 -name => 'organisations',
700 -labels => \%org_labels,
705 $amountold = $temp[1];
708 issued_itemtypes_count_loop => $issued_itemtypes_loop,
709 findborrower => $findborrower,
710 borrower => $borrower,
711 borrowernumber => $borrowernumber,
714 printername => $printer,
715 firstname => $borrower->{'firstname'},
716 surname => $borrower->{'surname'},
718 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
719 categorycode => $borrower->{'categorycode'},
720 streetaddress => $borrower->{'address'},
721 email => $borrower->{'email'},
722 emailpro => $borrower->{'emailpro'},
723 borrowernotes => $borrower->{'borrowernotes'},
724 city => $borrower->{'city'},
725 phone => $borrower->{'phone'} || $borrower->{'mobile'},
726 cardnumber => $borrower->{'cardnumber'},
727 amountold => $amountold,
729 stickyduedate => $stickyduedate,
731 CGIselectborrower => $CGIselectborrower,
732 todayissues => \@realtodayissues,
733 previssues => \@realprevissues,
734 inprocess => $inprocess,
735 memberofinstution => $member_of_institution,
736 CGIorganisations => $CGIorganisations,
739 # set return date if stickyduedate
740 if ($stickyduedate) {
741 my $t_year = "year" . $year;
742 my $t_month = "month" . $month;
743 my $t_day = "day" . $day;
751 #if ($branchcookie) {
752 #$cookie=[$cookie, $branchcookie, $printercookie];
756 SpecifyDueDate => C4::Context->preference("SpecifyDueDate")
758 output_html_with_http_headers $query, $cookie, $template->output;