moving cataloguing scripts to a cataloguing directory (more logic than acqui.simple...
[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 = substr($issuedate, 0, 8);
208                 if ($todaysdate == $issuedate) {
209                         push @todaysissues, $issueslist->{$it};
210                 } else {
211                         push @previousissues, $issueslist->{$it};
212                 }
213     }
214         my $od; # overdues
215         my $i = 0;
216         my $togglecolor;
217         # parses today & build Template array
218         foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
219                 my $dd = $book->{'date_due'};
220                 my $datedue = $book->{'date_due'};
221                 $dd=format_date($dd);
222                 $datedue=~s/-//g;
223                 if ($datedue < $todaysdate) {
224                         $od = 1;
225                 } else {
226                         $od=0;
227                 }
228                 if ($i%2) {
229                         $togglecolor=0;
230                 } else {
231                         $togglecolor=1;
232                 }
233                 $book->{'togglecolor'} = $togglecolor;
234                 $book->{'od'}=$od;
235                 $book->{'dd'}=$dd;
236                 if ($book->{'author'} eq ''){
237                         $book->{'author'}=' ';
238                 }    
239                 push @realtodayissues,$book;
240                 $i++;
241         }
242
243         # parses previous & build Template array
244         $i = 0;
245     foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
246                 my $dd = $book->{'date_due'};
247                 my $datedue = $book->{'date_due'};
248                 $dd=format_date($dd);
249                 my $pcolor = '';
250                 my $od = '';
251                 $datedue=~s/-//g;
252                 if ($datedue < $todaysdate) {
253                         $od = 1;
254                 } else {
255                         $od = 0;
256                 }
257                 if ($i%2) {
258                         $togglecolor=0;
259                 } else {
260                         $togglecolor=1;
261                 }
262                 $book->{'togglecolor'} = $togglecolor;
263                 $book->{'dd'}=$dd; 
264                 $book->{'od'}=$od;
265                 if ($book->{'author'} eq ''){
266                         $book->{'author'}=' ';
267                 }    
268                 push @realprevissues,$book;
269                 $i++;
270         }
271 }
272
273
274 my @values;
275 my %labels;
276 my $CGIselectborrower;
277 if ($borrowerslist) {
278         foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
279                 push @values,$_->{'borrowernumber'};
280                 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'streetaddress'} ";
281         }
282         $CGIselectborrower=CGI::scrolling_list( -name     => 'borrnumber',
283                                 -values   => \@values,
284                                 -labels   => \%labels,
285                                 -size     => 7,
286                                 -multiple => 0 );
287 }
288 #title
289
290 my ($patrontable, $flaginfotable) = patrontable($borrower);
291 my $amountold=$borrower->{flags}->{'CHARGES'}->{'message'} || 0;
292 my @temp=split(/\$/,$amountold);
293 $amountold=$temp[1];
294 $template->param(
295                 findborrower => $findborrower,
296                 borrower => $borrower,
297                 borrowernumber => $borrowernumber,
298                 branch => $branch,
299                 printer => $printer,
300                 branchname => $branches->{$branch}->{'branchname'},
301                 printername => $printers->{$printer}->{'printername'},
302                 firstname => $borrower->{'firstname'},
303                 surname => $borrower->{'surname'},
304                 categorycode => $borrower->{'categorycode'},
305                 streetaddress => $borrower->{'streetaddress'},
306                 emailaddress => $borrower->{'emailaddress'},
307                 borrowernotes => $borrower->{'borrowernotes'},
308                 city => $borrower->{'city'},
309                 phone => $borrower->{'phone'},
310                 cardnumber => $borrower->{'cardnumber'},
311                 amountold => $amountold,
312                 barcode => $barcode,
313                 stickyduedate => $stickyduedate,
314                 message => $message,
315                 CGIselectborrower => $CGIselectborrower,
316                 todayissues => \@realtodayissues,
317                 previssues => \@realprevissues,
318         );
319 # set return date if stickyduedate
320 if ($stickyduedate) {
321         my $t_year = "year".$year;
322         my $t_month = "month".$month;
323         my $t_day = "day".$day;
324         $template->param(
325                 $t_year => 1,
326                 $t_month => 1,
327                 $t_day => 1,
328         );
329 }
330
331
332 if ($branchcookie) {
333     $cookie=[$cookie, $branchcookie, $printercookie];
334 }
335
336 output_html_with_http_headers $query, $cookie, $template->output;
337
338 ####################################################################
339 # Extra subroutines,,,
340
341 sub patrontable {
342     my ($borrower) = @_;
343     my $flags = $borrower->{'flags'};
344     my $flaginfotable='';
345     my $flaginfotext;
346     #my $flaginfotext='';
347     my $flag;
348     my $color='';
349     foreach $flag (sort keys %$flags) {
350 #       my @itemswaiting='';
351         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
352         if ($flags->{$flag}->{'noissues'}) {
353                 $template->param(
354                         flagged => 1,
355                         noissues => 'true',
356                          );
357                 if ($flag eq 'GNA'){
358                         $template->param(
359                                 gna => 'true'
360                                 );
361                         }
362                 if ($flag eq 'LOST'){
363                         $template->param(
364                                 lost => 'true'
365                         );
366                         }
367                 if ($flag eq 'DBARRED'){
368                         $template->param(
369                                 dbarred => 'true'
370                         );
371                         }
372                 if ($flag eq 'CHARGES') {
373                         $template->param(
374                                 charges => 'true',
375                                 chargesmsg => $flags->{'CHARGES'}->{'message'}
376                                  );
377                 }
378         } else {
379                  if ($flag eq 'CHARGES') {
380                         $template->param(
381                                 charges => 'true',
382                                 flagged => 1,
383                                 chargesmsg => $flags->{'CHARGES'}->{'message'}
384                          );
385                 }
386                 if ($flag eq 'WAITING') {
387                         my $items=$flags->{$flag}->{'itemlist'};
388                         my @itemswaiting;
389                         foreach my $item (@$items) {
390                         my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
391                         $iteminformation->{'branchname'} = $branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
392                         push @itemswaiting, $iteminformation;
393                         }
394                         $template->param(
395                                 flagged => 1,
396                                 waiting => 'true',
397                                 waitingmsg => $flags->{'WAITING'}->{'message'},
398                                 itemswaiting => \@itemswaiting,
399                                  );
400                 }
401                 if ($flag eq 'ODUES') {
402                         $template->param(
403                                 odues => 'true',
404                                 flagged => 1,
405                                 oduesmsg => $flags->{'ODUES'}->{'message'}
406                                  );
407
408                         my $items=$flags->{$flag}->{'itemlist'};
409                         {
410                             my @itemswaiting;
411                         foreach my $item (@$items) {
412                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
413                                 push @itemswaiting, $iteminformation;
414                         }
415                         }
416                         if ($query->param('module') ne 'returns'){
417                                 $template->param( nonreturns => 'true' );
418                         }
419                 }
420                 if ($flag eq 'NOTES') {
421                         $template->param(
422                                 notes => 'true',
423                                 flagged => 1,
424                                 notesmsg => $flags->{'NOTES'}->{'message'}
425                                  );
426                 }
427         }
428     }
429     return($patrontable, $flaginfotext);
430 }
431
432 sub cuecatbarcodedecode {
433     my ($barcode) = @_;
434     chomp($barcode);
435     my @fields = split(/\./,$barcode);
436     my @results = map(decode($_), @fields[1..$#fields]);
437     if ($#results == 2){
438         return $results[2];
439     } else {
440         return $barcode;
441     }
442 }
443
444 # Local Variables:
445 # tab-width: 8
446 # End: