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