fix for bug 858 (no warning when issuing a reserved book)
[koha.git] / circ / circulation.pl
1 #!/usr/bin/perl
2
3 # Please use 8-character tabs for this file (indents are every 4 characters)
4
5 #written 8/5/2002 by Finlay
6 #script to execute issuing of books
7
8
9 # Copyright 2000-2002 Katipo Communications
10 #
11 # This file is part of Koha.
12 #
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
16 # version.
17 #
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.
21 #
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
25
26 use strict;
27 use CGI;
28 use C4::Circulation::Circ2;
29 use C4::Search;
30 use C4::Output;
31 use C4::Print;
32 use DBI;
33 use C4::Auth;
34 use C4::Interface::CGI::Output;
35 use C4::Koha;
36 use HTML::Template;
37 use C4::Date;
38
39 #
40 # PARAMETERS READING
41 #
42 my $query=new CGI;
43
44 my ($template, $loggedinuser, $cookie) = get_template_and_user
45     ({
46         template_name   => 'circ/circulation.tmpl',
47         query           => $query,
48         type            => "intranet",
49         authnotrequired => 0,
50         flagsrequired   => { circulate => 1 },
51     });
52 my $branches = getbranches();
53 my $printers = getprinters();
54 my $branch = getbranch($query, $branches);
55 my $printer = getprinter($query, $printers);
56
57 my $findborrower = $query->param('findborrower');
58 my $borrowernumber = $query->param('borrnumber');
59 my $print=$query->param('print');
60 my $barcode = $query->param('barcode');
61 my $year=$query->param('year');
62 my $month=$query->param('month');
63 my $day=$query->param('day');
64 my $stickyduedate=$query->param('stickyduedate');
65 my $issueconfirmed = $query->param('issueconfirmed');
66
67
68 #set up cookie.....
69 my $branchcookie;
70 my $printercookie;
71 if ($query->param('setcookies')) {
72         $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
73         $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
74 }
75
76 my %env; # FIXME env is used as an "environment" variable. Could be dropped probably...
77
78 $env{'branchcode'}=$branch;
79 $env{'printer'}=$printer;
80 $env{'queue'}=$printer;
81
82 my @datearr = localtime(time());
83 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
84 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", ($datearr[3]));
85
86 # my $message;
87
88 #
89 # STEP 2 : FIND BORROWER
90 # if there is a list of find borrowers....
91 #
92 my $borrowerslist;
93 if ($findborrower) {
94         my ($count,$borrowers)=BornameSearch(\%env,$findborrower,'web');
95         my @borrowers=@$borrowers;
96         if ($#borrowers == -1) {
97                 $query->param('findborrower', '');
98         } elsif ($#borrowers == 0) {
99                 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
100                 $query->param('barcode','');
101                 $borrowernumber=$borrowers[0]->{'borrowernumber'};
102         } else {
103                 $borrowerslist = \@borrowers;
104         }
105 }
106
107 # get the borrower information.....
108 my $borrower;
109 if ($borrowernumber) {
110         $borrower = getpatroninformation(\%env,$borrowernumber,0);
111         my ($od,$issue,$fines)=borrdata2(\%env,$borrowernumber);
112         $template->param(overduecount => $od,
113                                                         issuecount => $issue,
114                                                         finetotal => $fines);
115 }
116
117
118 #
119 # STEP 3 : ISSUING
120 #
121 #
122
123 # check and see if we should print
124 # if ($barcode eq ''  && $print eq 'maybe'){
125 #       $print = 'yes';
126 # }
127 # if ($print eq 'yes' && $borrowernumber ne ''){
128 #       printslip(\%env,$borrowernumber);
129 #       $query->param('borrnumber','');
130 #       $borrowernumber='';
131 # }
132
133 if ($barcode) {
134         $barcode = cuecatbarcodedecode($barcode);
135         my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
136         if ($issueconfirmed) {
137                         issuebook(\%env, $borrower, $barcode, $datedue);
138         } else {
139                 my ($error, $question) = canbookbeissued(\%env, $borrower, $barcode, $year, $month, $day);
140                 my $noerror=1;
141                 my $noquestion = 1;
142                 foreach my $impossible (keys %$error) {
143                         $template->param($impossible => $$error{$impossible},
144                                                         IMPOSSIBLE => 1);
145                         $noerror = 0;
146                 }
147                 foreach my $needsconfirmation (keys %$question) {
148                         $template->param($needsconfirmation => $$question{$needsconfirmation},
149                                                         NEEDSCONFIRMATION => 1);
150                         $noquestion = 0;
151                 }
152                 $template->param(day => $day,
153                                                 month => $month,
154                                                 year => $year);
155                 if ($noerror && ($noquestion || $issueconfirmed)) {
156                         issuebook(\%env, $borrower, $barcode, $datedue);
157                 }
158         }
159 }
160
161 # reload the borrower info for the sake of reseting the flags.....
162 if ($borrowernumber) {
163         $borrower = getpatroninformation(\%env,$borrowernumber,0);
164 }
165
166
167 ##################################################################################
168 # BUILD HTML
169
170 # make the issued books table.....
171 my $todaysissues='';
172 my $previssues='';
173 my @realtodayissues;
174 my @realprevissues;
175 my $allowborrow;
176 if ($borrower) {
177 # get each issue of the borrower & separate them in todayissues & previous issues
178         my @todaysissues;
179         my @previousissues;
180         my $issueslist = getissues($borrower);
181         # split in 2 arrays for today & previous
182         foreach my $it (keys %$issueslist) {
183                 my $issuedate = $issueslist->{$it}->{'timestamp'};
184                 $issuedate = substr($issuedate, 0, 8);
185                 if ($todaysdate == $issuedate) {
186                         push @todaysissues, $issueslist->{$it};
187                 } else {
188                         push @previousissues, $issueslist->{$it};
189                 }
190     }
191         my $od; # overdues
192         my $togglecolor;
193         # parses today & build Template array
194         foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
195                 my $dd = $book->{'date_due'};
196                 my $datedue = $book->{'date_due'};
197                 $dd=format_date($dd);
198                 $datedue=~s/-//g;
199                 if ($datedue < $todaysdate) {
200                         $od = 1;
201                 } else {
202                         $od=0;
203                 }
204                 $book->{'od'}=$od;
205                 $book->{'dd'}=$dd;
206                 $book->{'tcolor'}=$togglecolor;
207                 if ($togglecolor) {
208                         $togglecolor=0;
209                 } else {
210                         $togglecolor=1;
211                 }
212                 if ($book->{'author'} eq ''){
213                         $book->{'author'}=' ';
214                 }    
215                 push @realtodayissues,$book;
216         }
217     
218         # parses previous & build Template array
219     foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
220                 my $dd = $book->{'date_due'};
221                 my $datedue = $book->{'date_due'};
222                 $dd=format_date($dd);
223                 my $pcolor = '';
224                 my $od = '';
225                 $datedue=~s/-//g;
226                 if ($datedue < $todaysdate) {
227                         $od = 1;
228                 } else {
229                         $od = 0;
230                 }
231                 $book->{'tcolor'}=$togglecolor;
232                 if ($togglecolor) {
233                         $togglecolor=0;
234                 } else {
235                         $togglecolor=1;
236                 }
237                 $book->{'dd'}=$dd; 
238                 $book->{'od'}=$od;
239                 $book->{'tcolor'}=$pcolor;
240                 if ($book->{'author'} eq ''){
241                         $book->{'author'}=' ';
242                 }    
243                 push @realprevissues,$book
244         }
245 }
246
247
248 my @values;
249 my %labels;
250 my $CGIselectborrower;
251 if ($borrowerslist) {
252         foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
253                 push @values,$_->{'borrowernumber'};
254                 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})";
255         }
256         $CGIselectborrower=CGI::scrolling_list( -name     => 'borrnumber',
257                                 -values   => \@values,
258                                 -labels   => \%labels,
259                                 -size     => 7,
260                                 -multiple => 0 );
261 }
262 #title
263
264 my ($patrontable, $flaginfotable) = patrontable($borrower);
265 my $amountold=$borrower->{flags}->{'CHARGES'}->{'message'};
266 my @temp=split(/\$/,$amountold);
267 $amountold=$temp[1];
268 $template->param(
269                 findborrower => $findborrower,
270                 borrower => $borrower,
271                 borrowernumber => $borrowernumber,
272                 branch => $branch,
273                 printer => $printer,
274                 branchname => $branches->{$branch}->{'branchname'},
275                 printername => $printers->{$printer}->{'printername'},
276                 firstname => $borrower->{'firstname'},
277                 surname => $borrower->{'surname'},
278                 categorycode => $borrower->{'categorycode'},
279                 streetaddress => $borrower->{'streetaddress'},
280                 borrowernotes => $borrower->{'borrowernotes'},
281                 city => $borrower->{'city'},
282                 phone => $borrower->{'phone'},
283                 cardnumber => $borrower->{'cardnumber'},
284                 amountold => $amountold,
285                 barcode => $barcode,
286                 stickyduedate => $stickyduedate,
287                 CGIselectborrower => $CGIselectborrower,
288                 todayissues => \@realtodayissues,
289                 previssues => \@realprevissues,
290         );
291 # set return date if stickyduedate
292 if ($stickyduedate) {
293         my $t_year = "year".$year;
294         my $t_month = "month".$month;
295         my $t_day = "day".$day;
296         $template->param(
297                 $t_year => 1,
298                 $t_month => 1,
299                 $t_day => 1,
300         );
301 }
302
303
304 if ($branchcookie) {
305     $cookie=[$cookie, $branchcookie, $printercookie];
306 }
307
308 output_html_with_http_headers $query, $cookie, $template->output;
309
310 ####################################################################
311 # Extra subroutines,,,
312
313 sub patrontable {
314     my ($borrower) = @_;
315     my $flags = $borrower->{'flags'};
316     my $flaginfotable='';
317     my $flaginfotext;
318     #my $flaginfotext='';
319     my $flag;
320     my $color='';
321     foreach $flag (sort keys %$flags) {
322 #       my @itemswaiting='';
323         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
324         if ($flags->{$flag}->{'noissues'}) {
325                 $template->param(
326                         noissues => 'true',
327                          );
328                 if ($flag eq 'GNA'){
329                         $template->param(
330                                 gna => 'true'
331                                 );
332                         }
333                 if ($flag eq 'LOST'){
334                         $template->param(
335                                 lost => 'true'
336                         );
337                         }
338                 if ($flag eq 'DBARRED'){
339                         $template->param(
340                                 dbarred => 'true'
341                         );
342                         }
343                 if ($flag eq 'CHARGES') {
344                         $template->param(
345                                 charges => 'true',
346                                 chargesmsg => $flags->{'CHARGES'}->{'message'}
347                                  );
348                 }
349         } else {
350                  if ($flag eq 'CHARGES') {
351                         $template->param(
352                                 charges => 'true',
353                                 chargesmsg => $flags->{'CHARGES'}->{'message'}
354                          );
355                 }
356                 if ($flag eq 'WAITING') {
357                         my $items=$flags->{$flag}->{'itemlist'};
358                         my @itemswaiting;
359                         foreach my $item (@$items) {
360                         my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
361                         $iteminformation->{'branchname'} = $branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
362                         push @itemswaiting, $iteminformation;
363                         }
364                         $template->param(
365                                 waiting => 'true',
366                                 waitingmsg => $flags->{'WAITING'}->{'message'},
367                                 itemswaiting => \@itemswaiting,
368                                  );
369                 }
370                 if ($flag eq 'ODUES') {
371                         $template->param(
372                                 odues => 'true',
373                                 oduesmsg => $flags->{'ODUES'}->{'message'}
374                                  );
375
376                         my $items=$flags->{$flag}->{'itemlist'};
377                         {
378                             my @itemswaiting;
379                         foreach my $item (@$items) {
380                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
381                                 push @itemswaiting, $iteminformation;
382                         }
383                         }
384                         if ($query->param('module') ne 'returns'){
385                                 $template->param( nonreturns => 'true' );
386                         }
387                 }
388                 if ($flag eq 'NOTES') {
389                         $template->param(
390                                 notes => 'true',
391                                 notesmsg => $flags->{'NOTES'}->{'message'}
392                                  );
393                 }
394         }
395     }
396     return($patrontable, $flaginfotext);
397 }
398
399 sub cuecatbarcodedecode {
400     my ($barcode) = @_;
401     chomp($barcode);
402     my @fields = split(/\./,$barcode);
403     my @results = map(decode($_), @fields[1..$#fields]);
404     if ($#results == 2){
405         return $results[2];
406     } else {
407         return $barcode;
408     }
409 }
410
411 # Local Variables:
412 # tab-width: 8
413 # End: