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