From b487e519505a9a4613568aa5af8494ee9057a41f Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 1 Oct 2025 07:24:28 +0100 Subject: [PATCH] Bug 38195: Convert admin/edi_accounts.pl to use Koha::EDI::Account(s) This patch modernizes the EDI accounts administration controller to use the new Koha::EDI::Account(s) object classes instead of raw DBIx::Class result access. Changes to admin/edi_accounts.pl: - Replaced Aqbookseller searches with Koha::Acquisition::Booksellers - Replaced FileTransport searches with Koha::File::Transports - Updated account save operations to use Koha::EDI::Account->new/set/store - Updated account delete to use Koha::EDI::Accounts->find->delete - Simplified display logic using to_api with embed parameter for automatic relationship inflation and JSON status decoding - Leverages Koha::File::Transport->to_api which handles JSON decoding of the status field automatically Benefits: - Much cleaner, more maintainable code following modern Koha patterns - Automatic relationship inflation via to_api embed parameter - Status decoding handled automatically by Koha::File::Transport->to_api - Single line conversion using map and to_api - Consistent API representation across the codebase - No manual JSON decoding or data transformation in controller --- admin/edi_accounts.pl | 72 +++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 48 deletions(-) diff --git a/admin/edi_accounts.pl b/admin/edi_accounts.pl index ffbf6e9b04e..be791a752d5 100755 --- a/admin/edi_accounts.pl +++ b/admin/edi_accounts.pl @@ -20,16 +20,17 @@ use Modern::Perl; use CGI; -use JSON qw( decode_json ); use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_html_with_http_headers ); -use Koha::Database; +use Koha::Acquisition::Booksellers; +use Koha::EDI::Account; +use Koha::EDI::Accounts; use Koha::Encryption; +use Koha::File::Transports; use Koha::Plugins; -our $input = CGI->new(); -our $schema = Koha::Database->new()->schema(); +our $input = CGI->new(); our ( $template, $loggedinuser, $cookie ) = get_template_and_user( { @@ -48,21 +49,18 @@ $op ||= 'display'; if ( $op eq 'acct_form' ) { show_account($crypt); $template->param( acct_form => 1 ); - my @vendors = $schema->resultset('Aqbookseller')->search( - undef, - { - columns => [ 'name', 'id' ], - order_by => { -asc => 'name' } - } + my $vendors = Koha::Acquisition::Booksellers->search( + {}, + { order_by => { -asc => 'name' } } ); - $template->param( vendors => \@vendors ); + $template->param( vendors => $vendors ); # Get available file transports for selection - my @file_transports = $schema->resultset('FileTransport')->search( + my $file_transports = Koha::File::Transports->search( {}, { order_by => { -asc => 'name' } } ); - $template->param( file_transports => \@file_transports ); + $template->param( file_transports => $file_transports ); if ( C4::Context->config("enable_plugins") ) { my @plugins = Koha::Plugins->new()->GetPlugins( @@ -80,7 +78,7 @@ if ( $op eq 'acct_form' ) { # validate & display my $id = $input->param('id'); - my $fields = { + my $params = { description => scalar $input->param('description'), vendor_id => scalar $input->param('vendor_id'), file_transport_id => scalar $input->param('file_transport_id') || undef, @@ -96,41 +94,26 @@ if ( $op eq 'acct_form' ) { }; if ($id) { - $schema->resultset('VendorEdiAccount')->search( - { - id => $id, - } - )->update_all($fields); + my $account = Koha::EDI::Accounts->find($id); + $account->set($params)->store if $account; } else { # new record - $schema->resultset('VendorEdiAccount')->create($fields); + Koha::EDI::Account->new($params)->store; } } elsif ( $op eq 'cud-delete_confirmed' ) { - - $schema->resultset('VendorEdiAccount')->search( { id => scalar $input->param('id'), } )->delete_all; + my $account = Koha::EDI::Accounts->find( scalar $input->param('id') ); + $account->delete if $account; } # we do a default display after deletes and saves # as well as when that's all you want $template->param( display => 1 ); - my $ediaccounts = $schema->resultset('VendorEdiAccount')->search( - {}, - { prefetch => [ 'vendor', 'file_transport' ] } - ); + my $ediaccounts = Koha::EDI::Accounts->search(); - # Decode file_transport status for each account - my @ediaccounts; - while ( my $ediaccount = $ediaccounts->next ) { - my $unblessed = { $ediaccount->get_inflated_columns }; - $unblessed->{vendor} = { $ediaccount->vendor->get_inflated_columns }; - if ( $ediaccount->file_transport ) { - $unblessed->{file_transport} = { $ediaccount->file_transport->get_inflated_columns }; - $unblessed->{file_transport}->{status} = - $ediaccount->file_transport->status ? decode_json( $ediaccount->file_transport->status ) : undef; - } - push @ediaccounts, $unblessed; - } + # Convert to API representation with embedded relationships + my @accounts_for_template = + map { $_->to_api( { embed => { vendor => {}, file_transport => {} } } ) } $ediaccounts->as_list; - $template->param( ediaccounts => \@ediaccounts ); + $template->param( ediaccounts => \@accounts_for_template ); } $template->param( @@ -159,21 +142,14 @@ output_html_with_http_headers( $input, $cookie, $template->output ); sub get_account { my $id = shift; - - my $account = $schema->resultset('VendorEdiAccount')->find($id); - if ($account) { - return $account; - } - - # passing undef will default to add - return; + return Koha::EDI::Accounts->find($id); } sub show_account { my $crypt = shift; my $acct_id = $input->param('id'); if ($acct_id) { - my $acct = $schema->resultset('VendorEdiAccount')->find($acct_id); + my $acct = Koha::EDI::Accounts->find($acct_id); if ($acct) { $template->param( account => $acct ); } -- 2.51.0