mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 21:22:58 +01:00
3d31f810c0
* Working on importing company gateways * Fix for companyuser settings object * Migrate client_gateway_tokens * Working on Notificaitons * Working on notifications * Failsafe for user-company * unlink files * Set DB for jobs * Always have a fallback for company_id * Fixes for user model * Formatting for MultiDB * Working on Company Ledger Tests * Fixes for contact request * Set Invitations as a default include for invoices
76 lines
2.1 KiB
PHP
76 lines
2.1 KiB
PHP
<?php
|
|
/**
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
*
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
*
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
*
|
|
* @license https://opensource.org/licenses/AAL
|
|
*/
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Factory\ClientContactFactory;
|
|
use App\Models\Client;
|
|
use App\Models\ClientContact;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* ClientContactRepository
|
|
*/
|
|
class ClientContactRepository extends BaseRepository
|
|
{
|
|
public function save(array $data, Client $client) : void
|
|
{
|
|
if(isset($data['contacts']))
|
|
$contacts = collect($data['contacts']);
|
|
else
|
|
$contacts = collect();
|
|
|
|
$client->contacts->pluck('id')->diff($contacts->pluck('id'))->each(function ($contact) {
|
|
ClientContact::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;
|
|
return $contact;
|
|
});
|
|
|
|
//loop and update/create contacts
|
|
$contacts->each(function ($contact) use ($client) {
|
|
$update_contact = null;
|
|
|
|
if (isset($contact['id'])) {
|
|
$update_contact = ClientContact::find($contact['id']);
|
|
}
|
|
|
|
if (!$update_contact) {
|
|
$update_contact = ClientContactFactory::create($client->company_id, $client->user_id);
|
|
$update_contact->client_id = $client->id;
|
|
}
|
|
|
|
$update_contact->fill($contact);
|
|
$update_contact->save();
|
|
|
|
});
|
|
|
|
|
|
|
|
//always made sure we have one blank contact to maintain state
|
|
if ($contacts->count() == 0) {
|
|
$new_contact = ClientContactFactory::create($client->company_id, $client->user_id);
|
|
$new_contact->client_id = $client->id;
|
|
$new_contact->contact_key = Str::random(40);
|
|
$new_contact->is_primary = true;
|
|
$new_contact->save();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|