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