From c3c39251cf0f511083eec402dc2a978f61af52b7 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Thu, 5 Dec 2024 13:58:30 +0000 Subject: [PATCH] Bug 38010: Prevent vendor contacts from duplicating --- Koha/Acquisition/Bookseller.pm | 25 ++++++++++++++++--- Koha/REST/V1/Acquisitions/Vendors.pm | 10 ++------ .../vue/components/Vendors/VendorContacts.vue | 4 +-- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/Koha/Acquisition/Bookseller.pm b/Koha/Acquisition/Bookseller.pm index 41d32ca0a29..f10c0ea4f52 100644 --- a/Koha/Acquisition/Bookseller.pm +++ b/Koha/Acquisition/Bookseller.pm @@ -60,11 +60,30 @@ Returns the list of contacts for the vendor =cut sub contacts { - my ($self) = @_; - my $contacts_rs = $self->_result->aqcontacts; - return Koha::Acquisition::Bookseller::Contacts->_new_from_dbic( $contacts_rs ); + my ( $self, $contacts ) = @_; + + if ($contacts) { + my $schema = $self->_result->result_source->schema; + $schema->txn_do( + sub { + $self->contacts->delete; + for my $contact (@$contacts) { + Koha::Acquisition::Bookseller::Contact->new( + { + %$contact, + booksellerid => $self->id, + } + )->store; + } + } + ); + } + + my $rs = $self->_result->aqcontacts; + return Koha::Acquisition::Bookseller::Contacts->_new_from_dbic($rs); } + =head3 contracts my $vendor = Koha::Acquisition::Booksellers->find( $id ); diff --git a/Koha/REST/V1/Acquisitions/Vendors.pm b/Koha/REST/V1/Acquisitions/Vendors.pm index 8cd8b02dcfe..13f464157ed 100644 --- a/Koha/REST/V1/Acquisitions/Vendors.pm +++ b/Koha/REST/V1/Acquisitions/Vendors.pm @@ -100,10 +100,7 @@ sub add { return try { $vendor_to_store->store; - foreach my $contact (@$contacts) { - $contact->{booksellerid} = $vendor_to_store->id; - Koha::Acquisition::Bookseller::Contact->new($contact)->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; @@ -145,10 +142,7 @@ sub update { $vendor->set_from_api($vendor_update); $vendor->store(); - foreach my $contact (@$contacts) { - $contact->{booksellerid} = $vendor_id; - Koha::Acquisition::Bookseller::Contact->new($contact)->store; - } + $vendor->contacts($contacts) if scalar($contacts) > 0; $vendor->aliases($aliases) if scalar($aliases) > 0; $vendor->interfaces($interfaces) if scalar($interfaces) > 0; diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorContacts.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorContacts.vue index 148abac87b2..7e605f51eb7 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorContacts.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorContacts.vue @@ -149,7 +149,7 @@
- Acquisitions options + {{ $__("Acquisitions options") }}
- Serials options + {{ $__("Serials options") }}