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

(-)a/C4/Biblio.pm (-44 / +47 lines)
Lines 2365-2370 sub GetMarcHosts { Link Here
2365
    return $marchostsarray;
2365
    return $marchostsarray;
2366
}
2366
}
2367
2367
2368
=head2 UpsertMarcSubfield
2369
2370
    my $record = C4::Biblio::UpsertMarcSubfield($MARC::Record, $fieldTag, $subfieldCode, $subfieldContent);
2371
2372
=cut
2373
2374
sub UpsertMarcSubfield {
2375
    my ($record, $tag, $code, $content) = @_;
2376
    my $f = $record->field($tag);
2377
2378
    if ($f) {
2379
        $f->update( $code => $content );
2380
    }
2381
    else {
2382
        my $f = MARC::Field->new( $tag, '', '', $code => $content);
2383
        $record->insert_fields_ordered( $f );
2384
    }
2385
}
2386
2387
=head2 UpsertMarcControlField
2388
2389
    my $record = C4::Biblio::UpsertMarcControlField($MARC::Record, $fieldTag, $content);
2390
2391
=cut
2392
2393
sub UpsertMarcControlField {
2394
    my ($record, $tag, $content) = @_;
2395
    die "UpsertMarcControlField() \$tag '$tag' is not a control field\n" unless 0+$tag < 10;
2396
    my $f = $record->field($tag);
2397
2398
    if ($f) {
2399
        $f->update( $content );
2400
    }
2401
    else {
2402
        my $f = MARC::Field->new($tag, $content);
2403
        $record->insert_fields_ordered( $f );
2404
    }
2405
}
2406
2368
=head2 GetFrameworkCode
2407
=head2 GetFrameworkCode
2369
2408
2370
  $frameworkcode = GetFrameworkCode( $biblionumber )
2409
  $frameworkcode = GetFrameworkCode( $biblionumber )
