Adding Batch edit for items
[koha.git] / tools / batchMod.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2009 Biblibre S.A
4 #                                         John Soros <john.soros@biblibre.com>
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use warnings;
23
24 #need to open cgi and get the fh before anything else opens a new cgi context (see C4::Auth)
25 use CGI;
26 my $input = new CGI;
27 my $barcodefh = $input->upload('uploadbarcodes');
28
29 use C4::Auth;
30 use C4::Context;
31 use C4::Output;
32 use C4::Branch qw(GetBranches);
33 use C4::Koha qw(GetAuthorisedValues GetAuthValCode);
34 use C4::Items qw(GetItemnumberFromBarcode GetItem ModItem DelItemCheck);
35 use C4::Biblio qw(GetBiblioData);
36 use C4::Koha qw(GetItemTypes);
37
38
39 my ($template, $borrowernumber, $cookie)
40     = get_template_and_user({template_name => "tools/batchMod.tmpl",
41                 query => $input,
42                 type => "intranet",
43                 authnotrequired => 0,
44                 flagsrequired => {tools => 'batchmod'},
45                 debug => 1,
46                 });
47
48 #get all input vars and put it in a hash
49 my $invars = $input->Vars;
50
51 # Global item status lists (this has proven to be very handy :)
52 my $authloop = [];
53 my $authvals = [['items.notforloan', 'Item not for loan', 'notforloan'],
54                              ['items.itemlost', 'Item lost', 'itemlost'],
55                              ['items.wthdrawn', 'item withdrawn', 'sele'],
56                              ['items.damaged', 'item damaged', 'damaged'],
57                              ['items.location', 'item location', 'location'],
58                              ['items.ccode', 'items.ccode FIXME???', 'ccode'],
59                             ];
60
61 my $itemlevelpref = C4::Context->preference('item-level_itypes');
62 #we use item -level itemtypes
63 if ( $itemlevelpref ){
64     push(@$authvals, ['items.itype', 'itemtype', 'itype']);
65 }
66 if ( $invars->{op} && $invars->{op} eq 'barcodes'){
67     #Parse barcodes list
68     my @barcodelist;
69     if ( $invars->{'uploadbarcodes'} && length($invars->{'uploadbarcodes'})>0){
70         while (my $barcode=<$barcodefh>){
71             chomp $barcode;
72             push @barcodelist, $barcode;
73         }
74     }
75     if ( $invars->{barcodelist} && length($invars->{barcodelist}) > 0){
76         @barcodelist = split(/\s\n/, $invars->{barcodelist});
77     }
78     #get all branches
79     my $brancheshash = GetBranches();
80     my $branches = [];
81     for my $branchcode (keys %$brancheshash){
82         my $branch;
83         $branch->{'name'} = $brancheshash->{$branchcode}->{'branchname'};
84         $branch->{'code'} = $branchcode;
85         push @$branches, $branch;
86     }
87     
88     #get all item statuses
89     for my $field (@$authvals){
90         my $fieldstatusauth = {};
91         my ($fieldname, $fielddesc, $hashfdname) = @$field;
92         $fieldstatusauth->{authcode} = GetAuthValCode($fieldname);
93         if ($fieldstatusauth->{authcode} && length($fieldstatusauth->{authcode}) > 0){
94             $fieldstatusauth->{values} = GetAuthorisedValues($fieldstatusauth->{authcode});
95             $fieldstatusauth->{fieldname} = $fieldname;
96             $fieldstatusauth->{description} = $fielddesc;
97             $fieldstatusauth->{itemfieldname} = $hashfdname;
98             push @$authloop, $fieldstatusauth;
99         }
100     }
101     my $itemtypes = [];
102     #we use biblio level itype
103     if ( ! $itemlevelpref){
104         my $itypes = GetItemTypes();
105         for my $key (keys %$itypes){
106             push(@$itemtypes, $itypes->{$key});
107         }
108     }
109     #build items list
110     my @items;
111     my $itemslst = '';
112     if (scalar @barcodelist > 0){
113         for my $barcode (@barcodelist){
114             my $itemno = GetItemnumberFromBarcode($barcode);
115             my $item = GetItem($itemno, $barcode);
116             my $iteminfo = GetBiblioData($item->{biblionumber});
117             for my $field (qw(title isbn itemtype)){
118                 $item->{$field} = $iteminfo->{$field};
119             }
120 #kind of flakey, though we can be pretty sure the values will be in the same order as in the authloop
121 #have to use this since in html::template::pro i can't access one loop from inside an other,
122 #and variable substitution doesn't work (<!-- TMPL_VAR name="<!-- TMPL_VAR name="foo" -->" -->)
123 #this pushes a list of authorized valuse into each item's hash
124             my $itemauthloop = [];
125             for my $authfield (@$authloop){
126                 my $authvaluename;
127 #looking for the authvalues human-readable form
128                 for my $val (@{$authfield->{values}}){
129                     if( $item->{$authfield->{itemfieldname}} eq $val->{lib} || $item->{$authfield->{itemfieldname}} eq $val->{authorised_value}){
130                         $authvaluename = $val->{lib};
131                     }
132                 }
133                 if ( ! $authvaluename){
134                     $authvaluename = "Not found or invalid";
135                 }
136                 push(@$itemauthloop, { 'authvalue' => $authvaluename} );
137             }
138             for my $type (@$itemtypes){
139                 if ( $item->{itemtype} eq $type->{itemtype} ) {
140                     $item->{itemtypedesc} = $type->{description};
141                 }
142             }
143             $item->{authloop} = $itemauthloop;
144             push @items, $item;
145             $itemslst .= $item->{'itemnumber'} . ',';
146         }
147     }
148     
149     $template->param( 'itemsloop' => \@items,
150                                         'authloop' => $authloop,
151                                         'branches' => $branches,
152                                         'actions'    => 1,
153                                         'op'            => '1',
154                                         'itemslst'   => $itemslst,
155                                         'itemtypes' => $itemtypes,
156                                       );
157 } elsif ( $invars->{'itemslst'} ) {
158     for my $itemno ( split(',', $invars->{itemslst}) ) {
159                 my $item = GetItem($itemno);
160                 if ( $invars->{'del'} ) {
161                         DelItemCheck(C4::Context->dbh, $item->{'biblionumber'}, $item->{'itemnumber'})
162                 } else {
163                         for my $auth (@$authvals){
164                                 my ($authfieldname, $description, $hashfdname) = @$auth;
165                                 my $authcode = GetAuthValCode($authfieldname);
166                                 if ($invars->{$authcode} && $invars->{$authcode} ne '0'){
167                                         $item->{$hashfdname}=$invars->{$authcode};
168                                 }
169                         }
170                         if ($invars->{holdingbranch} && $invars->{holdingbranch} ne '0'){
171                                 $item->{holdingbranch} = $invars->{holdingbranch};
172                         }
173                         if ($invars->{homebranch} && $invars->{homebranch} ne '0'){
174                                 $item->{homebranch} = $invars->{homebranch};
175                         }
176                         ModItem($item, $item->{biblionumber}, $item->{itemnumber});
177                 }
178     }
179 }
180 $template->param('del' => $input->param('del'));
181 output_html_with_http_headers $input, $cookie, $template->output;