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