Lines 3317-3372 the MARC XML. Link Here
3317
sub _koha_marc_update_bib_ids {
3356
sub _koha_marc_update_bib_ids {
3318
    my ( $record, $frameworkcode, $biblionumber, $biblioitemnumber ) = @_;
3357
    my ( $record, $frameworkcode, $biblionumber, $biblioitemnumber ) = @_;
3319
3358
3320
    # we must add bibnum and bibitemnum in MARC::Record...
3321
    # we build the new field with biblionumber and biblioitemnumber
3322
    # we drop the original field
3323
    # we add the new builded field.
3324
    my ( $biblio_tag,     $biblio_subfield )     = GetMarcFromKohaField( "biblio.biblionumber",          $frameworkcode );
3359
    my ( $biblio_tag,     $biblio_subfield )     = GetMarcFromKohaField( "biblio.biblionumber",          $frameworkcode );
3325
    die qq{No biblionumber tag for framework "$frameworkcode"} unless $biblio_tag;
3360
    die qq{No biblionumber tag for framework "$frameworkcode"} unless $biblio_tag;
3326
    my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
3361
    my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
3327
    die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
3362
    die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
3328
3363
3329
    if ( $biblio_tag == $biblioitem_tag ) {
3364
    if ( $biblio_tag < 10 ) {
3330
3365
        C4::Biblio::UpsertMarcControlField( $record, $biblio_tag, $biblionumber );
3331
        # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
3332
        my $new_field = MARC::Field->new(
3333
            $biblio_tag, '', '',
3334
            "$biblio_subfield"     => $biblionumber,
3335
            "$biblioitem_subfield" => $biblioitemnumber
3336
        );
3337
3338
        # drop old field and create new one...
3339
        my $old_field = $record->field($biblio_tag);
3340
        $record->delete_field($old_field) if $old_field;
3341
        $record->insert_fields_ordered($new_field);
3342
    } else {
3366
    } else {
3343
3367
        C4::Biblio::UpsertMarcSubfield($record, $biblio_tag, $biblio_subfield, $biblionumber);
3344
        # biblionumber & biblioitemnumber are in different fields
3368
    }
3345
3369
    if ( $biblioitem_tag < 10 ) {
3346
        # deal with biblionumber
3370
        C4::Biblio::UpsertMarcControlField( $record, $biblioitem_tag, $biblioitemnumber );
3347
        my ( $new_field, $old_field );
3371
    } else {
3348
        if ( $biblio_tag < 10 ) {
3372
        C4::Biblio::UpsertMarcSubfield($record, $biblioitem_tag, $biblioitem_subfield, $biblioitemnumber);
3349
            $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
3350
        } else {
3351
            $new_field = MARC::Field->new( $biblio_tag, '', '', "$biblio_subfield" => $biblionumber );
3352
        }
3353
3354
        # drop old field and create new one...
3355
        $old_field = $record->field($biblio_tag);
3356
        $record->delete_field($old_field) if $old_field;
3357
        $record->insert_fields_ordered($new_field);
3358
3359
        # deal with biblioitemnumber
3360
        if ( $biblioitem_tag < 10 ) {
3361
            $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
3362
        } else {
3363
            $new_field = MARC::Field->new( $biblioitem_tag, '', '', "$biblioitem_subfield" => $biblioitemnumber, );
3364
        }
3365
3366
        # drop old field and create new one...
3367
        $old_field = $record->field($biblioitem_tag);
3368
        $record->delete_field($old_field) if $old_field;
3369
        $record->insert_fields_ordered($new_field);
3370
    }
3373
    }
3371
}
3374
}
3372
3375
(-)a/t/Biblio2.t (-1 / +55 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use Test::More;
5
use Test::MockModule;
6
7
use MARC::Record;
8
9
use C4::Biblio;
10
11
subtest "_koha_marc_update_bib_ids basic Field", \&_koha_marc_update_bib_ids_simple;
12
sub _koha_marc_update_bib_ids_simple {
13
    my $module = Test::MockModule->new('C4::Biblio');
14
    $module->mock('GetMarcFromKohaField', sub {
15
            my ($source) = @_;
16
            return ('999','c') if $source eq 'biblio.biblionumber';
17
            return ('999','d') if $source eq 'biblioitems.biblioitemnumber';
18
        }
19
    );
20
21
    my $r = MARC::Record->new();
22
    C4::Biblio::_koha_marc_update_bib_ids($r, undef, 10, 20);
23
    is($r->subfield('999', 'c'), 10, 'Biblionumber');
24
    is($r->subfield('999', 'd'), 20, 'Biblioitemnumber');
25
26
    C4::Biblio::_koha_marc_update_bib_ids($r, undef, 10, 20);
27
    my @f = $r->field('999');
28
    is(scalar(@f), 1, 'Field not duplicated');
29
    is($r->subfield('999', 'c'), 10, 'Biblionumber');
30
    is($r->subfield('999', 'd'), 20, 'Biblioitemnumber');
31
}
32
33
subtest "_koha_marc_update_bib_ids ControlField", \&_koha_marc_update_bib_ids_control;
34
sub _koha_marc_update_bib_ids_control {
35
    my $module = Test::MockModule->new('C4::Biblio');
36
    $module->mock('GetMarcFromKohaField', sub {
37
            my ($source) = @_;
38
            return ('001',undef) if $source eq 'biblio.biblionumber';
39
            return ('004',undef) if $source eq 'biblioitems.biblioitemnumber';
40
        }
41
    );
42
43
    my $r = MARC::Record->new();
44
    C4::Biblio::_koha_marc_update_bib_ids($r, undef, 10, 20);
45
    is($r->field('001')->data(), 10, 'Biblionumber to control field');
46
    is($r->field('004')->data(), 20, 'Biblioitemnumber to control field');
47
48
    C4::Biblio::_koha_marc_update_bib_ids($r, undef, 10, 20);
49
    my @f = $r->field('001');
50
    is(scalar(@f), 1, 'Control field not duplicated');
51
    is($r->field('001')->data(), 10, 'Biblionumber to control field');
52
    is($r->field('004')->data(), 20, 'Biblioitemnumber to control field');
53
}
54
55
done_testing();

Return to bug 16556