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