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