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