From f09773713ce991c3895443301d7895f03563111f Mon Sep 17 00:00:00 2001
From: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
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   | 23 ++++++++++++++----
 Koha/REST/V1/ExtendedAttributeTypes.pm |  1 +
 Koha/Schema/Result/Aqbookseller.pm     | 32 ++++++++++++++++++++++++++
 6 files changed, 63 insertions(+), 14 deletions(-)

diff --git a/Koha/Acquisition/Bookseller.pm b/Koha/Acquisition/Bookseller.pm
index b845aba7370..ad2b5d8cec3 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 2852bd250a9..ab9fd2741ef 100644
--- a/Koha/REST/V1/Acquisitions/Vendors.pm
+++ b/Koha/REST/V1/Acquisitions/Vendors.pm
@@ -99,10 +99,11 @@ 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 $vendor              = $c->req->json;
+    my $contacts            = delete $vendor->{contacts};
+    my $interfaces          = delete $vendor->{interfaces};
+    my $aliases             = delete $vendor->{aliases};
+    my $extended_attributes = delete $vendor->{extended_attributes};
 
     my $vendor_to_store = Koha::Acquisition::Bookseller->new_from_api( $c->req->json );
 
@@ -113,6 +114,12 @@ sub add {
         $vendor_to_store->aliases( $aliases       || [] );
         $vendor_to_store->interfaces( $interfaces || [] );
 
+        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(
             status  => 201,
@@ -143,6 +150,8 @@ sub update {
         my $contacts      = exists $vendor_update->{contacts}   ? delete $vendor_update->{contacts}   : undef;
         my $interfaces    = exists $vendor_update->{interfaces} ? delete $vendor_update->{interfaces} : undef;
         my $aliases       = exists $vendor_update->{aliases}    ? delete $vendor_update->{aliases}    : undef;
+        my $extended_attributes =
+            exists $vendor_update->{extended_attributes} ? delete $vendor_update->{extended_attributes} : undef;
 
         $vendor->set_from_api($vendor_update);
         $vendor->store();
@@ -151,6 +160,12 @@ sub update {
         $vendor->aliases( $aliases       || [] ) if defined $aliases;
         $vendor->interfaces( $interfaces || [] ) if defined $interfaces;
 
+        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,
             openapi => $c->objects->to_api($vendor),
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 dc066a36327..de9405e918c 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 },
+);
+
 =head2 koha_object_class
 
 Missing POD for koha_object_class.
-- 
2.48.1