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

(-)a/C4/Acquisition.pm (-52 lines)
Lines 59-66 BEGIN { Link Here
59
        ModOrderUsers
59
        ModOrderUsers
60
        NotifyOrderUsers
60
        NotifyOrderUsers
61
61
62
        FillWithDefaultValues
63
64
        get_rounded_price
62
        get_rounded_price
65
        get_rounding_sql
63
        get_rounding_sql
66
    );
64
    );
Lines 2957-3012 sub NotifyOrderUsers { Link Here
2957
    }
2955
    }
2958
}
2956
}
2959
2957
2960
=head3 FillWithDefaultValues
2961
2962
FillWithDefaultValues( $marc_record, $params );
2963
2964
This will update the record with default value defined in the ACQ framework.
2965
For all existing fields, if a default value exists and there are no subfield, it will be created.
2966
If the field does not exist, it will be created too.
2967
2968
If the parameter only_mandatory => 1 is passed via $params, only the mandatory
2969
defaults are being applied to the record.
2970
2971
=cut
2972
2973
sub FillWithDefaultValues {
2974
    my ( $record, $params ) = @_;
2975
    my $mandatory = $params->{only_mandatory};
2976
    my $tagslib   = C4::Biblio::GetMarcStructure( 1, 'ACQ', { unsafe => 1 } );
2977
    if ($tagslib) {
2978
        my ($itemfield) = C4::Biblio::GetMarcFromKohaField('items.itemnumber');
2979
        for my $tag ( sort keys %$tagslib ) {
2980
            next unless $tag;
2981
            next if $tag == $itemfield;
2982
            for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2983
                next if IsMarcStructureInternal( $tagslib->{$tag}{$subfield} );
2984
                next if $mandatory && !$tagslib->{$tag}{$subfield}{mandatory};
2985
                my $defaultvalue = $tagslib->{$tag}{$subfield}{defaultvalue};
2986
                if ( defined $defaultvalue and $defaultvalue ne '' ) {
2987
                    my @fields = $record->field($tag);
2988
                    if (@fields) {
2989
                        for my $field (@fields) {
2990
                            if ( $field->is_control_field ) {
2991
                                $field->update($defaultvalue) if not defined $field->data;
2992
                            } elsif ( not defined $field->subfield($subfield) ) {
2993
                                $field->add_subfields( $subfield => $defaultvalue );
2994
                            }
2995
                        }
2996
                    } else {
2997
                        if ( $tag < 10 ) {    # is_control_field
2998
                            $record->insert_fields_ordered( MARC::Field->new( $tag, $defaultvalue ) );
2999
                        } else {
3000
                            $record->insert_fields_ordered(
3001
                                MARC::Field->new( $tag, '', '', $subfield => $defaultvalue ) );
3002
                        }
3003
                    }
3004
                }
3005
            }
3006
        }
3007
    }
3008
}
3009
3010
1;
2958
1;
3011
__END__
2959
__END__
3012
2960
(-)a/acqui/addorder.pl (-2 / +6 lines)
Lines 120-126 use Modern::Perl; Link Here
120
use CGI  qw ( -utf8 );
120
use CGI  qw ( -utf8 );
121
use JSON qw ( to_json encode_json );
121
use JSON qw ( to_json encode_json );
122
122
123
use C4::Acquisition qw( FillWithDefaultValues ModOrderUsers );
123
use C4::Acquisition qw( ModOrderUsers );
124
use C4::Auth        qw( get_template_and_user );
124
use C4::Auth        qw( get_template_and_user );
125
use C4::Barcodes;
125
use C4::Barcodes;
126
use C4::Biblio      qw( AddBiblio GetMarcFromKohaField TransformHtmlToXml TransformKohaToMarc );
126
use C4::Biblio      qw( AddBiblio GetMarcFromKohaField TransformHtmlToXml TransformKohaToMarc );
Lines 134-139 use Koha::Acquisition::Baskets; Link Here
134
use Koha::Acquisition::Currencies;
134
use Koha::Acquisition::Currencies;
135
use Koha::Acquisition::Orders;
135
use Koha::Acquisition::Orders;
136
use Koha::AdditionalFields;
136
use Koha::AdditionalFields;
137
use Koha::BiblioFrameworks;
137
use Koha::DateUtils qw( dt_from_string );
138
use Koha::DateUtils qw( dt_from_string );
138
139
139
### "-------------------- addorder.pl ----------"
140
### "-------------------- addorder.pl ----------"
Lines 329-335 if ( $op eq 'cud-order' ) { Link Here
329
                    }
330
                    }
330
                );
331
                );
331
            }
332
            }
332
            C4::Acquisition::FillWithDefaultValues($record);
333
            my $framework = Koha::BiblioFrameworks->find('ACQ');
334
            if ($framework) {
335
                $framework->fill_with_default_values($record);
336
            }
