This commit fixes a bug in biblio encoding. things were mixed up when reporting item...
[koha.git] / cataloguing / additem.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use CGI;
23 use strict;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Biblio;
27 use C4::Context;
28 use C4::Koha; # XXX subfield_is_koha_internal_p
29
30 use MARC::File::XML;
31
32 sub find_value {
33     my ($tagfield,$insubfield,$record) = @_;
34     my $result;
35     my $indicator;
36     foreach my $field ($record->field($tagfield)) {
37         my @subfields = $field->subfields();
38         foreach my $subfield (@subfields) {
39             if (@$subfield[0] eq $insubfield) {
40                 $result .= @$subfield[1];
41                 $indicator = $field->indicator(1).$field->indicator(2);
42             }
43         }
44     }
45     return($indicator,$result);
46 }
47
48 sub get_item_from_barcode {
49     my ($barcode)=@_;
50     my $dbh=C4::Context->dbh;
51     my $result;
52     my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
53     $rq->execute($barcode);
54     ($result)=$rq->fetchrow;
55     return($result);
56 }
57
58 my $input = new CGI;
59 my $dbh = C4::Context->dbh;
60 my $error = $input->param('error');
61 my $biblionumber = $input->param('biblionumber');
62 my $itemnumber = $input->param('itemnumber');
63
64 my $op = $input->param('op');
65
66 # find itemtype
67 my $frameworkcode = &GetFrameworkCode($biblionumber);
68
69 my $tagslib = &GetMarcStructure(1,$frameworkcode);
70 my $record = GetMarcBiblio($biblionumber);
71 my $oldrecord = TransformMarcToKoha($dbh,$record);
72 my $itemrecord;
73 my $nextop="additem";
74 my @errors; # store errors found while checking data BEFORE saving item.
75 #-------------------------------------------------------------------------------
76 if ($op eq "additem") {
77 #-------------------------------------------------------------------------------
78     # rebuild
79     my @tags = $input->param('tag');
80     my @subfields = $input->param('subfield');
81     my @values = $input->param('field_value');
82     # build indicator hash.
83     my @ind_tag = $input->param('ind_tag');
84     my @indicator = $input->param('indicator');
85     my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
86         my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
87     # if autoBarcode is ON, calculate barcode...
88     if (C4::Context->preference('autoBarcode')) {
89         my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
90         unless ($record->field($tagfield)->subfield($tagsubfield)) {
91             my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
92             $sth_barcode->execute;
93             my ($newbarcode) = $sth_barcode->fetchrow;
94             $newbarcode++;
95             # OK, we have the new barcode, now create the entry in MARC record
96             my $fieldItem = $record->field($tagfield);
97             $record->delete_field($fieldItem);
98             $fieldItem->add_subfields($tagsubfield => $newbarcode);
99             $record->insert_fields_ordered($fieldItem);
100         }
101     }
102 # check for item barcode # being unique
103     my $addedolditem = TransformMarcToKoha($dbh,$record);
104     my $exists = get_item_from_barcode($addedolditem->{'barcode'});
105     push @errors,"barcode_not_unique" if($exists);
106     # if barcode exists, don't create, but report The problem.
107     my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItem($record,$biblionumber) unless ($exists);
108     if ($exists) {
109     $nextop = "additem";
110         $itemrecord = $record;
111     } else {
112         $nextop = "additem";
113     }
114 #-------------------------------------------------------------------------------
115 } elsif ($op eq "edititem") {
116 #-------------------------------------------------------------------------------
117 # retrieve item if exist => then, it's a modif
118     $itemrecord = GetMarcItem($biblionumber,$itemnumber);
119     $nextop="saveitem";
120 #-------------------------------------------------------------------------------
121 } elsif ($op eq "delitem") {
122 #-------------------------------------------------------------------------------
123     # check that there is no issue on this item before deletion.
124     my $sth=$dbh->prepare("select * from issues i where i.returndate is null and i.itemnumber=?");
125     $sth->execute($itemnumber);
126     my $onloan=$sth->fetchrow;
127     push @errors,"book_on_loan" if ($onloan); ##error book_on_loan added to template as well
128     if ($onloan){
129     $nextop="additem";
130     } else {
131         &DelItem($biblionumber,$itemnumber);
132         print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
133         #$nextop="additem";
134     }
135 #-------------------------------------------------------------------------------
136 } elsif ($op eq "saveitem") {
137 #-------------------------------------------------------------------------------
138     # rebuild
139     my @tags = $input->param('tag');
140     my @subfields = $input->param('subfield');
141     my @values = $input->param('field_value');
142     # build indicator hash.
143     my @ind_tag = $input->param('ind_tag');
144     my @indicator = $input->param('indicator');
145 #    my $itemnumber = $input->param('itemnumber');
146     my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
147         my $itemrecord=MARC::Record::new_from_xml($xml, 'UTF-8');
148 # MARC::Record builded => now, record in DB
149 # warn "R: ".$record->as_formatted;
150     my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItem($itemrecord,$biblionumber,$itemnumber,0);
151     $itemnumber="";
152     $nextop="additem";
153 }
154
155 #
156 #-------------------------------------------------------------------------------
157 # build screen with existing items. and "new" one
158 #-------------------------------------------------------------------------------
159 my ($template, $loggedinuser, $cookie)
160     = get_template_and_user({template_name => "cataloguing/additem.tmpl",
161                  query => $input,
162                  type => "intranet",
163                  authnotrequired => 0,
164                  flagsrequired => {editcatalogue => 1},
165                  debug => 1,
166                  });
167
168 my %indicators;
169 $indicators{995}='  ';
170 # now, build existiing item list
171 my $temp = GetMarcBiblio( $biblionumber );
172 my @fields = $temp->fields();
173 #my @fields = $record->fields();
174 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
175 my @big_array;
176 #---- finds where items.itemnumber is stored
177 my ($itemtagfield,$itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber",$frameworkcode);
178 my ($branchtagfield,$branchtagsubfield) = &GetMarcFromKohaField("items.homebranch",$frameworkcode);
179
180 foreach my $field (@fields) {
181     next if ($field->tag()<10);
182     my @subf=$field->subfields;
183     my %this_row;
184 # loop through each subfield
185     for my $i (0..$#subf) {
186         next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
187         $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
188         $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
189         if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
190             #verifying rights
191             my $userenv = C4::Context->userenv;
192             unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
193                     $this_row{'nomod'}=1;
194             }
195         }
196         $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
197     }
198     if (%this_row) {
199         push(@big_array, \%this_row);
200     }
201 }
202 #fill big_row with missing datas
203 foreach my $subfield_code  (keys(%witness)) {
204     for (my $i=0;$i<=$#big_array;$i++) {
205         $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
206     }
207 }
208 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
209 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
210
211 # now, construct template !
212 my @item_value_loop;
213 my @header_value_loop;
214 for (my $i=0;$i<=$#big_array; $i++) {
215     my $items_data;
216     foreach my $subfield_code (sort keys(%witness)) {
217         $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
218     }
219     my %row_data;
220     $items_data =~ s/"/&quot;/g;
221     $row_data{item_value} = $items_data;
222     $row_data{itemnumber} = $big_array[$i]->{itemnumber};
223     #reporting this_row values
224     $row_data{'nomod'} = $big_array[$i]{'nomod'};
225     push(@item_value_loop,\%row_data);
226 }
227 foreach my $subfield_code (sort keys(%witness)) {
228     my %header_value;
229     $header_value{header_value} = $witness{$subfield_code};
230     push(@header_value_loop, \%header_value);
231 }
232
233 # next item form
234 my @loop_data =();
235 my $i=0;
236 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
237
238 foreach my $tag (sort keys %{$tagslib}) {
239   my $previous_tag = '';
240 # loop through each subfield
241   foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
242     next if subfield_is_koha_internal_p($subfield);
243     next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
244     my %subfield_data;
245     $subfield_data{tag}=$tag;
246     $subfield_data{subfield}=$subfield;
247 #        $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
248     $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".substr($tagslib->{$tag}->{$subfield}->{lib},0,12)."</span>";
249     $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
250     $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
251     $subfield_data{hidden}= "display:none" if $tagslib->{$tag}->{$subfield}->{hidden};
252     my ($x,$value);
253     ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
254     $value =~ s/"/&quot;/g;
255     #testing branch value if IndependantBranches.
256     my $test = (C4::Context->preference("IndependantBranches")) &&
257               ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
258               (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
259 #         print $input->redirect(".pl?biblionumber=$biblionumber") if ($test);
260         # search for itemcallnumber if applicable
261     if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
262       my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
263       my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
264                         my $CNsubfield2 = substr(C4::Context->preference('itemcallnumber'),4,1);
265                         my $temp2 = $temp->field($CNtag);
266                         if ($temp2) {
267                                 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
268 #remove any trailing space incase one subfield is used
269         $value=~s/^\s+|\s+$//g;
270       }
271     }
272     if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
273       my @authorised_values;
274       my %authorised_lib;
275       # builds list, depending on authorised value...
276       #---- branch
277       if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
278         if ((C4::Context->preference("IndependantBranches")) && (C4::Context->userenv->{flags} != 1)){
279           my $sth=$dbh->prepare("select branchcode,branchname from branches where branchcode = ? order by branchname");
280           $sth->execute(C4::Context->userenv->{branch});
281           push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
282           while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
283               push @authorised_values, $branchcode;
284               $authorised_lib{$branchcode}=$branchname;
285           }
286         } else {
287           my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
288           $sth->execute;
289           push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
290           while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
291               push @authorised_values, $branchcode;
292               $authorised_lib{$branchcode}=$branchname;
293           }
294         }
295         #----- itemtypes
296         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
297             my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
298             $sth->execute;
299             push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
300             while (my ($itemtype,$description) = $sth->fetchrow_array) {
301                 push @authorised_values, $itemtype;
302                 $authorised_lib{$itemtype}=$description;
303             }
304       #---- "true" authorised value
305       } else {
306           $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
307           push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
308           while (my ($authvalue,$lib) = $authorised_values_sth->fetchrow_array) {
309               push @authorised_values, $authvalue;
310               $authorised_lib{$authvalue}=$lib;
311           }
312       }
313       $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
314                                                                   -values=> \@authorised_values,
315                                                                   -default=>"$value",
316                                                                   -labels => \%authorised_lib,
317                                                                   -size=>1,
318                                                                     -tabindex=>'',
319                                                                   -multiple=>0,
320                                                                   );
321     } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
322       $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255> <a href=\"javascript:Dopop('cataloguing/thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>";
323           #"
324     } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
325       my $plugin="value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
326       require $plugin;
327       my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
328       my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
329       $subfield_data{marc_value}="<input type=\"text\" value=\"$value\" name=\"field_value\"  size=47 maxlength=255 OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
330     } else {
331       $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
332     }
333 #        $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
334         push(@loop_data, \%subfield_data);
335         $i++
336     }
337 }
338
339 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
340 $template->param( title => $record->title() ) if ($record ne "-1");
341 $template->param(item_loop => \@item_value_loop,
342                         item_header_loop => \@header_value_loop,
343                         biblionumber => $biblionumber,
344                         title => $oldrecord->{title},
345                         author => $oldrecord->{author},
346                         item => \@loop_data,
347                         itemnumber => $itemnumber,
348                         itemtagfield => $itemtagfield,
349                         itemtagsubfield =>$itemtagsubfield,
350                         op => $nextop,
351                         opisadd => ($nextop eq "saveitem")?0:1);
352 foreach my $error (@errors) {
353     $template->param($error => 1);
354 }
355 output_html_with_http_headers $input, $cookie, $template->output;