]> git.koha-community.org Git - koha.git/blob - acqui.simple/additem.pl
fix for 565 : item barcode not checked unique
[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 $bibid = $input->param('bibid');
53 my $oldbiblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
54
55 my $op = $input->param('op');
56 my $itemnum = $input->param('itemnum');
57
58 my $tagslib = &MARCgettagslib($dbh,1);
59 my $record = MARCgetbiblio($dbh,$bibid);
60 my $itemrecord;
61 my $nextop="additem";
62 my @errors; # store errors found while checking data BEFORE saving item.
63 #------------------------------------------------------------------------------------------------------------------------------
64 if ($op eq "additem") {
65 #------------------------------------------------------------------------------------------------------------------------------
66         # rebuild
67         my @tags = $input->param('tag');
68         my @subfields = $input->param('subfield');
69         my @values = $input->param('field_value');
70         # build indicator hash.
71         my @ind_tag = $input->param('ind_tag');
72         my @indicator = $input->param('indicator');
73         my %indicators;
74         for (my $i=0;$i<=$#ind_tag;$i++) {
75                 $indicators{$ind_tag[$i]} = $indicator[$i];
76         }
77         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
78 # check for item barcode # being unique
79         my $oldbibid = MARCmarc2koha($dbh,$record);
80         my $exists = itemdata($oldbibid->{'barcode'});
81         push @errors,"barcode_not_unique" if($exists);
82 # MARC::Record builded => now, record in DB
83         # if barcode exists, don't create, but report The problem.
84         my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWnewitem($dbh,$record,$bibid) unless ($exists);
85         if ($exists) {
86                 $nextop = "additem";
87                 $itemrecord = $record;
88                 warn "==>".$record->as_formatted;
89         } else {
90                 $nextop = "additem";
91         }
92 #------------------------------------------------------------------------------------------------------------------------------
93 } elsif ($op eq "edititem") {
94 #------------------------------------------------------------------------------------------------------------------------------
95 # retrieve item if exist => then, it's a modif
96         $itemrecord = MARCgetitem($dbh,$bibid,$itemnum);
97         $nextop="saveitem";
98 #------------------------------------------------------------------------------------------------------------------------------
99 } elsif ($op eq "saveitem") {
100 #------------------------------------------------------------------------------------------------------------------------------
101         # rebuild
102         my @tags = $input->param('tag');
103         my @subfields = $input->param('subfield');
104         my @values = $input->param('field_value');
105         # build indicator hash.
106         my @ind_tag = $input->param('ind_tag');
107         my @indicator = $input->param('indicator');
108 #       my $itemnum = $input->param('itemnum');
109         my %indicators;
110         for (my $i=0;$i<=$#ind_tag;$i++) {
111                 $indicators{$ind_tag[$i]} = $indicator[$i];
112         }
113         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
114 # MARC::Record builded => now, record in DB
115         my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$record,$bibid,$itemnum,0);
116         $itemnum="";
117         $nextop="additem";
118 }
119
120 #
121 #------------------------------------------------------------------------------------------------------------------------------
122 # build screen with existing items. and "new" one
123 #------------------------------------------------------------------------------------------------------------------------------
124
125 my %indicators;
126 $indicators{995}='  ';
127 # now, build existiing item list
128 my $temp = MARCgetbiblio($dbh,$bibid);
129 my @fields = $temp->fields();
130 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
131 my @big_array;
132 #---- finds where items.itemnumber is stored
133 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber");
134 my @itemnums; # array to store itemnums
135 foreach my $field (@fields) {
136         next if ($field->tag()<10);
137         my @subf=$field->subfields;
138         my %this_row;
139 # loop through each subfield
140         for my $i (0..$#subf) {
141                 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
142                 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
143                 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
144                 push @itemnums,$this_row{$subf[$i][0]} =$subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
145         }
146         if (%this_row) {
147                 push(@big_array, \%this_row);
148         }
149 }
150 #fill big_row with missing datas
151 foreach my $subfield_code  (keys(%witness)) {
152         for (my $i=0;$i<=$#big_array;$i++) {
153                 $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
154         }
155 }
156 # now, construct template !
157 my @item_value_loop;
158 my @header_value_loop;
159 for (my $i=0;$i<=$#big_array; $i++) {
160         my $items_data;
161         foreach my $subfield_code (sort keys(%witness)) {
162                 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
163         }
164         my %row_data;
165         $row_data{item_value} = $items_data;
166         $row_data{itemnum} = $itemnums[$i];
167         push(@item_value_loop,\%row_data);
168 }
169 foreach my $subfield_code (sort keys(%witness)) {
170         my %header_value;
171         $header_value{header_value} = $witness{$subfield_code};
172         push(@header_value_loop, \%header_value);
173 }
174
175 # next item form
176 my @loop_data =();
177 my $i=0;
178 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by authorised_value");
179
180 foreach my $tag (sort keys %{$tagslib}) {
181         my $previous_tag = '';
182 # loop through each subfield
183         foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
184                 next if subfield_is_koha_internal_p($subfield);
185                 next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
186                 my %subfield_data;
187                 $subfield_data{tag}=$tag;
188                 $subfield_data{subfield}=$subfield;
189 #               $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
190                 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
191                 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
192                 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
193                 my ($x,$value);
194                 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
195                 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
196                         my @authorised_values;
197                         my %authorised_lib;
198                         # builds list, depending on authorised value...
199                         #---- branch
200                         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
201                                 my $sth=$dbh->prepare("select branchcode,branchname from branches");
202                                 $sth->execute;
203                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
204                                 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
205                                         push @authorised_values, $branchcode;
206                                         $authorised_lib{$branchcode}=$branchname;
207                                 }
208                         #----- itemtypes
209                         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
210                                 my $sth=$dbh->prepare("select itemtype,description from itemtypes");
211                                 $sth->execute;
212                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
213                                 while (my ($itemtype,$description) = $sth->fetchrow_array) {
214                                         push @authorised_values, $itemtype;
215                                         $authorised_lib{$itemtype}=$description;
216                                 }
217                         #---- "true" authorised value
218                         } else {
219                                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
220                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
221                                 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
222                                         push @authorised_values, $value;
223                                         $authorised_lib{$value}=$lib;
224                                 }
225                         }
226                         $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
227                                                                                                                                                 -values=> \@authorised_values,
228                                                                                                                                                 -default=>"$value",
229                                                                                                                                                 -labels => \%authorised_lib,
230                                                                                                                                                 -size=>1,
231                                                                                                                                                 -multiple=>0,
232                                                                                                                                                 );
233                 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
234                         $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>";
235                 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
236                         my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
237                         require $plugin;
238                         my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
239                         my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
240                         $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";
241                 } else {
242                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
243                 }
244 #               $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
245                 push(@loop_data, \%subfield_data);
246                 $i++
247         }
248 }
249 my ($template, $loggedinuser, $cookie)
250     = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
251                              query => $input,
252                              type => "intranet",
253                              authnotrequired => 0,
254                              flagsrequired => {parameters => 1},
255                              debug => 1,
256                              });
257
258 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
259 $template->param(item_loop => \@item_value_loop,
260                                                 item_header_loop => \@header_value_loop,
261                                                 bibid => $bibid,
262                                                 biblionumber =>$oldbiblionumber,
263                                                 item => \@loop_data,
264                                                 itemnum => $itemnum,
265                                                 itemtagfield => $itemtagfield,
266                                                 itemtagsubfield =>$itemtagsubfield,
267                                                 op => $nextop,
268                                                 opisadd => ($nextop eq "saveitem")?0:1);
269 foreach my $error (@errors) {
270         $template->param($error => 1);
271 }
272 output_html_with_http_headers $input, $cookie, $template->output;