From ea10ac9de6dcffce16eb068c2240374f905ee8e5 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 | 1 + Koha/REST/V1/Acquisitions/Vendors.pm | 49 ++++++++++++++++---------- Koha/REST/V1/ExtendedAttributeTypes.pm | 1 + Koha/Schema/Result/Aqbookseller.pm | 32 +++++++++++++++++ 6 files changed, 67 insertions(+), 20 deletions(-) diff --git a/Koha/Acquisition/Bookseller.pm b/Koha/Acquisition/Bookseller.pm index 41d32ca0a29..020b7ebd6e9 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 ec0263697f9..989bc63ca66 100644 --- a/Koha/AdditionalField.pm +++ b/Koha/AdditionalField.pm @@ -63,6 +63,7 @@ sub to_api { '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 8cd8b02dcfe..1309033651d 100644 --- a/Koha/REST/V1/Acquisitions/Vendors.pm +++ b/Koha/REST/V1/Acquisitions/Vendors.pm @@ -87,13 +87,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 ); @@ -104,8 +105,13 @@ sub add { $contact->{booksellerid} = $vendor_to_store->id; Koha::Acquisition::Bookseller::Contact->new($contact)->store; } - $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( @@ -134,13 +140,14 @@ 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(); @@ -149,8 +156,14 @@ sub update { $contact->{booksellerid} = $vendor_id; Koha::Acquisition::Bookseller::Contact->new($contact)->store; } - $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..07e70d520d5 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 528713c9bff..8b52c7b92c8 100644 --- a/Koha/Schema/Result/Aqbookseller.pm +++ b/Koha/Schema/Result/Aqbookseller.pm @@ -601,6 +601,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.39.3 (Apple Git-146)