Bug 20882: Move items.uri to mediumtext
[koha-ffzg.git] / acqui / updatesupplier.pl
index 04f4b5f..5b077f1 100755 (executable)
@@ -46,16 +46,16 @@ contact_serialsprimary.
 
 =cut
 
-use strict;
-use warnings;
+use Modern::Perl;
 use List::Util;
 use C4::Context;
 use C4::Auth;
 
-use C4::Bookseller qw( ModBookseller AddBookseller );
-use C4::Bookseller::Contact;
 use C4::Biblio;
 use C4::Output;
+
+use Koha::Acquisition::Bookseller::Contacts;
+use Koha::Acquisition::Booksellers;
 use CGI qw ( -utf8 );
 
 my $input=new CGI;
@@ -98,7 +98,7 @@ my @contacts;
 my %contact_info;
 
 foreach (qw(id name position phone altphone fax email notes orderacquisition claimacquisition claimissues acqprimary serialsprimary)) {
-    $contact_info{$_} = [ $input->param('contact_' . $_) ];
+    $contact_info{$_} = [ $input->multi_param('contact_' . $_) ];
 }
 
 for my $cnt (0..scalar(@{$contact_info{'id'}})) {
@@ -108,15 +108,27 @@ for my $cnt (0..scalar(@{$contact_info{'id'}})) {
         $contact{$_} = $contact_info{$_}->[$cnt];
         $real_contact = 1 if $contact{$_};
     }
-    push @contacts, C4::Bookseller::Contact->new(\%contact) if $real_contact;
+    push @contacts, \%contact if $real_contact;
 }
 
 if($data{'name'}) {
-       if ($data{'id'}){
-        ModBookseller(\%data, \@contacts);
-       } else {
-        $data{id}=AddBookseller(\%data, \@contacts);
-       }
+    if ( $data{id} ) {
+        # Update
+        my $bookseller = Koha::Acquisition::Booksellers->find( $data{id} )->set(\%data)->store;
+        # Delete existing contacts
+        $bookseller->contacts->delete;
+    } else {
+        # Insert
+        delete $data{id}; # Remove the key if exists
+        my $bookseller = Koha::Acquisition::Bookseller->new( \%data )->store;
+        $data{id} = $bookseller->id;
+    }
+    # Insert contacts
+    for my $contact ( @contacts ) {
+        $contact->{booksellerid} = $data{id};
+        Koha::Acquisition::Bookseller::Contact->new( $contact )->store
+    }
+
     #redirect to booksellers.pl
     print $input->redirect("booksellers.pl?booksellerid=".$data{id});
 } else {