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