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