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