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