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
9 # Copyright 2000-2002 Katipo Communications
11 # This file is part of Koha.
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License along with
23 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24 # Suite 330, Boston, MA 02111-1307 USA
28 use C4::Circulation::Circ2;
34 use C4::Interface::CGI::Output;
45 my ($template, $loggedinuser, $cookie) = get_template_and_user
47 template_name => 'circ/circulation.tmpl',
51 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');
75 if ($query->param('setcookies')) {
76 $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
77 $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
80 my %env; # FIXME env is used as an "environment" variable. Could be dropped probably...
82 $env{'branchcode'}=$branch;
83 $env{'printer'}=$printer;
84 $env{'queue'}=$printer;
86 my @datearr = localtime(time());
87 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
88 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", ($datearr[3]));
91 # check and see if we should print
92 if ($barcode eq '' && $print eq 'maybe'){
95 if ($print eq 'yes' && $borrowernumber ne ''){
96 printslip(\%env,$borrowernumber);
97 $query->param('borrnumber','');
102 # STEP 2 : FIND BORROWER
103 # if there is a list of find borrowers....
108 my ($count,$borrowers)=BornameSearch(\%env,$findborrower,'cardnumber','web');
109 my @borrowers=@$borrowers;
110 if ($#borrowers == -1) {
111 $query->param('findborrower', '');
112 $message = "'$findborrower'";
113 } elsif ($#borrowers == 0) {
114 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
115 $query->param('barcode','');
116 $borrowernumber=$borrowers[0]->{'borrowernumber'};
118 $borrowerslist = \@borrowers;
122 # get the borrower information.....
127 if ($borrowernumber) {
128 $borrower = getpatroninformation(\%env,$borrowernumber,0);
129 my ($od,$issue,$fines)=borrdata2(\%env,$borrowernumber);
130 warn $borrower->{'expiry'};
131 my $warningdate = DateCalc($borrower->{'expiry'},"- ".C4::Context->preference('NotifyBorrowerDeparture')." days");
132 my $warning=Date_Cmp(ParseDate("today"),$warningdate);
134 #borrowercard expired
135 $template->param(warndeparture=>$warning);
137 $template->param(overduecount => $od,
138 issuecount => $issue,
139 finetotal => $fines);
140 my $htdocs = C4::Context->config('intrahtdocs');
141 $picture = "/borrowerimages/".$borrowernumber.".jpg";
142 if (-e $htdocs."$picture")
144 $template->param(picture => $picture)
157 $barcode = cuecatbarcodedecode($barcode);
158 my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
159 if ($issueconfirmed) {
160 issuebook(\%env, $borrower, $barcode, $datedue,$cancelreserve);
162 my ($error, $question) = canbookbeissued(\%env, $borrower, $barcode, $year, $month, $day);
165 foreach my $impossible (keys %$error) {
166 $template->param($impossible => $$error{$impossible},
170 foreach my $needsconfirmation (keys %$question) {
171 $template->param($needsconfirmation => $$question{$needsconfirmation},
172 NEEDSCONFIRMATION => 1);
175 $template->param(day => $day,
178 if ($noerror && ($noquestion || $issueconfirmed)) {
179 issuebook(\%env, $borrower, $barcode, $datedue);
184 # reload the borrower info for the sake of reseting the flags.....
185 if ($borrowernumber) {
186 $borrower = getpatroninformation(\%env,$borrowernumber,0);
190 ##################################################################################
193 # make the issued books table.....
200 # get each issue of the borrower & separate them in todayissues & previous issues
203 my $issueslist = getissues($borrower);
204 # split in 2 arrays for today & previous
205 foreach my $it (keys %$issueslist) {
206 my $issuedate = $issueslist->{$it}->{'timestamp'};
207 $issuedate =~ s/-//g;
208 $issuedate = substr($issuedate, 0, 8);
209 if ($todaysdate == $issuedate) {
210 push @todaysissues, $issueslist->{$it};
212 push @previousissues, $issueslist->{$it};
218 # parses today & build Template array
219 foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
220 my $dd = $book->{'date_due'};
221 my $datedue = $book->{'date_due'};
222 $dd=format_date($dd);
224 if ($datedue < $todaysdate) {
234 $book->{'togglecolor'} = $togglecolor;
237 if ($book->{'author'} eq ''){
238 $book->{'author'}=' ';
240 push @realtodayissues,$book;
244 # parses previous & build Template array
246 foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
247 my $dd = $book->{'date_due'};
248 my $datedue = $book->{'date_due'};
249 $dd=format_date($dd);
253 if ($datedue < $todaysdate) {
263 $book->{'togglecolor'} = $togglecolor;
266 if ($book->{'author'} eq ''){
267 $book->{'author'}=' ';
269 push @realprevissues,$book;
277 my $CGIselectborrower;
278 if ($borrowerslist) {
279 foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
280 push @values,$_->{'borrowernumber'};
281 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ... $_->{'streetaddress'} ";
283 $CGIselectborrower=CGI::scrolling_list( -name => 'borrnumber',
291 my ($patrontable, $flaginfotable) = patrontable($borrower);
292 my $amountold=$borrower->{flags}->{'CHARGES'}->{'message'} || 0;
293 my @temp=split(/\$/,$amountold);
296 findborrower => $findborrower,
297 borrower => $borrower,
298 borrowernumber => $borrowernumber,
301 branchname => $branches->{$branch}->{'branchname'},
302 printername => $printers->{$printer}->{'printername'},
303 firstname => $borrower->{'firstname'},
304 surname => $borrower->{'surname'},
305 categorycode => $borrower->{'categorycode'},
306 streetaddress => $borrower->{'streetaddress'},
307 emailaddress => $borrower->{'emailaddress'},
308 borrowernotes => $borrower->{'borrowernotes'},
309 city => $borrower->{'city'},
310 phone => $borrower->{'phone'},
311 cardnumber => $borrower->{'cardnumber'},
312 amountold => $amountold,
314 stickyduedate => $stickyduedate,
316 CGIselectborrower => $CGIselectborrower,
317 todayissues => \@realtodayissues,
318 previssues => \@realprevissues,
320 # set return date if stickyduedate
321 if ($stickyduedate) {
322 my $t_year = "year".$year;
323 my $t_month = "month".$month;
324 my $t_day = "day".$day;
334 $cookie=[$cookie, $branchcookie, $printercookie];
337 output_html_with_http_headers $query, $cookie, $template->output;
339 ####################################################################
340 # Extra subroutines,,,
344 my $flags = $borrower->{'flags'};
345 my $flaginfotable='';
347 #my $flaginfotext='';
350 foreach $flag (sort keys %$flags) {
351 # my @itemswaiting='';
352 $flags->{$flag}->{'message'}=~s/\n/<br>/g;
353 if ($flags->{$flag}->{'noissues'}) {
363 if ($flag eq 'LOST'){
368 if ($flag eq 'DBARRED'){
373 if ($flag eq 'CHARGES') {
376 chargesmsg => $flags->{'CHARGES'}->{'message'}
380 if ($flag eq 'CHARGES') {
384 chargesmsg => $flags->{'CHARGES'}->{'message'}
387 if ($flag eq 'WAITING') {
388 my $items=$flags->{$flag}->{'itemlist'};
390 foreach my $item (@$items) {
391 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
392 $iteminformation->{'branchname'} = $branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
393 push @itemswaiting, $iteminformation;
398 waitingmsg => $flags->{'WAITING'}->{'message'},
399 itemswaiting => \@itemswaiting,
402 if ($flag eq 'ODUES') {
406 oduesmsg => $flags->{'ODUES'}->{'message'}
409 my $items=$flags->{$flag}->{'itemlist'};
412 foreach my $item (@$items) {
413 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
414 push @itemswaiting, $iteminformation;
417 if ($query->param('module') ne 'returns'){
418 $template->param( nonreturns => 'true' );
421 if ($flag eq 'NOTES') {
425 notesmsg => $flags->{'NOTES'}->{'message'}
430 return($patrontable, $flaginfotext);
433 sub cuecatbarcodedecode {
436 my @fields = split(/\./,$barcode);
437 my @results = map(decode($_), @fields[1..$#fields]);