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