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