Fixing item batch modification
[koha_fer] / 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', 'ccode'],
59                             ];
60
61 my $itemlevelpref = C4::Context->preference('item-level_itypes');
62 if ( $invars->{op} && $invars->{op} eq 'barcodes'){
63     #Parse barcodes list
64     my @barcodelist;
65     if ( $invars->{'uploadbarcodes'} && length($invars->{'uploadbarcodes'})>0){
66         while (my $barcode=<$barcodefh>){
67             chomp $barcode;
68             push @barcodelist, $barcode;
69         }
70     }
71     if ( $invars->{barcodelist} && length($invars->{barcodelist}) > 0){
72         @barcodelist = split(/\s\n/, $invars->{barcodelist});
73     }
74     #get all branches
75     my $brancheshash = GetBranches();
76     my $branches = [];
77     for my $branchcode (keys %$brancheshash){
78         my $branch;
79         $branch->{'name'} = $brancheshash->{$branchcode}->{'branchname'};
80         $branch->{'code'} = $branchcode;
81         push @$branches, $branch;
82     }
83     
84     #get all item statuses
85     for my $field (@$authvals){
86         my $fieldstatusauth = {};
87         my ($fieldname, $fielddesc, $hashfdname) = @$field;
88         $fieldstatusauth->{authcode} = GetAuthValCode($fieldname);
89         if ($fieldstatusauth->{authcode} && length($fieldstatusauth->{authcode}) > 0){
90             $fieldstatusauth->{values} = GetAuthorisedValues($fieldstatusauth->{authcode});
91             $fieldstatusauth->{fieldname} = $fieldname;
92             $fieldstatusauth->{description} = $fielddesc;
93             $fieldstatusauth->{itemfieldname} = $hashfdname;
94             push @$authloop, $fieldstatusauth;
95         }
96     }
97     
98     #we use item level itype
99         my $itemtypes = [];
100     if ($itemlevelpref){
101         my $itypes = GetItemTypes();
102         for my $key (keys %$itypes){
103             push(@$itemtypes, $itypes->{$key});
104         }
105     }
106     #build items list
107     my @items;
108     my $itemslst = '';
109     if (scalar @barcodelist > 0){
110         for my $barcode (@barcodelist){
111             my $itemno = GetItemnumberFromBarcode($barcode);
112             my $item = GetItem($itemno, $barcode);
113             my $iteminfo = GetBiblioData($item->{biblionumber});
114             for my $field (qw(title isbn itemtype)){
115                 $item->{$field} = $iteminfo->{$field};
116             }
117             if ($itemlevelpref) {
118                 my $iteminfo = GetItem($itemno);
119                 $item->{itemtype} = $iteminfo->{itype}; 
120             }
121 #kind of flakey, though we can be pretty sure the values will be in the same order as in the authloop
122 #have to use this since in html::template::pro i can't access one loop from inside an other,
123 #and variable substitution doesn't work (<!-- TMPL_VAR name="<!-- TMPL_VAR name="foo" -->" -->)
124 #this pushes a list of authorized valuse into each item's hash
125             my $itemauthloop = [];
126             for my $authfield (@$authloop){
127                 my $authvaluename;
128 #looking for the authvalues human-readable form
129                 for my $val (@{$authfield->{values}}){
130                     if( $item->{$authfield->{itemfieldname}} eq $val->{lib} || $item->{$authfield->{itemfieldname}} eq $val->{authorised_value}){
131                         $authvaluename = $val->{lib};
132                     }
133                 }
134                 if ( ! $authvaluename){
135                     $authvaluename = $item->{$authfield->{itemfieldname}}; 
136                 }
137                 push(@$itemauthloop, { 'authvalue' => $authvaluename} );
138             }
139
140             for my $type (@$itemtypes){
141                 if ( $item->{itemtype} eq $type->{itemtype} ) {
142                     $item->{itemtypedesc} = $type->{description};
143                 }
144             }
145             $item->{authloop} = $itemauthloop;
146             push @items, $item;
147             $itemslst .= $item->{'itemnumber'} . ',';
148         }
149     }
150     $template->param( 'itemsloop' => \@items,
151                                         'authloop' => $authloop,
152                                         'branches' => $branches,
153                                         'actions'    => 1,
154                                         'op'            => '1',
155                                         'itemslst'   => $itemslst,
156                                         'itemtypes' => $itemtypes,
157                                       );
158 } elsif ( $invars->{'itemslst'} ) {
159     for my $itemno ( split(',', $invars->{itemslst}) ) {
160                 my $item = GetItem($itemno);
161                 if ( $invars->{'del'} ) {
162                         DelItemCheck(C4::Context->dbh, $item->{'biblionumber'}, $item->{'itemnumber'})
163                 } else {
164                         for my $auth (@$authvals){
165                                 my ($authfieldname, $description, $hashfdname) = @$auth;
166                                 my $authcode = GetAuthValCode($authfieldname);
167                                 if ($authcode && $invars->{$authcode} && $invars->{$authcode} ne '0'){
168                                         $item->{$hashfdname}=$invars->{$authcode};
169                                 }
170                         }
171                         if ($invars->{holdingbranch} && $invars->{holdingbranch} ne '0'){
172                                 $item->{holdingbranch} = $invars->{holdingbranch};
173                         }
174                         if ($invars->{homebranch} && $invars->{homebranch} ne '0'){
175                                 $item->{homebranch} = $invars->{homebranch};
176                         }
177                         if ($invars->{itemtypes} && $invars->{itemtypes} ne '0') {
178                                 $item->{itype} = $invars->{itemtypes};
179                         }
180                         ModItem($item, $item->{biblionumber}, $item->{itemnumber});
181                 }
182     }
183 }
184 $template->param('del' => $input->param('del'));
185 output_html_with_http_headers $input, $cookie, $template->output;