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 C4::Templates qw(gettemplate); Link Here
33
use Koha::DateUtils qw( dt_from_string output_pref );
32
use Koha::DateUtils qw( dt_from_string output_pref );
34
use Koha::Acquisition::Order;
33
use Koha::Acquisition::Order;
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 2896-2901 sub GetBiblioCountByBasketno { Link Here
2896
    return $sth->fetchrow;
2902
    return $sth->fetchrow;
2897
}
2903
}
2898
2904
2905
=head3 FillWithDefaultValues
2906
2907
FillWithDefaultValues( $marc_record );
2908
2909
This will update the record with default value defined in the ACQ framework.
2910
For all existing fields, if a default value exists and there are no subfield, it will be created.
2911
If the field does not exist, it will be created too.
2912
2913
=cut
2914
2915
sub FillWithDefaultValues {
2916
    my ($record) = @_;
2917
    my $tagslib = C4::Biblio::GetMarcStructure( 1, 'ACQ' );
2918
    if ($tagslib) {
2919
        my ($itemfield) =
2920
          C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' );
2921
        for my $tag ( sort keys %$tagslib ) {
2922
            next unless $tag;
2923
            next if $tag == $itemfield;
2924
            for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2925
                next if ( subfield_is_koha_internal_p($subfield) );
2926
                my $defaultvalue = $tagslib->{$tag}{$subfield}{defaultvalue};
2927
                if ( defined $defaultvalue and $defaultvalue ne '' ) {
2928
                    my @fields = $record->field($tag);
2929
                    if (@fields) {
2930
                        for my $field (@fields) {
2931
                            unless ( defined $field->subfield($subfield) ) {
2932
                                $field->add_subfields(
2933
                                    $subfield => $defaultvalue );
2934
                            }
2935
                        }
2936
                    }
2937
                    else {
2938
                        $record->insert_fields_ordered(
2939
                            MARC::Field->new(
2940
                                $tag, '', '', $subfield => $defaultvalue
2941
                            )
2942
                        );
2943
                    }
2944
                }
2945
            }
2946
        }
2947
    }
2948
}
2949
2899
1;
2950
1;
2900
__END__
2951
__END__
2901
2952
(-)a/acqui/addorder.pl (+2 lines)
Lines 257-262 if ( $orderinfo->{quantity} ne '0' ) { Link Here
257
                "aqorders.discount"           => $$orderinfo{discount} ? $$orderinfo{discount} : "",
257
                "aqorders.discount"           => $$orderinfo{discount} ? $$orderinfo{discount} : "",
258
            });
258
            });
259
259
260
        C4::Acquisition::FillWithDefaultValues( $record );
261
260
        # create the record in catalogue, with framework ''
262
        # create the record in catalogue, with framework ''
261
        my ($biblionumber,$bibitemnum) = AddBiblio($record,'');
263
        my ($biblionumber,$bibitemnum) = AddBiblio($record,'');
262
        # change suggestion status if applicable
264
        # change suggestion status if applicable
(-)a/t/db_dependent/Acquisition/FillWithDefaultValues.t (-1 / +80 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
);
80

Return to bug 12743