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 );
141 warn $borrower->{'expiry'};
143 DateCalc( $borrower->{'expiry'},
144 "- " . C4::Context->preference('NotifyBorrowerDeparture') . " days" );
145 my $warning = Date_Cmp( ParseDate("today"), $warningdate );
146 if ( $warning > 0 ) {
148 #borrowercard expired
149 $template->param( warndeparture => $warning );
153 issuecount => $issue,
156 my $htdocs = C4::Context->config('intrahtdocs');
157 $picture = "/borrowerimages/" . $borrowernumber . ".jpg";
158 if ( -e $htdocs . "$picture" ) {
159 $template->param( picture => $picture );
169 $barcode = cuecatbarcodedecode($barcode);
170 my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
171 if ($issueconfirmed) {
172 issuebook( \%env, $borrower, $barcode, $datedue, $cancelreserve );
175 my ( $error, $question ) =
176 canbookbeissued( \%env, $borrower, $barcode, $year, $month, $day );
179 foreach my $impossible ( keys %$error ) {
181 $impossible => $$error{$impossible},
186 foreach my $needsconfirmation ( keys %$question ) {
188 $needsconfirmation => $$question{$needsconfirmation},
189 NEEDSCONFIRMATION => 1
198 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
199 issuebook( \%env, $borrower, $barcode, $datedue );
204 # reload the borrower info for the sake of reseting the flags.....
205 if ($borrowernumber) {
206 $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
209 ##################################################################################
211 # show all reserves of this borrower, and the position of the reservation ....
212 if ($borrowernumber) {
214 # now we show the status of the borrower's reservations
215 my @borrowerreserv = FastFindReserves(0,$borrowernumber);
217 foreach my $num_res (@borrowerreserv) {
220 my $getiteminfo = getiteminformation(\%env,$num_res->{'itemnumber'});
221 my $itemtypeinfo = getitemtypeinfo($getiteminfo->{'itemtype'});
222 my ($transfertwhen,$transfertfrom,$transfertto) = checktransferts($num_res->{'itemnumber'});
224 $getreserv{waiting} = 0;
225 $getreserv{transfered} = 0;
226 $getreserv{nottransfered} = 0;
228 $getreserv{reservedate} = format_date($num_res->{'reservedate'});
229 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
230 $getreserv{title} = $getiteminfo->{'title'};
231 $getreserv{itemtype} = $itemtypeinfo->{'description'};
232 $getreserv{author} = $getiteminfo->{'author'};
233 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
234 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
235 # check if we have a waitin status for reservations
236 if ($num_res->{'found'} eq 'W'){
237 $getreserv{color} = 'reserved';
238 $getreserv{waiting} = 1;
241 # check transfers with the itemnumber foud in th reservation loop
243 $getreserv{color} = 'transfered';
244 $getreserv{transfered} = 1;
245 $getreserv{datesent} = format_date($transfertwhen);
246 $getreserv{frombranch} = getbranchname($transfertfrom);
249 if (($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'}) and not $transfertwhen){
250 $getreserv{nottransfered} = 1;
251 $getreserv{nottransferedby} = getbranchname($getiteminfo->{'holdingbranch'});
254 # if we don't have a reserv on item, we put the biblio infos and the waiting position
255 if ($getiteminfo->{'title'} eq '' ){
256 my $getbibinfo = bibitemdata($num_res->{'biblionumber'});
257 my $getbibtype = getitemtypeinfo($getbibinfo->{'itemtype'});
258 $getreserv{color} = 'inwait';
259 $getreserv{title} = $getbibinfo->{'title'};
260 $getreserv{waitingposition} = $num_res->{'priority'};
261 $getreserv{nottransfered} = 0;
262 $getreserv{itemtype} = $getbibtype->{'description'};
263 $getreserv{author} = $getbibinfo->{'author'};
264 $getreserv{itemcallnumber} = '----------';
268 push(@reservloop, \%getreserv);
270 # return result to the template
271 $template->param(reservloop => \@reservloop);
276 # make the issued books table.....
277 my $todaysissues = '';
284 # get each issue of the borrower & separate them in todayissues & previous issues
287 my $issueslist = getissues($borrower);
289 # split in 2 arrays for today & previous
290 foreach my $it ( keys %$issueslist ) {
291 my $issuedate = $issueslist->{$it}->{'timestamp'};
292 $issuedate =~ s/-//g;
293 $issuedate = substr( $issuedate, 0, 8 );
294 if ( $todaysdate == $issuedate ) {
295 push @todaysissues, $issueslist->{$it};
298 push @previousissues, $issueslist->{$it};
305 # parses today & build Template array
306 foreach my $book ( sort { $b->{'timestamp'} <=> $a->{'timestamp'} }
309 my $dd = $book->{'date_due'};
310 my $datedue = $book->{'date_due'};
311 $dd = format_date($dd);
313 if ( $datedue < $todaysdate ) {
325 $book->{'togglecolor'} = $togglecolor;
328 if ( $book->{'author'} eq '' ) {
329 $book->{'author'} = ' ';
331 push @realtodayissues, $book;
335 # parses previous & build Template array
337 foreach my $book ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
340 my $dd = $book->{'date_due'};
341 my $datedue = $book->{'date_due'};
342 $dd = format_date($dd);
346 if ( $datedue < $todaysdate ) {
358 $book->{'togglecolor'} = $togglecolor;
361 if ( $book->{'author'} eq '' ) {
362 $book->{'author'} = ' ';
364 push @realprevissues, $book;
371 my $CGIselectborrower;
372 if ($borrowerslist) {
376 . $a->{'firstname'} cmp $b->{'surname'}
381 push @values, $_->{'borrowernumber'};
382 $labels{ $_->{'borrowernumber'} } =
383 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'streetaddress'} ";
385 $CGIselectborrower = CGI::scrolling_list(
386 -name => 'borrnumber',
396 my ( $patrontable, $flaginfotable ) = patrontable($borrower);
397 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
398 my @temp = split( /\$/, $amountold );
400 my $CGIorganisations;
401 my $member_of_institution;
402 if ( C4::Context->preference("memberofinstitution") ) {
403 my $organisations = get_institutions();
406 foreach my $organisation ( keys %$organisations ) {
407 push @orgs, $organisation;
408 $org_labels{$organisation} =
409 $organisations->{$organisation}->{'surname'};
411 $member_of_institution = 1;
412 $CGIorganisations = CGI::popup_menu(
413 -id => 'organisations',
414 -name => 'organisations',
415 -labels => \%org_labels,
421 $amountold = $temp[1];
423 findborrower => $findborrower,
424 borrower => $borrower,
425 borrowernumber => $borrowernumber,
428 printername => $printer,
429 firstname => $borrower->{'firstname'},
430 surname => $borrower->{'surname'},
431 categorycode => $borrower->{'categorycode'},
432 streetaddress => $borrower->{'streetaddress'},
433 emailaddress => $borrower->{'emailaddress'},
434 borrowernotes => $borrower->{'borrowernotes'},
435 city => $borrower->{'city'},
436 phone => $borrower->{'phone'},
437 cardnumber => $borrower->{'cardnumber'},
438 amountold => $amountold,
440 stickyduedate => $stickyduedate,
442 CGIselectborrower => $CGIselectborrower,
443 todayissues => \@realtodayissues,
444 previssues => \@realprevissues,
445 memberofinstution => $member_of_institution,
446 CGIorganisations => $CGIorganisations,
449 # set return date if stickyduedate
450 if ($stickyduedate) {
451 my $t_year = "year" . $year;
452 my $t_month = "month" . $month;
453 my $t_day = "day" . $day;
462 # if ($branchcookie) {
463 # $cookie=[$cookie, $branchcookie, $printercookie];
466 output_html_with_http_headers $query, $cookie, $template->output;
468 ####################################################################
469 # Extra subroutines,,,
473 my $flags = $borrower->{'flags'};
474 my $flaginfotable = '';
477 #my $flaginfotext='';
480 foreach $flag ( sort keys %$flags ) {
482 # my @itemswaiting='';
483 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
484 if ( $flags->{$flag}->{'noissues'} ) {
489 if ( $flag eq 'GNA' ) {
490 $template->param( gna => 'true' );
492 if ( $flag eq 'LOST' ) {
493 $template->param( lost => 'true' );
495 if ( $flag eq 'DBARRED' ) {
496 $template->param( dbarred => 'true' );
498 if ( $flag eq 'CHARGES' ) {
501 chargesmsg => $flags->{'CHARGES'}->{'message'}
506 if ( $flag eq 'CHARGES' ) {
510 chargesmsg => $flags->{'CHARGES'}->{'message'}
513 # FIXME this part can be removed if we keep new display of reserves "reservloop"
514 # if ( $flag eq 'WAITING' ) {
515 # my $items = $flags->{$flag}->{'itemlist'};
517 # foreach my $item (@$items) {
518 # my ($iteminformation) =
519 # getiteminformation( \%env, $item->{'itemnumber'}, 0 );
520 # $iteminformation->{'branchname'} =
521 # $branches->{ $iteminformation->{'holdingbranch'} }
523 # push @itemswaiting, $iteminformation;
528 # waitingmsg => $flags->{'WAITING'}->{'message'},
529 # itemswaiting => \@itemswaiting,
532 if ( $flag eq 'ODUES' ) {
536 oduesmsg => $flags->{'ODUES'}->{'message'}
539 my $items = $flags->{$flag}->{'itemlist'};
542 foreach my $item (@$items) {
543 my ($iteminformation) =
544 getiteminformation( \%env, $item->{'itemnumber'}, 0 );
545 push @itemswaiting, $iteminformation;
548 if ( $query->param('module') ne 'returns' ) {
549 $template->param( nonreturns => 'true' );
552 if ( $flag eq 'NOTES' ) {
556 notesmsg => $flags->{'NOTES'}->{'message'}
561 return ( $patrontable, $flaginfotext );
564 sub cuecatbarcodedecode {
567 my @fields = split( /\./, $barcode );
568 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
569 if ( $#results == 2 ) {