3 # Copyright 2000-2009 Biblibre S.A
4 # John Soros <john.soros@biblibre.com>
6 # This file is part of Koha.
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
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.
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.
24 #need to open cgi and get the fh before anything else opens a new cgi context (see C4::Auth)
27 my $uploadbarcodes = $input->param('uploadbarcodes');
34 use C4::Dates qw/format_date format_date_in_iso/;
36 use C4::Branch; # GetBranches
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);
58 my ($template, $borrowernumber, $cookie)
59 = get_template_and_user({template_name => "tools/inventory.tmpl",
63 flagsrequired => {tools => 'inventory'},
67 my $branches = GetBranches();
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)};
75 @branch_loop = sort {$a->{branchname} cmp $b->{branchname}} @branch_loop;
76 my @authorised_value_list;
77 my $authorisedvalue_categories = '';
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));
89 push @authorised_value_list,@$data;
94 for my $statfield (qw/items.notforloan items.itemlost items.wthdrawn items.damaged/){
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;
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};
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 ";
122 $statussth =~ s, and $,,g;
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,
131 ignoreissued=>$ignoreissued,
132 branchcode=>$branchcode,
135 pagesize => $pagesize,
136 datelastseen => $datelastseen,
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');
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);
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};
154 my $item = GetItem('', $barcode);
155 if (defined $item && $item->{'itemnumber'}){
156 ModItem({ datelastseen => $date }, undef, $item->{'itemnumber'});
157 push @brcditems, $item;
159 $qonloan->execute($barcode);
161 my $data = $qonloan->fetchrow_hashref;
162 my ($doreturn, $messages, $iteminformation, $borrower) =AddReturn($barcode, $data->{homebranch});
164 push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_RET'=>1}
166 push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_NOT_RET'=>1}
170 push @errorloop, {'barcode'=>$barcode,'ERR_BARCODE'=>1};
176 $template->param(date=>format_date($date),Number=>$count);
177 # $template->param(errorfile=>$errorfile) if ($errorfile);
178 $template->param(errorloop=>\@errorloop) if (@errorloop);
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) ) {
183 foreach ($input->param) {
184 /SEEN-(.+)/ and &ModDateLastSeen($1);
187 if ($markseen or $op) {
188 $res = GetItemsForInventory( $minlocation, $maxlocation, $location, $ignoreissued, $itemtype, $datelastseen, $branchcode, $branch, $offset, $pagesize, $staton );
189 $template->param(loop =>$res,
190 nextoffset => ($offset+$pagesize),
191 prevoffset => ($offset?$offset-$pagesize:0),
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;
204 for my $item (@$res) {
205 if ( ! grep(/$item->{barcode}/, @brcditems) ){
206 $item->{notfoundbarcode} = 1;
207 push @notfound, $item;
210 $res = [@$res, @notfound];
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(
221 -attachment => 'inventory.csv',
225 for my $key (keys %$re) {
226 push @line, $re->{$key};
228 $csv->combine(@line);
229 print $csv->string, "\n";
234 output_html_with_http_headers $input, $cookie, $template->output;