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

(-)a/C4/Items.pm (-6 / +11 lines)
Lines 449-460 Returns item record Link Here
449
449
450
=cut
450
=cut
451
451
452
our %default_values_for_mod_from_marc;
453
454
sub _build_default_values_for_mod_marc {
452
sub _build_default_values_for_mod_marc {
455
    my ($frameworkcode) = @_;
453
    my ($frameworkcode) = @_;
456
    return $default_values_for_mod_from_marc{$frameworkcode}
454
457
      if exists $default_values_for_mod_from_marc{$frameworkcode};
455
    my $cache     = Koha::Cache->get_instance();
456
    my $cache_key = "default_value_for_mod_marc-$frameworkcode";
457
    my $cached    = $cache->get_from_cache($cache_key);
458
    return $cached if $cached;
459
458
    my $marc_structure = C4::Biblio::GetMarcStructure( 1, $frameworkcode );
460
    my $marc_structure = C4::Biblio::GetMarcStructure( 1, $frameworkcode );
459
    my $default_values = {
461
    my $default_values = {
460
        barcode                  => undef,
462
        barcode                  => undef,
Lines 487-501 sub _build_default_values_for_mod_marc { Link Here
487
        uri                      => undef,
489
        uri                      => undef,
488
        withdrawn                => 0,
490
        withdrawn                => 0,
489
    };
491
    };
492
    my %default_values_for_mod_from_marc;
490
    while ( my ( $field, $default_value ) = each %$default_values ) {
493
    while ( my ( $field, $default_value ) = each %$default_values ) {
491
        my $kohafield = $field;
494
        my $kohafield = $field;
492
        $kohafield =~ s|^([^\.]+)$|items.$1|;
495
        $kohafield =~ s|^([^\.]+)$|items.$1|;
493
        $default_values_for_mod_from_marc{$frameworkcode}{$field} =
496
        $default_values_for_mod_from_marc{$field} =
494
          $default_value
497
          $default_value
495
          if C4::Koha::IsKohaFieldLinked(
498
          if C4::Koha::IsKohaFieldLinked(
496
            { kohafield => $kohafield, frameworkcode => $frameworkcode } );
499
            { kohafield => $kohafield, frameworkcode => $frameworkcode } );
497
    }
500
    }
498
    return $default_values_for_mod_from_marc{$frameworkcode};
501
502
    $cache->set_in_cache($cache_key, \%default_values_for_mod_from_marc);
503
    return \%default_values_for_mod_from_marc;
499
}
504
}
500
505
501
sub ModItemFromMarc {
506
sub ModItemFromMarc {
(-)a/admin/marc_subfields_structure.pl (+2 lines)
Lines 341-346 elsif ( $op eq 'add_validate' ) { Link Here
341
    $sth_update->finish;
341
    $sth_update->finish;
342
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
342
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
343
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
343
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
344
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
344
345
345
    print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
346
    print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
346
    exit;
347
    exit;
Lines 384-389 elsif ( $op eq 'delete_confirmed' ) { Link Here
384
    }
385
    }
385
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
386
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
386
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
387
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
388
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
387
    print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
389
    print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
388
    exit;
390
    exit;
389
391
(-)a/t/db_dependent/Items.t (-3 / +114 lines)
Lines 26-32 use Koha::Library; Link Here
26
use t::lib::Mocks;
26
use t::lib::Mocks;
27
use t::lib::TestBuilder;
27
use t::lib::TestBuilder;
28
28
29
use Test::More tests => 9;
29
use Test::More tests => 10;
30
30
31
use Test::Warn;
31
use Test::Warn;
32
32
Lines 558-570 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { Link Here
558
    $schema->storage->txn_rollback;
558
    $schema->storage->txn_rollback;
559
};
559
};
560
560
561
562
subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
563
    plan tests => 4;
564
565
    $schema->storage->txn_begin();
566
567
    # Clear cache
568
    $C4::Context::context->{marcfromkohafield} = undef;
569
    $C4::Biblio::inverted_field_map = undef;
570
571
    my $builder = t::lib::TestBuilder->new;
572
    my $framework = $builder->build({
573
        source => 'BiblioFramework',
574
    });
575
    # Link biblio.biblionumber and biblioitems.biblioitemnumber to avoid _koha_marc_update_bib_ids to fail with 'no biblio[item]number tag for framework"
576
    $builder->build({
577
        source => 'MarcSubfieldStructure',
578
        value => {
579
            frameworkcode => $framework->{frameworkcode},
580
            kohafield => 'biblio.biblionumber',
581
            tagfield => '999',
582
            tagsubfield => 'c',
583
        }
584
    });
585
    $builder->build({
586
        source => 'MarcSubfieldStructure',
587
        value => {
588
            frameworkcode => $framework->{frameworkcode},
589
            kohafield => 'biblioitems.biblioitemnumber',
590
            tagfield => '999',
591
            tagsubfield => 'd',
592
        }
593
    });
594
    my $mss_itemnumber = $builder->build({
595
        source => 'MarcSubfieldStructure',
596
        value => {
597
            frameworkcode => $framework->{frameworkcode},
598
            kohafield => 'items.itemnumber',
599
            tagfield => '952',
600
            tagsubfield => '9',
601
        }
602
    });
603
604
    my $mss_barcode = $builder->build({
605
        source => 'MarcSubfieldStructure',
606
        value => {
607
            frameworkcode => $framework->{frameworkcode},
608
            kohafield => 'items.barcode',
609
            tagfield => '952',
610
            tagsubfield => 'p',
611
        }
612
    });
613
614
    # Create a record with a barcode
615
    my ($biblionumber) = get_biblio( $framework->{frameworkcode} );
616
    my $item_record = new MARC::Record;
617
    my $a_barcode = 'a barcode';
618
    my $barcode_field = MARC::Field->new(
619
        '952', ' ', ' ',
620
        p => $a_barcode,
621
    );
622
    $item_record->append_fields( $barcode_field );
623
    my (undef, undef, $item_itemnumber) = AddItemFromMarc($item_record, $biblionumber);
624
625
    # Make sure everything has been set up
626
    my $item = GetItem($item_itemnumber);
627
    is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
628
629
    # Delete the barcode field and save the record
630
    $item_record->delete_fields( $barcode_field );
631
    ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
632
    $item = GetItem($item_itemnumber);
633
    is( $item->{barcode}, undef, 'The default value should have been set to the barcode, the field is mapped to a kohafield' );
634
635
    # Re-add the barcode field and save the record
636
    $item_record->append_fields( $barcode_field );
637
    ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
638
    $item = GetItem($item_itemnumber);
639
    is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
640
641
    # Remove the mapping
642
    my $dbh = C4::Context->dbh;
643
    $dbh->do(q|
644
        DELETE FROM marc_subfield_structure
645
        WHERE kohafield = 'items.barcode'
646
        AND frameworkcode = ?
647
    |, undef, $framework->{frameworkcode} );
648
649
    # And make sure the caches are cleared
650
    $C4::Context::context->{marcfromkohafield} = undef;
651
    $C4::Biblio::inverted_field_map = undef;
652
    my $cache     = Koha::Cache->get_instance();
653
    $cache->clear_from_cache("default_value_for_mod_marc-" . $framework->{frameworkcode} );
654
655
    # Update the MARC field with another value
656
    $item_record->delete_fields( $barcode_field );
657
    my $another_barcode = 'another_barcode';
658
    my $another_barcode_field = MARC::Field->new(
659
        '952', ' ', ' ',
660
        p => $another_barcode,
661
    );
662
    $item_record->append_fields( $another_barcode_field );
663
    # The DB value should not have been updated
664
    ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
665
    $item = GetItem($item_itemnumber);
666
    is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
667
668
    $schema->storage->txn_rollback;
669
};
670
561
# Helper method to set up a Biblio.
671
# Helper method to set up a Biblio.
562
sub get_biblio {
672
sub get_biblio {
673
    my ( $frameworkcode ) = @_;
674
    $frameworkcode //= '';
563
    my $bib = MARC::Record->new();
675
    my $bib = MARC::Record->new();
564
    $bib->append_fields(
676
    $bib->append_fields(
565
        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
677
        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
566
        MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
678
        MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
567
    );
679
    );
568
    my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
680
    my ($bibnum, $bibitemnum) = AddBiblio($bib, $frameworkcode);
569
    return ($bibnum, $bibitemnum);
681
    return ($bibnum, $bibitemnum);
570
}
682
}
571
- 

Return to bug 13074