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