inventory: two fixes
[koha.git] / tools / inventory.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use CGI;
22 use C4::Auth;
23 use C4::Context;
24 use C4::Output;
25 use C4::Biblio;
26 use C4::Items;
27 use C4::Dates qw/format_date format_date_in_iso/;
28 use C4::Koha;
29 use C4::Branch; # GetBranches
30
31 my $input = new CGI;
32 my $minlocation=$input->param('minlocation') || 'A';
33 my $maxlocation=$input->param('maxlocation');
34 $maxlocation=$minlocation.'Z' unless $maxlocation;
35 my $location=$input->param('location');
36 my $datelastseen = $input->param('datelastseen');
37 my $offset = $input->param('offset');
38 my $markseen = $input->param('markseen');
39 $offset=0 unless $offset;
40 my $pagesize = $input->param('pagesize');
41 $pagesize=50 unless $pagesize;
42 my $uploadbarcodes = $input->param('uploadbarcodes');
43 my $branchcode = $input->param('branchcode');
44 my $op = $input->param('op');
45 # warn "uploadbarcodes : ".$uploadbarcodes;
46 # use Data::Dumper; warn Dumper($input);
47 my ($template, $borrowernumber, $cookie)
48     = get_template_and_user({template_name => "tools/inventory.tmpl",
49                 query => $input,
50                 type => "intranet",
51                 authnotrequired => 0,
52                 flagsrequired => {tools => 1},
53                 debug => 1,
54                 });
55
56 my $branches = GetBranches();
57 my @branch_loop;
58 push @branch_loop, {value => "", branchname => "All Locations", };
59 for my $branch_hash (keys %$branches) {
60         push @branch_loop, {value => "$branch_hash",
61                            branchname => $branches->{$branch_hash}->{'branchname'}, 
62                            selected => ($branch_hash eq $branchcode?1:0)};      
63 }
64  
65 my @authorised_value_list;
66 my $authorisedvalue_categories;
67
68 my $dbh=C4::Context->dbh;
69 my $rqauthcategorie=$dbh->prepare("select authorised_value from marc_subfield_structure where frameworkcode=? and kohafield='items.location'");
70 my $rq=$dbh->prepare("select frameworkcode from biblio_framework");
71 $rq->execute;
72 while (my ($fwkcode)=$rq->fetchrow){
73   $rqauthcategorie->execute($fwkcode);
74   while (my ($authcat)=$rqauthcategorie->fetchrow){
75     if ($authcat && $authorisedvalue_categories!~/\b$authcat\W/){
76       $authorisedvalue_categories.="$authcat ";
77       my $data=GetAuthorisedValues($authcat);
78       foreach my $value (@$data){
79         $value->{selected}=1 if ($value->{authorised_value} eq ($location));
80       }      
81       push @authorised_value_list,@$data;
82     }  
83   }
84 }
85
86
87  
88 $template->param(branchloop => \@branch_loop,
89                 authorised_values=>\@authorised_value_list,   
90                 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
91                 minlocation => $minlocation,
92                 maxlocation => $maxlocation,
93                 location=>$location,
94                 branchcode=>$branchcode,      
95                 offset => $offset,
96                 pagesize => $pagesize,
97                 datelastseen => $datelastseen,
98                 );
99 if ($uploadbarcodes && length($uploadbarcodes)>0){
100     my $dbh=C4::Context->dbh;
101     my $date = format_date_in_iso($input->param('setdate')) || C4::Dates->today('iso');
102 #       warn "$date";
103     my $strsth="select * from issues, items where items.itemnumber=issues.itemnumber and items.barcode =? and issues.returndate is null";
104     my $qonloan = $dbh->prepare($strsth);
105     $strsth="select * from items where items.barcode =? and issues.wthdrawn=1";
106     my $qwthdrawn = $dbh->prepare($strsth);
107     my @errorloop;
108     my $count=0;
109     while (my $barcode=<$uploadbarcodes>){
110         chomp $barcode;
111 #               warn "$barcode";
112         if ($qwthdrawn->execute($barcode) &&$qwthdrawn->rows){
113             push @errorloop, {'barcode'=>$barcode,'ERR_WTHDRAWN'=>1};
114         }else{
115             my $item = GetItem('', $barcode);
116             if (defined $item){
117                 ModItem({ datelastseen => $date }, undef, $item->{'itemnumber'});
118                 $count++;
119                 $qonloan->execute($barcode);
120                 if ($qonloan->rows){
121                     my $data = $qonloan->fetchrow_hashref;
122                     my ($doreturn, $messages, $iteminformation, $borrower) =AddReturn($barcode, $data->{homebranch});
123                     if ($doreturn){push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_RET'=>1}}
124                     else {push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_NOT_RET'=>1}}
125                 }
126             } else {
127                 push @errorloop, {'barcode'=>$barcode,'ERR_BARCODE'=>1};
128             }
129         }
130     }
131     $qonloan->finish;
132     $qwthdrawn->finish;
133     $template->param(date=>format_date($date),Number=>$count);
134 #       $template->param(errorfile=>$errorfile) if ($errorfile);
135     $template->param(errorloop=>\@errorloop) if (@errorloop);
136 }else{
137     if ($markseen) {
138         foreach ($input->param) {
139             /SEEN-(.+)/ and &ModDateLastSeen($1);
140         }
141     }
142     if ($markseen or $op) {
143         my $res = GetItemsForInventory($minlocation,$maxlocation,$location,$datelastseen,$branchcode,$offset,$pagesize);
144         $template->param(loop =>$res,
145                         nextoffset => ($offset+$pagesize),
146                         prevoffset => ($offset?$offset-$pagesize:0),
147                         );
148     }
149 }
150 output_html_with_http_headers $input, $cookie, $template->output;
151
152 # Local Variables:
153 # tab-width: 8
154 # End: