From 68cac4e646bc9465bc74cb56df3af3ab49e7dd46 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 1 Oct 2025 11:30:08 +0100 Subject: [PATCH] Bug 38195: Convert Koha::EDI to use Koha object classes This patch updates Koha::EDI to use modern Koha object classes instead of raw DBIx::Class access. Changes to vendor_edi_accounts access: - Added use Koha::EDI::Accounts; - Replaced three instances of $schema->resultset('VendorEdiAccount') with Koha::EDI::Accounts in: 1. create_edi_order() - Finding vendor account by vendor_id 2. process_invoice() - Finding vendor account by SAN 3. process_quote() - Finding vendor account for auto_orders check Changes to aqorders access: - Added use Koha::Acquisition::Orders; - Converted five instances from raw DBIx::Class to Koha::Objects: 1. create_edi_order() - Changed from ->all array to resultset with ->count, ->next, ->reset, and ->_resultset->all for iteration 2. process_invoice() - Changed ->find to use Koha::Acquisition::Orders 3-4. process_quote() - Changed two ->create calls to use Koha::Acquisition::Order->new()->store pattern 5. process_quote() - Changed ->find to use Koha::Acquisition::Orders Additional improvements: - Used idiomatic Koha::Objects patterns (->count, ->next, ->reset) - Maintained backward compatibility with existing code expectations - Cleaner variable naming (e.g., $vendor -> $edi_account) Benefits: - Consistent use of Koha object patterns throughout EDI code - Enables use of relationship methods from Koha objects - More maintainable and testable code - Better integration with Koha's ORM layer --- Koha/EDI.pm | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/Koha/EDI.pm b/Koha/EDI.pm index 42d08a3d3f1..f68019b2037 100644 --- a/Koha/EDI.pm +++ b/Koha/EDI.pm @@ -44,6 +44,8 @@ use Koha::Plugins; # Adds plugin dirs to @INC use Koha::Plugins::Handler; use Koha::Acquisition::Baskets; use Koha::Acquisition::Booksellers; +use Koha::Acquisition::Orders; +use Koha::EDI::Accounts; use Koha::Util::FrameworkPlugin qw( biblio_008 ); our $VERSION = 1.1; @@ -77,21 +79,24 @@ sub create_edi_order { my $schema = Koha::Database->new()->schema(); - my @orderlines = $schema->resultset('Aqorder')->search( + my $orderlines = Koha::Acquisition::Orders->search( { basketno => $basketno, orderstatus => 'new', } - )->all; + ); - if ( !@orderlines ) { + if ( !$orderlines->count ) { $logger->warn("No orderlines for basket $basketno"); return; } - my $vendor = $schema->resultset('VendorEdiAccount')->search( + my $first_order = $orderlines->next; + $orderlines->reset; # Reset iterator for later use + + my $edi_account = Koha::EDI::Accounts->search( { - vendor_id => $orderlines[0]->basketno->booksellerid->id, + vendor_id => $first_order->basket->booksellerid, } )->single; @@ -124,17 +129,17 @@ sub create_edi_order { my $response = @{$arr_ref} ? 1 : 0; my $edifact_order_params = { - orderlines => \@orderlines, - vendor => $vendor, + orderlines => [ $orderlines->_resultset->all ], + vendor => $edi_account, ean => $ean_obj, is_response => $response, }; my $edifact; - if ( $vendor->plugin ) { + if ( $edi_account->plugin ) { $edifact = Koha::Plugins::Handler->run( { - class => $vendor->plugin, + class => $edi_account->plugin, method => 'edifact_order', params => { params => $edifact_order_params, @@ -158,12 +163,12 @@ sub create_edi_order { my $order = { message_type => 'ORDERS', raw_msg => $m, - vendor_id => $vendor->vendor_id, + vendor_id => $edi_account->vendor_id, status => 'Pending', basketno => $basketno, filename => $edifact->filename(), transfer_date => $edifact->msg_date_string(), - edi_acct => $vendor->id, + edi_acct => $edi_account->id, }; $schema->resultset('EdifactMessage')->create($order); @@ -287,7 +292,7 @@ sub process_invoice { my $vendor_ean = $msg->supplier_ean; if ( !defined $vendor_acct || $vendor_ean ne $vendor_acct->san ) { - $vendor_acct = $schema->resultset('VendorEdiAccount')->search( + $vendor_acct = Koha::EDI::Accounts->search( { san => $vendor_ean, } @@ -335,7 +340,7 @@ sub process_invoice { } # ModReceiveOrder does not validate that $ordernumber exists validate here - my $order = $schema->resultset('Aqorder')->find($ordernumber); + my $order = Koha::Acquisition::Orders->find($ordernumber); if ( !$order ) { $invoice_message->add_to_edifact_errors( { @@ -350,7 +355,7 @@ sub process_invoice { next; } - my $bib = $order->biblionumber; + my $bib = $order->biblio; if ( !$bib ) { $invoice_message->add_to_edifact_errors( { @@ -404,7 +409,7 @@ sub process_invoice { $order->orderstatus('partial'); $order->quantity( $ordered - $quantity ); $order->update; - my $received_order = $order->copy( + my $received_order = $order->_result->copy( { ordernumber => undef, quantity => $quantity, @@ -695,12 +700,12 @@ sub process_quote { $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( + my $edi_account = Koha::EDI::Accounts->search( { vendor_id => $quote_message->vendor_id, } )->single; - if ( $v->auto_orders ) { + if ( $edi_account->auto_orders ) { for my $b (@added_baskets) { create_edi_order( { @@ -879,7 +884,7 @@ sub quote_item { if ( !$skip ) { $order_hash->{budget_id} = $budget->budget_id; - my $first_order = $schema->resultset('Aqorder')->create($order_hash); + my $first_order = Koha::Acquisition::Order->new($order_hash)->store; my $o = $first_order->ordernumber(); $logger->trace("Order created: $o"); @@ -965,7 +970,7 @@ sub quote_item { $order_hash->{budget_id} = $budget->budget_id; - my $new_order = $schema->resultset('Aqorder')->create($order_hash); + my $new_order = Koha::Acquisition::Order->new($order_hash)->store; my $o = $new_order->ordernumber(); $logger->trace("Order created: $o"); @@ -1049,7 +1054,7 @@ sub quote_item { # increment quantity in orderline for EXISTING budget in $budgets else { - my $row = $schema->resultset('Aqorder')->find( { ordernumber => $ordernumber{ $budget->budget_id } } ); + my $row = Koha::Acquisition::Orders->find( { ordernumber => $ordernumber{ $budget->budget_id } } ); if ($row) { my $qty = $row->quantity; $qty++; -- 2.51.0