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
27 use C4::Circulation::Circ2;
32 use C4::Interface::CGI::Output;
33 use C4::Branch; # GetBranches
34 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');
102 ; # FIXME env is used as an "environment" variable. Could be dropped probably...
105 $env{'branchcode'} = $branch;
106 $env{'printer'} = $printer;
107 $env{'organisation'} = $organisation;
109 # $env{'queue'}=$printer;
111 my @datearr = localtime( time() );
113 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
115 ( 1900 + $datearr[5] )
116 . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
117 . sprintf( "%0.2d", ( $datearr[3] ) );
119 # check and see if we should print
120 if ( $barcode eq '' && $print eq 'maybe' ) {
124 my $inprocess = $query->param('inprocess');
125 if ( $barcode eq '' ) {
131 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
134 borrowernumber => $borrowernumber
138 if ( $print eq 'yes' && $borrowernumber ne '' ) {
139 printslip( \%env, $borrowernumber );
140 $query->param( 'borrowernumber', '' );
141 $borrowernumber = '';
145 # STEP 2 : FIND BORROWER
146 # if there is a list of find borrowers....
151 my ( $count, $borrowers ) =
152 BornameSearch( \%env, $findborrower, 'cardnumber', 'web' );
153 my @borrowers = @$borrowers;
154 if ( $#borrowers == -1 ) {
155 $query->param( 'findborrower', '' );
156 $message = "'$findborrower'";
158 elsif ( $#borrowers == 0 ) {
159 $query->param( 'borrowernumber', $borrowers[0]->{'borrowernumber'} );
160 $query->param( 'barcode', '' );
161 $borrowernumber = $borrowers[0]->{'borrowernumber'};
164 $borrowerslist = \@borrowers;
168 # get the borrower information.....
172 if ($borrowernumber) {
173 $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
174 my ( $od, $issue, $fines ) = borrdata2( \%env, $borrowernumber );
176 # Warningdate is the date that the warning starts appearing
177 my ( $today_year, $today_month, $today_day ) = Today();
178 my ( $warning_year, $warning_month, $warning_day ) = split /-/,
179 $borrower->{'dateexpiry'};
181 # Renew day is calculated by adding the enrolment period to today
182 my ( $renew_year, $renew_month, $renew_day ) =
183 Add_Delta_YM( $today_year, $today_month, $today_day,
184 $borrower->{'enrolmentperiod'}, 0 );
185 # if the expiry date is before today
186 if ( Date_to_Days( $today_year, $today_month, $today_day ) >
187 Date_to_Days( $warning_year, $warning_month, $warning_day ) )
190 #borrowercard expired or nearly expired, warn the librarian
193 warndeparture => "1",
194 renewaldate => "$renew_year-$renew_month-$renew_day"
197 # check for NotifyBorrowerDeparture
198 if (C4::Context->preference('NotifyBorrowerDeparture') &&
199 Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
200 Date_to_Days( $today_year, $today_month, $today_day ) )
202 $template->param("warndeparture" => 1);
206 issuecount => $issue,
217 $barcode = cuecatbarcodedecode($barcode);
218 my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
219 if ($issueconfirmed) {
220 issuebook( \%env, $borrower, $barcode, $datedue, $cancelreserve );
224 my ( $error, $question ) =
225 canbookbeissued( \%env, $borrower, $barcode, $year, $month, $day,
229 # Get the item title for more information
230 my $getmessageiteminfo = getiteminformation( undef, $barcode );
232 foreach my $impossible ( keys %$error ) {
234 $impossible => $$error{$impossible},
239 foreach my $needsconfirmation ( keys %$question ) {
241 $needsconfirmation => $$question{$needsconfirmation},
242 getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
243 NEEDSCONFIRMATION => 1
252 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
253 issuebook( \%env, $borrower, $barcode, $datedue );
258 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue
259 my ( $od, $issue, $fines ) = borrdata2( \%env, $borrowernumber );
261 issuecount => $issue,
265 # reload the borrower info for the sake of reseting the flags.....
266 if ($borrowernumber) {
267 $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
270 ##################################################################################
272 # show all reserves of this borrower, and the position of the reservation ....
273 if ($borrowernumber) {
276 # now we show the status of the borrower's reservations
277 my @borrowerreserv = GetReservations( 0, $borrowernumber );
279 my @WaitingReserveLoop;
281 foreach my $num_res (@borrowerreserv) {
283 my %getWaitingReserveInfo;
285 my $getiteminfo = getiteminformation( $num_res->{'itemnumber'} );
286 my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
287 my ( $transfertwhen, $transfertfrom, $transfertto ) =
288 checktransferts( $num_res->{'itemnumber'} );
290 $getreserv{waiting} = 0;
291 $getreserv{transfered} = 0;
292 $getreserv{nottransfered} = 0;
294 $getreserv{reservedate} = format_date( $num_res->{'reservedate'} );
295 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
296 $getreserv{title} = $getiteminfo->{'title'};
297 $getreserv{itemtype} = $itemtypeinfo->{'description'};
298 $getreserv{author} = $getiteminfo->{'author'};
299 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
300 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
302 # check if we have a waiting status for reservations
303 if ( $num_res->{'found'} eq 'W' ) {
304 $getreserv{color} = 'reserved';
305 $getreserv{waiting} = 1;
306 # genarate information displaying only waiting reserves
307 $getWaitingReserveInfo{title} = $getiteminfo->{'title'};
308 $getWaitingReserveInfo{itemtype} = $itemtypeinfo->{'description'};
309 $getWaitingReserveInfo{author} = $getiteminfo->{'author'};
310 $getWaitingReserveInfo{reservedate} = format_date( $num_res->{'reservedate'} );
311 if ($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} ) {
312 $getWaitingReserveInfo{waitingat} = GetBranchName( $num_res->{'branchcode'} );
316 # check transfers with the itemnumber foud in th reservation loop
317 if ($transfertwhen) {
318 $getreserv{color} = 'transfered';
319 $getreserv{transfered} = 1;
320 $getreserv{datesent} = format_date($transfertwhen);
321 $getreserv{frombranch} = GetBranchName($transfertfrom);
324 if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
325 and not $transfertwhen )
327 $getreserv{nottransfered} = 1;
328 $getreserv{nottransferedby} =
329 GetBranchName( $getiteminfo->{'holdingbranch'} );
332 # if we don't have a reserv on item, we put the biblio infos and the waiting position
333 if ( $getiteminfo->{'title'} eq '' ) {
334 my $getbibinfo = GetBiblioItemData( $num_res->{'biblionumber'} );
335 my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
336 $getreserv{color} = 'inwait';
337 $getreserv{title} = $getbibinfo->{'title'};
338 $getreserv{waitingposition} = $num_res->{'priority'};
339 $getreserv{nottransfered} = 0;
340 $getreserv{itemtype} = $getbibtype->{'description'};
341 $getreserv{author} = $getbibinfo->{'author'};
342 $getreserv{itemcallnumber} = '----------';
345 push( @reservloop, \%getreserv );
347 # if we have a reserve waiting, initiate waitingreserveloop
348 if ($getreserv{waiting} eq 1) {
349 push (@WaitingReserveLoop, \%getWaitingReserveInfo)
354 # return result to the template
356 countreserv => scalar @reservloop,
357 reservloop => \@reservloop ,
358 WaitingReserveLoop => \@WaitingReserveLoop,
362 # make the issued books table.
363 my $todaysissues = '';
368 ## ADDED BY JF: new itemtype issuingrules counter stuff
369 my $issued_itemtypes_loop;
370 my $issued_itemtypes_count;
371 my $issued_itemtypes_allowed_count; # hashref with total allowed by itemtype
372 my $issued_itemtypes_remaining; # hashref with remaining
373 my $issued_itemtypes_flags; #hashref that stores flags
377 # get each issue of the borrower & separate them in todayissues & previous issues
380 my $issueslist = getissues($borrower);
382 # split in 2 arrays for today & previous
383 my $dbh = C4::Context->dbh;
384 foreach my $it ( keys %$issueslist ) {
385 my $issuedate = $issueslist->{$it}->{'timestamp'};
386 $issuedate =~ s/-//g;
387 $issuedate = substr( $issuedate, 0, 8 );
389 # to let perl sort this correctly
390 $issueslist->{$it}->{'timestamp'} =~ s/(-|\:| )//g;
392 if ( $todaysdate == $issuedate ) {
394 $issueslist->{$it}->{'charge'},
395 $issueslist->{$it}->{'itemtype_charge'}
399 $issueslist->{$it}->{'itemnumber'},
400 $borrower->{'borrowernumber'}
402 $issueslist->{$it}->{'charge'} =
403 sprintf( "%.2f", $issueslist->{$it}->{'charge'} );
405 $issueslist->{$it}->{'can_renew'},
406 $issueslist->{$it}->{'can_renew_error'}
410 $borrower->{'borrowernumber'},
411 $issueslist->{$it}->{'itemnumber'}
413 my ( $restype, $reserves ) =
414 CheckReserves( $issueslist->{$it}->{'itemnumber'} );
416 $issueslist->{$it}->{'can_renew'} = 0;
418 push @todaysissues, $issueslist->{$it};
422 $issueslist->{$it}->{'charge'},
423 $issueslist->{$it}->{'itemtype_charge'}
427 $issueslist->{$it}->{'itemnumber'},
428 $borrower->{'borrowernumber'}
430 $issueslist->{$it}->{'charge'} =
431 sprintf( "%.2f", $issueslist->{$it}->{'charge'} );
433 $issueslist->{$it}->{'can_renew'},
434 $issueslist->{$it}->{'can_renew_error'}
438 $borrower->{'borrowernumber'},
439 $issueslist->{$it}->{'itemnumber'}
441 my ( $restype, $reserves ) =
442 CheckReserves( $issueslist->{$it}->{'itemnumber'} );
444 $issueslist->{$it}->{'can_renew'} = 0;
446 push @previousissues, $issueslist->{$it};
453 # parses today & build Template array
454 foreach my $book ( sort { $b->{'timestamp'} <=> $a->{'timestamp'} }
457 #warn "TIMESTAMP".$book->{'timestamp'};
458 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
459 $issued_itemtypes_count->{ $book->{'itemtype'} }++;
461 my $dd = $book->{'date_due'};
462 my $datedue = $book->{'date_due'};
464 #$dd=format_date($dd);
466 if ( $datedue < $todaysdate ) {
478 $book->{'togglecolor'} = $togglecolor;
479 $book->{'od'} = format_date($od);
480 $book->{'dd'} = format_date($dd);
481 if ( $book->{'author'} eq '' ) {
482 $book->{'author'} = ' ';
484 push @realtodayissues, $book;
488 # parses previous & build Template array
490 foreach my $book ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
494 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
495 $issued_itemtypes_count->{ $book->{'itemtype'} }++;
497 my $dd = format_date($book->{'date_due'});
498 my $datedue = format_date($book->{'date_due'});
500 #$dd=format_date($dd);
504 if ( $datedue < $todaysdate ) {
516 $book->{'togglecolor'} = $togglecolor;
519 if ( $book->{'author'} eq '' ) {
520 $book->{'author'} = ' ';
522 push @realprevissues, $book;
527 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
528 # FIXME: This should utilize all the issuingrules options rather than just the defaults
529 # and it should be moved to a module
530 my $dbh = C4::Context->dbh;
532 # how many of each is allowed?
533 my $issueqty_sth = $dbh->prepare( "
534 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
536 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
539 my @issued_itemtypes_count;
540 $issueqty_sth->execute("*");
541 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
543 # subtract how many of each this borrower has
544 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
546 ( $data->{'maxissueqty'} -
547 $issued_itemtypes_count->{ $data->{'description'} } );
549 # can't have a negative number of remaining
550 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
551 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
552 unless ( ( $data->{'maxissueqty'} < 1 )
553 || ( $data->{'itemtype'} eq "*" )
554 || ( $data->{'itemtype'} eq "CIRC" ) )
556 push @issued_itemtypes_count, $data;
559 $issued_itemtypes_loop = \@issued_itemtypes_count;
565 my $CGIselectborrower;
566 if ($borrowerslist) {
570 . $a->{'firstname'} cmp $b->{'surname'}
575 push @values, $_->{'borrowernumber'};
576 $labels{ $_->{'borrowernumber'} } =
577 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
579 $CGIselectborrower = CGI::scrolling_list(
580 -name => 'borrowernumber',
590 my $flags = $borrower->{'flags'};
593 foreach $flag ( sort keys %$flags ) {
595 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
596 if ( $flags->{$flag}->{'noissues'} ) {
601 if ( $flag eq 'GNA' ) {
602 $template->param( gna => 'true' );
604 if ( $flag eq 'LOST' ) {
605 $template->param( lost => 'true' );
607 if ( $flag eq 'DBARRED' ) {
608 $template->param( dbarred => 'true' );
610 if ( $flag eq 'CHARGES' ) {
613 chargesmsg => $flags->{'CHARGES'}->{'message'}
616 if ( $flag eq 'CREDITS' ) {
619 creditsmsg => $flags->{'CREDITS'}->{'message'}
624 if ( $flag eq 'CHARGES' ) {
628 chargesmsg => $flags->{'CHARGES'}->{'message'}
631 if ( $flag eq 'CREDITS' ) {
634 creditsmsg => $flags->{'CREDITS'}->{'message'}
637 if ( $flag eq 'ODUES' ) {
641 oduesmsg => $flags->{'ODUES'}->{'message'}
644 my $items = $flags->{$flag}->{'itemlist'};
647 foreach my $item (@$items) {
648 my ($iteminformation) =
649 getiteminformation( $item->{'itemnumber'}, 0 );
650 push @itemswaiting, $iteminformation;
653 if ( $query->param('module') ne 'returns' ) {
654 $template->param( nonreturns => 'true' );
657 if ( $flag eq 'NOTES' ) {
661 notesmsg => $flags->{'NOTES'}->{'message'}
667 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
668 my @temp = split( /\$/, $amountold );
670 my $CGIorganisations;
671 my $member_of_institution;
672 if ( C4::Context->preference("memberofinstitution") ) {
673 my $organisations = get_institutions();
676 foreach my $organisation ( keys %$organisations ) {
677 push @orgs, $organisation;
678 $org_labels{$organisation} =
679 $organisations->{$organisation}->{'surname'};
681 $member_of_institution = 1;
682 $CGIorganisations = CGI::popup_menu(
683 -id => 'organisations',
684 -name => 'organisations',
685 -labels => \%org_labels,
690 $amountold = $temp[1];
693 issued_itemtypes_count_loop => $issued_itemtypes_loop,
694 findborrower => $findborrower,
695 borrower => $borrower,
696 borrowernumber => $borrowernumber,
699 printername => $printer,
700 firstname => $borrower->{'firstname'},
701 surname => $borrower->{'surname'},
703 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
704 categorycode => $borrower->{'categorycode'},
705 streetaddress => $borrower->{'address'},
706 emailaddress => $borrower->{'emailaddress'},
707 borrowernotes => $borrower->{'borrowernotes'},
708 city => $borrower->{'city'},
709 phone => $borrower->{'phone'},
710 cardnumber => $borrower->{'cardnumber'},
711 amountold => $amountold,
713 stickyduedate => $stickyduedate,
715 CGIselectborrower => $CGIselectborrower,
716 todayissues => \@realtodayissues,
717 previssues => \@realprevissues,
718 inprocess => $inprocess,
719 memberofinstution => $member_of_institution,
720 CGIorganisations => $CGIorganisations,
723 # set return date if stickyduedate
724 if ($stickyduedate) {
725 my $t_year = "year" . $year;
726 my $t_month = "month" . $month;
727 my $t_day = "day" . $day;
735 #if ($branchcookie) {
736 #$cookie=[$cookie, $branchcookie, $printercookie];
740 SpecifyDueDate => C4::Context->preference("SpecifyDueDate")
742 output_html_with_http_headers $query, $cookie, $template->output;
744 ####################################################################
745 # Extra subroutines,,,
747 sub cuecatbarcodedecode {
750 my @fields = split( /\./, $barcode );
751 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
752 if ( $#results == 2 ) {