removing unused package
[koha.git] / acqui.simple / 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::Interface::CGI::Output;
27 use C4::Biblio;
28 use C4::Context;
29 use C4::Koha; # XXX subfield_is_koha_internal_p
30 use C4::Search;
31 use HTML::Template;
32 use MARC::File::USMARC;
33
34 sub find_value {
35         my ($tagfield,$insubfield,$record) = @_;
36         my $result;
37         my $indicator;
38         foreach my $field ($record->field($tagfield)) {
39                 my @subfields = $field->subfields();
40                 foreach my $subfield (@subfields) {
41                         if (@$subfield[0] eq $insubfield) {
42                                 $result .= @$subfield[1];
43                                 $indicator = $field->indicator(1).$field->indicator(2);
44                         }
45                 }
46         }
47         return($indicator,$result);
48 }
49 my $input = new CGI;
50 my $dbh = C4::Context->dbh;
51 my $error = $input->param('error');
52 my $biblionumber = $input->param('biblionumber');
53 my $biblioitemnumber = find_biblioitemnumber($dbh,$biblionumber);
54 my $itemnumber = $input->param('itemnumber');
55 my $op = $input->param('op');
56
57 # find itemtype
58 my $itemtype = &MARCfind_frameworkcode($dbh,$biblionumber);
59
60 my $tagslib = &MARCgettagslib($dbh,1,$itemtype);
61 my $record = MARCgetbiblio($dbh,$bibid);
62 # warn "==>".$record->as_formatted;
63 my $oldrecord = MARCmarc2koha($dbh,$record);
64 my $itemrecord;
65 my $nextop="additem";
66 my @errors; # store errors found while checking data BEFORE saving item.
67 #------------------------------------------------------------------------------------------------------------------------------
68 if ($op eq "additem") {
69 #------------------------------------------------------------------------------------------------------------------------------
70         # rebuild
71         my @tags = $input->param('tag');
72         my @subfields = $input->param('subfield');
73         my @values = $input->param('field_value');
74         # build indicator hash.
75         my @ind_tag = $input->param('ind_tag');
76         my @indicator = $input->param('indicator');
77         my %indicators;
78         for (my $i=0;$i<=$#ind_tag;$i++) {
79                 $indicators{$ind_tag[$i]} = $indicator[$i];
80         }
81         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
82         # if autoBarcode is ON, calculate barcode...
83         if (C4::Context->preference('autoBarcode')) {
84                 my ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.barcode");
85                 unless ($record->field($tagfield)->subfield($tagsubfield)) {
86                         my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
87                         $sth_barcode->execute;
88                         my ($newbarcode) = $sth_barcode->fetchrow;
89                         $newbarcode++;
90                         # OK, we have the new barcode, now create the entry in MARC record
91                         my $fieldItem = $record->field($tagfield);
92                         $record->delete_field($fieldItem);
93                         $fieldItem->add_subfields($tagsubfield => $newbarcode);
94                         $record->insert_fields_ordered($fieldItem);
95                 }
96         }
97 # check for item barcode # being unique
98         my $addedolditem = MARCmarc2koha($dbh,$addeditem);
99         my $exists = get_item_from_barcode($addedolditem->{'barcode'});
100         push @errors,"barcode_not_unique" if($exists);
101         # if barcode exists, don't create, but report The problem.
102         $itemnumber = NEWnewitem($dbh,$addeditem,$biblionumber,$biblioitemnumber) unless ($exists);
103         $nextop = "additem";
104 #------------------------------------------------------------------------------------------------------------------------------
105 } elsif ($op eq "edititem") {
106 #------------------------------------------------------------------------------------------------------------------------------
107 # retrieve item if exist => then, it's a modif
108         $itemrecord = MARCgetitem($dbh,$biblionumber,$itemnumber);
109         $nextop="saveitem";
110 #------------------------------------------------------------------------------------------------------------------------------
111 } elsif ($op eq "delitem") {
112 #------------------------------------------------------------------------------------------------------------------------------
113 # retrieve item if exist => then, it's a modif
114         &NEWdelitem($dbh,$biblionumber,$itemnumber);
115         $nextop="additem";
116 #------------------------------------------------------------------------------------------------------------------------------
117 } elsif ($op eq "saveitem") {
118 #------------------------------------------------------------------------------------------------------------------------------
119         # rebuild
120         my @tags = $input->param('tag');
121         my @subfields = $input->param('subfield');
122         my @values = $input->param('field_value');
123         # build indicator hash.
124         my @ind_tag = $input->param('ind_tag');
125         my @indicator = $input->param('indicator');
126 #       my $itemnum = $input->param('itemnum');
127         my %indicators;
128         for (my $i=0;$i<=$#ind_tag;$i++) {
129                 $indicators{$ind_tag[$i]} = $indicator[$i];
130         }
131         my $itemrecord = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
132 # MARC::Record builded => now, record in DB
133 # warn "R: ".$record->as_formatted;
134         my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$record,$bibid,$itemnum,0);
135         $itemnum="";
136         $nextop="additem";
137 }
138
139 #
140 #------------------------------------------------------------------------------------------------------------------------------
141 # build screen with existing items. and "new" one
142 #------------------------------------------------------------------------------------------------------------------------------
143 my ($template, $loggedinuser, $cookie)
144     = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
145                              query => $input,
146                              type => "intranet",
147                              authnotrequired => 0,
148                              flagsrequired => {editcatalogue => 1},
149                              debug => 1,
150                              });
151
152 my %indicators;
153 $indicators{995}='  ';
154 # now, build existiing item list
155 my $temp = MARCgetbiblio($dbh,$bibid);
156 my @fields = $temp->fields();
157 #my @fields = $record->fields();
158 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
159 my @big_array;
160 #---- finds where items.itemnumber is stored
161 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber",$itemtype);
162 my ($branchtagfield,$branchtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.homebranch",$itemtype);
163
164 foreach my $field (@fields) {
165         next if ($field->tag()<10);
166         my @subf=$field->subfields;
167         my %this_row;
168 # loop through each subfield
169         for my $i (0..$#subf) {
170                 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
171                 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
172                 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
173                 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
174                         #verifying rights
175                         my $userenv = C4::Context->userenv;
176                         unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
177                                         $this_row{'nomod'}=1;
178                         }
179                 }
180                 $this_row{itemnum} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
181         }
182         if (%this_row) {
183                 push(@big_array, \%this_row);
184         }
185 }
186 #fill big_row with missing datas
187 foreach my $subfield_code  (keys(%witness)) {
188         for (my $i=0;$i<=$#big_array;$i++) {
189                 $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
190         }
191 }
192 my ($holdingbrtagf,$holdingbrtagsubf) = &MARCfind_marc_from_kohafield($dbh,"items.holdingbranch",$itemtype);
193 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
194
195 # now, construct template !
196 my @item_value_loop;
197 my @header_value_loop;
198 for (my $i=0;$i<=$#big_array; $i++) {
199         my $items_data;
200         foreach my $subfield_code (sort keys(%witness)) {
201                 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
202         }
203         my %row_data;
204         $row_data{item_value} = $items_data;
205         $row_data{itemnum} = $big_array[$i]->{itemnum};
206         #reporting this_row values
207         $row_data{'nomod'} = $big_array[$i]{'nomod'};
208         push(@item_value_loop,\%row_data);
209 }
210 foreach my $subfield_code (sort keys(%witness)) {
211         my %header_value;
212         $header_value{header_value} = $witness{$subfield_code};
213         push(@header_value_loop, \%header_value);
214 }
215
216 # next item form
217 my @loop_data =();
218 my $i=0;
219 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
220
221 foreach my $tag (sort keys %{$tagslib}) {
222         my $previous_tag = '';
223 # loop through each subfield
224         foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
225                 next if subfield_is_koha_internal_p($subfield);
226                 next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
227                 my %subfield_data;
228                 $subfield_data{tag}=$tag;
229                 $subfield_data{subfield}=$subfield;
230 #               $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
231                 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
232                 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
233                 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
234                 my ($x,$value);
235                 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
236                 #testing branch value if IndependantBranches.
237                 my $test = (C4::Context->preference("IndependantBranches")) && 
238                                         ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
239                                         (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
240 #               print $input->redirect(".pl?bibid=$bibid") if ($test);
241                 # search for itemcallnumber if applicable
242                 if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
243                         my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
244                         my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
245                         my $temp = $record->field($CNtag);
246                         if ($temp) {
247                                 $value = $temp->subfield($CNsubfield);
248                         }
249                 }
250                 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
251                         my @authorised_values;
252                         my %authorised_lib;
253                         # builds list, depending on authorised value...
254                         #---- branch
255                         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
256                                 if ((C4::Context->preference("IndependantBranches")) && (C4::Context->userenv->{flags} != 1)){
257                                                 my $sth=$dbh->prepare("select branchcode,branchname from branches where branchcode = ? order by branchname");
258                                                 $sth->execute(C4::Context->userenv->{branch});
259                                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
260                                                 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
261                                                         push @authorised_values, $branchcode;
262                                                         $authorised_lib{$branchcode}=$branchname;
263                                                 }
264                                 } else {
265                                         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
266                                         $sth->execute;
267                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
268                                         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
269                                                 push @authorised_values, $branchcode;
270                                                 $authorised_lib{$branchcode}=$branchname;
271                                         }
272                                 }
273                         #----- itemtypes
274                         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
275                                 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
276                                 $sth->execute;
277                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
278                                 while (my ($itemtype,$description) = $sth->fetchrow_array) {
279                                         push @authorised_values, $itemtype;
280                                         $authorised_lib{$itemtype}=$description;
281                                 }
282                         #---- "true" authorised value
283                         } else {
284                                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
285                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
286                                 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
287                                         push @authorised_values, $value;
288                                         $authorised_lib{$value}=$lib;
289                                 }
290                         }
291                         $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
292                                                                                                                                                 -values=> \@authorised_values,
293                                                                                                                                                 -default=>"$value",
294                                                                                                                                                 -labels => \%authorised_lib,
295                                                                                                                                                 -size=>1,
296                                                                                                                                                 -multiple=>0,
297                                                                                                                                                 );
298                 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
299                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255> <a href=\"javascript:Dopop('../thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>";
300                         #"
301                 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
302                         my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
303                         require $plugin;
304                         my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
305                         my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
306                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255 DISABLE READONLY OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
307                 } else {
308                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
309                 }
310 #               $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
311                 push(@loop_data, \%subfield_data);
312                 $i++
313         }
314 }
315
316 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
317 $template->param(item_loop => \@item_value_loop,
318                                                 item_header_loop => \@header_value_loop,
319                                                 bibid => $bibid,
320                                                 biblionumber =>$oldbiblionumber,
321                                                 title => $oldrecord->{title},
322                                                 author => $oldrecord->{author},
323                                                 item => \@loop_data,
324                                                 itemnumber => $itemnumber,
325                                                 itemtagfield => $itemtagfield,
326                                                 itemtagsubfield =>$itemtagsubfield,
327                                                 op => $nextop,
328                                                 opisadd => ($nextop eq "saveitem")?0:1);
329 foreach my $error (@errors) {
330         $template->param($error => 1);
331 }
332 output_html_with_http_headers $input, $cookie, $template->output;