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