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