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