From 94687db05c7911e9e932afed8092c1311565b04e Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Tue, 29 Oct 2024 17:12:09 +0000 Subject: [PATCH] Bug 38262: Update object classes --- Koha/Acquisition/Bookseller.pm | 2 +- Koha/Acquisition/Booksellers.pm | 2 +- Koha/AdditionalField.pm | 17 ++++----- Koha/REST/V1/Acquisitions/Vendors.pm | 50 ++++++++++++++++---------- Koha/REST/V1/ExtendedAttributeTypes.pm | 1 + Koha/Schema/Result/Aqbookseller.pm | 32 +++++++++++++++++ 6 files changed, 76 insertions(+), 28 deletions(-) diff --git a/Koha/Acquisition/Bookseller.pm b/Koha/Acquisition/Bookseller.pm index 3745e506739..9a49210449a 100644 --- a/Koha/Acquisition/Bookseller.pm +++ b/Koha/Acquisition/Bookseller.pm @@ -25,7 +25,7 @@ use Koha::Subscriptions; use C4::Contract qw( GetContracts ); -use base qw( Koha::Object ); +use base qw( Koha::Object::Mixin::AdditionalFields Koha::Object ); =head1 NAME diff --git a/Koha/Acquisition/Booksellers.pm b/Koha/Acquisition/Booksellers.pm index 1484396256d..466270751db 100644 --- a/Koha/Acquisition/Booksellers.pm +++ b/Koha/Acquisition/Booksellers.pm @@ -19,7 +19,7 @@ use Modern::Perl; use Koha::Acquisition::Bookseller; -use base qw( Koha::Objects ); +use base qw( Koha::Object::Mixin::AdditionalFields Koha::Objects ); =head1 NAME diff --git a/Koha/AdditionalField.pm b/Koha/AdditionalField.pm index 160d248322d..6b656087f3a 100644 --- a/Koha/AdditionalField.pm +++ b/Koha/AdditionalField.pm @@ -55,14 +55,15 @@ sub to_api { my ( $self, $params ) = @_; my $table_to_resource = { - 'accountlines:credit' => 'credit', - 'accountlines:debit' => 'debit', - 'aqbasket' => 'basket', - 'aqinvoices' => 'invoice', - 'erm_licenses' => 'license', - 'erm_agreements' => 'agreement', - 'erm_packages' => 'package', - 'aqorders' => 'order', + 'accountlines:credit' => 'credit', + 'accountlines:debit' => 'debit', + 'aqbasket' => 'basket', + 'aqinvoices' => 'invoice', + 'erm_licenses' => 'license', + 'erm_agreements' => 'agreement', + 'erm_packages' => 'package', + 'aqorders' => 'order', + 'aqbooksellers:vendor' => 'vendor', }; my $json = $self->SUPER::to_api($params); diff --git a/Koha/REST/V1/Acquisitions/Vendors.pm b/Koha/REST/V1/Acquisitions/Vendors.pm index 4f5fb17dae8..a470dc25334 100644 --- a/Koha/REST/V1/Acquisitions/Vendors.pm +++ b/Koha/REST/V1/Acquisitions/Vendors.pm @@ -85,13 +85,14 @@ Controller function that handles adding a new Koha::Acquisition::Bookseller obje sub add { my $c = shift->openapi->valid_input or return; - my $vendor = $c->req->json; - my $contacts = delete $vendor->{contacts}; - my $interfaces = delete $vendor->{interfaces}; - my $aliases = delete $vendor->{aliases}; - my $subscriptions = delete $vendor->{subscriptions}; - my $baskets = delete $vendor->{baskets}; - my $contracts = delete $vendor->{contracts}; + my $vendor = $c->req->json; + my $contacts = delete $vendor->{contacts}; + my $interfaces = delete $vendor->{interfaces}; + my $aliases = delete $vendor->{aliases}; + my $subscriptions = delete $vendor->{subscriptions}; + my $baskets = delete $vendor->{baskets}; + my $contracts = delete $vendor->{contracts}; + my $extended_attributes = delete $vendor->{extended_attributes}; my $vendor_to_store = Koha::Acquisition::Bookseller->new_from_api( $c->req->json ); @@ -99,8 +100,14 @@ sub add { $vendor_to_store->store; $vendor_to_store->contacts($contacts) if scalar($contacts) > 0; - $vendor_to_store->aliases($aliases) if scalar($aliases) > 0; - $vendor_to_store->interfaces($interfaces) if scalar($interfaces) > 0; + $vendor_to_store->aliases($aliases) if scalar(@$aliases) > 0; + $vendor_to_store->interfaces($interfaces) if scalar(@$interfaces) > 0; + + if ( scalar(@$extended_attributes) > 0 ) { + my @extended_attributes = + map { { 'id' => $_->{field_id}, 'value' => $_->{value} } } @{$extended_attributes}; + $vendor_to_store->extended_attributes( \@extended_attributes ); + } $c->res->headers->location( $c->req->url->to_string . '/' . $vendor_to_store->id ); return $c->render( @@ -128,20 +135,27 @@ sub update { unless $vendor; return try { - my $vendor_update = $c->req->json; - my $contacts = delete $vendor_update->{contacts}; - my $interfaces = delete $vendor_update->{interfaces}; - my $aliases = delete $vendor_update->{aliases}; - my $subscriptions = delete $vendor_update->{subscriptions}; - my $baskets = delete $vendor_update->{baskets}; - my $contracts = delete $vendor_update->{contracts}; + my $vendor_update = $c->req->json; + my $contacts = delete $vendor_update->{contacts}; + my $interfaces = delete $vendor_update->{interfaces}; + my $aliases = delete $vendor_update->{aliases}; + my $subscriptions = delete $vendor_update->{subscriptions}; + my $baskets = delete $vendor_update->{baskets}; + my $contracts = delete $vendor_update->{contracts}; + my $extended_attributes = delete $vendor_update->{extended_attributes}; $vendor->set_from_api($vendor_update); $vendor->store(); $vendor->contacts($contacts) if scalar($contacts) > 0; - $vendor->aliases($aliases) if scalar($aliases) > 0; - $vendor->interfaces($interfaces) if scalar($interfaces) > 0; + $vendor->aliases($aliases) if scalar(@$aliases) > 0; + $vendor->interfaces($interfaces) if scalar(@$interfaces) > 0; + + if ( scalar(@$extended_attributes) > 0 ) { + my @extended_attributes = + map { { 'id' => $_->{field_id}, 'value' => $_->{value} } } @{$extended_attributes}; + $vendor->extended_attributes( \@extended_attributes ); + } return $c->render( status => 200, diff --git a/Koha/REST/V1/ExtendedAttributeTypes.pm b/Koha/REST/V1/ExtendedAttributeTypes.pm index 844582c5a48..97d2cb916ab 100644 --- a/Koha/REST/V1/ExtendedAttributeTypes.pm +++ b/Koha/REST/V1/ExtendedAttributeTypes.pm @@ -54,6 +54,7 @@ sub list { agreement => 'erm_agreements', package => 'erm_packages', order => 'aqorders', + vendor => 'aqbooksellers:vendor', }; return try { diff --git a/Koha/Schema/Result/Aqbookseller.pm b/Koha/Schema/Result/Aqbookseller.pm index 269aa4d36d5..074feb16016 100644 --- a/Koha/Schema/Result/Aqbookseller.pm +++ b/Koha/Schema/Result/Aqbookseller.pm @@ -600,6 +600,38 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +__PACKAGE__->has_many( + "additional_field_values", + "Koha::Schema::Result::AdditionalFieldValue", + sub { + my ($args) = @_; + + return { + "$args->{foreign_alias}.record_id" => { -ident => "$args->{self_alias}.id" }, + + "$args->{foreign_alias}.field_id" => + { -in => \'(SELECT id FROM additional_fields WHERE tablename LIKE "aqbooksellers:%")' }, + }; + }, + { cascade_copy => 0, cascade_delete => 0 }, +); + +__PACKAGE__->has_many( + "extended_attributes", + "Koha::Schema::Result::AdditionalFieldValue", + sub { + my ($args) = @_; + + return { + "$args->{foreign_alias}.record_id" => { -ident => "$args->{self_alias}.id" }, + + "$args->{foreign_alias}.field_id" => + { -in => \'(SELECT id FROM additional_fields WHERE tablename LIKE "aqbooksellers:%")' }, + }; + }, + { cascade_copy => 0, cascade_delete => 0 }, +); + sub koha_object_class { 'Koha::Acquisition::Bookseller'; } -- 2.48.1