2018-11-22 12:12:41 +01:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02: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)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2018-11-22 12:12:41 +01:00
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2020-02-12 11:03:17 +01:00
|
|
|
use App\Factory\ClientContactFactory;
|
2018-11-27 07:59:16 +01:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
2020-03-02 11:22:37 +01:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2020-09-06 11:38:10 +02:00
|
|
|
use Illuminate\Support\Str;
|
2018-11-27 07:59:16 +01:00
|
|
|
|
2018-11-22 12:12:41 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* ClientContactRepository.
|
2018-11-22 12:12:41 +01:00
|
|
|
*/
|
|
|
|
class ClientContactRepository extends BaseRepository
|
|
|
|
{
|
2021-11-15 11:46:58 +01:00
|
|
|
private bool $is_primary = true;
|
|
|
|
|
|
|
|
private bool $set_send_email_on_contact = false;
|
2021-03-12 15:00:33 +01:00
|
|
|
|
2020-02-12 11:03:17 +01:00
|
|
|
public function save(array $data, Client $client) : void
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2022-09-06 09:42:33 +02:00
|
|
|
//06-09-2022 sometimes users pass a contact object instead of a nested array, this sequence handles this scenario
|
2022-09-06 09:41:37 +02:00
|
|
|
if (isset($data['contacts']) && (count($data['contacts']) !== count($data['contacts'], COUNT_RECURSIVE))) {
|
2022-09-06 09:42:33 +02:00
|
|
|
|
2020-02-12 11:03:17 +01:00
|
|
|
$contacts = collect($data['contacts']);
|
2022-09-06 09:42:33 +02:00
|
|
|
|
2022-09-06 09:41:37 +02:00
|
|
|
} elseif(isset($data['contacts'])){
|
|
|
|
|
|
|
|
$temp_array[] = $data['contacts'];
|
|
|
|
$contacts = collect($temp_array);
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2022-09-06 09:42:33 +02:00
|
|
|
|
2020-02-12 11:03:17 +01:00
|
|
|
$contacts = collect();
|
2022-09-06 09:42:33 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
|
2020-02-24 11:15:30 +01:00
|
|
|
$client->contacts->pluck('id')->diff($contacts->pluck('id'))->each(function ($contact) {
|
2019-12-30 22:59:12 +01:00
|
|
|
ClientContact::destroy($contact);
|
|
|
|
});
|
|
|
|
|
2021-11-15 11:46:58 +01:00
|
|
|
/* Ensure send_email always exists in at least one contact */
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $contacts->contains('send_email', true)) {
|
2021-11-15 11:46:58 +01:00
|
|
|
$this->set_send_email_on_contact = true;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-03-12 15:00:33 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/* Set first record to primary - always */
|
|
|
|
$contacts = $contacts->sortByDesc('is_primary')->map(function ($contact) {
|
2022-09-06 09:36:56 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$contact['is_primary'] = $this->is_primary;
|
|
|
|
$this->is_primary = false;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->set_send_email_on_contact) {
|
2021-11-15 11:46:58 +01:00
|
|
|
$contact['send_email'] = true;
|
|
|
|
$this->set_send_email_on_contact = false;
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $contact;
|
|
|
|
});
|
|
|
|
|
|
|
|
//loop and update/create contacts
|
|
|
|
$contacts->each(function ($contact) use ($client) {
|
2020-02-24 11:15:30 +01:00
|
|
|
$update_contact = null;
|
2019-12-30 22:59:12 +01:00
|
|
|
|
|
|
|
if (isset($contact['id'])) {
|
2020-02-12 10:18:56 +01:00
|
|
|
$update_contact = ClientContact::find($contact['id']);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $update_contact) {
|
2020-02-12 11:03:17 +01:00
|
|
|
$update_contact = ClientContactFactory::create($client->company_id, $client->user_id);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-09-09 23:44:58 +02:00
|
|
|
//10-09-2021 - enforce the client->id and remove client_id from fillables
|
|
|
|
$update_contact->client_id = $client->id;
|
2019-12-30 22:59:12 +01:00
|
|
|
|
2021-01-21 00:01:13 +01:00
|
|
|
/* We need to set NULL email addresses to blank strings to pass authentication*/
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists('email', $contact) && is_null($contact['email'])) {
|
2021-01-21 00:01:13 +01:00
|
|
|
$contact['email'] = '';
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-01-21 00:01:13 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$update_contact->fill($contact);
|
2020-03-02 11:22:37 +01:00
|
|
|
|
2020-09-12 11:53:28 +02:00
|
|
|
if (array_key_exists('password', $contact) && strlen($contact['password']) > 1) {
|
|
|
|
$update_contact->password = Hash::make($contact['password']);
|
2021-09-04 03:54:20 +02:00
|
|
|
|
|
|
|
$client->company->client_contacts()->where('email', $update_contact->email)->update(['password' => $update_contact->password]);
|
2020-03-02 11:22:37 +01:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists('email', $contact)) {
|
2021-09-12 07:29:30 +02:00
|
|
|
$update_contact->email = trim($contact['email']);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-09-12 07:29:30 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$update_contact->save();
|
2020-05-27 06:46:19 +02:00
|
|
|
});
|
2019-12-30 22:59:12 +01:00
|
|
|
|
2020-07-29 04:13:12 +02:00
|
|
|
//need to reload here to shake off stale contacts
|
2021-09-29 20:58:48 +02:00
|
|
|
$client->fresh();
|
2020-07-29 04:13:12 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
//always made sure we have one blank contact to maintain state
|
2021-09-29 20:58:48 +02:00
|
|
|
if ($client->contacts()->count() == 0) {
|
2020-02-12 11:03:17 +01:00
|
|
|
$new_contact = ClientContactFactory::create($client->company_id, $client->user_id);
|
2019-12-30 22:59:12 +01:00
|
|
|
$new_contact->client_id = $client->id;
|
|
|
|
$new_contact->contact_key = Str::random(40);
|
|
|
|
$new_contact->is_primary = true;
|
2021-01-18 21:29:36 +01:00
|
|
|
$new_contact->confirmed = true;
|
2021-01-21 00:01:13 +01:00
|
|
|
$new_contact->email = ' ';
|
2019-12-30 22:59:12 +01:00
|
|
|
$new_contact->save();
|
|
|
|
}
|
2021-09-09 13:18:04 +02:00
|
|
|
|
|
|
|
$client = null;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
|
|
|
}
|