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

(-)a/Koha/Import/Record.pm (+73 lines)
Lines 21-28 use Carp; Link Here
21
use MARC::Record;
21
use MARC::Record;
22
22
23
use C4::Context;
23
use C4::Context;
24
use C4::Biblio qw(ModBiblio);
25
use C4::AuthoritiesMarc qw(GuessAuthTypeCode ModAuthority);
24
use Koha::Database;
26
use Koha::Database;
25
use Koha::Import::Record::Biblios;
27
use Koha::Import::Record::Biblios;
28
use Koha::Import::Record::Auths;
26
use Koha::Import::Record::Matches;
29
use Koha::Import::Record::Matches;
27
30
28
use base qw(Koha::Object);
31
use base qw(Koha::Object);
Lines 72-77 sub import_biblio { Link Here
72
    return Koha::Import::Record::Biblio->_new_from_dbic( $import_biblio_rs );
75
    return Koha::Import::Record::Biblio->_new_from_dbic( $import_biblio_rs );
73
}
76
}
74
77
78
=head3 import_auth
79
80
Returns the import auth object for this import record
81
82
    my $import_auth = $import_record->import_auth()
83
84
=cut
85
86
sub import_auth {
87
    my ( $self ) = @_;
88
    my $import_auth_rs = $self->_result->import_auth;
89
    return Koha::Import::Record::Auth->_new_from_dbic( $import_auth_rs );
90
}
91
75
=head3 get_import_record_matches
92
=head3 get_import_record_matches
76
93
77
Returns the Import::Record::Matches for the record
94
Returns the Import::Record::Matches for the record
Lines 93-98 sub get_import_record_matches { Link Here
93
    return $matches->search({},{ order_by => { -desc => ['score','candidate_match_id'] } });
110
    return $matches->search({},{ order_by => { -desc => ['score','candidate_match_id'] } });
94
}
111
}
95
112
113
=head3 replace
114
115
Import the record to replace an existing record which is passed to this sub
116
117
    $import_record->replace({ biblio => $biblio_object });
118
119
=cut
120
121
sub replace {
122
    my ($self, $params) = @_;
123
    my $biblio = $params->{biblio};
124
    my $authority = $params->{authority};
125
126
    my $userenv = C4::Context->userenv;
127
    my $logged_in_patron = Koha::Patrons->find( $userenv->{number} );
128
129
    my $marc_record = $self->get_marc_record;
130
    my $xmlrecord;
131
    if( $biblio ){
132
        my $record = $biblio->metadata->record;
133
        $xmlrecord = $record->as_xml;
134
        my $context = { source => 'batchimport' };
135
        if ($logged_in_patron) {
136
            $context->{categorycode} = $logged_in_patron->categorycode;
137
            $context->{userid} = $logged_in_patron->userid;
138
        }
139
        ModBiblio(
140
            $marc_record,
141
            $biblio->id,
142
            $biblio->frameworkcode,
143
            {
144
                overlay_context   => $context,
145
                skip_record_index => 1
146
            }
147
        );
148
        $self->import_biblio->matched_biblionumber( $biblio->id )->store;
149
    } elsif( $authority ) {
150
        $xmlrecord = $authority->marcxml;
151
        ModAuthority(
152
            $authority->id,
153
            $marc_record,
154
            GuessAuthTypeCode($marc_record)
155
        );
156
        $self->import_auth->matched_authid( $authority->id )->store;
157
    } else {
158
        # We could also throw an exception
159
        return;
160
    }
161
    $self->marcxml_old( $xmlrecord );
162
    $self->status('imported');
163
    $self->overlay_status('match_applied');
164
    $self->store;
165
166
    return 1;
167
}
168
96
=head2 Internal methods
169
=head2 Internal methods
97
170
98
=head3 _type
171
=head3 _type
(-)a/t/db_dependent/Koha/Import/Records.t (-2 / +55 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 10;
22
use Test::More tests => 11;
23
23
24
use Koha::Import::Records;
24
use Koha::Import::Records;
25
use Koha::Database;
25
use Koha::Database;
Lines 86-89 is_deeply( $matches->next->unblessed, $match_2->unblessed, "Match 2 is the chose Link Here
86
$retrieved_record_1->delete;
86
$retrieved_record_1->delete;
87
is( Koha::Import::Records->search->count, $nb_of_records + 1, 'Delete should have deleted the record' );
87
is( Koha::Import::Records->search->count, $nb_of_records + 1, 'Delete should have deleted the record' );
88
88
89
subtest 'replace' => sub {
90
    plan tests => 4;
91
92
93
    # Replace biblio
94
    my $import_record = $builder->build_object({ class => 'Koha::Import::Records' });
95
    my $import_record_biblio = $builder->build_object({
96
        class => 'Koha::Import::Record::Biblios',
97
        value => {
98
            import_record_id => $import_record->id
99
        }
100
    });
101
102
    my $koha_biblio = $builder->build_sample_biblio({ title => "The before" });
103
    my $koha_xml = $koha_biblio->metadata->record->as_xml;
104
    my $import_biblio = $builder->build_sample_biblio({ title => "The after" });
105
    $import_record->marcxml( $import_biblio->metadata->record->as_xml )->store->discard_changes;
106
107
    $import_record->replace({ biblio => $koha_biblio });
108
    $koha_biblio->discard_changes;
109
    $import_record->discard_changes;
110
    is( $koha_biblio->title, "The after", "The Koha biblio is successfully updated" );
111
    is( $import_record->marcxml_old, $koha_xml, "The old marcxml in import records is correctly updated" );
112
113
    # Replace authority
114
    my $auth_record = MARC::Record->new;
115
    $auth_record->append_fields(
116
        MARC::Field->new( '100', '', '', a => 'Author' ),
117
    );
118
    my $auth_id = C4::AuthoritiesMarc::AddAuthority($auth_record, undef, 'PERSO_NAME');
119
    my $koha_auth = Koha::Authorities->find( $auth_id );
120
    $koha_xml = $koha_auth->marcxml;
121
    $import_record = $builder->build_object({ class => 'Koha::Import::Records' });
122
    my $import_record_auth = $builder->build_object({
123
        class => 'Koha::Import::Record::Auths',
124
        value => {
125
            import_record_id => $import_record->id
126
        }
127
    });
128
129
130
    my $field = $auth_record->field('100');
131
    $field->update( 'a' => 'Other author' );
132
    $import_record->marcxml( $auth_record->as_xml )->store->discard_changes;
133
134
    $import_record->replace({ authority => $koha_auth });
135
    $koha_auth->discard_changes;
136
    $import_record->discard_changes;
137
    my $updated_record = MARC::Record->new_from_xml( $koha_auth->marcxml, 'UTF-8');
138
    is( $updated_record->field('100')->as_string, $auth_record->field('100')->as_string, "The Koha auhtority record is correctly updated" );
139
    is( $import_record->marcxml_old, $koha_xml, "The old marcxml in import record is correctly updated" );
140
141
};
142
89
$schema->storage->txn_rollback;
143
$schema->storage->txn_rollback;
90
- 

Return to bug 32437