CIRCULATION : the big rewrite...
[koha.git] / circ / circulation.pl
1 #!/usr/bin/perl
2 # Please use 8-character tabs for this file (indents are every 4 characters)
3
4 #written 8/5/2002 by Finlay
5 #script to execute issuing of books
6
7
8 # Copyright 2000-2002 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use strict;
26 use CGI;
27 use C4::Circulation::Circ2;
28 use C4::Search;
29 use C4::Output;
30 use C4::Print;
31 use DBI;
32 use C4::Auth;
33 use C4::Interface::CGI::Output;
34 use C4::Koha;
35 use HTML::Template;
36 use C4::Date;
37
38 #
39 # PARAMETERS READING
40 #
41 my $query=new CGI;
42
43 my ($template, $loggedinuser, $cookie) = get_template_and_user
44     ({
45         template_name   => 'circ/circulation.tmpl',
46         query           => $query,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { circulate => 1 },
50     });
51 my $branches = getbranches();
52 my $printers = getprinters();
53 my $branch = getbranch($query, $branches);
54 my $printer = getprinter($query, $printers);
55
56 my $findborrower = $query->param('findborrower');
57 my $borrowernumber = $query->param('borrnumber');
58 my $print=$query->param('print');
59 my $barcode = $query->param('barcode');
60 my $year=$query->param('year');
61 my $month=$query->param('month');
62 my $day=$query->param('day');
63 my $stickyduedate=$query->param('stickyduedate');
64 my $issueconfirmed = $query->param('issueconfirmed');
65
66
67 #set up cookie.....
68 my $branchcookie;
69 my $printercookie;
70 if ($query->param('setcookies')) {
71         $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
72         $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
73 }
74
75 my %env; # env is used as an "environment" variable. Could be dropped probably...
76 $env{'branchcode'}=$branch;
77 $env{'printer'}=$printer;
78 $env{'queue'}=$printer;
79
80 my @datearr = localtime(time());
81 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
82 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", ($datearr[3]));
83
84 # get the borrower information.....
85 my $borrower;
86 if ($borrowernumber) {
87     $borrower = getpatroninformation(\%env,$borrowernumber,0);
88 }
89
90 # my $message;
91
92 #
93 # STEP 2 : FIND BORROWER
94 # if there is a list of find borrowers....
95 #
96 my $borrowerslist;
97 if ($findborrower) {
98         my ($count,$borrowers)=BornameSearch(\%env,$findborrower,'web');
99         my @borrowers=@$borrowers;
100         if ($#borrowers == -1) {
101                 $query->param('findborrower', '');
102         } elsif ($#borrowers == 0) {
103                 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
104                 $query->param('barcode','');
105         } else {
106                 $borrowerslist = \@borrowers;
107         }
108 }
109
110 #
111 # STEP 3 : ISSUING
112 #
113 #
114
115 # check and see if we should print
116 # if ($barcode eq ''  && $print eq 'maybe'){
117 #       $print = 'yes';
118 # }
119 # if ($print eq 'yes' && $borrowernumber ne ''){
120 #       printslip(\%env,$borrowernumber);
121 #       $query->param('borrnumber','');
122 #       $borrowernumber='';
123 # }
124
125 if ($barcode) {
126         $barcode = cuecatbarcodedecode($barcode);
127         my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
128 #       unless ($invalidduedate) {
129                 my ($error, $question) = canbookbeissued(\%env, $borrower, $barcode, $year, $month, $day);
130                 my $noerror=1;
131                 my $noquestion = 1;
132                 foreach my $impossible (keys %$error) {
133                         warn "Impossible : $impossible : ";#.%$error->{$impossible};
134                         $template->param($impossible => 1,
135                                                         IMPOSSIBLE => 1);
136                         $noerror = 0;
137                 }
138                 foreach my $needsconfirmation (keys %$question) {
139                         warn "needsconfirmation : $needsconfirmation : "; #.%$error->{$needsconfirmation};
140                         $template->param($needsconfirmation => 1,
141                                                         NEEDSCONFIRMATION => 1);
142                         $noquestion = 0;
143                 }
144                 if ($noerror && ($noquestion || $issueconfirmed)) {
145                         warn "NO ERROR";
146 #                       issuebook(\%env, $borrower, $barcode, $datedue);
147                 }
148
149 #       }
150 }
151
152 # reload the borrower info for the sake of reseting the flags.....
153 if ($borrowernumber) {
154         $borrower = getpatroninformation(\%env,$borrowernumber,0);
155 }
156
157
158 ##################################################################################
159 # BUILD HTML
160
161 # make the issued books table.....
162 my $todaysissues='';
163 my $previssues='';
164 my @realtodayissues;
165 my @realprevissues;
166 my $allowborrow;
167 if ($borrower) {
168 # get each issue of the borrower & separate them in todayissues & previous issues
169         my @todaysissues;
170         my @previousissues;
171         my $issueslist = getissues($borrower);
172         # split in 2 arrays for today & previous
173         foreach my $it (keys %$issueslist) {
174                 my $issuedate = $issueslist->{$it}->{'timestamp'};
175                 $issuedate = substr($issuedate, 0, 8);
176                 if ($todaysdate == $issuedate) {
177                         push @todaysissues, $issueslist->{$it};
178                 } else {
179                         push @previousissues, $issueslist->{$it};
180                 }
181     }
182         my $od; # overdues
183         my $togglecolor;
184         # parses today & build Template array
185         foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
186                 my $dd = $book->{'date_due'};
187                 my $datedue = $book->{'date_due'};
188                 $dd=format_date($dd);
189                 $datedue=~s/-//g;
190                 if ($datedue < $todaysdate) {
191                         $od = 1;
192                 } else {
193                         $od=0;
194                 }
195                 $book->{'od'}=$od;
196                 $book->{'dd'}=$dd;
197                 $book->{'tcolor'}=$togglecolor;
198                 if ($togglecolor) {
199                         $togglecolor=0;
200                 } else {
201                         $togglecolor=1;
202                 }
203                 if ($book->{'author'} eq ''){
204                         $book->{'author'}=' ';
205                 }    
206                 push @realtodayissues,$book;
207         }
208     
209         # parses previous & build Template array
210     foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
211                 my $dd = $book->{'date_due'};
212                 my $datedue = $book->{'date_due'};
213                 $dd=format_date($dd);
214                 my $pcolor = '';
215                 my $od = '';
216                 $datedue=~s/-//g;
217                 if ($datedue < $todaysdate) {
218                         $od = 1;
219                 } else {
220                         $od = 0;
221                 }
222                 $book->{'tcolor'}=$togglecolor;
223                 if ($togglecolor) {
224                         $togglecolor=0;
225                 } else {
226                         $togglecolor=1;
227                 }
228                 $book->{'dd'}=$dd; 
229                 $book->{'od'}=$od;
230                 $book->{'tcolor'}=$pcolor;
231                 if ($book->{'author'} eq ''){
232                         $book->{'author'}=' ';
233                 }    
234                 push @realprevissues,$book
235         }
236 }
237
238
239 my @values;
240 my %labels;
241 my $CGIselectborrower;
242 if ($borrowerslist) {
243         foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
244                 push @values,$_->{'borrowernumber'};
245                 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})";
246         }
247         $CGIselectborrower=CGI::scrolling_list( -name     => 'borrnumber',
248                                 -values   => \@values,
249                                 -labels   => \%labels,
250                                 -size     => 7,
251                                 -multiple => 0 );
252 }
253 #title
254
255 my ($patrontable, $flaginfotable) = patrontable($borrower);
256 my $amountold=$borrower->{flags}->{'CHARGES'}->{'message'};
257 my @temp=split(/\$/,$amountold);
258 $amountold=$temp[1];
259 $template->param(
260                 findborrower => $findborrower,
261                 borrower => $borrower,
262                 borrowernumber => $borrowernumber,
263                 branch => $branch,
264                 printer => $printer,
265                 branchname => $branches->{$branch}->{'branchname'},
266                 printername => $printers->{$printer}->{'printername'},
267 #               title => $iteminformation->{'title'},
268 #               author => $iteminformation->{'author'},
269                 firstname => $borrower->{'firstname'},
270                 surname => $borrower->{'surname'},
271                 categorycode => $borrower->{'categorycode'},
272                 streetaddress => $borrower->{'streetaddress'},
273                 city => $borrower->{'city'},
274                 phone => $borrower->{'phone'},
275                 cardnumber => $borrower->{'cardnumber'},
276                 amountold => $amountold,
277                 barcode => $barcode,
278                 stickyduedate => $stickyduedate,
279                 CGIselectborrower => $CGIselectborrower,
280                 todayissues => \@realtodayissues,
281                 previssues => \@realprevissues,
282         );
283 # set return date if stickyduedate
284 if ($stickyduedate) {
285         my $t_year = "year".$year;
286         my $t_month = "month".$month;
287         my $t_day = "day".$day;
288         $template->param(
289                 $t_year => 1,
290                 $t_month => 1,
291                 $t_day => 1,
292         );
293 }
294
295
296 if ($branchcookie) {
297     $cookie=[$cookie, $branchcookie, $printercookie];
298 }
299
300 output_html_with_http_headers $query, $cookie, $template->output;
301
302 ####################################################################
303 # Extra subroutines,,,
304
305 sub cuecatbarcodedecode {
306     my ($barcode) = @_;
307     chomp($barcode);
308     my @fields = split(/\./,$barcode);
309     my @results = map(decode($_), @fields[1..$#fields]);
310     if ($#results == 2){
311         return $results[2];
312     } else {
313         return $barcode;
314     }
315 }
316
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         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
328         if ($flags->{$flag}->{'noissues'}) {
329                 $template->param(
330                         noissues => 'true',
331                         color => $color,
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                         my $currentcolor=$color;
383                         {
384                         my $color=$currentcolor;
385                             my @itemswaiting;
386                         foreach my $item (@$items) {
387 #                               ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
388                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
389                                 push @itemswaiting, $iteminformation;
390                         }
391                         }
392                         if ($query->param('module') ne 'returns'){
393                                 $template->param( nonreturns => 'true' );
394                         }
395                 }
396                 if ($flag eq 'NOTES') {
397                         $template->param(
398                                 notes => 'true',
399                                 notesmsg => $flags->{'NOTES'}->{'message'}
400                                  );
401                 }
402         }
403     }
404     return($patrontable, $flaginfotext);
405 }
406
407 # Local Variables:
408 # tab-width: 8
409 # End: