Merge remote branch 'kc/new/bug_5563' into kcmaster
[koha.git] / tools / inventory.pl
blobc87a7e9afffe0915ffde0e6a66676647dd463053
1 #!/usr/bin/perl
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
11 # version.
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.
21 use strict;
22 use warnings;
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');
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;
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 $op = $input->param('op');
53 my $res; #contains the results loop
54 # warn "uploadbarcodes : ".$uploadbarcodes;
55 # use Data::Dumper; warn Dumper($input);
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => "tools/inventory.tmpl",
59 query => $input,
60 type => "intranet",
61 authnotrequired => 0,
62 flagsrequired => {tools => 'inventory'},
63 debug => 1,
64 });
66 my $branches = GetBranches();
67 my @branch_loop;
68 for my $branch_hash (keys %$branches) {
69 push @branch_loop, {value => "$branch_hash",
70 branchname => $branches->{$branch_hash}->{'branchname'},
71 selected => ($branch_hash eq $branchcode?1:0)};
74 @branch_loop = sort {$a->{branchname} cmp $b->{branchname}} @branch_loop;
75 my @authorised_value_list;
76 my $authorisedvalue_categories;
78 my $frameworks = getframeworks();
79 for my $fwk (keys %$frameworks){
80 my $fwkcode = $frameworks->{$fwk}->{'frameworkcode'};
81 my $authcode = GetAuthValCode('items.location', $fwkcode);
82 if ($authcode && $authorisedvalue_categories!~/\b$authcode\W/){
83 $authorisedvalue_categories.="$authcode ";
84 my $data=GetAuthorisedValues($authcode);
85 foreach my $value (@$data){
86 $value->{selected}=1 if ($value->{authorised_value} eq ($location));
88 push @authorised_value_list,@$data;
92 my $statuses = [];
93 for my $statfield (qw/items.notforloan items.itemlost items.wthdrawn items.damaged/){
94 my $hash = {};
95 $hash->{fieldname} = $statfield;
96 $hash->{authcode} = GetAuthValCode($statfield);
97 if ($hash->{authcode}){
98 my $arr = GetAuthorisedValues($hash->{authcode});
99 $hash->{values} = $arr;
100 push @$statuses, $hash;
103 $template->param( statuses => $statuses );
104 my $staton = {}; #authorized values that are ticked
105 for my $authvfield (@$statuses) {
106 $staton->{$authvfield->{fieldname}} = [];
107 for my $authval (@{$authvfield->{values}}){
108 if ( $input->param('status-' . $authvfield->{fieldname} . '-' . $authval->{id}) eq 'on' ){
109 push @{$staton->{$authvfield->{fieldname}}}, $authval->{id};
114 my $statussth = '';
115 for my $authvfield (@$statuses) {
116 if ( scalar @{$staton->{$authvfield->{fieldname}}} > 0 ){
117 my $joinedvals = join ',', @{$staton->{$authvfield->{fieldname}}};
118 $statussth .= "$authvfield->{fieldname} in ($joinedvals) and ";
121 $statussth =~ s, and $,,g;
123 $template->param(branchloop => \@branch_loop,
124 authorised_values=>\@authorised_value_list,
125 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
126 today => C4::Dates->today(),
127 minlocation => $minlocation,
128 maxlocation => $maxlocation,
129 location=>$location,
130 ignoreissued=>$ignoreissued,
131 branchcode=>$branchcode,
132 offset => $offset,
133 pagesize => $pagesize,
134 datelastseen => $datelastseen,
136 my @brcditems;
137 if ($uploadbarcodes && length($uploadbarcodes)>0){
138 my $dbh=C4::Context->dbh;
139 my $date = format_date_in_iso($input->param('setdate')) || C4::Dates->today('iso');
140 # warn "$date";
141 my $strsth="select * from issues, items where items.itemnumber=issues.itemnumber and items.barcode =?";
142 my $qonloan = $dbh->prepare($strsth);
143 $strsth="select * from items where items.barcode =? and items.wthdrawn = 1";
144 my $qwthdrawn = $dbh->prepare($strsth);
145 my @errorloop;
146 my $count=0;
147 while (my $barcode=<$uploadbarcodes>){
148 $barcode =~ s/\r?\n$//;
149 if ($qwthdrawn->execute($barcode) &&$qwthdrawn->rows){
150 push @errorloop, {'barcode'=>$barcode,'ERR_WTHDRAWN'=>1};
151 }else{
152 my $item = GetItem('', $barcode);
153 if (defined $item && $item->{'itemnumber'}){
154 ModItem({ datelastseen => $date }, undef, $item->{'itemnumber'});
155 push @brcditems, $item;
156 $count++;
157 $qonloan->execute($barcode);
158 if ($qonloan->rows){
159 my $data = $qonloan->fetchrow_hashref;
160 my ($doreturn, $messages, $iteminformation, $borrower) =AddReturn($barcode, $data->{homebranch});
161 if ($doreturn){
162 push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_RET'=>1}
163 } else {
164 push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_NOT_RET'=>1}
167 } else {
168 push @errorloop, {'barcode'=>$barcode,'ERR_BARCODE'=>1};
172 $qonloan->finish;
173 $qwthdrawn->finish;
174 $template->param(date=>format_date($date),Number=>$count);
175 # $template->param(errorfile=>$errorfile) if ($errorfile);
176 $template->param(errorloop=>\@errorloop) if (@errorloop);
178 #if we want to compare the results to a list of barcodes, or we have no barcode file
179 if ( ! ($uploadbarcodes && length($uploadbarcodes)>0 ) || ( $input->param('compareinv2barcd') eq 'on' && length($uploadbarcodes)>0) ) {
180 if ($markseen) {
181 foreach ($input->param) {
182 /SEEN-(.+)/ and &ModDateLastSeen($1);
185 if ($markseen or $op) {
186 $res = GetItemsForInventory($minlocation,$maxlocation,$location, $ignoreissued,$itemtype,$datelastseen,$branchcode,$offset,$pagesize,$staton);
187 $template->param(loop =>$res,
188 nextoffset => ($offset+$pagesize),
189 prevoffset => ($offset?$offset-$pagesize:0),
192 if ( ( ( $input->param('compareinv2barcd') eq 'on' ) && ( scalar @brcditems != scalar @$res ) ) && length($uploadbarcodes) > 0 ){
193 if ( scalar @brcditems > scalar @$res ){
194 for my $brcditem (@brcditems) {
195 if (! grep(/$brcditem->{barcode}/, @$res) ){
196 $brcditem->{notfoundkoha} = 1;
197 push @$res, $brcditem;
200 } else {
201 my @notfound;
202 for my $item (@$res) {
203 if ( ! grep(/$item->{barcode}/, @brcditems) ){
204 $item->{notfoundbarcode} = 1;
205 push @notfound, $item;
208 $res = [@$res, @notfound];
213 if ($input->param('CSVexport') eq 'on'){
214 eval {use Text::CSV};
215 my $csv = Text::CSV->new or
216 die Text::CSV->error_diag ();
217 print $input->header(
218 -type => 'text/csv',
219 -attachment => 'inventory.csv',
221 for my $re (@$res){
222 my @line;
223 for my $key (keys %$re) {
224 push @line, $re->{$key};
226 $csv->combine(@line);
227 print $csv->string, "\n";
229 exit;
232 output_html_with_http_headers $input, $cookie, $template->output;