From bb4cd033f3ac1b8c8f284fce8d4eadd605d4be71 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Fri, 25 Oct 2024 11:19:24 +0000 Subject: [PATCH] Bug 38271: Handle missing 008 fields in EDI This patch adds handling to create a default 008 field on MARC records that are missing one when created through EDI. Test plan: 1) prove t/db_dependent/Koha/EDI.t Signed-off-by: Kyle M Hall --- Koha/EDI.pm | 24 +++++ t/db_dependent/Koha/EDI.t | 168 ++++++++++++++++++++++++++++- t/edi_testfiles/QUOTES_SMALL_2.CEQ | 1 + 3 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 t/edi_testfiles/QUOTES_SMALL_2.CEQ diff --git a/Koha/EDI.pm b/Koha/EDI.pm index 137a2210c27..4aeb1884d89 100644 --- a/Koha/EDI.pm +++ b/Koha/EDI.pm @@ -45,6 +45,7 @@ use Koha::Plugins; # Adds plugin dirs to @INC use Koha::Plugins::Handler; use Koha::Acquisition::Baskets; use Koha::Acquisition::Booksellers; +use Koha::Util::FrameworkPlugin qw( biblio_008 ); our $VERSION = 1.1; @@ -684,6 +685,10 @@ sub quote_item { if ( !defined $bib ) { $bib = {}; my $bib_record = _create_bib_from_quote( $item, $quote ); + + # Check for and add default 008 as this is a mandatory field + $bib_record = _handle_008_field($bib_record); + ( $bib->{biblionumber}, $bib->{biblioitemnumber} ) = AddBiblio( $bib_record, q{} ); $logger->trace("New biblio added $bib->{biblionumber}"); @@ -1215,6 +1220,17 @@ sub _create_item_from_quote { return $item_hash; } +sub _handle_008_field { + my ( $bib_record ) = @_; + + if ( !$bib_record->field('008') ) { + my $default008 = biblio_008(); + $bib_record->insert_fields_ordered( MARC::Field->new('008', $default008)); + } + + return $bib_record +} + 1; __END__ @@ -1373,6 +1389,14 @@ Koha::EDI Returns the Aqbudget object for the active budget given the passed budget_code or undefined if one does not exist +=head2 _handle_008_field + + bib_record = _handle_008_field($bib_record) + + Checks whether an 008 field exists on the record and adds a default field it does not + + Returns the bib_record + =head1 AUTHOR Colin Campbell diff --git a/t/db_dependent/Koha/EDI.t b/t/db_dependent/Koha/EDI.t index 46c24557603..a82ad168b15 100755 --- a/t/db_dependent/Koha/EDI.t +++ b/t/db_dependent/Koha/EDI.t @@ -20,8 +20,9 @@ use Modern::Perl; use FindBin qw( $Bin ); -use Test::More tests => 1; +use Test::More tests => 2; use Test::Warn; +use Test::MockModule; use t::lib::Mocks; use t::lib::TestBuilder; @@ -123,3 +124,168 @@ subtest 'process_quote' => sub { $schema->storage->txn_rollback; }; + +subtest '_handle_008_field' => sub { + plan tests => 4; + + $schema->storage->txn_begin; + + # Add our test quote file to the database for testing against + my $account = $builder->build( + { + source => 'VendorEdiAccount', + value => { + description => 'test vendor', transport => 'FILE', + } + } + ); + my $dirname = ( $Bin =~ /^(.*\/t\/)/ ? $1 . 'edi_testfiles/' : q{} ); + my $filename = 'QUOTES_SMALL.CEQ'; + ok( -e $dirname . $filename, 'File QUOTES_SMALL.CEQ found' ); + + my $trans = Koha::Edifact::Transport->new( $account->{id} ); + $trans->working_directory($dirname); + + my $mhash = $trans->message_hash(); + $mhash->{message_type} = 'QUOTE'; + $trans->ingest( $mhash, $filename ); + + my $quote = $schema->resultset('EdifactMessage')->find( { filename => $filename } ); + + # Test quote expects REF to be a valid and active fund code + my $active_period = $builder->build( + { + source => 'Aqbudgetperiod', + value => { budget_period_active => 1 } + } + ); + my $fund = $builder->build( + { + source => 'Aqbudget', + value => { + budget_code => 'REF', + budget_period_id => $active_period->{budget_period_id} + } + } + ); + + # The quote expects a ROT1 stock rotation roata to exist + my $rota = $builder->build_object( + { + class => 'Koha::StockRotationRotas', + value => { title => 'ROT1' } + } + ); + $builder->build( + { + source => 'Stockrotationstage', + value => { rota_id => $rota->rota_id }, + } + ); + + # Process the test quote file + process_quote($quote); + + $quote->get_from_storage; + + # Tests for generated basket for passed quote file + my $baskets = Koha::Acquisition::Baskets->search( { booksellerid => $account->{vendor_id} } ); + my $basket = $baskets->next; + + my $orders = $basket->orders; + my $order = $orders->next; + + my $biblio = $order->biblio; + my $record = $biblio->record; + my $record_field = $record->field('008'); + + is(exists($record_field->{_data}), 1, 'Field has been added'); + + # Test without calling the 008 handler + $account = $builder->build( + { + source => 'VendorEdiAccount', + value => { + description => 'test vendor', transport => 'FILE', + } + } + ); + $dirname = ( $Bin =~ /^(.*\/t\/)/ ? $1 . 'edi_testfiles/' : q{} ); + $filename = 'QUOTES_SMALL_2.CEQ'; + ok( -e $dirname . $filename, 'File QUOTES_SMALL_2.CEQ found' ); + + $trans = Koha::Edifact::Transport->new( $account->{id} ); + $trans->working_directory($dirname); + + $mhash = $trans->message_hash(); + $mhash->{message_type} = 'QUOTE'; + $trans->ingest( $mhash, $filename ); + + $quote = $schema->resultset('EdifactMessage')->find( { filename => $filename } ); + + # Test quote expects REF to be a valid and active fund code + $active_period = $builder->build( + { + source => 'Aqbudgetperiod', + value => { budget_period_active => 1 } + } + ); + $fund = $builder->build( + { + source => 'Aqbudget', + value => { + budget_code => 'REF2', + budget_period_id => $active_period->{budget_period_id} + } + } + ); + + # The quote expects a ROT1 stock rotation roata to exist + $rota = $builder->build_object( + { + class => 'Koha::StockRotationRotas', + value => { title => 'ROT2' } + } + ); + $builder->build( + { + source => 'Stockrotationstage', + value => { rota_id => $rota->rota_id }, + } + ); + + # Process the test quote file + my $edi_module = Test::MockModule->new('Koha::EDI'); + $edi_module->mock( + '_check_for_existing_bib', + sub { + my $bib_record = shift; + return undef; + } + ); + $edi_module->mock( + '_handle_008_field', + sub { + my $bib_record = shift; + return $bib_record; + } + ); + process_quote($quote); + + $quote->get_from_storage; + + # Tests for generated basket for passed quote file + $baskets = Koha::Acquisition::Baskets->search( { booksellerid => $account->{vendor_id} } ); + $basket = $baskets->next; + + $orders = $basket->orders; + $order = $orders->next; + + $biblio = $order->biblio; + $record = $biblio->record; + $record_field = $record->field('008'); + + is( $record_field->{_data}, undef, 'Field has not been added' ); + + $schema->storage->txn_rollback; +} \ No newline at end of file diff --git a/t/edi_testfiles/QUOTES_SMALL_2.CEQ b/t/edi_testfiles/QUOTES_SMALL_2.CEQ new file mode 100644 index 00000000000..26126a27a40 --- /dev/null +++ b/t/edi_testfiles/QUOTES_SMALL_2.CEQ @@ -0,0 +1 @@ +UNA:+.? 'UNB+UNOC:3+5013546027173+5013546098818+240123:0945+1044416+ASKEDI:+QUOTES++++'UNH+1044416001+QUOTES:D:96A:UN:EAN002'BGM+31C::28+WO0800131+9'DTM+137:20240123:102'RFF+ON:orders 23/1'CUX+2:GBP:12'NAD+BY+5013546098818::9'NAD+SU+5013546027173::9'LIN+1++9781529923766:EN'PIA+5+152992376X:IB'IMD+L+010+:::Thompson'IMD+L+011+:::Louise'IMD+L+050+:::Lucky'IMD+L+060+:::learning to live again'IMD+L+120+:::Ebury Spotlight'IMD+L+170+:::2024'IMD+L+180+:::304'IMD+L+220+:::Hbk'IMD+L+230+:::791.45'IMD+L+250+:::AN'QTY+1:1'GIR+001+BOOK:LST+ANF:LSQ+CPL:LLO+REF:LFN+791.45:LCL'GIR+001+22.00:LCV'GIR+001+ROT1:LRP'PRI+AAE:22.00'RFF+QLI:WO0800131000001'UNS+S'CNT+2:1'UNT+27+1044416001'UNZ+1+1044416' -- 2.39.5 (Apple Git-154)