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

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