diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 9265c40e52c4..314d4c4b264a 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -474,7 +474,7 @@ class Import implements ShouldQueue unset($modified['id']); unset($modified['contacts']); - $client = $vendor_repository->save( + $vendor = $vendor_repository->save( $modified, VendorFactory::create( $this->company->id, @@ -482,29 +482,29 @@ class Import implements ShouldQueue ) ); - $client->contacts()->forceDelete(); + $vendor->contacts()->forceDelete(); if (array_key_exists('contacts', $resource)) { // need to remove after importing new migration.json $modified_contacts = $resource['contacts']; - foreach ($modified_contacts as $key => $client_contacts) { + foreach ($modified_contacts as $key => $vendor_contacts) { $modified_contacts[$key]['company_id'] = $this->company->id; $modified_contacts[$key]['user_id'] = $this->processUserId($resource); - $modified_contacts[$key]['client_id'] = $client->id; + $modified_contacts[$key]['client_id'] = $vendor->id; $modified_contacts[$key]['password'] = 'mysuperpassword'; // @todo, and clean up the code.. unset($modified_contacts[$key]['id']); } $saveable_contacts['contacts'] = $modified_contacts; - $contact_repository->save($saveable_contacts, $client); + $contact_repository->save($saveable_contacts, $vendor); } $key = "vendors_{$resource['id']}"; $this->ids['vendors'][$key] = [ 'old' => $resource['id'], - 'new' => $client->id, + 'new' => $vendor->id, ]; } @@ -1052,10 +1052,18 @@ class Import implements ShouldQueue $modified['company_id'] = $this->company->id; $modified['user_id'] = $this->transformId('users', $resource['user_id']); - $modified['client_id'] = $this->transformId('clients', $resource['client_id']); - $modified['invoice_id'] = $this->transformId('invoices', $resource['invoice_id']); - $modified['project_id'] = $this->transformId('projects', $resource['project_id']); - $modified['status_id'] = $this->transformId('task_statuses', $resource['status_id']); + + if(isset($modified['client_id'])) + $modified['client_id'] = $this->transformId('clients', $resource['client_id']); + + if(isset($modified['invoice_id'])) + $modified['invoice_id'] = $this->transformId('invoices', $resource['invoice_id']); + + if(isset($modified['project_id'])) + $modified['project_id'] = $this->transformId('projects', $resource['project_id']); + + if(isset($modified['status_id'])) + $modified['status_id'] = $this->transformId('task_statuses', $resource['status_id']); $task = Task::Create($modified); @@ -1085,7 +1093,9 @@ class Import implements ShouldQueue $modified['company_id'] = $this->company->id; $modified['user_id'] = $this->transformId('users', $resource['user_id']); - $modified['client_id'] = $this->transformId('clients', $resource['client_id']); + + if(isset($modified['client_id'])) + $modified['client_id'] = $this->transformId('clients', $resource['client_id']); $project = Project::Create($modified); diff --git a/app/Repositories/VendorContactRepository.php b/app/Repositories/VendorContactRepository.php index 8682c4ec460f..28596ccb4fb1 100644 --- a/app/Repositories/VendorContactRepository.php +++ b/app/Repositories/VendorContactRepository.php @@ -26,11 +26,14 @@ class VendorContactRepository extends BaseRepository public function save($contacts, Vendor $vendor) : void { - /* Convert array to collection */ - $contacts = collect($contacts); + if (isset($data['contacts'])) { + $contacts = collect($data['contacts']); + } else { + $contacts = collect(); + } /* Get array of IDs which have been removed from the contacts array and soft delete each contact */ - collect($vendor->contacts->pluck('id'))->diff($contacts->pluck('id'))->each(function ($contact) { + $vendor->contacts->pluck('id')->diff($contacts->pluck('id'))->each(function ($contact) { VendorContact::destroy($contact); }); diff --git a/tests/Unit/Migration/migration.json b/tests/Unit/Migration/migration.json index 536166cd58f9..b377a58d5d6d 100644 --- a/tests/Unit/Migration/migration.json +++ b/tests/Unit/Migration/migration.json @@ -1373,12 +1373,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 1, @@ -1425,12 +1419,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 2, @@ -1477,12 +1465,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 3, @@ -1529,12 +1511,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 4, @@ -1581,12 +1557,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 5, @@ -1633,12 +1603,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 6, @@ -1685,12 +1649,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 7, @@ -1737,12 +1695,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 8, @@ -1789,12 +1741,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 9, @@ -1841,12 +1787,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 10, @@ -1893,12 +1833,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 11, @@ -1945,12 +1879,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 12, @@ -1997,12 +1925,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 13, @@ -2049,12 +1971,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 14, @@ -2101,12 +2017,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 15, @@ -2153,12 +2063,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 16, @@ -2205,12 +2109,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 17, @@ -2257,12 +2155,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 18, @@ -2309,12 +2201,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 19, @@ -2361,12 +2247,6 @@ "custom_value3": "", "custom_value4": "", "transaction_name": "", - "shipping_address1": null, - "shipping_address2": null, - "shipping_city": null, - "shipping_state": null, - "shipping_postal_code": null, - "shipping_country_id": null, "contacts": [ { "id": 20,