Merge remote-tracking branch 'origin/new/bug_7284'
[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                 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
129                 today                   =>      C4::Dates->today(),
130                 minlocation => $minlocation,
131                 maxlocation => $maxlocation,
132                 location=>$location,
133                 ignoreissued=>$ignoreissued,
134                 branchcode=>$branchcode,      
135                 branch    => $branch,
136                 offset => $offset,
137                 pagesize => $pagesize,
138                 datelastseen => $datelastseen,
139                 );
140 my @brcditems;
141 if ($uploadbarcodes && length($uploadbarcodes)>0){
142     my $dbh=C4::Context->dbh;
143     my $date = format_date_in_iso($input->param('setdate')) || C4::Dates->today('iso');
144 #       warn "$date";
145     my $strsth="select * from issues, items where items.itemnumber=issues.itemnumber and items.barcode =?";
146     my $qonloan = $dbh->prepare($strsth);
147     $strsth="select * from items where items.barcode =? and items.wthdrawn = 1";
148     my $qwthdrawn = $dbh->prepare($strsth);
149     my @errorloop;
150     my $count=0;
151     while (my $barcode=<$uploadbarcodes>){
152         $barcode =~ s/\r?\n$//;
153         if ($qwthdrawn->execute($barcode) &&$qwthdrawn->rows){
154             push @errorloop, {'barcode'=>$barcode,'ERR_WTHDRAWN'=>1};
155         }else{
156             my $item = GetItem('', $barcode);
157             if (defined $item && $item->{'itemnumber'}){
158                 ModItem({ datelastseen => $date }, undef, $item->{'itemnumber'});
159                 push @brcditems, $item;
160                 $count++;
161                 $qonloan->execute($barcode);
162                 if ($qonloan->rows){
163                     my $data = $qonloan->fetchrow_hashref;
164                     my ($doreturn, $messages, $iteminformation, $borrower) =AddReturn($barcode, $data->{homebranch});
165                     if ($doreturn){
166                         push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_RET'=>1}
167                     } else {
168                         push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_NOT_RET'=>1}
169                     }
170                 }
171             } else {
172                 push @errorloop, {'barcode'=>$barcode,'ERR_BARCODE'=>1};
173             }
174         }
175     }
176     $qonloan->finish;
177     $qwthdrawn->finish;
178     $template->param(date=>format_date($date),Number=>$count);
179 #       $template->param(errorfile=>$errorfile) if ($errorfile);
180     $template->param(errorloop=>\@errorloop) if (@errorloop);
181 }
182 #if we want to compare the results to a list of barcodes, or we have no barcode file
183 if ( ! ($uploadbarcodes && length($uploadbarcodes)>0 ) || ( $input->param('compareinv2barcd') eq 'on' && length($uploadbarcodes)>0) ) {
184     if ($markseen) {
185         foreach ($input->param) {
186             /SEEN-(.+)/ and &ModDateLastSeen($1);
187         }
188     }
189     if ($markseen or $op) {
190         $res = GetItemsForInventory( $minlocation, $maxlocation, $location, $itemtype, $ignoreissued, $datelastseen, $branchcode, $branch, $offset, $pagesize, $staton );
191         $template->param(loop =>$res,
192                         nextoffset => ($offset+$pagesize),
193                         prevoffset => ($offset?$offset-$pagesize:0),
194                         );
195     }
196     if ( defined $input->param('compareinv2barcd') && ( ( $input->param('compareinv2barcd') eq 'on' ) && ( scalar @brcditems != scalar @$res ) ) && length($uploadbarcodes) > 0 ){
197         if ( scalar @brcditems > scalar @$res ){
198             for my $brcditem (@brcditems) {
199                 if (! grep( $_->{barcode} =~ /$brcditem->{barcode}/ , @$res) ){
200                     $brcditem->{notfoundkoha} = 1;
201                     push @$res, $brcditem;
202                 }
203             }
204         } else {
205             my @notfound;
206             for my $item (@$res) {
207                 if ( ! grep( $_->{barcode} =~ /$item->{barcode}/ , @brcditems) ){
208                     $item->{notfoundbarcode} = 1;
209                     push @notfound, $item;
210                 }
211             }
212             $res = [@$res, @notfound];
213         }
214     }
215 }
216
217 if (defined $input->param('CSVexport') && $input->param('CSVexport') eq 'on'){
218     eval {use Text::CSV};
219     my $csv = Text::CSV->new or
220             die Text::CSV->error_diag ();
221     print $input->header(
222         -type       => 'text/csv',
223         -attachment => 'inventory.csv',
224     );
225     for my $re (@$res){
226         my @line;
227         for my $key (keys %$re) {
228             push @line, $re->{$key};
229         }
230         $csv->combine(@line);
231         print $csv->string, "\n";
232     }
233     exit;
234 }
235
236 output_html_with_http_headers $input, $cookie, $template->output;