2018-11-22 12:12:41 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2018-11-27 07:59:16 +01:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
2018-11-22 12:12:41 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class ClientContactRepository extends BaseRepository
|
|
|
|
{
|
|
|
|
|
2018-11-27 07:59:16 +01:00
|
|
|
public function save(array $contacts, Client $client) : void
|
2018-11-22 12:12:41 +01:00
|
|
|
{
|
|
|
|
|
2018-11-27 07:59:16 +01:00
|
|
|
/* Convert array to collection */
|
|
|
|
$contacts = collect($contacts);
|
|
|
|
|
|
|
|
/* Get array of IDs which have been removed from the contacts array and soft delete each contact */
|
|
|
|
collect($client->contacts->pluck('id'))->diffKeys($contacts->pluck('id'))->each(function($contact){
|
|
|
|
ClientContact::destroy($contact);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Set first record to primary - always*/
|
|
|
|
$contacts = $contacts->sortBy('is_primary');
|
|
|
|
|
|
|
|
$contacts->first(function($contact){
|
|
|
|
$contact['is_primary'] = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
//loop and update/create contacts
|
|
|
|
$contacts->each(function ($contact) use ($client){
|
|
|
|
|
|
|
|
$update_contact = ClientContact::firstOrNew(
|
|
|
|
['id' => $contact['id']],
|
|
|
|
[
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'company_id' => $client->company_id
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$update_contact->fill($contact);
|
|
|
|
$update_contact->save();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-11-22 12:12:41 +01:00
|
|
|
}
|
2018-11-27 07:59:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-22 12:12:41 +01:00
|
|
|
|
|
|
|
}
|