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

(-)a/C4/Acquisition.pm (-1 / +52 lines)
Lines 23-29 use Carp; Link Here
23
use C4::Context;
23
use C4::Context;
24
use C4::Debug;
24
use C4::Debug;
25
use C4::Dates qw(format_date format_date_in_iso);
25
use C4::Dates qw(format_date format_date_in_iso);
26
use MARC::Record;
27
use C4::Suggestions;
26
use C4::Suggestions;
28
use C4::Biblio;
27
use C4::Biblio;
29
use C4::Contract;
28
use C4::Contract;
Lines 33-38 use Koha::DateUtils qw( dt_from_string output_pref ); Link Here
33
use Koha::Acquisition::Order;
32
use Koha::Acquisition::Order;
34
use Koha::Acquisition::Bookseller;
33
use Koha::Acquisition::Bookseller;
35
34
35
use C4::Koha qw( subfield_is_koha_internal_p );
36
37
use MARC::Field;
38
use MARC::Record;
39
36
use Time::localtime;
40
use Time::localtime;
37
use HTML::Entities;
41
use HTML::Entities;
38
42
Lines 81-86 BEGIN { Link Here
81
85
82
        &AddClaim
86
        &AddClaim
83
        &GetBiblioCountByBasketno
87
        &GetBiblioCountByBasketno
88
89
        &FillWithDefaultValues
84
    );
90
    );
85
}
91
}
86
92
Lines 2888-2893 sub GetBiblioCountByBasketno { Link Here
2888
    return $sth->fetchrow;
2894
    return $sth->fetchrow;
2889
}
2895
}
2890
2896
2897
=head3 FillWithDefaultValues
2898
2899
FillWithDefaultValues( $marc_record );
2900
2901
This will update the record with default value defined in the ACQ framework.
2902
For all existing fields, if a default value exists and there are no subfield, it will be created.
2903
If the field does not exist, it will be created too.
2904
2905
=cut
2906
2907
sub FillWithDefaultValues {
2908
    my ($record) = @_;
2909
    my $tagslib = C4::Biblio::GetMarcStructure( 1, 'ACQ' );
2910
    if ($tagslib) {
2911
        my ($itemfield) =
2912
          C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' );
2913
        for my $tag ( sort keys %$tagslib ) {
2914
            next unless $tag;
2915
            next if $tag == $itemfield;
2916
            for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2917
                next if ( subfield_is_koha_internal_p($subfield) );
2918
                my $defaultvalue = $tagslib->{$tag}{$subfield}{defaultvalue};
2919
                if ( defined $defaultvalue and $defaultvalue ne '' ) {
2920
                    my @fields = $record->field($tag);
2921
                    if (@fields) {
2922
                        for my $field (@fields) {
2923
                            unless ( defined $field->subfield($subfield) ) {
2924
                                $field->add_subfields(
2925
                                    $subfield => $defaultvalue );
2926
                            }
2927
                        }
2928
                    }
2929
                    else {
2930
                        $record->insert_fields_ordered(
2931
                            MARC::Field->new(
2932
                                $tag, '', '', $subfield => $defaultvalue
2933
                            )
2934
                        );
2935
                    }
2936
                }
2937
            }
2938
        }
2939
    }
2940
}
2941
2891
1;
2942
1;
2892
__END__
2943
__END__
2893
2944
(-)a/acqui/addorder.pl (+2 lines)
Lines 250-255 if ( $orderinfo->{quantity} ne '0' ) { Link Here
250
                "biblioitems.editionstatement"=> $$orderinfo{editionstatement} ? $$orderinfo{editionstatement} : "",
250
                "biblioitems.editionstatement"=> $$orderinfo{editionstatement} ? $$orderinfo{editionstatement} : "",
251
            });
251
            });
252
252
253
        C4::Acquisition::FillWithDefaultValues( $record );
254
253
        # create the record in catalogue, with framework ''
255
        # create the record in catalogue, with framework ''
254
        my ($biblionumber,$bibitemnum) = AddBiblio($record,'');
256
        my ($biblionumber,$bibitemnum) = AddBiblio($record,'');
255
        # change suggestion status if applicable
257
        # change suggestion status if applicable
(-)a/t/db_dependent/Acquisition/FillWithDefaultValues.t (-1 / +79 lines)
Line 0 Link Here
0
- 
1
use Modern::Perl;
2
use Test::More tests => 5;
3
use Test::MockModule;
4
5
use MARC::Record;
6
use MARC::Field;
7
8
use C4::Context;
9
use C4::Acquisition qw( FillWithDefaultValues );
10
11
my $dbh = C4::Context->dbh;
12
$dbh->{AutoCommit} = 0;
13
$dbh->{RaiseError} = 1;
14
15
my $biblio_module  = Test::MockModule->new('C4::Biblio');
16
my $default_author = 'default author';
17
my $default_x      = 'my default value';
18
$biblio_module->mock(
19
    'GetMarcStructure',
20
    sub {
21
        {
22
            # default value for an existing field
23
            '245' => {
24
                c => { defaultvalue => $default_author },
25
            },
26
27
            # default for a nonexisting field
28
            '099' => {
29
                x => { defaultvalue => $default_x },
30
            },
31
        };
32
    }
33
);
34
35
my $record = MARC::Record->new;
36
$record->leader('03174nam a2200445 a 4500');
37
my @fields = (
38
    MARC::Field->new(
39
        100, '1', ' ',
40
        a => 'Knuth, Donald Ervin',
41
        d => '1938',
42
    ),
43
    MARC::Field->new(
44
        245, '1', '4',
45
        a => 'The art of computer programming',
46
        c => 'Donald E. Knuth.',
47
    ),
48
    MARC::Field->new(
49
        245, '1', '4', a => 'my second title',
50
    ),
51
);
52
53
$record->append_fields(@fields);
54
55
C4::Acquisition::FillWithDefaultValues($record);
56
57
my @fields_245 = $record->field(245);
58
is( scalar(@fields_245), 2, 'No new 245 field has been created' );
59
my @subfields_245_0 = $fields_245[0]->subfields;
60
my @subfields_245_1 = $fields_245[1]->subfields;
61
is_deeply(
62
    \@subfields_245_0,
63
    [ [ 'a', 'The art of computer programming' ], [ 'c', 'Donald E. Knuth.' ] ],
64
    'first 245 field has not been updated'
65
);
66
is_deeply(
67
    \@subfields_245_1,
68
    [ [ 'a', 'my second title' ], [ 'c', $default_author ] ],
69
    'second 245 field has a new subfield c with a default value'
70
);
71
72
my @fields_099 = $record->field('099');
73
is( scalar(@fields_099), 1, '1 new 099 field has been created' );
74
my @subfields_099 = $fields_099[0]->subfields;
75
is_deeply(
76
    \@subfields_099,
77
    [ [ 'x', $default_x ] ],
78
    '099$x contains the default value'
79
);

Return to bug 12743