333
337
334
            if ( !$confirm_not_duplicate ) {
338
            if ( !$confirm_not_duplicate ) {
335
                my ( $dupe_biblionumber, $dupe_title ) = FindDuplicate($record);
339
                my ( $dupe_biblionumber, $dupe_title ) = FindDuplicate($record);
(-)a/acqui/neworderempty.pl (-2 / +5 lines)
Lines 70-76 use C4::Context; Link Here
70
use C4::Auth    qw( get_template_and_user );
70
use C4::Auth    qw( get_template_and_user );
71
use C4::Budgets qw( GetBudget GetBudgetHierarchy CanUserUseBudget );
71
use C4::Budgets qw( GetBudget GetBudgetHierarchy CanUserUseBudget );
72
72
73
use C4::Acquisition qw( GetOrder GetBasket FillWithDefaultValues GetOrderUsers );
73
use C4::Acquisition qw( GetOrder GetBasket GetOrderUsers );
74
use C4::Contract    qw( GetContract );
74
use C4::Contract    qw( GetContract );
75
use C4::Suggestions qw( GetSuggestionInfo );
75
use C4::Suggestions qw( GetSuggestionInfo );
76
use C4::Biblio      qw(
76
use C4::Biblio      qw(
Lines 185-191 if ( $ordernumber eq '' and defined $breedingid ) { Link Here
185
    }
185
    }
186
186
187
    #from this point: add a new record
187
    #from this point: add a new record
188
    C4::Acquisition::FillWithDefaultValues( $marcrecord, { only_mandatory => 1 } );
188
    my $framework = Koha::BiblioFrameworks->find('ACQ');
189
    if ($framework) {
190
        $framework->fill_with_default_values( $marcrecord, { only_mandatory => 1 } );
191
    }
189
    my $bibitemnum;
192
    my $bibitemnum;
190
    ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $frameworkcode );
193
    ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $frameworkcode );
191
194
(-)a/t/db_dependent/Acquisition/FillWithDefaultValues.t (-123 lines)
Lines 1-122 Link Here
1
use Modern::Perl;
2
use Test::NoWarnings;
3
use Test::More tests => 13;
4
use Test::MockModule;
5
6
use MARC::Record;
7
use MARC::Field;
8
9
use C4::Context;
10
use C4::Acquisition qw( FillWithDefaultValues );
11
use Koha::Database;
12
13
my $schema = Koha::Database->new->schema;
14
$schema->storage->txn_begin;
15
16
my $biblio_module  = Test::MockModule->new('C4::Biblio');
17
my $default_author = 'default author';
18
my $default_x      = 'my default value';
19
$biblio_module->mock(
20
    'GetMarcStructure',
21
    sub {
22
        {
23
            # default for a control field
24
            '008' => {
25
                x => { defaultvalue => $default_x },
26
            },
27
28
            # default value for an existing field
29
            '245' => {
30
                c          => { defaultvalue => $default_author, mandatory => 1 },
31
                mandatory  => 0,
32
                repeatable => 0,
33
                tab        => 0,
34
                lib        => 'a lib',
35
            },
36
37
            # default for a nonexisting field
38
            '099' => {
39
                x => { defaultvalue => $default_x },
40
            },
41
            '942' => {
42
                c => { defaultvalue => 'BK', mandatory => 1 },
43
                d => { defaultvalue => '942d_val' },
44
                f => { defaultvalue => '942f_val' },
45
            },
46
        };
47
    }
48
);
49
50
my $record = MARC::Record->new;
51
$record->leader('03174nam a2200445 a 4500');
52
my @fields = (
53
    MARC::Field->new(
54
        '008', '1', ' ',
55
        '@' => '120829t20132012nyu bk 001 0ceng',
56
    ),
57
    MARC::Field->new(
58
        100, '1', ' ',
59
        a => 'Knuth, Donald Ervin',
60
        d => '1938',
61
    ),
62
    MARC::Field->new(
63
        245, '1', '4',
64
        a => 'The art of computer programming',
65
        c => 'Donald E. Knuth.',
66
    ),
67
    MARC::Field->new(
68
        245, '1', '4', a => 'my second title',
69
    ),
70
);
71
72
$record->append_fields(@fields);
73
74
C4::Acquisition::FillWithDefaultValues($record);
75
76
my @fields_245 = $record->field(245);
77
is( scalar(@fields_245), 2, 'No new 245 field has been created' );
78
my @subfields_245_0 = $fields_245[0]->subfields;
79
my @subfields_245_1 = $fields_245[1]->subfields;
80
is_deeply(
81
    \@subfields_245_0,
82
    [ [ 'a', 'The art of computer programming' ], [ 'c', 'Donald E. Knuth.' ] ],
83
    'first 245 field has not been updated'
84
);
85
is_deeply(
86
    \@subfields_245_1,
87
    [ [ 'a', 'my second title' ], [ 'c', $default_author ] ],
88
    'second 245 field has a new subfield c with a default value'
89
);
90
91
my @fields_099 = $record->field('099');
92
is( scalar(@fields_099), 1, '1 new 099 field has been created' );
93
my @subfields_099 = $fields_099[0]->subfields;
94
is_deeply(
95
    \@subfields_099,
96
    [ [ 'x', $default_x ] ],
97
    '099$x contains the default value'
98
);
99
100
# Test controlfield default
101
$record->field('008')->update(undef);
102
C4::Acquisition::FillWithDefaultValues($record);
103
is( $record->field('008')->data, $default_x, 'Controlfield got default' );
104
105
is( $record->subfield( '942', 'd' ), '942d_val', 'Check 942d' );
106
107
# Now test only_mandatory parameter
108
$record->delete_fields( $record->field('245') );
109
$record->delete_fields( $record->field('942') );
110
$record->append_fields( MARC::Field->new( '942', '', '', 'f' => 'f val' ) );
111
112
# We deleted 245 and replaced 942. If we only apply mandatories, we should get
113
# back 245c again and 942c but not 942d. 942f should be left alone.
114
C4::Acquisition::FillWithDefaultValues( $record, { only_mandatory => 1 } );
115
@fields_245 = $record->field(245);
116
is( scalar @fields_245, 1, 'Only one 245 expected' );
117
is( $record->subfield( '245', 'c' ), $default_author, '245c restored' );
118
is( $record->subfield( '942', 'c' ), 'BK',            '942c also restored' );
119
is( $record->subfield( '942', 'd' ), undef,           '942d should not be there' );
120
is( $record->subfield( '942', 'f' ), 'f val',         '942f untouched' );
121
122
$schema->storage->txn_rollback;
123
- 

Return to bug 41763