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 34-39 use Koha::Acquisition::Order; Link Here
34
use Koha::Acquisition::Bookseller;
33
use Koha::Acquisition::Bookseller;
35
use Koha::Number::Price;
34
use Koha::Number::Price;
36
35
36
use C4::Koha qw( subfield_is_koha_internal_p );
37
38
use MARC::Field;
39
use MARC::Record;
40
37
use Time::localtime;
41
use Time::localtime;
38
use HTML::Entities;
42
use HTML::Entities;
39
43
Lines 86-91 BEGIN { Link Here
86
        &GetOrderUsers
90
        &GetOrderUsers
87
        &ModOrderUsers
91
        &ModOrderUsers
88
        &NotifyOrderUsers
92
        &NotifyOrderUsers
93
94
        &FillWithDefaultValues
89
    );
95
    );
90
}
96
}
91
97
Lines 2986-2991 sub NotifyOrderUsers { Link Here
2986
    }
2992
    }
2987
}
2993
}
2988
2994
2995
=head3 FillWithDefaultValues
2996
2997
FillWithDefaultValues( $marc_record );
2998
2999
This will update the record with default value defined in the ACQ framework.
3000
For all existing fields, if a default value exists and there are no subfield, it will be created.
3001
If the field does not exist, it will be created too.
3002
3003
=cut
3004
3005
sub FillWithDefaultValues {
3006
    my ($record) = @_;
3007
    my $tagslib = C4::Biblio::GetMarcStructure( 1, 'ACQ' );
3008
    if ($tagslib) {
3009
        my ($itemfield) =
3010
          C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' );
3011
        for my $tag ( sort keys %$tagslib ) {
3012
            next unless $tag;
3013
            next if $tag == $itemfield;
3014
            for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
3015
                next if ( subfield_is_koha_internal_p($subfield) );
3016
                my $defaultvalue = $tagslib->{$tag}{$subfield}{defaultvalue};
3017
                if ( defined $defaultvalue and $defaultvalue ne '' ) {
3018
                    my @fields = $record->field($tag);
3019
                    if (@fields) {
3020
                        for my $field (@fields) {
3021
                            unless ( defined $field->subfield($subfield) ) {
3022
                                $field->add_subfields(
3023
                                    $subfield => $defaultvalue );
3024
                            }
3025
                        }
3026
                    }
3027
                    else {
3028
                        $record->insert_fields_ordered(
3029
                            MARC::Field->new(
3030
                                $tag, '', '', $subfield => $defaultvalue
3031
                            )
3032
                        );
3033
                    }
3034
                }
3035
            }
3036
        }
3037
    }
3038
}
3039
2989
1;
3040
1;
2990
__END__
3041
__END__
2991
3042
(-)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