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;
33 use C4::Interface::CGI::Output;
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
46 template_name => 'circ/circulation.tmpl',
50 flagsrequired => { circulate => 1 },
53 my $branches = getbranches();
54 my $printers = getprinters();
55 my $branch = getbranch( $query, $branches );
56 my $printer = getprinter( $query, $printers );
58 my $findborrower = $query->param('findborrower');
59 $findborrower =~ s|,| |g;
60 $findborrower =~ s|'| |g;
61 my $borrowernumber = $query->param('borrnumber');
62 my $print = $query->param('print') || '';
63 my $barcode = $query->param('barcode') || '';
64 my $year = $query->param('year');
65 my $month = $query->param('month');
66 my $day = $query->param('day');
67 my $stickyduedate = $query->param('stickyduedate');
68 my $issueconfirmed = $query->param('issueconfirmed');
69 my $cancelreserve = $query->param('cancelreserve');
70 my $organisation = $query->param('organisations');
75 if ( $query->param('setcookies') ) {
76 $branchcookie = $query->cookie(
81 $printercookie = $query->cookie(
89 ; # FIXME env is used as an "environment" variable. Could be dropped probably...
91 $env{'branchcode'} = $branch;
92 $env{'printer'} = $printer;
93 $env{'queue'} = $printer;
94 $env{'organisation'} = $organisation;
96 my @datearr = localtime( time() );
98 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
100 ( 1900 + $datearr[5] )
101 . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
102 . sprintf( "%0.2d", ( $datearr[3] ) );
104 # check and see if we should print
105 if ( $barcode eq '' && $print eq 'maybe' ) {
108 if ( $print eq 'yes' && $borrowernumber ne '' ) {
109 printslip( \%env, $borrowernumber );
110 $query->param( 'borrnumber', '' );
111 $borrowernumber = '';
115 # STEP 2 : FIND BORROWER
116 # if there is a list of find borrowers....
121 my ( $count, $borrowers ) =
122 BornameSearch( \%env, $findborrower, 'cardnumber', 'web' );
123 my @borrowers = @$borrowers;
124 if ( $#borrowers == -1 ) {
125 $query->param( 'findborrower', '' );
126 $message = "'$findborrower'";
128 elsif ( $#borrowers == 0 ) {
129 $query->param( 'borrnumber', $borrowers[0]->{'borrowernumber'} );
130 $query->param( 'barcode', '' );
131 $borrowernumber = $borrowers[0]->{'borrowernumber'};
134 $borrowerslist = \@borrowers;
138 # get the borrower information.....
142 if ($borrowernumber) {
143 $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
144 my ( $od, $issue, $fines ) = borrdata2( \%env, $borrowernumber );
145 warn $borrower->{'expiry'};
147 DateCalc( $borrower->{'expiry'},
148 "- " . C4::Context->preference('NotifyBorrowerDeparture') . " days" );
149 my $warning = Date_Cmp( ParseDate("today"), $warningdate );
150 if ( $warning > 0 ) {
152 #borrowercard expired
153 $template->param( warndeparture => $warning );
157 issuecount => $issue,
160 my $htdocs = C4::Context->config('intrahtdocs');
161 $picture = "/borrowerimages/" . $borrowernumber . ".jpg";
162 if ( -e $htdocs . "$picture" ) {
163 $template->param( picture => $picture );
173 $barcode = cuecatbarcodedecode($barcode);
174 my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
175 if ($issueconfirmed) {
176 issuebook( \%env, $borrower, $barcode, $datedue, $cancelreserve );
179 my ( $error, $question ) =
180 canbookbeissued( \%env, $borrower, $barcode, $year, $month, $day );
183 foreach my $impossible ( keys %$error ) {
185 $impossible => $$error{$impossible},
190 foreach my $needsconfirmation ( keys %$question ) {
192 $needsconfirmation => $$question{$needsconfirmation},
193 NEEDSCONFIRMATION => 1
202 if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
203 issuebook( \%env, $borrower, $barcode, $datedue );
208 # reload the borrower info for the sake of reseting the flags.....
209 if ($borrowernumber) {
210 $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
213 ##################################################################################
216 # make the issued books table.....
217 my $todaysissues = '';
224 # get each issue of the borrower & separate them in todayissues & previous issues
227 my $issueslist = getissues($borrower);
229 # split in 2 arrays for today & previous
230 foreach my $it ( keys %$issueslist ) {
231 my $issuedate = $issueslist->{$it}->{'timestamp'};
232 $issuedate =~ s/-//g;
233 $issuedate = substr( $issuedate, 0, 8 );
234 if ( $todaysdate == $issuedate ) {
235 push @todaysissues, $issueslist->{$it};
238 push @previousissues, $issueslist->{$it};
245 # parses today & build Template array
246 foreach my $book ( sort { $b->{'timestamp'} <=> $a->{'timestamp'} }
249 my $dd = $book->{'date_due'};
250 my $datedue = $book->{'date_due'};
251 $dd = format_date($dd);
253 if ( $datedue < $todaysdate ) {
265 $book->{'togglecolor'} = $togglecolor;
268 if ( $book->{'author'} eq '' ) {
269 $book->{'author'} = ' ';
271 push @realtodayissues, $book;
275 # parses previous & build Template array
277 foreach my $book ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
280 my $dd = $book->{'date_due'};
281 my $datedue = $book->{'date_due'};
282 $dd = format_date($dd);
286 if ( $datedue < $todaysdate ) {
298 $book->{'togglecolor'} = $togglecolor;
301 if ( $book->{'author'} eq '' ) {
302 $book->{'author'} = ' ';
304 push @realprevissues, $book;
311 my $CGIselectborrower;
312 if ($borrowerslist) {
316 . $a->{'firstname'} cmp $b->{'surname'}
321 push @values, $_->{'borrowernumber'};
322 $labels{ $_->{'borrowernumber'} } =
323 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'streetaddress'} ";
325 $CGIselectborrower = CGI::scrolling_list(
326 -name => 'borrnumber',
336 my ( $patrontable, $flaginfotable ) = patrontable($borrower);
337 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
338 my @temp = split( /\$/, $amountold );
340 my $CGIorganisations;
341 my $member_of_institution;
342 if ( C4::Context->preference("memberofinstitution") ) {
343 my $organisations = get_institutions();
346 foreach my $organisation ( keys %$organisations ) {
347 push @orgs, $organisation;
348 $org_labels{$organisation} =
349 $organisations->{$organisation}->{'surname'};
351 $member_of_institution = 1;
352 $CGIorganisations = CGI::popup_menu(
353 -id => 'organisations',
354 -name => 'organisations',
355 -labels => \%org_labels,
361 $amountold = $temp[1];
363 findborrower => $findborrower,
364 borrower => $borrower,
365 borrowernumber => $borrowernumber,
368 branchname => $branches->{$branch}->{'branchname'},
369 printername => $printers->{$printer}->{'printername'},
370 firstname => $borrower->{'firstname'},
371 surname => $borrower->{'surname'},
372 categorycode => $borrower->{'categorycode'},
373 streetaddress => $borrower->{'streetaddress'},
374 emailaddress => $borrower->{'emailaddress'},
375 borrowernotes => $borrower->{'borrowernotes'},
376 city => $borrower->{'city'},
377 phone => $borrower->{'phone'},
378 cardnumber => $borrower->{'cardnumber'},
379 amountold => $amountold,
381 stickyduedate => $stickyduedate,
383 CGIselectborrower => $CGIselectborrower,
384 todayissues => \@realtodayissues,
385 previssues => \@realprevissues,
386 memberofinstution => $member_of_institution,
387 CGIorganisations => $CGIorganisations,
390 # set return date if stickyduedate
391 if ($stickyduedate) {
392 my $t_year = "year" . $year;
393 my $t_month = "month" . $month;
394 my $t_day = "day" . $day;
403 $cookie = [ $cookie, $branchcookie, $printercookie ];
406 output_html_with_http_headers $query, $cookie, $template->output;
408 ####################################################################
409 # Extra subroutines,,,
413 my $flags = $borrower->{'flags'};
414 my $flaginfotable = '';
417 #my $flaginfotext='';
420 foreach $flag ( sort keys %$flags ) {
422 # my @itemswaiting='';
423 $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
424 if ( $flags->{$flag}->{'noissues'} ) {
429 if ( $flag eq 'GNA' ) {
430 $template->param( gna => 'true' );
432 if ( $flag eq 'LOST' ) {
433 $template->param( lost => 'true' );
435 if ( $flag eq 'DBARRED' ) {
436 $template->param( dbarred => 'true' );
438 if ( $flag eq 'CHARGES' ) {
441 chargesmsg => $flags->{'CHARGES'}->{'message'}
446 if ( $flag eq 'CHARGES' ) {
450 chargesmsg => $flags->{'CHARGES'}->{'message'}
453 if ( $flag eq 'WAITING' ) {
454 my $items = $flags->{$flag}->{'itemlist'};
456 foreach my $item (@$items) {
457 my ($iteminformation) =
458 getiteminformation( \%env, $item->{'itemnumber'}, 0 );
459 $iteminformation->{'branchname'} =
460 $branches->{ $iteminformation->{'holdingbranch'} }
462 push @itemswaiting, $iteminformation;
467 waitingmsg => $flags->{'WAITING'}->{'message'},
468 itemswaiting => \@itemswaiting,
471 if ( $flag eq 'ODUES' ) {
475 oduesmsg => $flags->{'ODUES'}->{'message'}
478 my $items = $flags->{$flag}->{'itemlist'};
481 foreach my $item (@$items) {
482 my ($iteminformation) =
483 getiteminformation( \%env, $item->{'itemnumber'}, 0 );
484 push @itemswaiting, $iteminformation;
487 if ( $query->param('module') ne 'returns' ) {
488 $template->param( nonreturns => 'true' );
491 if ( $flag eq 'NOTES' ) {
495 notesmsg => $flags->{'NOTES'}->{'message'}
500 return ( $patrontable, $flaginfotext );
503 sub cuecatbarcodedecode {
506 my @fields = split( /\./, $barcode );
507 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
508 if ( $#results == 2 ) {