View | Details | Raw Unified | Return to bug 27526
Collapse All | Expand All

(-)a/cataloguing/additem.pl (-110 / +138 lines)
Lines 40-45 use C4::Context; Link Here
40
use C4::Circulation qw( LostItem );
40
use C4::Circulation qw( LostItem );
41
use C4::Koha qw( GetAuthorisedValues );
41
use C4::Koha qw( GetAuthorisedValues );
42
use C4::ClassSource qw( GetClassSources GetClassSource );
42
use C4::ClassSource qw( GetClassSources GetClassSource );
43
use C4::Barcodes;
44
use C4::Barcodes::ValueBuilder;
43
use Koha::DateUtils qw( dt_from_string );
45
use Koha::DateUtils qw( dt_from_string );
44
use Koha::Items;
46
use Koha::Items;
45
use Koha::ItemTypes;
47
use Koha::ItemTypes;
Lines 485-504 if ($prefillitem) { Link Here
485
}
487
}
486
488
487
#-------------------------------------------------------------------------------
489
#-------------------------------------------------------------------------------
490
my $current_item;
488
if ($op eq "additem") {
491
if ($op eq "additem") {
489
492
490
    #-------------------------------------------------------------------------------
491
    # rebuild
492
    my @tags      = $input->multi_param('tag');
493
    my @subfields = $input->multi_param('subfield');
494
    my @values    = $input->multi_param('field_value');
495
    # build indicator hash.
496
    my @ind_tag   = $input->multi_param('ind_tag');
497
    my @indicator = $input->multi_param('indicator');
498
    my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
499
    my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
500
501
    # type of add
502
    my $add_submit                 = $input->param('add_submit');
493
    my $add_submit                 = $input->param('add_submit');
503
    my $add_duplicate_submit       = $input->param('add_duplicate_submit');
494
    my $add_duplicate_submit       = $input->param('add_duplicate_submit');
504
    my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
495
    my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
Lines 512-652 if ($op eq "additem") { Link Here
512
    $add_duplicate_submit = 1 if ($prefillitem);
503
    $add_duplicate_submit = 1 if ($prefillitem);
513
    $justaddeditem = 1;
504
    $justaddeditem = 1;
514
505
515
    # if autoBarcode is set to 'incremental', calculate barcode...
506
    my @columns = Koha::Items->columns;
516
    if ( C4::Context->preference('autoBarcode') eq 'incremental' ) {
507
    my $biblio = Koha::Biblios->find($biblionumber);
517
        $record = _increment_barcode($record, $frameworkcode);
508
    my $item = Koha::Item->new;
509
    $item->biblionumber($biblio->biblionumber);
510
    for my $c ( @columns ) {
511
        if ( $c eq 'more_subfields_xml' ) {
512
            my @more_subfields_xml = $input->multi_param("items.more_subfields_xml");
513
            my @unlinked_item_subfields;
514
            for my $subfield ( @more_subfields_xml ) {
515
                my $v = $input->param('items.more_subfields_xml_' . $subfield);
516
                push @unlinked_item_subfields, $subfield, $v;
517
            }
518
            if ( @unlinked_item_subfields ) {
519
                my $marc = MARC::Record->new();
520
                # use of tag 999 is arbitrary, and doesn't need to match the item tag
521
                # used in the framework
522
                $marc->append_fields(MARC::Field->new('999', ' ', ' ', @unlinked_item_subfields));
523
                $marc->encoding("UTF-8");
524
                $item->more_subfields_xml($marc->as_xml("USMARC"));
525
                next;
526
            }
527
            $item->more_subfields_xml(undef);
528
        } else {
529
            my $v = $input->param("items.".$c);
530
            next unless defined $v;
531
            $item->$c($v);
532
        }
518
    }
533
    }
519
534
520
    my $addedolditem = TransformMarcToKoha( $record );
535
    # if autoBarcode is set to 'incremental', calculate barcode...
536
    if ( not $item->barcode && C4::Context->preference('autoBarcode') eq 'incremental' ) {
537
        my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
538
        $item->barcode($barcode);
539
    }
521
540
522
    # If we have to add or add & duplicate, we add the item
541
    # If we have to add or add & duplicate, we add the item
523
    if ( $add_submit || $add_duplicate_submit ) {
542
    if ( $add_submit || $add_duplicate_submit ) {
524
543
525
        # check for item barcode # being unique
544
        # check for item barcode # being unique
526
        my $exist_itemnumber = get_item_from_barcode( $addedolditem->{'barcode'} );
545
        if ( Koha::Items->search({ barcode => $item->barcode })->count ) {
527
        push @errors, "barcode_not_unique" if ($exist_itemnumber);
546
            # if barcode exists, don't create, but report The problem.
528
547
            push @errors, "barcode_not_unique";
529
        # if barcode exists, don't create, but report The problem.
548
        }
530
        unless ($exist_itemnumber) {
549
        else {
531
            my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) = AddItemFromMarc( $record, $biblionumber );
550
            $item->store->discard_changes;
532
551
533
            # Pushing the last created item cookie back
552
            # FIXME This need to be rewritten, we must store $item->unblessed instead
534
            if ($prefillitem && defined $record) {
553
            ## Pushing the last created item cookie back
535
                my $itemcookie = $input->cookie(
554
            #if ($prefillitem && defined $record) {
536
                    -name => 'LastCreatedItem',
555
            #    my $itemcookie = $input->cookie(
537
                    # We encode_base64url the whole freezed structure so we're sure we won't have any encoding problems
556
            #        -name => 'LastCreatedItem',
538
                    -value   => encode_base64url( freeze( $record ) ),
557
            #        # We encode_base64url the whole freezed structure so we're sure we won't have any encoding problems
539
                    -HttpOnly => 1,
558
            #        -value   => encode_base64url( freeze( $record ) ),
540
                    -expires => ''
559
            #        -HttpOnly => 1,
541
                );
560
            #        -expires => ''
542
561
            #    );
543
                $cookie = [ $cookie, $itemcookie ];
562
544
            }
563
            #    $cookie = [ $cookie, $itemcookie ];
564
            #}
545
565
546
        }
566
        }
547
        $nextop = "additem";
567
        $nextop = "additem";
548
        if ($exist_itemnumber) {
568
549
            $itemrecord = $record;
569
550
        }
570
        # FIXME reset item to the item we were editing
571
        #if ($exist_itemnumber) {
572
573
        #    $itemrecord = $record;
574
        #}
575
        $current_item = $item->unblessed;
551
    }
576
    }
552
577
553
    # If we have to add & duplicate
578
    # If we have to add & duplicate
554
    if ($add_duplicate_submit) {
579
    if ($add_duplicate_submit) {
555
        $itemrecord = $record;
556
        if (C4::Context->preference('autoBarcode') eq 'incremental') {
580
        if (C4::Context->preference('autoBarcode') eq 'incremental') {
557
            $itemrecord = _increment_barcode($itemrecord, $frameworkcode);
581
            my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
582
            $current_item->{barcode} = $barcode;
558
        }
583
        }
559
        else {
584
        else {
560
            # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
585
            # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
561
            my ($tagfield,$tagsubfield) = &GetMarcFromKohaField( "items.barcode" );
586
            $current_item->{barcode} = undef; # FIXME or delete?
562
            my $fieldItem = $itemrecord->field($tagfield);
563
            $itemrecord->delete_field($fieldItem);
564
            $fieldItem->delete_subfields($tagsubfield);
565
            $itemrecord->insert_fields_ordered($fieldItem);
566
        }
587
        }
567
    $itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem);
588
        # FIXME This subroutine needs to be adjusted
589
        # We want to pass $item
590
        # $itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem);
568
    }
591
    }
569
592
570
    # If we have to add multiple copies
593
    # If we have to add multiple copies
571
    if ($add_multiple_copies_submit) {
594
    if ($add_multiple_copies_submit) {
572
595
573
        use C4::Barcodes;
596
        my $copynumber = $current_item->{copynumber};
574
        my $barcodeobj = C4::Barcodes->new;
597
        my $oldbarcode = $current_item->{barcode};
575
        my $copynumber = $addedolditem->{'copynumber'};
576
        my $oldbarcode = $addedolditem->{'barcode'};
577
        my ($tagfield,$tagsubfield) = &GetMarcFromKohaField( "items.barcode" );
578
        my ($copytagfield,$copytagsubfield) = &GetMarcFromKohaField( "items.copynumber" );
579
598
580
    # If there is a barcode and we can't find their new values, we can't add multiple copies
599
        # If there is a barcode and we can't find their new values, we can't add multiple copies
581
	my $testbarcode;
600
        my $testbarcode;
601
        my $barcodeobj = C4::Barcodes->new;
582
        $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
602
        $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
583
	if ($oldbarcode && !$testbarcode) {
603
        if ( $oldbarcode && !$testbarcode ) {
584
604
585
	    push @errors, "no_next_barcode";
605
            push @errors, "no_next_barcode";
586
	    $itemrecord = $record;
606
            $itemrecord = $record;
587
588
	} else {
589
	# We add each item
590
607
591
	    # For the first iteration
608
        }
592
	    my $barcodevalue = $oldbarcode;
609
        else {
593
	    my $exist_itemnumber;
610
            # We add each item
611
612
            # For the first iteration
613
            my $barcodevalue = $oldbarcode;
614
            my $exist_itemnumber;
615
616
            for ( my $i = 0 ; $i < $number_of_copies ; ) {
617
618
                # If there is a barcode
619
                if ($barcodevalue) {
620
621
# Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
622
                    $barcodevalue = $barcodeobj->next_value($oldbarcode)
623
                      if ( $i > 0 || $exist_itemnumber );
624
625
                    # Putting it into the record
626
                    if ($barcodevalue) {
627
                        if ( C4::Context->preference("autoBarcode") eq
628
                            'hbyymmincr' && $i > 0 )
629
                        { # The first copy already contains the homebranch prefix
630
                             # This is terribly hacky but the easiest way to fix the way hbyymmincr is working
631
                             # Contrary to what one might think, the barcode plugin does not prefix the returned string with the homebranch
632
                             # For a single item, it is handled with some JS code (see cataloguing/value_builder/barcode.pl)
633
                             # But when adding multiple copies we need to prefix it here,
634
                             # so we retrieve the homebranch from the item and prefix the barcode with it.
635
                            my $homebranch = $current_item->{homebranch};
636
                            $barcodevalue = $homebranch . $barcodevalue;
637
                        }
638
                        $current_item->{barcode} = $barcodevalue;
639
                    }
594
640
641
                    # Checking if the barcode already exists
642
                    $exist_itemnumber = Koha::Items->search({ barcode => $barcodevalue })->count;
643
                }
595
644
596
	    for (my $i = 0; $i < $number_of_copies;) {
645
                # Updating record with the new copynumber
646
                if ($copynumber) {
647
                    $current_item->{copynumber} = $copynumber;
648
                }
597
649
598
		# If there is a barcode
650
                # Adding the item
599
		if ($barcodevalue) {
651
                if ( !$exist_itemnumber ) {
652
                    delete $current_item->{itemnumber};
653
                    $current_item = Koha::Item->new($current_item)->store(
654
                        { skip_record_index => 1 } )->discard_changes->unblessed;
655
                    set_item_default_location($current_item->{itemnumber});
600
656
601
		    # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
657
# We count the item only if it was really added
602
		    $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
658
# That way, all items are added, even if there was some already existing barcodes
659
# FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
660
                    $i++;
603
661
604
		    # Putting it into the record
662
                    # Only increment copynumber if item was really added
605
		    if ($barcodevalue) {
663
                    $copynumber++ if ( $copynumber && $copynumber =~ m/^\d+$/ );
606
                if ( C4::Context->preference("autoBarcode") eq 'hbyymmincr' && $i > 0 ) { # The first copy already contains the homebranch prefix
607
                    # This is terribly hacky but the easiest way to fix the way hbyymmincr is working
608
                    # Contrary to what one might think, the barcode plugin does not prefix the returned string with the homebranch
609
                    # For a single item, it is handled with some JS code (see cataloguing/value_builder/barcode.pl)
610
                    # But when adding multiple copies we need to prefix it here,
611
                    # so we retrieve the homebranch from the item and prefix the barcode with it.
612
                    my ($hb_field, $hb_subfield) = GetMarcFromKohaField( "items.homebranch" );
613
                    my $homebranch = $record->subfield($hb_field, $hb_subfield);
614
                    $barcodevalue = $homebranch . $barcodevalue;
615
                }
664
                }
616
                $record->field($tagfield)->update($tagsubfield => $barcodevalue);
617
		    }
618
619
		    # Checking if the barcode already exists
620
		    $exist_itemnumber = get_item_from_barcode($barcodevalue);
621
		}
622
        # Updating record with the new copynumber
623
        if ( $copynumber  ){
624
            $record->field($copytagfield)->update($copytagsubfield => $copynumber);
625
        }
626
627
		# Adding the item
628
        if (!$exist_itemnumber) {
629
            my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) =
630
                AddItemFromMarc( $record, $biblionumber, { skip_record_index => 1 } );
631
665
632
            # We count the item only if it was really added
666
                # Preparing the next iteration
633
            # That way, all items are added, even if there was some already existing barcodes
667
                $oldbarcode = $barcodevalue;
634
            # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
668
            }
635
            $i++;
636
            # Only increment copynumber if item was really added
637
            $copynumber++  if ( $copynumber && $copynumber =~ m/^\d+$/ );
638
        }
639
640
		# Preparing the next iteration
641
		$oldbarcode = $barcodevalue;
642
	    }
643
669
644
        my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
670
            my $indexer = Koha::SearchEngine::Indexer->new(
645
        $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" );
671
                { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
672
            $indexer->index_records( $biblionumber, "specialUpdate",
673
                "biblioserver" );
646
674
647
	    undef($itemrecord);
675
            undef($current_item);
648
	}
676
        }
649
    }	
677
    }
650
    if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
678
    if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
651
        print $input->redirect(
679
        print $input->redirect(
652
           '/cgi-bin/koha/circ/circulation.pl?'
680
           '/cgi-bin/koha/circ/circulation.pl?'
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt (-3 / +6 lines)
Lines 246-252 Link Here
246
    </fieldset>
246
    </fieldset>
247
    <input type="hidden" name="indicator" value=" " />
247
    <input type="hidden" name="indicator" value=" " />
248
    <input type="hidden" name="indicator" value=" " />
248
    <input type="hidden" name="indicator" value=" " />
249
    <input type="hidden" name="itemnumber" value="[% itemnumber | html %]" />
249
    [% IF op != 'add_item' %]
250
        <input type="hidden" name="itemnumber" value="[% itemnumber | html %]" />
251
    [% END %]
250
252
251
<fieldset class="action">    [% IF op != 'saveitem' %]
253
<fieldset class="action">    [% IF op != 'saveitem' %]
252
    <input type="submit" name="phony_submit" value="phony_submit" id="phony_submit" style="display:none;" onclick="return false;" />
254
    <input type="submit" name="phony_submit" value="phony_submit" id="phony_submit" style="display:none;" onclick="return false;" />
Lines 271-277 Link Here
271
    [% ELSE %]
273
    [% ELSE %]
272
    <input type="hidden" name="tag" value="[% itemtagfield | html %]" />
274
    <input type="hidden" name="tag" value="[% itemtagfield | html %]" />
273
    <input type="hidden" name="subfield" value="[% itemtagsubfield | html %]" />
275
    <input type="hidden" name="subfield" value="[% itemtagsubfield | html %]" />
274
    <input type="hidden" name="itemnumber" value="[% itemnumber | html %]" />
276
    [% IF op != 'add_item' %]
277
        <input type="hidden" name="itemnumber" value="[% itemnumber | html %]" />
278
    [% END %]
275
    <input type="submit" value="Save changes" onclick="return Check(this.form)">
279
    <input type="submit" value="Save changes" onclick="return Check(this.form)">
276
    <input type="button" id="addnewitem" value="Add a new item">
280
    <input type="button" id="addnewitem" value="Add a new item">
277
    <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% biblionumber | uri %]">Cancel</a>
281
    <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% biblionumber | uri %]">Cancel</a>
278
- 

Return to bug 27526