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