use MARC::Record;
use C4::Context;
+use C4::Biblio qw(ModBiblio);
+use C4::AuthoritiesMarc qw(GuessAuthTypeCode ModAuthority);
use Koha::Database;
use Koha::Import::Record::Biblios;
+use Koha::Import::Record::Auths;
use Koha::Import::Record::Matches;
use base qw(Koha::Object);
return Koha::Import::Record::Biblio->_new_from_dbic( $import_biblio_rs );
}
+=head3 import_auth
+
+Returns the import auth object for this import record
+
+ my $import_auth = $import_record->import_auth()
+
+=cut
+
+sub import_auth {
+ my ( $self ) = @_;
+ my $import_auth_rs = $self->_result->import_auth;
+ return Koha::Import::Record::Auth->_new_from_dbic( $import_auth_rs );
+}
+
=head3 get_import_record_matches
Returns the Import::Record::Matches for the record
return $matches->search({},{ order_by => { -desc => ['score','candidate_match_id'] } });
}
+=head3 replace
+
+Import the record to replace an existing record which is passed to this sub
+
+ $import_record->replace({ biblio => $biblio_object });
+
+=cut
+
+sub replace {
+ my ($self, $params) = @_;
+ my $biblio = $params->{biblio};
+ my $authority = $params->{authority};
+
+ my $userenv = C4::Context->userenv;
+ my $logged_in_patron = Koha::Patrons->find( $userenv->{number} );
+
+ my $marc_record = $self->get_marc_record;
+ my $xmlrecord;
+ if( $biblio ){
+ my $record = $biblio->metadata->record;
+ $xmlrecord = $record->as_xml;
+ my $context = { source => 'batchimport' };
+ if ($logged_in_patron) {
+ $context->{categorycode} = $logged_in_patron->categorycode;
+ $context->{userid} = $logged_in_patron->userid;
+ }
+ ModBiblio(
+ $marc_record,
+ $biblio->id,
+ $biblio->frameworkcode,
+ {
+ overlay_context => $context,
+ skip_record_index => 1
+ }
+ );
+ $self->import_biblio->matched_biblionumber( $biblio->id )->store;
+ } elsif( $authority ) {
+ $xmlrecord = $authority->marcxml;
+ ModAuthority(
+ $authority->id,
+ $marc_record,
+ GuessAuthTypeCode($marc_record)
+ );
+ $self->import_auth->matched_authid( $authority->id )->store;
+ } else {
+ # We could also throw an exception
+ return;
+ }
+ $self->marcxml_old( $xmlrecord );
+ $self->status('imported');
+ $self->overlay_status('match_applied');
+ $self->store;
+
+ return 1;
+}
+
=head2 Internal methods
=head3 _type
use Modern::Perl;
-use Test::More tests => 10;
+use Test::More tests => 11;
use Koha::Import::Records;
use Koha::Database;
$retrieved_record_1->delete;
is( Koha::Import::Records->search->count, $nb_of_records + 1, 'Delete should have deleted the record' );
+subtest 'replace' => sub {
+ plan tests => 4;
+
+
+ # Replace biblio
+ my $import_record = $builder->build_object({ class => 'Koha::Import::Records' });
+ my $import_record_biblio = $builder->build_object({
+ class => 'Koha::Import::Record::Biblios',
+ value => {
+ import_record_id => $import_record->id
+ }
+ });
+
+ my $koha_biblio = $builder->build_sample_biblio({ title => "The before" });
+ my $koha_xml = $koha_biblio->metadata->record->as_xml;
+ my $import_biblio = $builder->build_sample_biblio({ title => "The after" });
+ $import_record->marcxml( $import_biblio->metadata->record->as_xml )->store->discard_changes;
+
+ $import_record->replace({ biblio => $koha_biblio });
+ $koha_biblio->discard_changes;
+ $import_record->discard_changes;
+ is( $koha_biblio->title, "The after", "The Koha biblio is successfully updated" );
+ is( $import_record->marcxml_old, $koha_xml, "The old marcxml in import records is correctly updated" );
+
+ # Replace authority
+ my $auth_record = MARC::Record->new;
+ $auth_record->append_fields(
+ MARC::Field->new( '100', '', '', a => 'Author' ),
+ );
+ my $auth_id = C4::AuthoritiesMarc::AddAuthority($auth_record, undef, 'PERSO_NAME');
+ my $koha_auth = Koha::Authorities->find( $auth_id );
+ $koha_xml = $koha_auth->marcxml;
+ $import_record = $builder->build_object({ class => 'Koha::Import::Records' });
+ my $import_record_auth = $builder->build_object({
+ class => 'Koha::Import::Record::Auths',
+ value => {
+ import_record_id => $import_record->id
+ }
+ });
+
+
+ my $field = $auth_record->field('100');
+ $field->update( 'a' => 'Other author' );
+ $import_record->marcxml( $auth_record->as_xml )->store->discard_changes;
+
+ $import_record->replace({ authority => $koha_auth });
+ $koha_auth->discard_changes;
+ $import_record->discard_changes;
+ my $updated_record = MARC::Record->new_from_xml( $koha_auth->marcxml, 'UTF-8');
+ is( $updated_record->field('100')->as_string, $auth_record->field('100')->as_string, "The Koha auhtority record is correctly updated" );
+ is( $import_record->marcxml_old, $koha_xml, "The old marcxml in import record is correctly updated" );
+
+};
+
$schema->storage->txn_rollback;