From 55563fb04850b60a76b8677e3fde67edc56c29f8 Mon Sep 17 00:00:00 2001
From: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Date: Mon, 30 Sep 2024 14:22:34 +0000
Subject: [PATCH] Bug 38010: Adjust vendor update logic

This patch handles the embedded data that might also need updating when editing a vendor
---
 Koha/REST/V1/Acquisitions/Vendors.pm | 31 ++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)

diff --git a/Koha/REST/V1/Acquisitions/Vendors.pm b/Koha/REST/V1/Acquisitions/Vendors.pm
index 56351fa9252..ddd414d0b4d 100644
--- a/Koha/REST/V1/Acquisitions/Vendors.pm
+++ b/Koha/REST/V1/Acquisitions/Vendors.pm
@@ -85,10 +85,13 @@ 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 $subscriptions = delete $vendor->{subscriptions};
+    my $baskets       = delete $vendor->{baskets};
+    my $contracts     = delete $vendor->{contracts};
 
     my $vendor_to_store = Koha::Acquisition::Bookseller->new_from_api( $c->req->json );
 
@@ -121,15 +124,31 @@ Controller function that handles updating a Koha::Acquisition::Bookseller object
 sub update {
     my $c = shift->openapi->valid_input or return;
 
-    my $vendor = Koha::Acquisition::Booksellers->find( $c->param('vendor_id') );
+    my $vendor_id = $c->param('vendor_id');
+    my $vendor    = Koha::Acquisition::Booksellers->find($vendor_id);
 
     return $c->render_resource_not_found("Vendor")
         unless $vendor;
 
     return try {
-        $vendor->set_from_api( $c->req->json );
+        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};
+
+        $vendor->set_from_api($vendor_update);
         $vendor->store();
 
+        foreach my $contact (@$contacts) {
+            $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;
+
         return $c->render(
             status  => 200,
             openapi => $c->objects->to_api($vendor),
-- 
2.48.1