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::Interface::CGI::Output;
32 use C4::Branch; # GetBranches
33 use C4::Koha; # GetPrinter
52 my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
54 template_name => 'circ/circulation.tmpl',
58 flagsrequired => { circulate => 1 },
61 my $branches = GetBranches();
63 my $printers = GetPrinters();
64 my $printer = GetPrinter($query, $printers);
66 my $findborrower = $query->param('findborrower');
67 $findborrower =~ s|,| |g;
68 #$findborrower =~ s|'| |g;
69 my $borrowernumber = $query->param('borrowernumber');
71 # new op dev the branch and the printer are now defined by the userenv
72 my $branch = C4::Context->userenv->{'branch'};
73 my $printer = C4::Context->userenv->{'branchprinter'};
75 # If Autolocated is not activated, we show the Circulation Parameters to chage settings of librarian
76 if (C4::Context->preference("AutoLocation") ne 1)
83 my $barcode = $query->param('barcode') || '';
84 my $year = $query->param('year');
85 my $month = $query->param('month');
86 my $day = $query->param('day');
87 my $stickyduedate = $query->param('stickyduedate');
88 my $issueconfirmed = $query->param('issueconfirmed');
89 my $cancelreserve = $query->param('cancelreserve');
90 my $organisation = $query->param('organisations');
91 my $print = $query->param('print');
96 # if ($query->param('setcookies')) {
97 # $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
98 # $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
103 my @datearr = localtime( time() );
105 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
107 ( 1900 + $datearr[5] )
108 . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
109 . sprintf( "%0.2d", ( $datearr[3] ) );
111 # check and see if we should print
112 if ( $barcode eq '' && $print eq 'maybe' ) {
116 my $inprocess = $query->param('inprocess');
117 if ( $barcode eq '' ) {
123 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
126 borrowernumber => $borrowernumber
130 if ( $print eq 'yes' && $borrowernumber ne '' ) {
131 printslip( $borrowernumber );
132 $query->param( 'borrowernumber', '' );
133 $borrowernumber = '';
137 # STEP 2 : FIND BORROWER
138 # if there is a list of find borrowers....
143 my ( $count, $borrowers ) =
144 SearchBorrower($findborrower, 'cardnumber', 'web' );
145 my @borrowers = @$borrowers;
146 if ( $#borrowers == -1 ) {
147 $query->param( 'findborrower', '' );
148 $message = "'$findborrower'";
150 elsif ( $#borrowers == 0 ) {
151 $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
152 $query->param( 'barcode', '' );
153 $borrowernumber = $borrowers[0]->{'borrowernumber'};
156 $borrowerslist = \@borrowers;
160 # get the borrower information.....
164 if ($borrowernumber) {
165 $borrower = GetMemberDetails( $borrowernumber, 0 );
166 my ( $od, $issue, $fines ) = GetBorrowerIssuesAndFines( $borrowernumber );
168 # Warningdate is the date that the warning starts appearing
169 my ( $today_year, $today_month, $today_day ) = Today();
170 my ( $warning_year, $warning_month, $warning_day ) = split /-/,
171 $borrower->{'dateexpiry'};
173 # Renew day is calculated by adding the enrolment period to today
174 my ( $renew_year, $renew_month, $renew_day ) =
175 Add_Delta_YM( $today_year, $today_month, $today_day,
176 $borrower->{'enrolmentperiod'}, 0 );
177 # if the expiry date is before today
178 if ( Date_to_Days( $today_year, $today_month, $today_day ) >
179 Date_to_Days( $warning_year, $warning_month, $warning_day ) )
182 #borrowercard expired or nearly expired, warn the librarian
185 warndeparture => "1",
186 renewaldate => "$renew_year-$renew_month-$renew_day"
189 # check for NotifyBorrowerDeparture
190 if (C4::Context->preference('NotifyBorrowerDeparture') &&
191 Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
192 Date_to_Days( $today_year, $today_month, $today_day ) )
194 $template->param("warndeparture" => 1);
198 issuecount => $issue,
209 $barcode = cuecatbarcodedecode($barcode);
210 my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
211 if ($issueconfirmed) {
212 AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
216 my ( $error, $question ) =
217 CanBookBeIssued( $borrower, $barcode, $year, $month, $day,
221 # Get the item title for more information
222 my $getmessageiteminfo = GetBiblioFromItemNumber( undef, $barcode );
224 foreach my $impossible ( keys %$error ) {
226 $impossible => $$error{$impossible},
231 foreach my $needsconfirmation ( keys %$question ) {
233 $needsconfirmation => $$question{$needsconfirmation},
234 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
235 NEEDSCONFIRMATION => 1
244 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
245 AddIssue( $borrower, $barcode, $datedue );
250 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue
251 my ( $od, $issue, $fines ) = GetBorrowerIssuesAndFines( $borrowernumber );
253 issuecount => $issue,
257 # reload the borrower info for the sake of reseting the flags.....
258 if ($borrowernumber) {
259 $borrower = GetMemberDetails( $borrowernumber, 0 );
262 ##################################################################################
264 # show all reserves of this borrower, and the position of the reservation ....
265 if ($borrowernumber) {
268 # now we show the status of the borrower's reservations
269 my @borrowerreserv = GetReservations( 0, $borrowernumber );
271 my @WaitingReserveLoop;
273 foreach my $num_res (@borrowerreserv) {
275 my %getWaitingReserveInfo;
276 my $getiteminfo = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
277 my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
278 my ( $transfertwhen, $transfertfrom, $transfertto ) =
279 GetTransfers( $num_res->{'itemnumber'} );
281 $getreserv{waiting} = 0;
282 $getreserv{transfered} = 0;
283 $getreserv{nottransfered} = 0;
285 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
286 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
287 $getreserv{title} = $getiteminfo->{'title'};
288 $getreserv{itemtype} = $itemtypeinfo->{'description'};
289 $getreserv{author} = $getiteminfo->{'author'};
290 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
291 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
293 # check if we have a waiting status for reservations
294 if ( $num_res->{'found'} eq 'W' ) {
295 $getreserv{color} = 'reserved';
296 $getreserv{waiting} = 1;
297 # genarate information displaying only waiting reserves
298 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
299 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
300 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
301 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
302 if ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} ) {
303 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
307 # check transfers with the itemnumber foud in th reservation loop
308 if ($transfertwhen) {
309 $getreserv{color} = 'transfered';
310 $getreserv{transfered} = 1;
311 $getreserv{datesent} = format_date($transfertwhen);
312 $getreserv{frombranch} = GetBranchName($transfertfrom);
315 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
316 and not $transfertwhen )
318 $getreserv{nottransfered} = 1;
319 $getreserv{nottransferedby} =
320 GetBranchName( $getiteminfo->{'holdingbranch'} );
323 # if we don't have a reserv on item, we put the biblio infos and the waiting position
324 if ( $getiteminfo->{'title'} eq '' ) {
325 my $getbibinfo = GetBiblioItemData( $num_res->{'biblionumber'} );
326 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
327 $getreserv{color} = 'inwait';
328 $getreserv{title} = $getbibinfo->{'title'};
329 $getreserv{waitingposition} = $num_res->{'priority'};
330 $getreserv{nottransfered} = 0;
331 $getreserv{itemtype} = $getbibtype->{'description'};
332 $getreserv{author} = $getbibinfo->{'author'};
333 $getreserv{itemcallnumber} = '----------';
336 push( @reservloop, \%getreserv );
338 # if we have a reserve waiting, initiate waitingreserveloop
339 if ($getreserv{waiting} eq 1) {
340 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
345 # return result to the template
347 countreserv => scalar @reservloop,
348 reservloop => \@reservloop ,
349 WaitingReserveLoop => \@WaitingReserveLoop,
353 # make the issued books table.
354 my $todaysissues = '';
359 ## ADDED BY JF: new itemtype issuingrules counter stuff
360 my $issued_itemtypes_loop;
361 my $issued_itemtypes_count;
362 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
363 my $issued_itemtypes_remaining; # hashref with remaining
364 my $issued_itemtypes_flags; #hashref that stores flags
368 # get each issue of the borrower & separate them in todayissues & previous issues
371 my $issueslist = GetBorrowerIssues($borrower);
373 # split in 2 arrays for today & previous
374 my $dbh = C4::Context->dbh;
375 foreach my $it ( @$issueslist ) {
376 my $issuedate = $it->{'timestamp'};
377 $issuedate =~ s/-//g;
378 $issuedate = substr( $issuedate, 0, 8 );
380 # to let perl sort this correctly
381 $it->{'timestamp'} =~ s/(-|\:| )//g;
383 if ( $todaysdate == $issuedate ) {
386 $it->{'itemtype_charge'}
390 $borrower->{'borrowernumber'}
393 sprintf( "%.2f", $it->{'charge'} );
396 $it->{'can_renew_error'}
399 $borrower->{'borrowernumber'},
402 my ( $restype, $reserves ) =
403 CheckReserves( $it->{'itemnumber'} );
405 $it->{'can_renew'} = 0;
407 push @todaysissues, $it;
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 @previousissues, $it;
440 # parses today & build Template array
441 foreach my $book ( sort { $b->{'timestamp'} <=> $a->{'timestamp'} }
444 #warn "TIMESTAMP".$book->{'timestamp'};
445 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
446 $issued_itemtypes_count->{ $book->{'itemtype'} }++;
448 my $dd = $book->{'date_due'};
449 my $datedue = $book->{'date_due'};
451 #$dd=format_date($dd);
453 if ( $datedue < $todaysdate ) {
465 $book->{'togglecolor'} = $togglecolor;
466 $book->{'od'} = format_date($od);
467 $book->{'dd'} = format_date($dd);
468 if ( $book->{'author'} eq '' ) {
469 $book->{'author'} = ' ';
471 push @realtodayissues, $book;
475 # parses previous & build Template array
477 foreach my $book ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
481 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
482 $issued_itemtypes_count->{ $book->{'itemtype'} }++;
484 my $dd = format_date($book->{'date_due'});
485 my $datedue = format_date($book->{'date_due'});
487 #$dd=format_date($dd);
491 if ( $datedue < $todaysdate ) {
503 $book->{'togglecolor'} = $togglecolor;
506 if ( $book->{'author'} eq '' ) {
507 $book->{'author'} = ' ';
509 push @realprevissues, $book;
514 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
515 # FIXME: This should utilize all the issuingrules options rather than just the defaults
516 # and it should be moved to a module
517 my $dbh = C4::Context->dbh;
519 # how many of each is allowed?
520 my $issueqty_sth = $dbh->prepare( "
521 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
523 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
526 my @issued_itemtypes_count;
527 $issueqty_sth->execute("*");
528 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
530 # subtract how many of each this borrower has
531 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
533 ( $data->{'maxissueqty'} -
534 $issued_itemtypes_count->{ $data->{'description'} } );
536 # can't have a negative number of remaining
537 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
538 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
539 unless ( ( $data->{'maxissueqty'} < 1 )
540 || ( $data->{'itemtype'} eq "*" )
541 || ( $data->{'itemtype'} eq "CIRC" ) )
543 push @issued_itemtypes_count, $data;
546 $issued_itemtypes_loop = \@issued_itemtypes_count;
552 my $CGIselectborrower;
553 if ($borrowerslist) {
557 . $a->{'firstname'} cmp $b->{'surname'}
562 push @values, $_->{'borrowernumber'};
563 $labels{ $_->{'borrowernumber'} } =
564 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
566 $CGIselectborrower = CGI::scrolling_list(
567 -name => 'borrowernumber',
577 my $flags = $borrower->{'flags'};
580 foreach $flag ( sort keys %$flags ) {
582 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
583 if ( $flags->{$flag}->{'noissues'} ) {
588 if ( $flag eq 'GNA' ) {
589 $template->param( gna => 'true' );
591 if ( $flag eq 'LOST' ) {
592 $template->param( lost => 'true' );
594 if ( $flag eq 'DBARRED' ) {
595 $template->param( dbarred => 'true' );
597 if ( $flag eq 'CHARGES' ) {
600 chargesmsg => $flags->{'CHARGES'}->{'message'}
603 if ( $flag eq 'CREDITS' ) {
606 creditsmsg => $flags->{'CREDITS'}->{'message'}
611 if ( $flag eq 'CHARGES' ) {
615 chargesmsg => $flags->{'CHARGES'}->{'message'}
618 if ( $flag eq 'CREDITS' ) {
621 creditsmsg => $flags->{'CREDITS'}->{'message'}
624 if ( $flag eq 'ODUES' ) {
628 oduesmsg => $flags->{'ODUES'}->{'message'}
631 my $items = $flags->{$flag}->{'itemlist'};
635 # foreach my $item (@$items) {
636 # my ($iteminformation) =
637 # getiteminformation( $item->{'itemnumber'}, 0 );
638 # push @itemswaiting, $iteminformation;
641 if ( $query->param('module') ne 'returns' ) {
642 $template->param( nonreturns => 'true' );
645 if ( $flag eq 'NOTES' ) {
649 notesmsg => $flags->{'NOTES'}->{'message'}
655 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
656 my @temp = split( /\$/, $amountold );
658 my $CGIorganisations;
659 my $member_of_institution;
660 if ( C4::Context->preference("memberofinstitution") ) {
661 my $organisations = get_institutions();
664 foreach my $organisation ( keys %$organisations ) {
665 push @orgs, $organisation;
666 $org_labels{$organisation} =
667 $organisations->{$organisation}->{'surname'};
669 $member_of_institution = 1;
670 $CGIorganisations = CGI::popup_menu(
671 -id => 'organisations',
672 -name => 'organisations',
673 -labels => \%org_labels,
678 $amountold = $temp[1];
681 issued_itemtypes_count_loop => $issued_itemtypes_loop,
682 findborrower => $findborrower,
683 borrower => $borrower,
684 borrowernumber => $borrowernumber,
687 printername => $printer,
688 firstname => $borrower->{'firstname'},
689 surname => $borrower->{'surname'},
691 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
692 categorycode => $borrower->{'categorycode'},
693 streetaddress => $borrower->{'address'},
694 emailaddress => $borrower->{'emailaddress'},
695 borrowernotes => $borrower->{'borrowernotes'},
696 city => $borrower->{'city'},
697 phone => $borrower->{'phone'},
698 cardnumber => $borrower->{'cardnumber'},
699 amountold => $amountold,
701 stickyduedate => $stickyduedate,
703 CGIselectborrower => $CGIselectborrower,
704 todayissues => \@realtodayissues,
705 previssues => \@realprevissues,
706 inprocess => $inprocess,
707 memberofinstution => $member_of_institution,
708 CGIorganisations => $CGIorganisations,
711 # set return date if stickyduedate
712 if ($stickyduedate) {
713 my $t_year = "year" . $year;
714 my $t_month = "month" . $month;
715 my $t_day = "day" . $day;
723 #if ($branchcookie) {
724 #$cookie=[$cookie, $branchcookie, $printercookie];
728 SpecifyDueDate => C4::Context->preference("SpecifyDueDate")
730 output_html_with_http_headers $query, $cookie, $template->output;
732 ####################################################################
733 # Extra subroutines,,,
735 sub cuecatbarcodedecode {
738 my @fields = split( /\./, $barcode );
739 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
740 if ( $#results == 2 ) {