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;
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
49 template_name => 'circ/circulation.tmpl',
53 flagsrequired => { circulate => 1 },
56 my $branches = getbranches();
57 # my $printers = getprinters();
58 # my $printer = getprinter($query, $printers);
60 my $findborrower = $query->param('findborrower');
61 $findborrower =~ s|,| |g;
62 $findborrower =~ s|'| |g;
63 my $borrowernumber = $query->param('borrnumber');
64 # new op dev the branch and the printer are now defined by the userenv
65 my $branch = C4::Context->userenv->{'branch'};
66 my $printer=C4::Context->userenv->{'branchprinter'};
68 my $barcode = $query->param('barcode') || '';
69 my $year=$query->param('year');
70 my $month=$query->param('month');
71 my $day=$query->param('day');
72 my $stickyduedate=$query->param('stickyduedate');
73 my $issueconfirmed = $query->param('issueconfirmed');
74 my $cancelreserve = $query->param('cancelreserve');
75 my $organisation = $query->param('organisations');
76 my $print = $query->param('print');
81 # if ($query->param('setcookies')) {
82 # $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
83 # $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
86 my %env; # FIXME env is used as an "environment" variable. Could be dropped probably...
89 $env{'branchcode'}= $branch;
90 $env{'printer'}= $printer;
91 # $env{'queue'}=$printer;
93 my @datearr = localtime(time());
94 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
96 ( 1900 + $datearr[5] )
97 . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
98 . sprintf( "%0.2d", ( $datearr[3] ) );
100 # check and see if we should print
101 if ( $barcode eq '' && $print eq 'maybe' ) {
104 if ( $print eq 'yes' && $borrowernumber ne '' ) {
105 printslip( \%env, $borrowernumber );
106 $query->param( 'borrnumber', '' );
107 $borrowernumber = '';
111 # STEP 2 : FIND BORROWER
112 # if there is a list of find borrowers....
117 my ( $count, $borrowers ) =
118 BornameSearch( \%env, $findborrower, 'cardnumber', 'web' );
119 my @borrowers = @$borrowers;
120 if ( $#borrowers == -1 ) {
121 $query->param( 'findborrower', '' );
122 $message = "'$findborrower'";
124 elsif ( $#borrowers == 0 ) {
125 $query->param( 'borrnumber', $borrowers[0]->{'borrowernumber'} );
126 $query->param( 'barcode', '' );
127 $borrowernumber = $borrowers[0]->{'borrowernumber'};
130 $borrowerslist = \@borrowers;
134 # get the borrower information.....
138 if ($borrowernumber) {
139 $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
140 my ( $od, $issue, $fines ) = borrdata2( \%env, $borrowernumber );
142 DateCalc( $borrower->{'expiry'},
143 "- " . C4::Context->preference('NotifyBorrowerDeparture') . " days" );
144 my $warning = Date_Cmp( ParseDate("today"), $warningdate );
145 if ( $warning > 0 ) {
147 #borrowercard expired
148 $template->param( warndeparture => $warning );
152 issuecount => $issue,
155 my $htdocs = C4::Context->config('intrahtdocs');
156 $picture = "/borrowerimages/" . $borrowernumber . ".jpg";
157 if ( -e $htdocs . "$picture" ) {
158 $template->param( picture => $picture );
168 $barcode = cuecatbarcodedecode($barcode);
169 my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
170 if ($issueconfirmed) {
171 issuebook( \%env, $borrower, $barcode, $datedue, $cancelreserve );
174 my ( $error, $question ) =
175 canbookbeissued( \%env, $borrower, $barcode, $year, $month, $day );
178 foreach my $impossible ( keys %$error ) {
180 $impossible => $$error{$impossible},
185 foreach my $needsconfirmation ( keys %$question ) {
187 $needsconfirmation => $$question{$needsconfirmation},
188 NEEDSCONFIRMATION => 1
197 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
198 issuebook( \%env, $borrower, $barcode, $datedue );
203 # reload the borrower info for the sake of reseting the flags.....
204 if ($borrowernumber) {
205 $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
208 ##################################################################################
210 # show all reserves of this borrower, and the position of the reservation ....
211 if ($borrowernumber) {
213 # now we show the status of the borrower's reservations
214 my @borrowerreserv = FastFindReserves(0,$borrowernumber);
216 foreach my $num_res (@borrowerreserv) {
219 my $getiteminfo = getiteminformation(\%env,$num_res->{'itemnumber'});
220 my $itemtypeinfo = getitemtypeinfo($getiteminfo->{'itemtype'});
221 my ($transfertwhen,$transfertfrom,$transfertto) = checktransferts($num_res->{'itemnumber'});
223 $getreserv{waiting} = 0;
224 $getreserv{transfered} = 0;
225 $getreserv{nottransfered} = 0;
227 $getreserv{reservedate} = format_date($num_res->{'reservedate'});
228 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
229 $getreserv{title} = $getiteminfo->{'title'};
230 $getreserv{itemtype} = $itemtypeinfo->{'description'};
231 $getreserv{author} = $getiteminfo->{'author'};
232 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
233 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
234 # check if we have a waitin status for reservations
235 if ($num_res->{'found'} eq 'W'){
236 $getreserv{color} = 'reserved';
237 $getreserv{waiting} = 1;
240 # check transfers with the itemnumber foud in th reservation loop
242 $getreserv{color} = 'transfered';
243 $getreserv{transfered} = 1;
244 $getreserv{datesent} = format_date($transfertwhen);
245 $getreserv{frombranch} = getbranchname($transfertfrom);
248 if (($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'}) and not $transfertwhen){
249 $getreserv{nottransfered} = 1;
250 $getreserv{nottransferedby} = getbranchname($getiteminfo->{'holdingbranch'});
253 # if we don't have a reserv on item, we put the biblio infos and the waiting position
254 if ($getiteminfo->{'title'} eq '' ){
255 my $getbibinfo = bibitemdata($num_res->{'biblionumber'});
256 my $getbibtype = getitemtypeinfo($getbibinfo->{'itemtype'});
257 $getreserv{color} = 'inwait';
258 $getreserv{title} = $getbibinfo->{'title'};
259 $getreserv{waitingposition} = $num_res->{'priority'};
260 $getreserv{nottransfered} = 0;
261 $getreserv{itemtype} = $getbibtype->{'description'};
262 $getreserv{author} = $getbibinfo->{'author'};
263 $getreserv{itemcallnumber} = '----------';
267 push(@reservloop, \%getreserv);
269 # return result to the template
270 $template->param(reservloop => \@reservloop);
275 # make the issued books table.....
276 my $todaysissues = '';
283 # get each issue of the borrower & separate them in todayissues & previous issues
286 my $issueslist = getissues($borrower);
288 # split in 2 arrays for today & previous
289 foreach my $it ( keys %$issueslist ) {
290 my $issuedate = $issueslist->{$it}->{'timestamp'};
291 $issuedate =~ s/-//g;
292 $issuedate = substr( $issuedate, 0, 8 );
293 if ( $todaysdate == $issuedate ) {
294 push @todaysissues, $issueslist->{$it};
297 push @previousissues, $issueslist->{$it};
304 # parses today & build Template array
305 foreach my $book ( sort { $b->{'timestamp'} <=> $a->{'timestamp'} }
308 my $dd = $book->{'date_due'};
309 my $datedue = $book->{'date_due'};
310 $dd = format_date($dd);
312 if ( $datedue < $todaysdate ) {
324 $book->{'togglecolor'} = $togglecolor;
327 if ( $book->{'author'} eq '' ) {
328 $book->{'author'} = ' ';
330 push @realtodayissues, $book;
334 # parses previous & build Template array
336 foreach my $book ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
339 my $dd = $book->{'date_due'};
340 my $datedue = $book->{'date_due'};
341 $dd = format_date($dd);
345 if ( $datedue < $todaysdate ) {
357 $book->{'togglecolor'} = $togglecolor;
360 if ( $book->{'author'} eq '' ) {
361 $book->{'author'} = ' ';
363 push @realprevissues, $book;
370 my $CGIselectborrower;
371 if ($borrowerslist) {
375 . $a->{'firstname'} cmp $b->{'surname'}
380 push @values, $_->{'borrowernumber'};
381 $labels{ $_->{'borrowernumber'} } =
382 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'streetaddress'} ";
384 $CGIselectborrower = CGI::scrolling_list(
385 -name => 'borrnumber',
395 my ( $patrontable, $flaginfotable ) = patrontable($borrower);
396 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
397 my @temp = split( /\$/, $amountold );
399 my $CGIorganisations;
400 my $member_of_institution;
401 if ( C4::Context->preference("memberofinstitution") ) {
402 my $organisations = get_institutions();
405 foreach my $organisation ( keys %$organisations ) {
406 push @orgs, $organisation;
407 $org_labels{$organisation} =
408 $organisations->{$organisation}->{'surname'};
410 $member_of_institution = 1;
411 $CGIorganisations = CGI::popup_menu(
412 -id => 'organisations',
413 -name => 'organisations',
414 -labels => \%org_labels,
420 $amountold = $temp[1];
422 findborrower => $findborrower,
423 borrower => $borrower,
424 borrowernumber => $borrowernumber,
427 printername => $printer,
428 firstname => $borrower->{'firstname'},
429 surname => $borrower->{'surname'},
430 categorycode => $borrower->{'categorycode'},
431 streetaddress => $borrower->{'streetaddress'},
432 emailaddress => $borrower->{'emailaddress'},
433 borrowernotes => $borrower->{'borrowernotes'},
434 city => $borrower->{'city'},
435 phone => $borrower->{'phone'},
436 cardnumber => $borrower->{'cardnumber'},
437 amountold => $amountold,
439 stickyduedate => $stickyduedate,
441 CGIselectborrower => $CGIselectborrower,
442 todayissues => \@realtodayissues,
443 previssues => \@realprevissues,
444 memberofinstution => $member_of_institution,
445 CGIorganisations => $CGIorganisations,
448 # set return date if stickyduedate
449 if ($stickyduedate) {
450 my $t_year = "year" . $year;
451 my $t_month = "month" . $month;
452 my $t_day = "day" . $day;
461 # if ($branchcookie) {
462 # $cookie=[$cookie, $branchcookie, $printercookie];
465 output_html_with_http_headers $query, $cookie, $template->output;
467 ####################################################################
468 # Extra subroutines,,,
472 my $flags = $borrower->{'flags'};
473 my $flaginfotable = '';
476 #my $flaginfotext='';
479 foreach $flag ( sort keys %$flags ) {
481 # my @itemswaiting='';
482 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
483 if ( $flags->{$flag}->{'noissues'} ) {
488 if ( $flag eq 'GNA' ) {
489 $template->param( gna => 'true' );
491 if ( $flag eq 'LOST' ) {
492 $template->param( lost => 'true' );
494 if ( $flag eq 'DBARRED' ) {
495 $template->param( dbarred => 'true' );
497 if ( $flag eq 'CHARGES' ) {
500 chargesmsg => $flags->{'CHARGES'}->{'message'}
505 if ( $flag eq 'CHARGES' ) {
509 chargesmsg => $flags->{'CHARGES'}->{'message'}
512 # FIXME this part can be removed if we keep new display of reserves "reservloop"
513 # if ( $flag eq 'WAITING' ) {
514 # my $items = $flags->{$flag}->{'itemlist'};
516 # foreach my $item (@$items) {
517 # my ($iteminformation) =
518 # getiteminformation( \%env, $item->{'itemnumber'}, 0 );
519 # $iteminformation->{'branchname'} =
520 # $branches->{ $iteminformation->{'holdingbranch'} }
522 # push @itemswaiting, $iteminformation;
527 # waitingmsg => $flags->{'WAITING'}->{'message'},
528 # itemswaiting => \@itemswaiting,
531 if ( $flag eq 'ODUES' ) {
535 oduesmsg => $flags->{'ODUES'}->{'message'}
538 my $items = $flags->{$flag}->{'itemlist'};
541 foreach my $item (@$items) {
542 my ($iteminformation) =
543 getiteminformation( \%env, $item->{'itemnumber'}, 0 );
544 push @itemswaiting, $iteminformation;
547 if ( $query->param('module') ne 'returns' ) {
548 $template->param( nonreturns => 'true' );
551 if ( $flag eq 'NOTES' ) {
555 notesmsg => $flags->{'NOTES'}->{'message'}
560 return ( $patrontable, $flaginfotext );
563 sub cuecatbarcodedecode {
566 my @fields = split( /\./, $barcode );
567 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
568 if ( $#results == 2 ) {