Bug 11861: Add ISSN column in serials claims table
[koha.git] / cataloguing / additem.pl
blob268ef66e21ae0b274ca3f28c80670607a0c6337c
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2004-2010 BibLibre
5 # Parts Copyright Catalyst IT 2011
7 # This file is part of Koha.
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.
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.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 use strict;
23 #use warnings; FIXME - Bug 2505
24 use CGI;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Context;
30 use C4::Circulation;
31 use C4::Koha; # XXX subfield_is_koha_internal_p
32 use C4::Branch; # XXX subfield_is_koha_internal_p
33 use C4::ClassSource;
34 use C4::Dates;
35 use List::MoreUtils qw/any/;
36 use C4::Search;
37 use Storable qw(thaw freeze);
38 use URI::Escape;
41 use MARC::File::XML;
42 use URI::Escape;
44 our $dbh = C4::Context->dbh;
46 sub find_value {
47 my ($tagfield,$insubfield,$record) = @_;
48 my $result;
49 my $indicator;
50 foreach my $field ($record->field($tagfield)) {
51 my @subfields = $field->subfields();
52 foreach my $subfield (@subfields) {
53 if (@$subfield[0] eq $insubfield) {
54 $result .= @$subfield[1];
55 $indicator = $field->indicator(1).$field->indicator(2);
59 return($indicator,$result);
62 sub get_item_from_barcode {
63 my ($barcode)=@_;
64 my $dbh=C4::Context->dbh;
65 my $result;
66 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
67 $rq->execute($barcode);
68 ($result)=$rq->fetchrow;
69 return($result);
72 sub set_item_default_location {
73 my $itemnumber = shift;
74 my $item = GetItem( $itemnumber );
75 if ( C4::Context->preference('NewItemsDefaultLocation') ) {
76 $item->{'permanent_location'} = $item->{'location'};
77 $item->{'location'} = C4::Context->preference('NewItemsDefaultLocation');
78 ModItem( $item, undef, $itemnumber);
80 else {
81 $item->{'permanent_location'} = $item->{'location'} if !defined($item->{'permanent_location'});
82 ModItem( $item, undef, $itemnumber);
86 # NOTE: This code is subject to change in the future with the implemenation of ajax based autobarcode code
87 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
88 sub _increment_barcode {
89 my ($record, $frameworkcode) = @_;
90 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
91 unless ($record->field($tagfield)->subfield($tagsubfield)) {
92 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
93 $sth_barcode->execute;
94 my ($newbarcode) = $sth_barcode->fetchrow;
95 $newbarcode++;
96 # OK, we have the new barcode, now create the entry in MARC record
97 my $fieldItem = $record->field($tagfield);
98 $record->delete_field($fieldItem);
99 $fieldItem->add_subfields($tagsubfield => $newbarcode);
100 $record->insert_fields_ordered($fieldItem);
102 return $record;
106 sub generate_subfield_form {
107 my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $today_iso, $biblionumber, $temp, $loop_data, $i) = @_;
109 my $frameworkcode = &GetFrameworkCode($biblionumber);
110 my %subfield_data;
111 my $dbh = C4::Context->dbh;
113 my $index_subfield = int(rand(1000000));
114 if ($subfieldtag eq '@'){
115 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
116 } else {
117 $subfield_data{id} = "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield;
120 $subfield_data{tag} = $tag;
121 $subfield_data{subfield} = $subfieldtag;
122 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
123 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$subfieldlib->{lib}."\">".$subfieldlib->{lib}."</span>";
124 $subfield_data{mandatory} = $subfieldlib->{mandatory};
125 $subfield_data{repeatable} = $subfieldlib->{repeatable};
126 $subfield_data{maxlength} = $subfieldlib->{maxlength};
128 $value =~ s/"/&quot;/g;
129 if ( ! defined( $value ) || $value eq '') {
130 $value = $subfieldlib->{defaultvalue};
131 # get today date & replace YYYY, MM, DD if provided in the default value
132 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
133 $value =~ s/YYYY/$year/g;
134 $value =~ s/MM/$month/g;
135 $value =~ s/DD/$day/g;
138 $subfield_data{visibility} = "display:none;" if (($subfieldlib->{hidden} > 4) || ($subfieldlib->{hidden} < -4));
140 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
141 if (!$value && $subfieldlib->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
142 my $CNtag = substr($pref_itemcallnumber, 0, 3);
143 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
144 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
145 my $temp2 = $temp->field($CNtag);
146 if ($temp2) {
147 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
148 #remove any trailing space incase one subfield is used
149 $value =~ s/^\s+|\s+$//g;
153 if ($frameworkcode eq 'FA' && $subfieldlib->{kohafield} eq 'items.barcode' && !$value){
154 my $input = new CGI;
155 $value = $input->param('barcode');
157 my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="50" maxlength="$subfield_data{maxlength}" );
158 my $attributes_no_value_textarea = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" rows="5" cols="64" );
159 my $attributes = qq($attributes_no_value value="$value" );
161 if ( $subfieldlib->{authorised_value} ) {
162 my @authorised_values;
163 my %authorised_lib;
164 # builds list, depending on authorised value...
165 if ( $subfieldlib->{authorised_value} eq "branches" ) {
166 foreach my $thisbranch (@$branches) {
167 push @authorised_values, $thisbranch->{value};
168 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
169 $value = $thisbranch->{value} if $thisbranch->{selected} && !$value;
172 elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) {
173 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
174 my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description");
175 $sth->execute;
176 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
177 push @authorised_values, $itemtype;
178 $authorised_lib{$itemtype} = $description;
181 unless ( $value ) {
182 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
183 $itype_sth->execute( $biblionumber );
184 ( $value ) = $itype_sth->fetchrow_array;
187 #---- class_sources
189 elsif ( $subfieldlib->{authorised_value} eq "cn_source" ) {
190 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
192 my $class_sources = GetClassSources();
193 my $default_source = C4::Context->preference("DefaultClassificationSource");
195 foreach my $class_source (sort keys %$class_sources) {
196 next unless $class_sources->{$class_source}->{'used'} or
197 ($value and $class_source eq $value) or
198 ($class_source eq $default_source);
199 push @authorised_values, $class_source;
200 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
202 $value = $default_source unless ($value);
204 #---- "true" authorised value
206 else {
207 push @authorised_values, qq{} unless ( $subfieldlib->{mandatory} );
208 my $av = GetAuthorisedValues( $subfieldlib->{authorised_value} );
209 for my $r ( @$av ) {
210 push @authorised_values, $r->{authorised_value};
211 $authorised_lib{$r->{authorised_value}} = $r->{lib};
215 if ($subfieldlib->{'hidden'}) {
216 $subfield_data{marc_value} = qq(<input type="hidden" $attributes /> $authorised_lib{$value});
218 else {
219 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
220 -name => "field_value",
221 -values => \@authorised_values,
222 -default => $value,
223 -labels => \%authorised_lib,
224 -override => 1,
225 -size => 1,
226 -multiple => 0,
227 -tabindex => 1,
228 -id => "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
229 -class => "input_marceditor",
234 # it's a thesaurus / authority field
235 elsif ( $subfieldlib->{authtypecode} ) {
236 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
237 <a href=\"#\" class=\"buttonDot\"
238 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$subfieldlib->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
241 # it's a plugin field
242 elsif ( $subfieldlib->{value_builder} ) {
243 # opening plugin
244 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $subfieldlib->{'value_builder'};
245 if (do $plugin) {
246 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, $loop_data );
247 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, $loop_data );
248 my $change = index($javascript, 'function Change') > -1 ?
249 "return Change$function_name($subfield_data{random}, '$subfield_data{id}');" :
250 'return 1;';
251 $subfield_data{marc_value} = qq[<input type="text" $attributes
252 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
253 onchange=" $change"
254 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
255 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
256 $javascript];
257 } else {
258 warn "Plugin Failed: $plugin";
259 $subfield_data{marc_value} = "<input type=\"text\" $attributes />"; # supply default input form
262 elsif ( $tag eq '' ) { # it's an hidden field
263 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
265 elsif ( $subfieldlib->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
266 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
268 elsif ( length($value) > 100
269 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
270 300 <= $tag && $tag < 400 && $subfieldtag eq 'a' )
271 or (C4::Context->preference("marcflavour") eq "MARC21" and
272 500 <= $tag && $tag < 600 )
274 # oversize field (textarea)
275 $subfield_data{marc_value} = "<textarea $attributes_no_value_textarea>$value</textarea>\n";
276 } else {
277 # it's a standard field
278 $subfield_data{marc_value} = "<input type=\"text\" $attributes />";
281 return \%subfield_data;
284 # Removes some subfields when prefilling items
285 # This function will remove any subfield that is not in the SubfieldsToUseWhenPrefill syspref
286 sub removeFieldsForPrefill {
288 my $item = shift;
290 # Getting item tag
291 my ($tag, $subtag) = GetMarcFromKohaField("items.barcode", '');
293 # Getting list of subfields to keep
294 my $subfieldsToUseWhenPrefill = C4::Context->preference('SubfieldsToUseWhenPrefill');
296 # Removing subfields that are not in the syspref
297 if ($tag && $subfieldsToUseWhenPrefill) {
298 my $field = $item->field($tag);
299 my @subfieldsToUse= split(/ /,$subfieldsToUseWhenPrefill);
300 foreach my $subfield ($field->subfields()) {
301 if (!grep { $subfield->[0] eq $_ } @subfieldsToUse) {
302 $field->delete_subfield(code => $subfield->[0]);
308 return $item;
312 my $input = new CGI;
313 my $error = $input->param('error');
314 my $biblionumber = $input->param('biblionumber');
315 my $itemnumber = $input->param('itemnumber');
316 my $op = $input->param('op');
317 my $hostitemnumber = $input->param('hostitemnumber');
318 my $marcflavour = C4::Context->preference("marcflavour");
319 my $searchid = $input->param('searchid');
320 # fast cataloguing datas
321 my $fa_circborrowernumber = $input->param('circborrowernumber');
322 my $fa_barcode = $input->param('barcode');
323 my $fa_branch = $input->param('branch');
324 my $fa_stickyduedate = $input->param('stickyduedate');
325 my $fa_duedatespec = $input->param('duedatespec');
327 my $frameworkcode = &GetFrameworkCode($biblionumber);
329 # Defining which userflag is needing according to the framework currently used
330 my $userflags;
331 if (defined $input->param('frameworkcode')) {
332 $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_items";
335 if (not defined $userflags) {
336 $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_items";
339 my ($template, $loggedinuser, $cookie)
340 = get_template_and_user({template_name => "cataloguing/additem.tt",
341 query => $input,
342 type => "intranet",
343 authnotrequired => 0,
344 flagsrequired => {editcatalogue => $userflags},
345 debug => 1,
349 my $today_iso = C4::Dates->today('iso');
350 my $tagslib = &GetMarcStructure(1,$frameworkcode);
351 my $record = GetMarcBiblio($biblionumber);
352 my $oldrecord = TransformMarcToKoha($dbh,$record);
353 my $itemrecord;
354 my $nextop="additem";
355 my @errors; # store errors found while checking data BEFORE saving item.
357 # Getting last created item cookie
358 my $prefillitem = C4::Context->preference('PrefillItem');
359 my $justaddeditem;
360 my $cookieitemrecord;
361 if ($prefillitem) {
362 my $lastitemcookie = $input->cookie('LastCreatedItem');
363 if ($lastitemcookie) {
364 $lastitemcookie = uri_unescape($lastitemcookie);
365 if ( thaw($lastitemcookie) ) {
366 $cookieitemrecord = thaw($lastitemcookie) ;
367 $cookieitemrecord = removeFieldsForPrefill($cookieitemrecord);
372 #-------------------------------------------------------------------------------
373 if ($op eq "additem") {
375 #-------------------------------------------------------------------------------
376 # rebuild
377 my @tags = $input->param('tag');
378 my @subfields = $input->param('subfield');
379 my @values = $input->param('field_value');
380 # build indicator hash.
381 my @ind_tag = $input->param('ind_tag');
382 my @indicator = $input->param('indicator');
383 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
384 my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
386 # type of add
387 my $add_submit = $input->param('add_submit');
388 my $add_duplicate_submit = $input->param('add_duplicate_submit');
389 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
390 my $number_of_copies = $input->param('number_of_copies');
392 # This is a bit tricky : if there is a cookie for the last created item and
393 # we just added an item, the cookie value is not correct yet (it will be updated
394 # next page). To prevent the form from being filled with outdated values, we
395 # force the use of "add and duplicate" feature, so the form will be filled with
396 # correct values.
397 $add_duplicate_submit = 1 if ($prefillitem);
398 $justaddeditem = 1;
400 # if autoBarcode is set to 'incremental', calculate barcode...
401 if ( C4::Context->preference('autoBarcode') eq 'incremental' ) {
402 $record = _increment_barcode($record, $frameworkcode);
405 my $addedolditem = TransformMarcToKoha( $dbh, $record );
407 # If we have to add or add & duplicate, we add the item
408 if ( $add_submit || $add_duplicate_submit ) {
410 # check for item barcode # being unique
411 my $exist_itemnumber = get_item_from_barcode( $addedolditem->{'barcode'} );
412 push @errors, "barcode_not_unique" if ($exist_itemnumber);
414 # if barcode exists, don't create, but report The problem.
415 unless ($exist_itemnumber) {
416 my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) = AddItemFromMarc( $record, $biblionumber );
417 set_item_default_location($oldbibitemnum);
419 # Pushing the last created item cookie back
420 if ($prefillitem && defined $record) {
421 my $itemcookie = $input->cookie(
422 -name => 'LastCreatedItem',
423 # We uri_escape the whole freezed structure so we're sure we won't have any encoding problems
424 -value => uri_escape_utf8( freeze( $record ) ),
425 -HttpOnly => 1,
426 -expires => ''
429 $cookie = [ $cookie, $itemcookie ];
433 $nextop = "additem";
434 if ($exist_itemnumber) {
435 $itemrecord = $record;
439 # If we have to add & duplicate
440 if ($add_duplicate_submit) {
441 $itemrecord = $record;
442 if (C4::Context->preference('autoBarcode') eq 'incremental') {
443 $itemrecord = _increment_barcode($itemrecord, $frameworkcode);
445 else {
446 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
447 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
448 my $fieldItem = $itemrecord->field($tagfield);
449 $itemrecord->delete_field($fieldItem);
450 $fieldItem->delete_subfields($tagsubfield);
451 $itemrecord->insert_fields_ordered($fieldItem);
453 $itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem);
456 # If we have to add multiple copies
457 if ($add_multiple_copies_submit) {
459 use C4::Barcodes;
460 my $barcodeobj = C4::Barcodes->new;
461 my $oldbarcode = $addedolditem->{'barcode'};
462 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
464 # If there is a barcode and we can't find him new values, we can't add multiple copies
465 my $testbarcode;
466 $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
467 if ($oldbarcode && !$testbarcode) {
469 push @errors, "no_next_barcode";
470 $itemrecord = $record;
472 } else {
473 # We add each item
475 # For the first iteration
476 my $barcodevalue = $oldbarcode;
477 my $exist_itemnumber;
480 for (my $i = 0; $i < $number_of_copies;) {
482 # If there is a barcode
483 if ($barcodevalue) {
485 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
486 $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
488 # Putting it into the record
489 if ($barcodevalue) {
490 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
493 # Checking if the barcode already exists
494 $exist_itemnumber = get_item_from_barcode($barcodevalue);
497 # Adding the item
498 if (!$exist_itemnumber) {
499 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
500 set_item_default_location($oldbibitemnum);
502 # We count the item only if it was really added
503 # That way, all items are added, even if there was some already existing barcodes
504 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
505 $i++;
508 # Preparing the next iteration
509 $oldbarcode = $barcodevalue;
511 undef($itemrecord);
514 if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
515 print $input->redirect(
516 '/cgi-bin/koha/circ/circulation.pl?'
517 .'borrowernumber='.$fa_circborrowernumber
518 .'&barcode='.uri_escape($fa_barcode)
519 .'&duedatespec='.$fa_duedatespec
520 .'&stickyduedate=1'
522 exit;
526 #-------------------------------------------------------------------------------
527 } elsif ($op eq "edititem") {
528 #-------------------------------------------------------------------------------
529 # retrieve item if exist => then, it's a modif
530 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
531 $nextop = "saveitem";
532 #-------------------------------------------------------------------------------
533 } elsif ($op eq "delitem") {
534 #-------------------------------------------------------------------------------
535 # check that there is no issue on this item before deletion.
536 $error = &DelItemCheck($dbh,$biblionumber,$itemnumber);
537 if($error == 1){
538 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
539 }else{
540 push @errors,$error;
541 $nextop="additem";
543 #-------------------------------------------------------------------------------
544 } elsif ($op eq "delallitems") {
545 #-------------------------------------------------------------------------------
546 my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
547 my $errortest=0;
548 my $itemfail;
549 foreach my $biblioitem (@biblioitems) {
550 my $items = &GetItemsByBiblioitemnumber( $biblioitem->{biblioitemnumber} );
552 foreach my $item (@$items) {
553 $error =&DelItemCheck( $dbh, $biblionumber, $item->{itemnumber} );
554 $itemfail =$item;
555 if($error == 1){
556 next
558 else {
559 push @errors,$error;
560 $errortest++
563 if($errortest > 0){
564 $nextop="additem";
566 else {
567 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
568 my $views = { C4::Search::enabled_staff_search_views };
569 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
570 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
571 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
572 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
573 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
574 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
575 } else {
576 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
578 exit;
581 #-------------------------------------------------------------------------------
582 } elsif ($op eq "saveitem") {
583 #-------------------------------------------------------------------------------
584 # rebuild
585 my @tags = $input->param('tag');
586 my @subfields = $input->param('subfield');
587 my @values = $input->param('field_value');
588 # build indicator hash.
589 my @ind_tag = $input->param('ind_tag');
590 my @indicator = $input->param('indicator');
591 # my $itemnumber = $input->param('itemnumber');
592 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
593 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
594 # MARC::Record builded => now, record in DB
595 # warn "R: ".$record->as_formatted;
596 # check that the barcode don't exist already
597 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
598 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
599 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
600 push @errors,"barcode_not_unique";
601 } else {
602 ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
603 $itemnumber="";
605 my $item = GetItem( $itemnumber );
606 my $olditemlost = $item->{'itemlost'};
608 my ($lost_tag,$lost_subfield) = GetMarcFromKohaField("items.itemlost",'');
610 my $newitemlost = $itemtosave->subfield( $lost_tag, $lost_subfield );
611 if (($olditemlost eq '0' or $olditemlost eq '' ) and $newitemlost ge '1'){
612 LostItem($itemnumber,'MARK RETURNED');
614 $nextop="additem";
615 } elsif ($op eq "delinkitem"){
616 my $analyticfield = '773';
617 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC'){
618 $analyticfield = '773';
619 } elsif ($marcflavour eq 'UNIMARC') {
620 $analyticfield = '461';
622 foreach my $field ($record->field($analyticfield)){
623 if ($field->subfield('9') eq $hostitemnumber){
624 $record->delete_field($field);
625 last;
628 my $modbibresult = ModBiblio($record, $biblionumber,'');
632 #-------------------------------------------------------------------------------
633 # build screen with existing items. and "new" one
634 #-------------------------------------------------------------------------------
636 # now, build existiing item list
637 my $temp = GetMarcBiblio( $biblionumber );
638 #my @fields = $record->fields();
641 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
642 my @big_array;
643 #---- finds where items.itemnumber is stored
644 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
645 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
646 C4::Biblio::EmbedItemsInMarcBiblio($temp, $biblionumber);
647 my @fields = $temp->fields();
650 my @hostitemnumbers;
651 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
652 my $analyticfield = '773';
653 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
654 $analyticfield = '773';
655 } elsif ($marcflavour eq 'UNIMARC') {
656 $analyticfield = '461';
658 foreach my $hostfield ($temp->field($analyticfield)){
659 my $hostbiblionumber = $hostfield->subfield('0');
660 if ($hostbiblionumber){
661 my $hostrecord = GetMarcBiblio($hostbiblionumber, 1);
662 if ($hostrecord) {
663 my ($itemfield, undef) = GetMarcFromKohaField( 'items.itemnumber', GetFrameworkCode($hostbiblionumber) );
664 foreach my $hostitem ($hostrecord->field($itemfield)){
665 if ($hostitem->subfield('9') eq $hostfield->subfield('9')){
666 push (@fields, $hostitem);
667 push (@hostitemnumbers, $hostfield->subfield('9'));
676 foreach my $field (@fields) {
677 next if ( $field->tag() < 10 );
679 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
680 my %this_row;
681 # loop through each subfield
682 my $i = 0;
683 foreach my $subfield (@subf){
684 my $subfieldcode = $subfield->[0];
685 my $subfieldvalue= $subfield->[1];
687 next if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} ne 10
688 && ($field->tag() ne $itemtagfield
689 && $subfieldcode ne $itemtagsubfield));
690 $witness{$subfieldcode} = $tagslib->{$field->tag()}->{$subfieldcode}->{lib} if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10);
691 if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10) {
692 $this_row{$subfieldcode} .= " | " if($this_row{$subfieldcode});
693 $this_row{$subfieldcode} .= GetAuthorisedValueDesc( $field->tag(),
694 $subfieldcode, $subfieldvalue, '', $tagslib)
695 || $subfieldvalue;
698 if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependentBranches")) {
699 #verifying rights
700 my $userenv = C4::Context->userenv();
701 unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $subfieldvalue))){
702 $this_row{'nomod'} = 1;
705 $this_row{itemnumber} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield);
707 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
708 foreach my $hostitemnumber (@hostitemnumbers){
709 if ($this_row{itemnumber} eq $hostitemnumber){
710 $this_row{hostitemflag} = 1;
711 $this_row{hostbiblionumber}= GetBiblionumberFromItemnumber($hostitemnumber);
712 last;
716 # my $countanalytics=GetAnalyticsCount($this_row{itemnumber});
717 # if ($countanalytics > 0){
718 # $this_row{countanalytics} = $countanalytics;
723 if (%this_row) {
724 push(@big_array, \%this_row);
728 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
729 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
731 # now, construct template !
732 # First, the existing items for display
733 my @item_value_loop;
734 my @header_value_loop;
735 for my $row ( @big_array ) {
736 my %row_data;
737 my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
738 $row_data{item_value} = [ @item_fields ];
739 $row_data{itemnumber} = $row->{itemnumber};
740 #reporting this_row values
741 $row_data{'nomod'} = $row->{'nomod'};
742 $row_data{'hostitemflag'} = $row->{'hostitemflag'};
743 $row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'};
744 # $row_data{'countanalytics'} = $row->{'countanalytics'};
745 push(@item_value_loop,\%row_data);
747 foreach my $subfield_code (sort keys(%witness)) {
748 my %header_value;
749 $header_value{header_value} = $witness{$subfield_code};
750 push(@header_value_loop, \%header_value);
753 # now, build the item form for entering a new item
754 my @loop_data =();
755 my $i=0;
757 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
759 my $onlymine =
760 C4::Context->preference('IndependentBranches')
761 && C4::Context->userenv
762 && !C4::Context->IsSuperLibrarian()
763 && C4::Context->userenv->{branch};
764 my $branch = $input->param('branch') || C4::Context->userenv->{branch};
765 my $branches = GetBranchesLoop($branch,$onlymine); # build once ahead of time, instead of multiple times later.
767 # We generate form, from actuel record
768 @fields = ();
769 if($itemrecord){
770 foreach my $field ($itemrecord->fields()){
771 my $tag = $field->{_tag};
772 foreach my $subfield ( $field->subfields() ){
774 my $subfieldtag = $subfield->[0];
775 my $value = $subfield->[1];
776 my $subfieldlib = $tagslib->{$tag}->{$subfieldtag};
778 next if subfield_is_koha_internal_p($subfieldtag);
779 next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10");
781 my $subfield_data = generate_subfield_form($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i);
783 push @fields, "$tag$subfieldtag";
784 push (@loop_data, $subfield_data);
785 $i++;
790 # and now we add fields that are empty
792 # Using last created item if it exists
794 $itemrecord = $cookieitemrecord if ($prefillitem and not $justaddeditem and $op ne "edititem");
796 # We generate form, and fill with values if defined
797 foreach my $tag ( keys %{$tagslib}){
798 foreach my $subtag (keys %{$tagslib->{$tag}}){
799 next if subfield_is_koha_internal_p($subtag);
800 next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10");
801 next if any { /^$tag$subtag$/ } @fields;
803 my @values = (undef);
804 @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)) && defined($itemrecord->field($tag)->subfield($subtag)));
805 for my $value (@values){
806 my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i);
807 push (@loop_data, $subfield_data);
808 $i++;
812 @loop_data = sort {$a->{subfield} cmp $b->{subfield} } @loop_data;
814 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
815 $template->param( title => $record->title() ) if ($record ne "-1");
816 $template->param(
817 biblionumber => $biblionumber,
818 title => $oldrecord->{title},
819 author => $oldrecord->{author},
820 item_loop => \@item_value_loop,
821 item_header_loop => \@header_value_loop,
822 item => \@loop_data,
823 itemnumber => $itemnumber,
824 barcode => GetBarcodeFromItemnumber($itemnumber),
825 itemtagfield => $itemtagfield,
826 itemtagsubfield => $itemtagsubfield,
827 op => $nextop,
828 opisadd => ($nextop eq "saveitem") ? 0 : 1,
829 popup => $input->param('popup') ? 1: 0,
830 C4::Search::enabled_staff_search_views,
832 $template->{'VARS'}->{'searchid'} = $searchid;
834 if ($frameworkcode eq 'FA'){
835 # fast cataloguing datas
836 $template->param(
837 'circborrowernumber' => $fa_circborrowernumber,
838 'barcode' => $fa_barcode,
839 'branch' => $fa_branch,
840 'stickyduedate' => $fa_stickyduedate,
841 'duedatespec' => $fa_duedatespec,
845 foreach my $error (@errors) {
846 $template->param($error => 1);
848 output_html_with_http_headers $input, $cookie, $template->output;