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