Changed some hidden values, items labels, and changed the namespace from ASMP_*
[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 # New functions (renew etc.) added 07-08-2005 Tumer Garip tgarip@neu.edu.tr
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
33 use C4::Auth;
34 use C4::Interface::CGI::Output;
35 use C4::Koha;
36
37 use C4::Date;
38 use C4::Context;
39 use C4::Members;
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 $branch=C4::Context->preference("defaultBranch");
57 my $printer = getprinter($query, $printers);
58
59 my $findborrower = $query->param('findborrower');
60 $findborrower =~ s|,| |g;
61 $findborrower =~ s|'| |g;
62 my $borrowernumber = $query->param('borrnumber');
63
64 my $print=$query->param('print');
65 my $barcode = $query->param('barcode');
66 my $year=$query->param('year');
67 my $month=$query->param('month');
68 my $day=$query->param('day');
69 my $stickyduedate=$query->param('stickyduedate');
70 my $issueconfirmed = $query->param('issueconfirmed');
71 my $cancelreserve = $query->param('cancelreserve');
72 my %error;
73 my  $errorflag=$query->param('error');
74 ## The following er
75 if ( $errorflag gt "1"){
76 %error=(TOO_EARLY=>{1},) if ($errorflag eq "2");
77 %error=(NO_MORE_RENEWALS=>{1},) if ($errorflag eq "3");
78 %error=(RESERVE_FOUND=>{1},) if ($errorflag eq "4");
79 }elsif ( $errorflag eq "1"){
80 %error=(SUCCESFULL_RENEW=>{1},)
81 }
82 #set up cookie.....
83 my $branchcookie;
84 my $printercookie;
85 #if ($query->param('setcookies')) {
86 #       $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
87 #       $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
88 #}
89
90 my %env; # FIXME env is used as an "environment" variable. Could be dropped probably...
91
92 $env{'branchcode'}=$branch;
93 $env{'printer'}=$printer;
94 $env{'queue'}=$printer;
95
96 my @datearr = localtime(time());
97 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
98 my $todaysdate = (1900+$datearr[5])."-".sprintf ("%0.2d", ($datearr[4]+1))."-".sprintf ("%0.2d", ($datearr[3]));
99
100
101 # check and see if we should print
102  if ($barcode eq ''  && $print eq 'maybe'){
103         $print = 'yes';
104  }
105  if ($print eq 'yes' && $borrowernumber ne ''){
106         printslip(\%env,$borrowernumber);
107         $query->param('borrnumber','');
108         $borrowernumber='';
109  }
110
111 #
112 # STEP 2 : FIND BORROWER
113 # if there is a list of find borrowers....
114 #
115 my $borrowerslist;
116 my $message;
117 if ($findborrower) {
118         my ($count,$borrowers)=BornameSearch(\%env,$findborrower,'cardnumber','web');
119         my @borrowers=@$borrowers;
120         if ($#borrowers == -1) {
121                 $query->param('findborrower', '');
122                 $message =  "'$findborrower'";
123         } elsif ($#borrowers == 0) {
124                 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
125                 $query->param('barcode','');
126                 $borrowernumber=$borrowers[0]->{'borrowernumber'};
127         } else {
128                 $borrowerslist = \@borrowers;
129         }
130 }
131
132 # get the borrower information.....
133 my $borrower;
134 my $bornum=$query->param('bornum');
135 if ($bornum){
136 $borrowernumber=$bornum;
137 }
138 my $issues;
139 if ($borrowernumber) {
140         $borrower = getpatroninformation(\%env,$borrowernumber,0);
141         my ($od,$issue,$fines)=borrdata2(\%env,$borrowernumber);
142 my $warning;
143
144         $template->param(overduecount => $od,
145                                                         issuecount => $issue.$warning,
146                                                         finetotal => $fines);
147 $issues=$issue;
148 my $picture;
149  my $htdocs = C4::Context->config('opacdir');
150
151 $picture =$htdocs. "/htdocs/uploaded-files/users-photo/".$borrower->{'cardnumber'}.".jpg";
152  if (-e $picture)
153
154    $template->param(borrowerphoto => "http://library.neu.edu.tr/uploaded-files/users-photo/".$borrower->{'cardnumber'}.".jpg");
155  }else{
156 $picture = "http://cc.neu.edu.tr/stdpictures/".$borrower->{'cardnumber'}.".jpg";
157   $template->param(borrowerphoto => $picture);
158 }
159 }
160
161 #
162 # STEP 3 : ISSUING
163 #
164 #Try to  issue
165
166
167 if ($barcode) {
168
169         $barcode = cuecatbarcodedecode($barcode);
170         my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
171         if ($issueconfirmed) {
172                 issuebook(\%env, $borrower, $barcode, $datedue,$cancelreserve);
173                 my ($od,$issue,$fines)=borrdata2(\%env,$borrowernumber);
174                 my $warning;
175
176         $template->param(overduecount => $od,
177                                                         issuecount => $issue.$warning,
178                                                         finetotal => $fines);   
179
180         } else {
181                 my ($error, $question) = canbookbeissued(\%env, $borrower, $barcode, $year, $month, $day) unless %error;
182                 $error=\%error if %error;
183
184                 my $noerror=1;
185                 my $noquestion = 1;
186                 foreach my $impossible (keys %$error) {
187                         $template->param($impossible => $$error{$impossible},
188                                                         IMPOSSIBLE => 1) unless ($impossible eq 'SUCCESFULL_RENEW');
189                         $noerror = 0;
190                 }
191                 foreach my $needsconfirmation (keys %$question) {
192                         $template->param($needsconfirmation => $$question{$needsconfirmation},
193                                                         NEEDSCONFIRMATION => 1);
194                         $noquestion = 0;
195                 }
196                 $template->param(day => $day,
197                                                 month => $month,
198                                                 year => $year);
199                 if ($noerror && ($noquestion || $issueconfirmed)) {
200
201                         issuebook(\%env, $borrower, $barcode, $datedue);
202                 my ($od,$issue,$fines)=borrdata2(\%env,$borrowernumber);
203                 my $warning;
204
205         $template->param(overduecount => $od,
206                                                         issuecount => $issue.$warning,
207                                                         finetotal => $fines);
208                 }
209         }
210
211 }
212
213
214
215
216 ##################################################################################
217 # BUILD HTML
218
219 # make the issued books table.....
220 my $todaysissues='';
221 my $previssues='';
222 my @realtodayissues;
223 my @realprevissues;
224 #my @renewissues;
225 my $allowborrow;
226 if ($borrower) {
227
228 # get each issue of the borrower & separate them in todayissues & previous issues
229         my @todaysissues;
230         my @previousissues;
231         my $issueslist = getissues($borrower);
232         # split in 2 arrays for today & previous
233         foreach my $it (keys %$issueslist) {
234                 my $issuedate = $issueslist->{$it}->{'issue_date'};
235 #               $issuedate = substr($issuedate, 0, 10);
236         
237                 if ($todaysdate eq $issuedate) {
238                         push @todaysissues, $issueslist->{$it};
239                 } else { 
240                         push @previousissues, $issueslist->{$it};
241                 }
242     }
243
244
245         my $od; # overdues
246         my $i = 0;
247         my $togglecolor;
248         # parses today & build Template array
249         foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
250                 my $dd = $book->{'date_due'};
251                 my $datedue = $book->{'date_due'};
252
253                 $dd=format_date($dd);
254 #               $datedue=~s/-//g;
255                 if ($datedue lt $todaysdate) {
256                         $od = 1;
257                 } else {
258                         $od=0;
259                 }
260                 $book->{'od'}=$od;
261                 $book->{'dd'}=$dd;
262                 
263                 if ($togglecolor) {
264                         $togglecolor=0;
265                 } else {
266                         $togglecolor=1;
267                 }
268                 $book->{'tcolor'}=$togglecolor;
269                 if ($book->{'author'} eq ''){
270                         $book->{'author'}=' ';
271                 }    
272                 push @realtodayissues,$book;
273         $i++;
274         }
275
276
277
278         # parses previous & build Template array
279         $i=0;
280     foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
281                 my $dd = $book->{'date_due'};
282                 my $datedue = $book->{'date_due'};
283                 $dd=format_date($dd);
284                 my $pcolor = '';
285                 my $od = '';
286 #               $datedue=~s/-//g;
287                 if ($datedue lt $todaysdate) {
288                 
289                         $od = 1;
290                 } else {
291                         $od = 0;
292                 }
293         
294                 if ($togglecolor) {
295                         $togglecolor=0;
296                 } else {
297                         $togglecolor=1;
298                 }
299         $book->{'tcolor'}=$togglecolor;
300                 $book->{'dd'}=$dd; 
301                 $book->{'od'}=$od;
302                 #$book->{'tcolor'}=$pcolor;
303                 if ($book->{'author'} eq ''){
304                         $book->{'author'}=' ';
305                 }    
306
307                 push @realprevissues,$book;
308         $i++;
309         }
310
311 }#borrower
312
313
314 my @values;
315 my %labels;
316 my $CGIselectborrower;
317 if ($borrowerslist) {
318         foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
319                 push @values,$_->{'borrowernumber'};
320                 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'streetaddress'} ";
321         }
322         $CGIselectborrower=CGI::scrolling_list( -name     => 'borrnumber',
323                                 -values   => \@values,
324                                 -labels   => \%labels,
325                                 -size     => 7,
326                                 -multiple => 0 );
327 }
328 #title
329
330 my ($patrontable, $flaginfotable) = patrontable($borrower);
331 my $amountold=$borrower->{flags}->{'CHARGES'}->{'message'};
332 my @temp=split(/\$/,$amountold);
333 $amountold=$temp[1];
334 $template->param( today=>format_date($todaysdate),
335                 findborrower => $findborrower,
336                 borrower => $borrower,
337                 borrowernumber => $borrowernumber,
338                 branch => $branch,
339                 printer => $printer,
340                 branchname => $branches->{$branch}->{'branchname'},
341                 printername => $printers->{$printer}->{'printername'},
342                 firstname => $borrower->{'firstname'},
343                 surname => $borrower->{'surname'},
344                 categorycode => getborrowercategory($borrower->{'categorycode'}),
345                 streetaddress => $borrower->{'streetaddress'},
346                 emailaddress => $borrower->{'emailaddress'},
347                 borrowernotes => $borrower->{'borrowernotes'},
348                 city => $borrower->{'city'},
349                 phone => $borrower->{'phone'},
350                 cardnumber => $borrower->{'cardnumber'},
351                 amountold => $amountold,
352                 barcode => $barcode,
353                 stickyduedate => $stickyduedate,
354                 message => $message,
355                 CGIselectborrower => $CGIselectborrower,
356                 todayissues => \@realtodayissues,
357                 previssues => \@realprevissues,
358                 
359         );
360 # set return date if stickyduedate
361 if ($stickyduedate) {
362         my $t_year = "year".$year;
363         my $t_month = "month".$month;
364         my $t_day = "day".$day;
365         $template->param(
366                 $t_year => 1,
367                 $t_month => 1,
368                 $t_day => 1,
369         );
370 }
371
372
373 if ($branchcookie) {
374     $cookie=[$cookie, $branchcookie, $printercookie];
375 }
376
377 output_html_with_http_headers $query, $cookie, $template->output;
378
379 ####################################################################
380 # Extra subroutines,,,
381
382 sub patrontable {
383     my ($borrower) = @_;
384     my $flags = $borrower->{'flags'};
385     my $flaginfotable='';
386     my $flaginfotext;
387     #my $flaginfotext='';
388     my $flag;
389     my $color='';
390     foreach $flag (sort keys %$flags) {
391 #       my @itemswaiting='';
392         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
393         if ($flags->{$flag}->{'noissues'}) {
394                 $template->param(
395                         flagged => 1,
396                         noissues => 'true',
397                          );
398                 if ($flag eq 'GNA'){
399                         $template->param(
400                                 gna => 'true'
401                                 );
402                         }
403                 if ($flag eq 'LOST'){
404                         $template->param(
405                                 lost => 'true'
406                         );
407                         }
408                 if ($flag eq 'DBARRED'){
409                         $template->param(
410                                 dbarred => 'true'
411                         );
412                         }
413                 if ($flag eq 'CHARGES') {
414                         $template->param(
415                                 charges => 'true',
416                                 chargesmsg => $flags->{'CHARGES'}->{'message'}
417                                  );
418                 }
419         } else {
420                  if ($flag eq 'CHARGES') {
421                         $template->param(
422                                 charges => 'true',
423                                 flagged => 1,
424                                 chargesmsg => $flags->{'CHARGES'}->{'message'}
425                          );
426                 }
427                 if ($flag eq 'WAITING') {
428                         my $items=$flags->{$flag}->{'itemlist'};
429                         my @itemswaiting;
430                         foreach my $item (@$items) {
431                         my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
432                         $iteminformation->{'branchname'} = $branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
433                         push @itemswaiting, $iteminformation;
434                         }
435                         $template->param(
436                                 flagged => 1,
437                                 waiting => 'true',
438                                 waitingmsg => $flags->{'WAITING'}->{'message'},
439                                 itemswaiting => \@itemswaiting,
440                                  );
441                 }
442                 if ($flag eq 'ODUES') {
443                         $template->param(
444                                 odues => 'true',
445                                 flagged => 1,
446                                 oduesmsg => $flags->{'ODUES'}->{'message'}
447                                  );
448
449                         my $items=$flags->{$flag}->{'itemlist'};
450                         {
451                             my @itemswaiting;
452                         foreach my $item (@$items) {
453                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
454                                 push @itemswaiting, $iteminformation;
455                         }
456                         }
457                         if ($query->param('module') ne 'returns'){
458                                 $template->param( nonreturns => 'true' );
459                         }
460                 }
461                 if ($flag eq 'NOTES') {
462                         $template->param(
463                                 notes => 'true',
464                                 flagged => 1,
465                                 notesmsg => $flags->{'NOTES'}->{'message'}
466                                  );
467                 }
468         }
469     }
470     return($patrontable, $flaginfotext);
471 }
472
473 sub cuecatbarcodedecode {
474     my ($barcode) = @_;
475     chomp($barcode);
476     my @fields = split(/\./,$barcode);
477     my @results = map(decode($_), @fields[1..$#fields]);
478     if ($#results == 2){
479         return $results[2];
480     } else {
481         return $barcode;
482     }
483 }
484
485 # Local Variables:
486 # tab-width: 8
487 # End: