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