Bug 10291: Clarify misleading variable name
[koha.git] / tools / inventory.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2009 Biblibre S.A
4 #                                         John Soros <john.soros@biblibre.com>
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23
24 #need to open cgi and get the fh before anything else opens a new cgi context (see C4::Auth)
25 use CGI;
26 my $input = CGI->new;
27 my $uploadbarcodes = $input->param('uploadbarcodes');
28
29 use C4::Auth;
30 use C4::Context;
31 use C4::Output;
32 use C4::Biblio;
33 use C4::Items;
34 use C4::Dates qw/format_date format_date_in_iso/;
35 use C4::Koha;
36 use C4::Branch; # GetBranches
37 use C4::Circulation;
38
39 my $minlocation=$input->param('minlocation') || '';
40 my $maxlocation=$input->param('maxlocation');
41 $maxlocation=$minlocation.'Z' unless ( $maxlocation || ! $minlocation );
42 my $location=$input->param('location') || '';
43 my $itemtype=$input->param('itemtype'); # FIXME note, template does not currently supply this
44 my $ignoreissued=$input->param('ignoreissued');
45 my $datelastseen = $input->param('datelastseen');
46 my $offset = $input->param('offset');
47 my $markseen = $input->param('markseen');
48 $offset=0 unless $offset;
49 my $pagesize = $input->param('pagesize');
50 $pagesize=50 unless $pagesize;
51 my $branchcode = $input->param('branchcode') || '';
52 my $branch     = $input->param('branch');
53 my $op = $input->param('op');
54 my $res;    #contains the results loop
55 # warn "uploadbarcodes : ".$uploadbarcodes;
56 # use Data::Dumper; warn Dumper($input);
57
58 my ($template, $borrowernumber, $cookie)
59     = get_template_and_user({template_name => "tools/inventory.tmpl",
60                 query => $input,
61                 type => "intranet",
62                 authnotrequired => 0,
63                 flagsrequired => {tools => 'inventory'},
64                 debug => 1,
65                 });
66
67 my $branches = GetBranches();
68 my @branch_loop;
69 for my $branch_hash (keys %$branches) {
70         push @branch_loop, {value => "$branch_hash",
71                            branchname => $branches->{$branch_hash}->{'branchname'}, 
72                            selected => ($branch_hash eq $branchcode?1:0)};      
73 }
74
75 @branch_loop = sort {$a->{branchname} cmp $b->{branchname}} @branch_loop;
76 my @authorised_value_list;
77 my $authorisedvalue_categories = '';
78
79 my $frameworks = getframeworks();
80 $frameworks->{''} = {frameworkcode => ''}; # Add the default framework
81
82 for my $fwk (keys %$frameworks){
83   my $fwkcode = $frameworks->{$fwk}->{'frameworkcode'};
84   my $authcode = GetAuthValCode('items.location', $fwkcode);
85     if ($authcode && $authorisedvalue_categories!~/\b$authcode\W/){
86       $authorisedvalue_categories.="$authcode ";
87       my $data=GetAuthorisedValues($authcode);
88       foreach my $value (@$data){
89         $value->{selected}=1 if ($value->{authorised_value} eq ($location));
90       }      
91       push @authorised_value_list,@$data;
92     }
93 }
94
95 my $statuses = [];
96 for my $statfield (qw/items.notforloan items.itemlost items.wthdrawn items.damaged/){
97     my $hash = {};
98     $hash->{fieldname} = $statfield;
99     $hash->{authcode} = GetAuthValCode($statfield);
100     if ($hash->{authcode}){
101         my $arr = GetAuthorisedValues($hash->{authcode});
102         $hash->{values} = $arr;
103         push @$statuses, $hash;
104     }
105 }
106 $template->param( statuses => $statuses );
107 my $staton = {};                                                                #authorized values that are ticked
108 for my $authvfield (@$statuses) {
109     $staton->{$authvfield->{fieldname}} = [];
110     for my $authval (@{$authvfield->{values}}){
111         if ( defined $input->param('status-' . $authvfield->{fieldname} . '-' . $authval->{id}) && $input->param('status-' . $authvfield->{fieldname} . '-' . $authval->{id}) eq 'on' ){
112             push @{$staton->{$authvfield->{fieldname}}}, $authval->{id};
113         }
114     }
115 }
116
117 my $statussth = '';
118 for my $authvfield (@$statuses) {
119     if ( scalar @{$staton->{$authvfield->{fieldname}}} > 0 ){
120         my $joinedvals = join ',', @{$staton->{$authvfield->{fieldname}}};
121         $statussth .= "$authvfield->{fieldname} in ($joinedvals) and ";
122     }
123 }
124 $statussth =~ s, and $,,g;
125  
126 $template->param(branchloop => \@branch_loop,
127                 authorised_values=>\@authorised_value_list,   
128                 today                   =>      C4::Dates->today(),
129                 minlocation => $minlocation,
130                 maxlocation => $maxlocation,
131                 location=>$location,
132                 ignoreissued=>$ignoreissued,
133                 branchcode=>$branchcode,      
134                 branch    => $branch,
135                 offset => $offset,
136                 pagesize => $pagesize,
137                 datelastseen => $datelastseen,
138                 );
139 my @brcditems;
140 if ($uploadbarcodes && length($uploadbarcodes)>0){
141     my $dbh=C4::Context->dbh;
142     my $date = format_date_in_iso($input->param('setdate')) || C4::Dates->today('iso');
143 #       warn "$date";
144     my $strsth="select * from issues, items where items.itemnumber=issues.itemnumber and items.barcode =?";
145     my $qonloan = $dbh->prepare($strsth);
146     $strsth="select * from items where items.barcode =? and items.wthdrawn = 1";
147     my $qwthdrawn = $dbh->prepare($strsth);
148     my @errorloop;
149     my $count=0;
150     while (my $barcode=<$uploadbarcodes>){
151         $barcode =~ s/\r?\n$//;
152         if ($qwthdrawn->execute($barcode) &&$qwthdrawn->rows){
153             push @errorloop, {'barcode'=>$barcode,'ERR_WTHDRAWN'=>1};
154         }else{
155             my $item = GetItem('', $barcode);
156             if (defined $item && $item->{'itemnumber'}){
157                 ModItem({ datelastseen => $date }, undef, $item->{'itemnumber'});
158                 push @brcditems, $item;
159                 $count++;
160                 $qonloan->execute($barcode);
161                 if ($qonloan->rows){
162                     my $data = $qonloan->fetchrow_hashref;
163                     my ($doreturn, $messages, $iteminformation, $borrower) =AddReturn($barcode, $data->{homebranch});
164                     if ($doreturn){
165                         push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_RET'=>1}
166                     } else {
167                         push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_NOT_RET'=>1}
168                     }
169                 }
170             } else {
171                 push @errorloop, {'barcode'=>$barcode,'ERR_BARCODE'=>1};
172             }
173         }
174     }
175     $qonloan->finish;
176     $qwthdrawn->finish;
177     $template->param(date=>format_date($date),Number=>$count);
178 #       $template->param(errorfile=>$errorfile) if ($errorfile);
179     $template->param(errorloop=>\@errorloop) if (@errorloop);
180 }
181 #if we want to compare the results to a list of barcodes, or we have no barcode file
182 if ( ! ($uploadbarcodes && length($uploadbarcodes)>0 ) || ( $input->param('compareinv2barcd') eq 'on' && length($uploadbarcodes)>0) ) {
183     if ($markseen) {
184         foreach ($input->param) {
185             /SEEN-(.+)/ and &ModDateLastSeen($1);
186         }
187     }
188     if ($markseen or $op) {
189         $res = GetItemsForInventory( $minlocation, $maxlocation, $location, $itemtype, $ignoreissued, $datelastseen, $branchcode, $branch, $offset, $pagesize, $staton );
190         $template->param(loop =>$res,
191                         nextoffset => ($offset+$pagesize),
192                         prevoffset => ($offset?$offset-$pagesize:0),
193                         );
194     }
195     if ( defined $input->param('compareinv2barcd') && ( ( $input->param('compareinv2barcd') eq 'on' ) && ( scalar @brcditems != scalar @$res ) ) && length($uploadbarcodes) > 0 ){
196         if ( scalar @brcditems > scalar @$res ){
197             for my $brcditem (@brcditems) {
198                 if (! grep( $_->{barcode} =~ /$brcditem->{barcode}/ , @$res) ){
199                     $brcditem->{notfoundkoha} = 1;
200                     push @$res, $brcditem;
201                 }
202             }
203         } else {
204             my @notfound;
205             for my $item (@$res) {
206                 if ( ! grep( $_->{barcode} =~ /$item->{barcode}/ , @brcditems) ){
207                     $item->{notfoundbarcode} = 1;
208                     push @notfound, $item;
209                 }
210             }
211             $res = [@$res, @notfound];
212         }
213     }
214 }
215
216 if (defined $input->param('CSVexport') && $input->param('CSVexport') eq 'on'){
217     eval {use Text::CSV};
218     my $csv = Text::CSV->new or
219             die Text::CSV->error_diag ();
220     print $input->header(
221         -type       => 'text/csv',
222         -attachment => 'inventory.csv',
223     );
224     for my $re (@$res){
225         my @line;
226         for my $key (keys %$re) {
227             push @line, $re->{$key};
228         }
229         $csv->combine(@line);
230         print $csv->string, "\n";
231     }
232     exit;
233 }
234
235 output_html_with_http_headers $input, $cookie, $template->output;