From 549f5879842f5341df8103e1b58da793e8668537 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 3 Jan 2025 13:53:47 +0000 Subject: [PATCH] Bug 38689: Record errors on quote processing --- Koha/EDI.pm | 123 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 84 insertions(+), 39 deletions(-) diff --git a/Koha/EDI.pm b/Koha/EDI.pm index 179e2b4826b..04c6365a66b 100644 --- a/Koha/EDI.pm +++ b/Koha/EDI.pm @@ -609,52 +609,54 @@ sub transfer_items { } sub process_quote { - my $quote = shift; + my $quote_message = shift; - $quote->status('processing'); - $quote->update; + $quote_message->status('processing'); + $quote_message->update; - my $edi = Koha::Edifact->new( { transmission => $quote->raw_msg, } ); + my $edi = Koha::Edifact->new( { transmission => $quote_message->raw_msg, } ); my $messages = $edi->message_array(); my $process_errors = 0; - my $logger = Koha::Logger->get({ interface => 'edi' }); + my $logger = Koha::Logger->get( { interface => 'edi' } ); my $schema = Koha::Database->new()->schema(); my $message_count = 0; + my $split_message; my @added_baskets; # if auto & multiple baskets need to order all - if ( @{$messages} && $quote->vendor_id ) { + if ( @{$messages} && $quote_message->vendor_id ) { foreach my $msg ( @{$messages} ) { ++$message_count; - my $basketno = - NewBasket( $quote->vendor_id, 0, $quote->filename, q{}, - q{} . q{} ); + my $basketno = NewBasket( + $quote_message->vendor_id, 0, $quote_message->filename, q{}, + q{} . q{} + ); push @added_baskets, $basketno; if ( $message_count > 1 ) { - my $m_filename = $quote->filename; + my $m_filename = $quote_message->filename; $m_filename .= "_$message_count"; - $schema->resultset('EdifactMessage')->create( + $split_message = $schema->resultset('EdifactMessage')->create( { - message_type => $quote->message_type, - transfer_date => $quote->transfer_date, - vendor_id => $quote->vendor_id, - edi_acct => $quote->edi_acct, + message_type => $quote_message->message_type, + transfer_date => $quote_message->transfer_date, + vendor_id => $quote_message->vendor_id, + edi_acct => $quote_message->edi_acct, status => 'recmsg', basketno => $basketno, raw_msg => q{}, filename => $m_filename, } ); - } - else { - $quote->basketno($basketno); + } else { + $quote_message->basketno($basketno); } $logger->trace("Created basket :$basketno"); my $items = $msg->lineitems(); my $refnum = $msg->message_refno; for my $item ( @{$items} ) { - if ( !quote_item( $item, $quote, $basketno ) ) { + my $message = $split_message // $quote_message; + if ( !quote_item( $item, $message, $basketno ) ) { ++$process_errors; } } @@ -665,12 +667,12 @@ sub process_quote { $status = 'error'; } - $quote->status($status); - $quote->update; # status and basketno link - # Do we automatically generate orders for this vendor + $quote_message->status($status); + $quote_message->update; # status and basketno link + # Do we automatically generate orders for this vendor my $v = $schema->resultset('VendorEdiAccount')->search( { - vendor_id => $quote->vendor_id, + vendor_id => $quote_message->vendor_id, } )->single; if ( $v->auto_orders ) { @@ -682,25 +684,25 @@ sub process_quote { } ); Koha::Acquisition::Baskets->find($b)->close; + # Log the approval - if (C4::Context->preference("AcquisitionLog")) { - my $approved = Koha::Acquisition::Baskets->find( $b ); + if ( C4::Context->preference("AcquisitionLog") ) { + my $approved = Koha::Acquisition::Baskets->find($b); logaction( 'ACQUISITIONS', 'APPROVE_BASKET', $b, - to_json($approved->unblessed) + to_json( $approved->unblessed ) ); } } } - return; } sub quote_item { - my ( $item, $quote, $basketno ) = @_; + my ( $item, $quote_message, $basketno ) = @_; my $schema = Koha::Database->new()->schema(); my $logger = Koha::Logger->get({ interface => 'edi' }); @@ -709,6 +711,12 @@ sub quote_item { # So this call should not fail unless that has my $basket = Koha::Acquisition::Baskets->find( $basketno ); unless ( $basket ) { + $quote_message->add_to_edifact_errors( + { + section => "", + details => "Failed to create basket" + } + ); $logger->error('Skipping order creation no valid basketno'); return; } @@ -716,7 +724,7 @@ sub quote_item { my $bib = _check_for_existing_bib( $item->item_number_id() ); if ( !defined $bib ) { $bib = {}; - my $bib_record = _create_bib_from_quote( $item, $quote ); + my $bib_record = _create_bib_from_quote( $item, $quote_message ); # Check for and add default 008 as this is a mandatory field $bib_record = _handle_008_field($bib_record); @@ -737,6 +745,12 @@ sub quote_item { $order_quantity ||= 1; # quantity not necessarily present if ( $gir_count > 1 ) { if ( $gir_count != $order_quantity ) { + $quote_message->add_to_edifact_errors( + { + section => "GIR", + details => "Order for $order_quantity items, $gir_count segments present" + } + ); $logger->error( "Order for $order_quantity items, $gir_count segments present"); } @@ -747,7 +761,7 @@ sub quote_item { if (!$price) { $price = $item->price_gross; } - my $vendor = Koha::Acquisition::Booksellers->find( $quote->vendor_id ); + my $vendor = Koha::Acquisition::Booksellers->find( $quote_message->vendor_id ); # NB quote will not include tax info it only contains the list price my $ecost = _discounted_price( $vendor->discount, $price, $item->price_info_inclusive ); @@ -817,12 +831,21 @@ sub quote_item { my $skip = '0'; if ( !$budget ) { if ( $item->quantity > 1 ) { - carp 'Skipping line with no budget info'; + $quote_message->add_to_edifact_errors( + { + section => "GIR", + details => "Skipped GIR line with invalid budget: " . $item->girfield('fund_allocation') + } + ); $logger->trace('girfield skipped for invalid budget'); $skip++; - } - else { - carp 'Skipping line with no budget info'; + } else { + $quote_message->add_to_edifact_errors( + { + section => "GIR", + details => "Skipped orderline line with invalid budget: " . $item->girfield('fund_allocation') + } + ); $logger->trace('orderline skipped for invalid budget'); return; } @@ -849,7 +872,7 @@ sub quote_item { $ordernumber{ $budget->budget_id } = $o; if ( C4::Context->preference('AcqCreateItem') eq 'ordering' ) { - $item_hash = _create_item_from_quote( $item, $quote ); + $item_hash = _create_item_from_quote( $item, $quote_message ); my $created = 0; while ( $created < $order_quantity ) { @@ -876,6 +899,12 @@ sub quote_item { $rota->add_item($itemnumber); $logger->trace("Item added to rota $rota->id"); } else { + $quote_message->add_to_edifact_errors( + { + section => "LRP", + details => "No rota found for passed $lrp in orderline" + } + ); $logger->error("No rota found matching $lrp in orderline"); } } @@ -894,7 +923,12 @@ sub quote_item { if ( !$budget ) { my $bad_budget = $item->girfield( 'fund_allocation', $occurrence ); - carp 'Skipping line with no budget info'; + $quote_message->add_to_edifact_errors( + { + section => "GIR", + details => "Skipped GIR line with invalid budget: $bad_budget" + } + ); $logger->trace( "girfield skipped for invalid budget:$bad_budget"); ++$occurrence; ## lets look at the next one not this one again @@ -926,7 +960,7 @@ sub quote_item { if ( C4::Context->preference('AcqCreateItem') eq 'ordering' ) { if ( !defined $item_hash ) { - $item_hash = _create_item_from_quote( $item, $quote ); + $item_hash = _create_item_from_quote( $item, $quote_message ); } my $new_item = { itype => @@ -981,9 +1015,15 @@ sub quote_item { { key => 'stockrotationrotas_title' } ); if ($rota) { $rota->add_item($itemnumber); - $logger->trace("Item added to rota $rota->id"); + $logger->trace("Item added to rota " .$rota->id); } else { + $quote_message->add_to_edifact_errors( + { + section => "LRP", + details => "No rota found for passed $lrp in orderline" + } + ); $logger->error( "No rota found matching $lrp in orderline"); } @@ -1054,6 +1094,12 @@ sub quote_item { $logger->trace("Item added to rota $rota->id"); } else { + $quote_message->add_to_edifact_errors( + { + section => "LRP", + details => "No rota found for passed $lrp in orderline" + } + ); $logger->error( "No rota found matching $lrp in orderline"); } @@ -1065,7 +1111,6 @@ sub quote_item { } } return 1; - } sub get_edifact_ean { -- 2.47.1