MARC import: part 3 of large file support
[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::Date;
27 use C4::Koha;
28 use C4::Branch; # GetBranches
29
30 my $input = new CGI;
31 my $minlocation=$input->param('minlocation') || 'A';
32 my $maxlocation=$input->param('maxlocation');
33 $maxlocation=$minlocation.'Z' unless $maxlocation;
34 my $location=$input->param('location');
35 my $datelastseen = $input->param('datelastseen');
36 my $offset = $input->param('offset');
37 my $markseen = $input->param('markseen');
38 $offset=0 unless $offset;
39 my $pagesize = $input->param('pagesize');
40 $pagesize=50 unless $pagesize;
41 my $uploadbarcodes = $input->param('uploadbarcodes');
42 my $branchcode = $input->param('branchcode');
43 my $op = $input->param('op');
44 # warn "uploadbarcodes : ".$uploadbarcodes;
45 # use Data::Dumper; warn Dumper($input);
46 my ($template, $borrowernumber, $cookie)
47     = get_template_and_user({template_name => "tools/inventory.tmpl",
48                 query => $input,
49                 type => "intranet",
50                 authnotrequired => 0,
51                 flagsrequired => {tools => 1},
52                 debug => 1,
53                 });
54
55 my $branches = GetBranches();
56 my @branch_loop;
57 push @branch_loop, {value => "", branchname => "All Locations", };
58 for my $branch_hash (keys %$branches) {
59         push @branch_loop, {value => "$branch_hash",
60                            branchname => $branches->{$branch_hash}->{'branchname'}, 
61                            selected => ($branch_hash eq $branchcode?1:0)};      
62 }
63  
64 my @authorised_value_list;
65 my $authorisedvalue_categories;
66
67 my $dbh=C4::Context->dbh;
68 my $rqauthcategorie=$dbh->prepare("select authorised_value from marc_subfield_structure where frameworkcode=? and kohafield='items.location'");
69 my $rq=$dbh->prepare("select frameworkcode from biblio_framework");
70 $rq->execute;
71 while (my ($fwkcode)=$rq->fetchrow){
72   $rqauthcategorie->execute($fwkcode);
73   while (my ($authcat)=$rqauthcategorie->fetchrow){
74     if ($authcat && $authorisedvalue_categories!~/\b$authcat\W/){
75       $authorisedvalue_categories.="$authcat ";
76       my $data=GetAuthorisedValues($authcat);
77       foreach my $value (@$data){
78         $value->{selected}=1 if ($value->{authorised_value} eq ($location));
79       }      
80       push @authorised_value_list,@$data;
81     }  
82   }
83 }
84
85
86  
87 $template->param(branchloop => \@branch_loop,
88                 authorised_values=>\@authorised_value_list,   
89                 DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
90                 minlocation => $minlocation,
91                 maxlocation => $maxlocation,
92                 location=>$location,
93                 branchcode=>$branchcode,      
94                 offset => $offset,
95                 pagesize => $pagesize,
96                 datelastseen => $datelastseen,
97                 );
98 if ($uploadbarcodes && length($uploadbarcodes)>0){
99     my $dbh=C4::Context->dbh;
100     my $date=format_date_in_iso($input->param('setdate'));
101     $date = format_date_in_iso("today") unless $date;
102 #       warn "$date";
103     my $strsth="update items set (datelastseen = $date) where items.barcode =?";
104     my $qupdate = $dbh->prepare($strsth);
105     my $strsth="select * from issues, items where items.itemnumber=issues.itemnumber and items.barcode =? and issues.returndate is null";
106     my $qonloan = $dbh->prepare($strsth);
107     my $strsth="select * from items where items.barcode =? and issues.wthdrawn=1";
108     my $qwthdrawn = $dbh->prepare($strsth);
109     my @errorloop;
110     my $count=0;
111     while (my $barcode=<$uploadbarcodes>){
112         chomp $barcode;
113 #               warn "$barcode";
114         if ($qwthdrawn->execute($barcode) &&$qwthdrawn->rows){
115             push @errorloop, {'barcode'=>$barcode,'ERR_WTHDRAWN'=>1};
116         }else{
117             $qupdate->execute($barcode);
118             $count += $qupdate->rows;
119 #                       warn "$count";
120             if ($count){
121                 $qonloan->execute($barcode);
122                 if ($qonloan->rows){
123                     my $data = $qonloan->fetchrow_hashref;
124                     my ($doreturn, $messages, $iteminformation, $borrower) =AddReturn($barcode, $data->{homebranch});
125                     if ($doreturn){push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_RET'=>1}}
126                     else {push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_NOT_RET'=>1}}
127                 }
128             } else {
129                 push @errorloop, {'barcode'=>$barcode,'ERR_BARCODE'=>1};
130             }
131         }
132     }
133     $qupdate->finish;
134     $qonloan->finish;
135     $qwthdrawn->finish;
136     $template->param(date=>format_date($date),Number=>$count);
137 #       $template->param(errorfile=>$errorfile) if ($errorfile);
138     $template->param(errorloop=>\@errorloop) if (@errorloop);
139 }else{
140     if ($markseen) {
141         foreach my $field ($input->param) {
142             if ($field =~ /SEEN-(.*)/) {
143                 &ModDateLastSeen($1);
144             }
145         }
146         my $res = GetItemsForInventory($minlocation,$maxlocation,$location,$datelastseen,$branchcode,$offset,$pagesize);
147         $template->param(loop =>$res,
148                         nextoffset => ($offset+$pagesize),
149                         prevoffset => ($offset?$offset-$pagesize:0),
150                         );
151     }
152     if ($op) {
153         my $res = GetItemsForInventory($minlocation,$maxlocation,$location,$datelastseen,$branchcode,$offset,$pagesize);
154         $template->param(loop =>$res,
155                         nextoffset => ($offset+$pagesize),
156                         prevoffset => ($offset?$offset-$pagesize:0),
157                         );
158     }
159 }
160 output_html_with_http_headers $input, $cookie, $template->output;
161
162 # Local Variables:
163 # tab-width: 8
164 # End: