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

(-)a/cataloguing/additem.pl (-112 / +139 lines)
Lines 30-35 use C4::Context; Link Here
30
use C4::Circulation;
30
use C4::Circulation;
31
use C4::Koha;
31
use C4::Koha;
32
use C4::ClassSource;
32
use C4::ClassSource;
33
use C4::Barcodes;
34
use C4::Barcodes::ValueBuilder;
33
use Koha::DateUtils;
35
use Koha::DateUtils;
34
use Koha::Items;
36
use Koha::Items;
35
use Koha::ItemTypes;
37
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-654 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
            set_item_default_location($oldbibitemnum);
551
            set_item_default_location($item->itemnumber); # FIXME There is a fetch of the item in this sub, it's not optimal
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
            set_item_default_location($oldbibitemnum);
633
666
634
            # We count the item only if it was really added
667
                # Preparing the next iteration
635
            # That way, all items are added, even if there was some already existing barcodes
668
                $oldbarcode = $barcodevalue;
636
            # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
669
            }
637
            $i++;
638
            # Only increment copynumber if item was really added
639
            $copynumber++  if ( $copynumber && $copynumber =~ m/^\d+$/ );
640
        }
641
642
		# Preparing the next iteration
643
		$oldbarcode = $barcodevalue;
644
	    }
645
670
646
        my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
671
            my $indexer = Koha::SearchEngine::Indexer->new(
647
        $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" );
672
                { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
673
            $indexer->index_records( $biblionumber, "specialUpdate",
674
                "biblioserver" );
648
675
649
	    undef($itemrecord);
676
            undef($current_item);
650
	}
677
        }
651
    }	
678
    }
652
    if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
679
    if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
653
        print $input->redirect(
680
        print $input->redirect(
654
           '/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 238-244 Link Here
238
    </fieldset>
238
    </fieldset>
239
    <input type="hidden" name="indicator" value=" " />
239
    <input type="hidden" name="indicator" value=" " />
240
    <input type="hidden" name="indicator" value=" " />
240
    <input type="hidden" name="indicator" value=" " />
241
    <input type="hidden" name="itemnumber" value="[% itemnumber | html %]" />
241
    [% IF op != 'add_item' %]
242
        <input type="hidden" name="itemnumber" value="[% itemnumber | html %]" />
243
    [% END %]
242
244
243
<fieldset class="action">    [% IF op != 'saveitem' %]
245
<fieldset class="action">    [% IF op != 'saveitem' %]
244
    <input type="submit" name="phony_submit" value="phony_submit" id="phony_submit" style="display:none;" onclick="return false;" />
246
    <input type="submit" name="phony_submit" value="phony_submit" id="phony_submit" style="display:none;" onclick="return false;" />
Lines 263-269 Link Here
263
    [% ELSE %]
265
    [% ELSE %]
264
    <input type="hidden" name="tag" value="[% itemtagfield | html %]" />
266
    <input type="hidden" name="tag" value="[% itemtagfield | html %]" />
265
    <input type="hidden" name="subfield" value="[% itemtagsubfield | html %]" />
267
    <input type="hidden" name="subfield" value="[% itemtagsubfield | html %]" />
266
    <input type="hidden" name="itemnumber" value="[% itemnumber | html %]" />
268
    [% IF op != 'add_item' %]
269
        <input type="hidden" name="itemnumber" value="[% itemnumber | html %]" />
270
    [% END %]
267
    <input type="submit" value="Save changes" onclick="return Check(this.form)">
271
    <input type="submit" value="Save changes" onclick="return Check(this.form)">
268
    <input type="button" id="addnewitem" value="Add a new item">
272
    <input type="button" id="addnewitem" value="Add a new item">
269
    <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% biblionumber | uri %]">Cancel</a>
273
    <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% biblionumber | uri %]">Cancel</a>
270
- 

Return to bug 27526