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