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