2020-01-20 02:31:58 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
|
|
use App\Models\Vendor;
|
|
|
|
use App\Models\VendorContact;
|
2020-09-23 02:16:19 +02:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2020-01-20 02:31:58 +01:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* VendorContactRepository.
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
|
|
|
class VendorContactRepository extends BaseRepository
|
|
|
|
{
|
2020-10-28 07:58:15 +01:00
|
|
|
public $is_primary;
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public function save(array $data, Vendor $vendor): void
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
2023-11-23 01:28:38 +01:00
|
|
|
|
|
|
|
if (isset($data['contacts']) && (count($data['contacts']) !== count($data['contacts'], COUNT_RECURSIVE))) {
|
2020-10-31 01:46:00 +01:00
|
|
|
$contacts = collect($data['contacts']);
|
2023-11-23 01:28:38 +01:00
|
|
|
} elseif (isset($data['contacts'])) {
|
|
|
|
$temp_array[] = $data['contacts'];
|
|
|
|
$contacts = collect($temp_array);
|
2020-10-31 01:46:00 +01:00
|
|
|
} else {
|
|
|
|
$contacts = collect();
|
|
|
|
}
|
2020-01-20 02:31:58 +01:00
|
|
|
|
|
|
|
/* Get array of IDs which have been removed from the contacts array and soft delete each contact */
|
2022-07-22 06:07:51 +02:00
|
|
|
$vendor->contacts->pluck('id')->diff($contacts->pluck('id'))->each(function ($contact) {
|
2020-01-20 02:31:58 +01:00
|
|
|
VendorContact::destroy($contact);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->is_primary = true;
|
|
|
|
/* Set first record to primary - always */
|
|
|
|
$contacts = $contacts->sortByDesc('is_primary')->map(function ($contact) {
|
|
|
|
$contact['is_primary'] = $this->is_primary;
|
|
|
|
$this->is_primary = false;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-20 02:31:58 +01:00
|
|
|
return $contact;
|
|
|
|
});
|
|
|
|
|
|
|
|
//loop and update/create contacts
|
|
|
|
$contacts->each(function ($contact) use ($vendor) {
|
|
|
|
$update_contact = null;
|
|
|
|
|
|
|
|
if (isset($contact['id'])) {
|
2022-07-22 06:07:51 +02:00
|
|
|
$update_contact = VendorContact::find($contact['id']);
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $update_contact) {
|
2024-01-14 05:05:00 +01:00
|
|
|
$update_contact = new VendorContact();
|
2020-01-20 02:31:58 +01:00
|
|
|
$update_contact->vendor_id = $vendor->id;
|
|
|
|
$update_contact->company_id = $vendor->company_id;
|
|
|
|
$update_contact->user_id = $vendor->user_id;
|
|
|
|
$update_contact->contact_key = Str::random(40);
|
|
|
|
}
|
|
|
|
|
2023-10-26 12:09:22 +02:00
|
|
|
if (array_key_exists('email', $contact) && is_null($contact['email'])) {
|
|
|
|
$contact['email'] = '';
|
|
|
|
}
|
|
|
|
|
2020-01-20 02:31:58 +01:00
|
|
|
$update_contact->fill($contact);
|
|
|
|
|
2020-09-23 02:16:19 +02:00
|
|
|
if (array_key_exists('password', $contact) && strlen($contact['password']) > 1) {
|
|
|
|
$update_contact->password = Hash::make($contact['password']);
|
2023-10-26 12:09:22 +02:00
|
|
|
$vendor->company->vendor_contacts()->where('email', $update_contact->email)->update(['password' => $update_contact->password]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('email', $contact)) {
|
|
|
|
$update_contact->email = trim($contact['email']);
|
2020-09-23 02:16:19 +02:00
|
|
|
}
|
|
|
|
|
2023-02-01 03:46:39 +01:00
|
|
|
$update_contact->saveQuietly();
|
2023-10-26 12:09:22 +02:00
|
|
|
|
2020-01-20 02:31:58 +01:00
|
|
|
});
|
|
|
|
|
2023-11-23 01:28:38 +01:00
|
|
|
$vendor->fresh();
|
2020-09-23 02:16:19 +02:00
|
|
|
|
2020-01-20 02:31:58 +01:00
|
|
|
//always made sure we have one blank contact to maintain state
|
2023-11-23 01:28:38 +01:00
|
|
|
if ($vendor->contacts()->count() == 0) {
|
2024-01-14 05:05:00 +01:00
|
|
|
$new_contact = new VendorContact();
|
2020-01-20 02:31:58 +01:00
|
|
|
$new_contact->vendor_id = $vendor->id;
|
|
|
|
$new_contact->company_id = $vendor->company_id;
|
|
|
|
$new_contact->user_id = $vendor->user_id;
|
|
|
|
$new_contact->contact_key = Str::random(40);
|
|
|
|
$new_contact->is_primary = true;
|
2023-02-01 03:46:39 +01:00
|
|
|
$new_contact->saveQuietly();
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|