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');
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 = GetMemberDetails( $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 AddIssue( \%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 = GetBiblioFromItemNumber( 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 AddIssue( \%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 = GetMemberDetails( $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 = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
286 my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
287 my ( $transfertwhen, $transfertfrom, $transfertto ) =
288 GetTransfers( $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 = GetBorrowerIssues($borrower);
382 # split in 2 arrays for today & previous
383 my $dbh = C4::Context->dbh;
384 foreach my $it ( @$issueslist ) {
385 my $issuedate = $it->{'timestamp'};
386 $issuedate =~ s/-//g;
387 $issuedate = substr( $issuedate, 0, 8 );
389 # to let perl sort this correctly
390 $it->{'timestamp'} =~ s/(-|\:| )//g;
392 if ( $todaysdate == $issuedate ) {
395 $it->{'itemtype_charge'}
399 $borrower->{'borrowernumber'}
402 sprintf( "%.2f", $it->{'charge'} );
405 $it->{'can_renew_error'}
408 $borrower->{'borrowernumber'},
411 my ( $restype, $reserves ) =
412 CheckReserves( $it->{'itemnumber'} );
414 $it->{'can_renew'} = 0;
416 push @todaysissues, $it;
421 $it->{'itemtype_charge'}
425 $borrower->{'borrowernumber'}
428 sprintf( "%.2f", $it->{'charge'} );
431 $it->{'can_renew_error'}
434 $borrower->{'borrowernumber'},
437 my ( $restype, $reserves ) =
438 CheckReserves( $it->{'itemnumber'} );
440 $it->{'can_renew'} = 0;
442 push @previousissues, $it;
449 # parses today & build Template array
450 foreach my $book ( sort { $b->{'timestamp'} <=> $a->{'timestamp'} }
453 #warn "TIMESTAMP".$book->{'timestamp'};
454 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
455 $issued_itemtypes_count->{ $book->{'itemtype'} }++;
457 my $dd = $book->{'date_due'};
458 my $datedue = $book->{'date_due'};
460 #$dd=format_date($dd);
462 if ( $datedue < $todaysdate ) {
474 $book->{'togglecolor'} = $togglecolor;
475 $book->{'od'} = format_date($od);
476 $book->{'dd'} = format_date($dd);
477 if ( $book->{'author'} eq '' ) {
478 $book->{'author'} = ' ';
480 push @realtodayissues, $book;
484 # parses previous & build Template array
486 foreach my $book ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
490 # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
491 $issued_itemtypes_count->{ $book->{'itemtype'} }++;
493 my $dd = format_date($book->{'date_due'});
494 my $datedue = format_date($book->{'date_due'});
496 #$dd=format_date($dd);
500 if ( $datedue < $todaysdate ) {
512 $book->{'togglecolor'} = $togglecolor;
515 if ( $book->{'author'} eq '' ) {
516 $book->{'author'} = ' ';
518 push @realprevissues, $book;
523 #### ADDED BY JF FOR COUNTS BY ITEMTYPE RULES
524 # FIXME: This should utilize all the issuingrules options rather than just the defaults
525 # and it should be moved to a module
526 my $dbh = C4::Context->dbh;
528 # how many of each is allowed?
529 my $issueqty_sth = $dbh->prepare( "
530 SELECT itemtypes.description AS description,issuingrules.itemtype,maxissueqty
532 LEFT JOIN itemtypes ON (itemtypes.itemtype=issuingrules.itemtype)
535 my @issued_itemtypes_count;
536 $issueqty_sth->execute("*");
537 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
539 # subtract how many of each this borrower has
540 $data->{'count'} = $issued_itemtypes_count->{ $data->{'description'} };
542 ( $data->{'maxissueqty'} -
543 $issued_itemtypes_count->{ $data->{'description'} } );
545 # can't have a negative number of remaining
546 if ( $data->{'left'} < 0 ) { $data->{'left'} = "0" }
547 $data->{'flag'} = 1 unless ( $data->{'maxissueqty'} > $data->{'count'} );
548 unless ( ( $data->{'maxissueqty'} < 1 )
549 || ( $data->{'itemtype'} eq "*" )
550 || ( $data->{'itemtype'} eq "CIRC" ) )
552 push @issued_itemtypes_count, $data;
555 $issued_itemtypes_loop = \@issued_itemtypes_count;
561 my $CGIselectborrower;
562 if ($borrowerslist) {
566 . $a->{'firstname'} cmp $b->{'surname'}
571 push @values, $_->{'borrowernumber'};
572 $labels{ $_->{'borrowernumber'} } =
573 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'address'} ";
575 $CGIselectborrower = CGI::scrolling_list(
576 -name => 'borrowernumber',
586 my $flags = $borrower->{'flags'};
589 foreach $flag ( sort keys %$flags ) {
591 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
592 if ( $flags->{$flag}->{'noissues'} ) {
597 if ( $flag eq 'GNA' ) {
598 $template->param( gna => 'true' );
600 if ( $flag eq 'LOST' ) {
601 $template->param( lost => 'true' );
603 if ( $flag eq 'DBARRED' ) {
604 $template->param( dbarred => 'true' );
606 if ( $flag eq 'CHARGES' ) {
609 chargesmsg => $flags->{'CHARGES'}->{'message'}
612 if ( $flag eq 'CREDITS' ) {
615 creditsmsg => $flags->{'CREDITS'}->{'message'}
620 if ( $flag eq 'CHARGES' ) {
624 chargesmsg => $flags->{'CHARGES'}->{'message'}
627 if ( $flag eq 'CREDITS' ) {
630 creditsmsg => $flags->{'CREDITS'}->{'message'}
633 if ( $flag eq 'ODUES' ) {
637 oduesmsg => $flags->{'ODUES'}->{'message'}
640 my $items = $flags->{$flag}->{'itemlist'};
644 # foreach my $item (@$items) {
645 # my ($iteminformation) =
646 # getiteminformation( $item->{'itemnumber'}, 0 );
647 # push @itemswaiting, $iteminformation;
650 if ( $query->param('module') ne 'returns' ) {
651 $template->param( nonreturns => 'true' );
654 if ( $flag eq 'NOTES' ) {
658 notesmsg => $flags->{'NOTES'}->{'message'}
664 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
665 my @temp = split( /\$/, $amountold );
667 my $CGIorganisations;
668 my $member_of_institution;
669 if ( C4::Context->preference("memberofinstitution") ) {
670 my $organisations = get_institutions();
673 foreach my $organisation ( keys %$organisations ) {
674 push @orgs, $organisation;
675 $org_labels{$organisation} =
676 $organisations->{$organisation}->{'surname'};
678 $member_of_institution = 1;
679 $CGIorganisations = CGI::popup_menu(
680 -id => 'organisations',
681 -name => 'organisations',
682 -labels => \%org_labels,
687 $amountold = $temp[1];
690 issued_itemtypes_count_loop => $issued_itemtypes_loop,
691 findborrower => $findborrower,
692 borrower => $borrower,
693 borrowernumber => $borrowernumber,
696 printername => $printer,
697 firstname => $borrower->{'firstname'},
698 surname => $borrower->{'surname'},
700 $borrower->{'dateexpiry'}, #format_date($borrower->{'dateexpiry'}),
701 categorycode => $borrower->{'categorycode'},
702 streetaddress => $borrower->{'address'},
703 emailaddress => $borrower->{'emailaddress'},
704 borrowernotes => $borrower->{'borrowernotes'},
705 city => $borrower->{'city'},
706 phone => $borrower->{'phone'},
707 cardnumber => $borrower->{'cardnumber'},
708 amountold => $amountold,
710 stickyduedate => $stickyduedate,
712 CGIselectborrower => $CGIselectborrower,
713 todayissues => \@realtodayissues,
714 previssues => \@realprevissues,
715 inprocess => $inprocess,
716 memberofinstution => $member_of_institution,
717 CGIorganisations => $CGIorganisations,
720 # set return date if stickyduedate
721 if ($stickyduedate) {
722 my $t_year = "year" . $year;
723 my $t_month = "month" . $month;
724 my $t_day = "day" . $day;
732 #if ($branchcookie) {
733 #$cookie=[$cookie, $branchcookie, $printercookie];
737 SpecifyDueDate => C4::Context->preference("SpecifyDueDate")
739 output_html_with_http_headers $query, $cookie, $template->output;
741 ####################################################################
742 # Extra subroutines,,,
744 sub cuecatbarcodedecode {
747 my @fields = split( /\./, $barcode );
748 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
749 if ( $#results == 2 ) {