fixes in circulation module
[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; # FIXME 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         my ($od,$issue,$fines)=borrdata2(\%env,$borrowernumber);
111         $template->param(overduecount => $od,
112                                                         issuecount => $issue,
113                                                         finetotal => $fines);
114 }
115
116
117 #
118 # STEP 3 : ISSUING
119 #
120 #
121
122 # check and see if we should print
123 # if ($barcode eq ''  && $print eq 'maybe'){
124 #       $print = 'yes';
125 # }
126 # if ($print eq 'yes' && $borrowernumber ne ''){
127 #       printslip(\%env,$borrowernumber);
128 #       $query->param('borrnumber','');
129 #       $borrowernumber='';
130 # }
131
132 if ($barcode) {
133         $barcode = cuecatbarcodedecode($barcode);
134         my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
135         if ($issueconfirmed) {
136                         issuebook(\%env, $borrower, $barcode, $datedue);
137         } else {
138                 my ($error, $question) = canbookbeissued(\%env, $borrower, $barcode, $year, $month, $day);
139                 my $noerror=1;
140                 my $noquestion = 1;
141                 foreach my $impossible (keys %$error) {
142                         $template->param($impossible => $$error{$impossible},
143                                                         IMPOSSIBLE => 1);
144                         $noerror = 0;
145                 }
146                 foreach my $needsconfirmation (keys %$question) {
147                         $template->param($needsconfirmation => $$question{$needsconfirmation},
148                                                         NEEDSCONFIRMATION => 1);
149                         $noquestion = 0;
150                 }
151                 if ($noerror && ($noquestion || $issueconfirmed)) {
152                         issuebook(\%env, $borrower, $barcode, $datedue);
153                 }
154         }
155 }
156
157 # reload the borrower info for the sake of reseting the flags.....
158 if ($borrowernumber) {
159         $borrower = getpatroninformation(\%env,$borrowernumber,0);
160 }
161
162
163 ##################################################################################
164 # BUILD HTML
165
166 # make the issued books table.....
167 my $todaysissues='';
168 my $previssues='';
169 my @realtodayissues;
170 my @realprevissues;
171 my $allowborrow;
172 if ($borrower) {
173 # get each issue of the borrower & separate them in todayissues & previous issues
174         my @todaysissues;
175         my @previousissues;
176         my $issueslist = getissues($borrower);
177         # split in 2 arrays for today & previous
178         foreach my $it (keys %$issueslist) {
179                 my $issuedate = $issueslist->{$it}->{'timestamp'};
180                 $issuedate = substr($issuedate, 0, 8);
181                 if ($todaysdate == $issuedate) {
182                         push @todaysissues, $issueslist->{$it};
183                 } else {
184                         push @previousissues, $issueslist->{$it};
185                 }
186     }
187         my $od; # overdues
188         my $togglecolor;
189         # parses today & build Template array
190         foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
191                 my $dd = $book->{'date_due'};
192                 my $datedue = $book->{'date_due'};
193                 $dd=format_date($dd);
194                 $datedue=~s/-//g;
195                 if ($datedue < $todaysdate) {
196                         $od = 1;
197                 } else {
198                         $od=0;
199                 }
200                 $book->{'od'}=$od;
201                 $book->{'dd'}=$dd;
202                 $book->{'tcolor'}=$togglecolor;
203                 if ($togglecolor) {
204                         $togglecolor=0;
205                 } else {
206                         $togglecolor=1;
207                 }
208                 if ($book->{'author'} eq ''){
209                         $book->{'author'}=' ';
210                 }    
211                 push @realtodayissues,$book;
212         }
213     
214         # parses previous & build Template array
215     foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
216                 my $dd = $book->{'date_due'};
217                 my $datedue = $book->{'date_due'};
218                 $dd=format_date($dd);
219                 my $pcolor = '';
220                 my $od = '';
221                 $datedue=~s/-//g;
222                 if ($datedue < $todaysdate) {
223                         $od = 1;
224                 } else {
225                         $od = 0;
226                 }
227                 $book->{'tcolor'}=$togglecolor;
228                 if ($togglecolor) {
229                         $togglecolor=0;
230                 } else {
231                         $togglecolor=1;
232                 }
233                 $book->{'dd'}=$dd; 
234                 $book->{'od'}=$od;
235                 $book->{'tcolor'}=$pcolor;
236                 if ($book->{'author'} eq ''){
237                         $book->{'author'}=' ';
238                 }    
239                 push @realprevissues,$book
240         }
241 }
242
243
244 my @values;
245 my %labels;
246 my $CGIselectborrower;
247 if ($borrowerslist) {
248         foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
249                 push @values,$_->{'borrowernumber'};
250                 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})";
251         }
252         $CGIselectborrower=CGI::scrolling_list( -name     => 'borrnumber',
253                                 -values   => \@values,
254                                 -labels   => \%labels,
255                                 -size     => 7,
256                                 -multiple => 0 );
257 }
258 #title
259
260 my $amountold=$borrower->{flags}->{'CHARGES'}->{'message'};
261 my @temp=split(/\$/,$amountold);
262 $amountold=$temp[1];
263 $template->param(
264                 findborrower => $findborrower,
265                 borrower => $borrower,
266                 borrowernumber => $borrowernumber,
267                 branch => $branch,
268                 printer => $printer,
269                 branchname => $branches->{$branch}->{'branchname'},
270                 printername => $printers->{$printer}->{'printername'},
271                 firstname => $borrower->{'firstname'},
272                 surname => $borrower->{'surname'},
273                 categorycode => $borrower->{'categorycode'},
274                 streetaddress => $borrower->{'streetaddress'},
275                 city => $borrower->{'city'},
276                 phone => $borrower->{'phone'},
277                 cardnumber => $borrower->{'cardnumber'},
278                 amountold => $amountold,
279                 barcode => $barcode,
280                 stickyduedate => $stickyduedate,
281                 CGIselectborrower => $CGIselectborrower,
282                 todayissues => \@realtodayissues,
283                 previssues => \@realprevissues,
284         );
285 # set return date if stickyduedate
286 if ($stickyduedate) {
287         my $t_year = "year".$year;
288         my $t_month = "month".$month;
289         my $t_day = "day".$day;
290         $template->param(
291                 $t_year => 1,
292                 $t_month => 1,
293                 $t_day => 1,
294         );
295 }
296
297
298 if ($branchcookie) {
299     $cookie=[$cookie, $branchcookie, $printercookie];
300 }
301
302 output_html_with_http_headers $query, $cookie, $template->output;
303
304 ####################################################################
305 # Extra subroutines,,,
306
307 sub cuecatbarcodedecode {
308     my ($barcode) = @_;
309     chomp($barcode);
310     my @fields = split(/\./,$barcode);
311     my @results = map(decode($_), @fields[1..$#fields]);
312     if ($#results == 2){
313         return $results[2];
314     } else {
315         return $barcode;
316     }
317 }
318
319 # Local Variables:
320 # tab-width: 8
321 # End: