Merge remote branch 'kc/new/enh/bug_5692' into kcmaster
[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 for my $fwk (keys %$frameworks){
81   my $fwkcode = $frameworks->{$fwk}->{'frameworkcode'};
82   my $authcode = GetAuthValCode('items.location', $fwkcode);
83     if ($authcode && $authorisedvalue_categories!~/\b$authcode\W/){
84       $authorisedvalue_categories.="$authcode ";
85       my $data=GetAuthorisedValues($authcode);
86       foreach my $value (@$data){
87         $value->{selected}=1 if ($value->{authorised_value} eq ($location));
88       }      
89       push @authorised_value_list,@$data;
90     }
91 }
92
93 my $statuses = [];
94 for my $statfield (qw/items.notforloan items.itemlost items.wthdrawn items.damaged/){
95     my $hash = {};
96     $hash->{fieldname} = $statfield;
97     $hash->{authcode} = GetAuthValCode($statfield);
98     if ($hash->{authcode}){
99         my $arr = GetAuthorisedValues($hash->{authcode});
100         $hash->{values} = $arr;
101         push @$statuses, $hash;
102     }
103 }
104 $template->param( statuses => $statuses );
105 my $staton = {};                                                                #authorized values that are ticked
106 for my $authvfield (@$statuses) {
107     $staton->{$authvfield->{fieldname}} = [];
108     for my $authval (@{$authvfield->{values}}){
109         if ( defined $input->param('status-' . $authvfield->{fieldname} . '-' . $authval->{id}) && $input->param('status-' . $authvfield->{fieldname} . '-' . $authval->{id}) eq 'on' ){
110             push @{$staton->{$authvfield->{fieldname}}}, $authval->{id};
111         }
112     }
113 }
114
115 my $statussth = '';
116 for my $authvfield (@$statuses) {
117     if ( scalar @{$staton->{$authvfield->{fieldname}}} > 0 ){
118         my $joinedvals = join ',', @{$staton->{$authvfield->{fieldname}}};
119         $statussth .= "$authvfield->{fieldname} in ($joinedvals) and ";
120     }
121 }
122 $statussth =~ s, and $,,g;
123  
124 $template->param(branchloop => \@branch_loop,
125                 authorised_values=>\@authorised_value_list,   
126                 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
127                 today                   =>      C4::Dates->today(),
128                 minlocation => $minlocation,
129                 maxlocation => $maxlocation,
130                 location=>$location,
131                 ignoreissued=>$ignoreissued,
132                 branchcode=>$branchcode,      
133                 branch    => $branch,
134                 offset => $offset,
135                 pagesize => $pagesize,
136                 datelastseen => $datelastseen,
137                 );
138 my @brcditems;
139 if ($uploadbarcodes && length($uploadbarcodes)>0){
140     my $dbh=C4::Context->dbh;
141     my $date = format_date_in_iso($input->param('setdate')) || C4::Dates->today('iso');
142 #       warn "$date";
143     my $strsth="select * from issues, items where items.itemnumber=issues.itemnumber and items.barcode =?";
144     my $qonloan = $dbh->prepare($strsth);
145     $strsth="select * from items where items.barcode =? and items.wthdrawn = 1";
146     my $qwthdrawn = $dbh->prepare($strsth);
147     my @errorloop;
148     my $count=0;
149     while (my $barcode=<$uploadbarcodes>){
150         $barcode =~ s/\r?\n$//;
151         if ($qwthdrawn->execute($barcode) &&$qwthdrawn->rows){
152             push @errorloop, {'barcode'=>$barcode,'ERR_WTHDRAWN'=>1};
153         }else{
154             my $item = GetItem('', $barcode);
155             if (defined $item && $item->{'itemnumber'}){
156                 ModItem({ datelastseen => $date }, undef, $item->{'itemnumber'});
157                 push @brcditems, $item;
158                 $count++;
159                 $qonloan->execute($barcode);
160                 if ($qonloan->rows){
161                     my $data = $qonloan->fetchrow_hashref;
162                     my ($doreturn, $messages, $iteminformation, $borrower) =AddReturn($barcode, $data->{homebranch});
163                     if ($doreturn){
164                         push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_RET'=>1}
165                     } else {
166                         push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_NOT_RET'=>1}
167                     }
168                 }
169             } else {
170                 push @errorloop, {'barcode'=>$barcode,'ERR_BARCODE'=>1};
171             }
172         }
173     }
174     $qonloan->finish;
175     $qwthdrawn->finish;
176     $template->param(date=>format_date($date),Number=>$count);
177 #       $template->param(errorfile=>$errorfile) if ($errorfile);
178     $template->param(errorloop=>\@errorloop) if (@errorloop);
179 }
180 #if we want to compare the results to a list of barcodes, or we have no barcode file
181 if ( ! ($uploadbarcodes && length($uploadbarcodes)>0 ) || ( $input->param('compareinv2barcd') eq 'on' && length($uploadbarcodes)>0) ) {
182     if ($markseen) {
183         foreach ($input->param) {
184             /SEEN-(.+)/ and &ModDateLastSeen($1);
185         }
186     }
187     if ($markseen or $op) {
188         $res = GetItemsForInventory( $minlocation, $maxlocation, $location, $itemtype, $ignoreissued, $datelastseen, $branchcode, $branch, $offset, $pagesize, $staton );
189         $template->param(loop =>$res,
190                         nextoffset => ($offset+$pagesize),
191                         prevoffset => ($offset?$offset-$pagesize:0),
192                         );
193     }
194     if ( defined $input->param('compareinv2barcd') && ( ( $input->param('compareinv2barcd') eq 'on' ) && ( scalar @brcditems != scalar @$res ) ) && length($uploadbarcodes) > 0 ){
195         if ( scalar @brcditems > scalar @$res ){
196             for my $brcditem (@brcditems) {
197                 if (! grep(/$brcditem->{barcode}/, @$res) ){
198                     $brcditem->{notfoundkoha} = 1;
199                     push @$res, $brcditem;
200                 }
201             }
202         } else {
203             my @notfound;
204             for my $item (@$res) {
205                 if ( ! grep(/$item->{barcode}/, @brcditems) ){
206                     $item->{notfoundbarcode} = 1;
207                     push @notfound, $item;
208                 }
209             }
210             $res = [@$res, @notfound];
211         }
212     }
213 }
214
215 if (defined $input->param('CSVexport') && $input->param('CSVexport') eq 'on'){
216     eval {use Text::CSV};
217     my $csv = Text::CSV->new or
218             die Text::CSV->error_diag ();
219     print $input->header(
220         -type       => 'text/csv',
221         -attachment => 'inventory.csv',
222     );
223     for my $re (@$res){
224         my @line;
225         for my $key (keys %$re) {
226             push @line, $re->{$key};
227         }
228         $csv->combine(@line);
229         print $csv->string, "\n";
230     }
231     exit;
232 }
233
234 output_html_with_http_headers $input, $cookie, $template->output;