rel_3_0 moved to HEAD (introducing new files)
[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::Interface::CGI::Output;
26 use C4::Circulation::Circ2;
27 use C4::Date;
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 $datelastseen = $input->param('datelastseen');
36 $datelastseen = format_date_in_iso($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
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 Branches", };
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 $template->param(branchloop => \@branch_loop,);
65
66 $template->param(minlocation => $minlocation,
67                 maxlocation => $maxlocation,
68                 offset => $offset,
69                 pagesize => $pagesize,
70                 datelastseen => $datelastseen,
71                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
72                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
73                 IntranetNav => C4::Context->preference("IntranetNav"),
74                 );
75 if ($uploadbarcodes && length($uploadbarcodes)>0){
76     my $dbh=C4::Context->dbh;
77     my $date=format_date($input->param('setdate'));
78     $date = format_date("today") unless $date;
79 #       warn "$date";
80     my $strsth="update items set (datelastseen = $date) where items.barcode =?";
81     my $qupdate = $dbh->prepare($strsth);
82     my $strsth="select * from issues, items where items.itemnumber=issues.itemnumber and items.barcode =? and issues.returndate is null";
83     my $qonloan = $dbh->prepare($strsth);
84     my $strsth="select * from items where items.barcode =? and issues.wthdrawn=1";
85     my $qwthdrawn = $dbh->prepare($strsth);
86     my @errorloop;
87     my $count=0;
88     while (my $barcode=<$uploadbarcodes>){
89         chomp $barcode;
90 #               warn "$barcode";
91         if ($qwthdrawn->execute($barcode) &&$qwthdrawn->rows){
92             push @errorloop, {'barcode'=>$barcode,'ERR_WTHDRAWN'=>1};
93         }else{
94             $qupdate->execute($barcode);
95             $count += $qupdate->rows;
96 #                       warn "$count";
97             if ($count){
98                 $qonloan->execute($barcode);
99                 if ($qonloan->rows){
100                     my $data = $qonloan->fetchrow_hashref;
101                     my ($doreturn, $messages, $iteminformation, $borrower) =returnbook($barcode, $data->{homebranch});
102                     if ($doreturn){push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_RET'=>1}}
103                     else {push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_NOT_RET'=>1}}
104                 }
105             } else {
106                 push @errorloop, {'barcode'=>$barcode,'ERR_BARCODE'=>1};
107             }
108         }
109     }
110     $qupdate->finish;
111     $qonloan->finish;
112     $qwthdrawn->finish;
113     $template->param(date=>$date,Number=>$count);
114 #       $template->param(errorfile=>$errorfile) if ($errorfile);
115     $template->param(errorloop=>\@errorloop) if (@errorloop);
116 }else{
117     if ($markseen) {
118         foreach my $field ($input->param) {
119             if ($field =~ /SEEN-(.*)/) {
120                 &itemseen($1);
121             }
122         }
123     }
124     if ($op) {
125         my $res = C4::Circulation::Circ2::GetItemsForInventory($minlocation,$maxlocation,$datelastseen,$branchcode,$offset,$pagesize);
126         $template->param(loop =>$res,
127                         nextoffset => ($offset+$pagesize),
128                         prevoffset => ($offset?$offset-$pagesize:0),
129                         );
130     }
131 }
132 output_html_with_http_headers $input, $cookie, $template->output;
133
134 # Local Variables:
135 # tab-width: 8
136 # End: