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;
34 use C4::Interface::CGI::Output;
41 use C4::Circulation::Date;
48 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
50 template_name => 'circ/circulation.tmpl',
54 flagsrequired => { circulate => 1 },
57 my $branches = getbranches();
58 # my $printers = getprinters();
59 # my $printer = getprinter($query, $printers);
61 my $findborrower = $query->param('findborrower');
62 $findborrower =~ s|,| |g;
63 $findborrower =~ s|'| |g;
64 my $borrowernumber = $query->param('borrnumber');
65 # new op dev the branch and the printer are now defined by the userenv
66 my $branch = C4::Context->userenv->{'branch'};
67 my $printer=C4::Context->userenv->{'branchprinter'};
69 my $barcode = $query->param('barcode') || '';
70 my $year=$query->param('year');
71 my $month=$query->param('month');
72 my $day=$query->param('day');
73 my $stickyduedate=$query->param('stickyduedate');
74 my $issueconfirmed = $query->param('issueconfirmed');
75 my $cancelreserve = $query->param('cancelreserve');
76 my $organisation = $query->param('organisations');
77 my $print = $query->param('print');
82 # if ($query->param('setcookies')) {
83 # $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
84 # $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
87 my %env; # FIXME env is used as an "environment" variable. Could be dropped probably...
90 $env{'branchcode'}= $branch;
91 $env{'printer'}= $printer;
92 # $env{'queue'}=$printer;
94 my @datearr = localtime(time());
95 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
97 ( 1900 + $datearr[5] )
98 . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
99 . sprintf( "%0.2d", ( $datearr[3] ) );
101 # check and see if we should print
102 if ( $barcode eq '' && $print eq 'maybe' ) {
106 my $inprocess = $query->param('inprocess');
113 if ($barcode eq '' && $query->param('charges') eq 'yes'){
114 $template->param( PAYCHARGES=>'yes',
115 bornum=>$borrowernumber);
118 if ( $print eq 'yes' && $borrowernumber ne '' ) {
119 printslip( \%env, $borrowernumber );
120 $query->param( 'borrnumber', '' );
121 $borrowernumber = '';
125 # STEP 2 : FIND BORROWER
126 # if there is a list of find borrowers....
131 my ( $count, $borrowers ) =
132 BornameSearch( \%env, $findborrower, 'cardnumber', 'web' );
133 my @borrowers = @$borrowers;
134 if ( $#borrowers == -1 ) {
135 $query->param( 'findborrower', '' );
136 $message = "'$findborrower'";
138 elsif ( $#borrowers == 0 ) {
139 $query->param( 'borrnumber', $borrowers[0]->{'borrowernumber'} );
140 $query->param( 'barcode', '' );
141 $borrowernumber = $borrowers[0]->{'borrowernumber'};
144 $borrowerslist = \@borrowers;
148 # get the borrower information.....
152 if ($borrowernumber) {
153 $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
154 my ( $od, $issue, $fines ) = borrdata2( \%env, $borrowernumber );
156 DateCalc( $borrower->{'expiry'},
157 "- " . C4::Context->preference('NotifyBorrowerDeparture') . " days" );
158 my $warning = Date_Cmp( ParseDate("today"), $warningdate );
159 if ( $warning > 0 ) {
161 #borrowercard expired
162 $template->param( warndeparture => $warning );
164 my ($reserved_num,$reserved_waiting) = CheckWaiting($borrowernumber);
165 if ($reserved_num > 0) {
166 for (my $i = 0; $i < $reserved_num; $i++) {
167 my ($count,$line) = getbiblio($reserved_waiting->[$i]->{'biblionumber'});
170 # warn Dumper(@lines);
175 issuecount => $issue,
177 returned_reserve => \@lines,
179 my $htdocs = C4::Context->config('intrahtdocs');
180 $picture = "/borrowerimages/" . $borrowernumber . ".jpg";
181 if ( -e $htdocs . "$picture" ) {
182 $template->param( picture => $picture );
192 $barcode = cuecatbarcodedecode($barcode);
193 my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
194 if ($issueconfirmed) {
195 issuebook( \%env, $borrower, $barcode, $datedue, $cancelreserve );
199 my ( $error, $question ) =
200 canbookbeissued( \%env, $borrower, $barcode, $year, $month, $day, $inprocess );
203 foreach my $impossible ( keys %$error ) {
205 $impossible => $$error{$impossible},
210 foreach my $needsconfirmation ( keys %$question ) {
212 $needsconfirmation => $$question{$needsconfirmation},
213 NEEDSCONFIRMATION => 1
222 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
223 issuebook( \%env, $borrower, $barcode, $datedue );
229 # reload the borrower info for the sake of reseting the flags.....
230 if ($borrowernumber) {
231 $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
234 ##################################################################################
236 # show all reserves of this borrower, and the position of the reservation ....
237 if ($borrowernumber) {
239 # now we show the status of the borrower's reservations
240 my @borrowerreserv = FastFindReserves(0,$borrowernumber);
242 foreach my $num_res (@borrowerreserv) {
245 my $getiteminfo = getiteminformation(\%env,$num_res->{'itemnumber'});
246 my $itemtypeinfo = getitemtypeinfo($getiteminfo->{'itemtype'});
247 my ($transfertwhen,$transfertfrom,$transfertto) = checktransferts($num_res->{'itemnumber'});
249 $getreserv{waiting} = 0;
250 $getreserv{transfered} = 0;
251 $getreserv{nottransfered} = 0;
253 $getreserv{reservedate} = format_date($num_res->{'reservedate'});
254 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
255 $getreserv{title} = $getiteminfo->{'title'};
256 $getreserv{itemtype} = $itemtypeinfo->{'description'};
257 $getreserv{author} = $getiteminfo->{'author'};
258 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
259 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
260 # check if we have a waitin status for reservations
261 if ($num_res->{'found'} eq 'W'){
262 $getreserv{color} = 'reserved';
263 $getreserv{waiting} = 1;
266 # check transfers with the itemnumber foud in th reservation loop
268 $getreserv{color} = 'transfered';
269 $getreserv{transfered} = 1;
270 $getreserv{datesent} = format_date($transfertwhen);
271 $getreserv{frombranch} = getbranchname($transfertfrom);
274 if (($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'}) and not $transfertwhen){
275 $getreserv{nottransfered} = 1;
276 $getreserv{nottransferedby} = getbranchname($getiteminfo->{'holdingbranch'});
279 # if we don't have a reserv on item, we put the biblio infos and the waiting position
280 if ($getiteminfo->{'title'} eq '' ){
281 my $getbibinfo = bibitemdata($num_res->{'biblionumber'});
282 my $getbibtype = getitemtypeinfo($getbibinfo->{'itemtype'});
283 $getreserv{color} = 'inwait';
284 $getreserv{title} = $getbibinfo->{'title'};
285 $getreserv{waitingposition} = $num_res->{'priority'};
286 $getreserv{nottransfered} = 0;
287 $getreserv{itemtype} = $getbibtype->{'description'};
288 $getreserv{author} = $getbibinfo->{'author'};
289 $getreserv{itemcallnumber} = '----------';
293 push(@reservloop, \%getreserv);
295 # return result to the template
296 $template->param(reservloop => \@reservloop);
301 # make the issued books table.....
302 my $todaysissues = '';
309 # get each issue of the borrower & separate them in todayissues & previous issues
312 my $issueslist = getissues($borrower);
314 # split in 2 arrays for today & previous
315 my $dbh = C4::Context->dbh;
316 foreach my $it ( keys %$issueslist ) {
317 my $issuedate = $issueslist->{$it}->{'timestamp'};
318 $issuedate =~ s/-//g;
319 $issuedate = substr( $issuedate, 0, 8 );
320 if ( $todaysdate == $issuedate ) {
321 ($issueslist->{$it}->{'charge'}, $issueslist->{$it}->{'itemtype_charge'})=calc_charges($dbh,$issueslist->{$it}->{'itemnumber'},$borrower->{'borrowernumber'});
322 $issueslist->{$it}->{'charge'} = sprintf("%.2f",$issueslist->{$it}->{'charge'});
323 ($issueslist->{$it}->{'can_renew'}, $issueslist->{$it}->{'can_renew_error'}) =renewstatus(\%env,$borrower->{'borrowernumber'}, $issueslist->{$it}->{'itemnumber'});
324 my ($restype,$reserves)=CheckReserves($issueslist->{$it}->{'itemnumber'});
326 $issueslist->{$it}->{'can_renew'}=0;
328 push @todaysissues, $issueslist->{$it};
331 ($issueslist->{$it}->{'charge'}, $issueslist->{$it}->{'itemtype_charge'})=calc_charges($dbh,$issueslist->{$it}->{'itemnumber'},$borrower->{'borrowernumber'});
332 $issueslist->{$it}->{'charge'} = sprintf("%.2f",$issueslist->{$it}->{'charge'});
333 ($issueslist->{$it}->{'can_renew'}, $issueslist->{$it}->{'can_renew_error'}) =renewstatus(\%env,$borrower->{'borrowernumber'}, $issueslist->{$it}->{'itemnumber'});
334 my ($restype,$reserves)=CheckReserves($issueslist->{$it}->{'itemnumber'});
336 $issueslist->{$it}->{'can_renew'}=0;
338 push @previousissues, $issueslist->{$it};
345 # parses today & build Template array
346 foreach my $book ( sort { $b->{'timestamp'} <=> $a->{'timestamp'} }
349 my $dd = $book->{'date_due'};
350 my $datedue = $book->{'date_due'};
351 $dd = format_date($dd);
353 if ( $datedue < $todaysdate ) {
365 $book->{'togglecolor'} = $togglecolor;
368 if ( $book->{'author'} eq '' ) {
369 $book->{'author'} = ' ';
371 push @realtodayissues, $book;
375 # parses previous & build Template array
377 foreach my $book ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
380 my $dd = $book->{'date_due'};
381 my $datedue = $book->{'date_due'};
382 $dd = format_date($dd);
386 if ( $datedue < $todaysdate ) {
398 $book->{'togglecolor'} = $togglecolor;
401 if ( $book->{'author'} eq '' ) {
402 $book->{'author'} = ' ';
404 push @realprevissues, $book;
411 my $CGIselectborrower;
412 if ($borrowerslist) {
416 . $a->{'firstname'} cmp $b->{'surname'}
421 push @values, $_->{'borrowernumber'};
422 $labels{ $_->{'borrowernumber'} } =
423 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'streetaddress'} ";
425 $CGIselectborrower = CGI::scrolling_list(
426 -name => 'borrnumber',
436 my ( $patrontable, $flaginfotable ) = patrontable($borrower);
437 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
438 my @temp = split( /\$/, $amountold );
440 my $CGIorganisations;
441 my $member_of_institution;
442 if ( C4::Context->preference("memberofinstitution") ) {
443 my $organisations = get_institutions();
446 foreach my $organisation ( keys %$organisations ) {
447 push @orgs, $organisation;
448 $org_labels{$organisation} =
449 $organisations->{$organisation}->{'surname'};
451 $member_of_institution = 1;
452 $CGIorganisations = CGI::popup_menu(
453 -id => 'organisations',
454 -name => 'organisations',
455 -labels => \%org_labels,
461 $amountold = $temp[1];
463 findborrower => $findborrower,
464 borrower => $borrower,
465 borrowernumber => $borrowernumber,
468 printername => $printer,
469 firstname => $borrower->{'firstname'},
470 surname => $borrower->{'surname'},
471 categorycode => $borrower->{'categorycode'},
472 streetaddress => $borrower->{'streetaddress'},
473 emailaddress => $borrower->{'emailaddress'},
474 borrowernotes => $borrower->{'borrowernotes'},
475 city => $borrower->{'city'},
476 phone => $borrower->{'phone'},
477 cardnumber => $borrower->{'cardnumber'},
478 amountold => $amountold,
480 stickyduedate => $stickyduedate,
482 CGIselectborrower => $CGIselectborrower,
483 todayissues => \@realtodayissues,
484 previssues => \@realprevissues,
485 inprocess => $inprocess,
486 memberofinstution => $member_of_institution,
487 CGIorganisations => $CGIorganisations,
490 # set return date if stickyduedate
491 if ($stickyduedate) {
492 my $t_year = "year" . $year;
493 my $t_month = "month" . $month;
494 my $t_day = "day" . $day;
503 # if ($branchcookie) {
504 # $cookie=[$cookie, $branchcookie, $printercookie];
507 output_html_with_http_headers $query, $cookie, $template->output;
509 ####################################################################
510 # Extra subroutines,,,
514 my $flags = $borrower->{'flags'};
515 my $flaginfotable = '';
518 #my $flaginfotext='';
521 foreach $flag ( sort keys %$flags ) {
523 # my @itemswaiting='';
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'}
567 # FIXME this part can be removed if we keep new display of reserves "reservloop"
568 # if ( $flag eq 'WAITING' ) {
569 # my $items = $flags->{$flag}->{'itemlist'};
571 # foreach my $item (@$items) {
572 # my ($iteminformation) =
573 # getiteminformation( \%env, $item->{'itemnumber'}, 0 );
574 # $iteminformation->{'branchname'} =
575 # $branches->{ $iteminformation->{'holdingbranch'} }
577 # push @itemswaiting, $iteminformation;
582 # waitingmsg => $flags->{'WAITING'}->{'message'},
583 # itemswaiting => \@itemswaiting,
586 if ( $flag eq 'ODUES' ) {
590 oduesmsg => $flags->{'ODUES'}->{'message'}
593 my $items = $flags->{$flag}->{'itemlist'};
596 foreach my $item (@$items) {
597 my ($iteminformation) =
598 getiteminformation( \%env, $item->{'itemnumber'}, 0 );
599 push @itemswaiting, $iteminformation;
602 if ( $query->param('module') ne 'returns' ) {
603 $template->param( nonreturns => 'true' );
606 if ( $flag eq 'NOTES' ) {
610 notesmsg => $flags->{'NOTES'}->{'message'}
615 return ( $patrontable, $flaginfotext );
618 sub cuecatbarcodedecode {
621 my @fields = split( /\./, $barcode );
622 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
623 if ( $#results == 2 ) {