2018-11-22 22:12:41 +11:00
|
|
|
<?php
|
2019-05-11 13:32:07 +10:00
|
|
|
/**
|
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
|
*
|
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
|
*
|
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
|
*
|
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
|
*/
|
2018-11-22 22:12:41 +11:00
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
2018-11-27 17:59:16 +11:00
|
|
|
use App\Models\Client;
|
|
|
|
|
use App\Models\ClientContact;
|
2019-10-02 19:40:32 +10:00
|
|
|
use Illuminate\Support\Str;
|
2018-11-27 17:59:16 +11:00
|
|
|
|
2018-11-22 22:12:41 +11:00
|
|
|
/**
|
2019-05-10 16:08:33 +10:00
|
|
|
* ClientContactRepository
|
2018-11-22 22:12:41 +11:00
|
|
|
*/
|
|
|
|
|
class ClientContactRepository extends BaseRepository
|
|
|
|
|
{
|
|
|
|
|
|
2019-03-27 19:38:01 +11:00
|
|
|
public function save($contacts, Client $client) : void
|
2018-11-22 22:12:41 +11:00
|
|
|
{
|
|
|
|
|
|
2018-11-27 17:59:16 +11: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 */
|
2018-12-07 21:57:20 +11:00
|
|
|
collect($client->contacts->pluck('id'))->diff($contacts->pluck('id'))->each(function($contact){
|
2019-05-10 16:08:33 +10:00
|
|
|
|
2018-11-27 17:59:16 +11:00
|
|
|
ClientContact::destroy($contact);
|
2019-05-10 16:08:33 +10:00
|
|
|
|
2018-11-27 17:59:16 +11:00
|
|
|
});
|
|
|
|
|
|
2019-12-08 18:33:44 +11:00
|
|
|
$this->is_primary = true;
|
2019-11-07 09:57:09 +11:00
|
|
|
/* Set first record to primary - always */
|
2019-12-08 21:28:52 +11:00
|
|
|
$contacts = $contacts->sortByDesc('is_primary')->map(function ($contact){
|
2019-12-08 18:33:44 +11:00
|
|
|
$contact['is_primary'] = $this->is_primary;
|
|
|
|
|
$this->is_primary = false;
|
|
|
|
|
return $contact;
|
2018-11-27 17:59:16 +11:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//loop and update/create contacts
|
|
|
|
|
$contacts->each(function ($contact) use ($client){
|
2019-10-02 17:52:38 +10:00
|
|
|
|
|
|
|
|
$update_contact = null;
|
2018-11-27 17:59:16 +11:00
|
|
|
|
2019-10-02 17:52:38 +10:00
|
|
|
if(isset($contact['id']))
|
|
|
|
|
$update_contact = ClientContact::find($this->decodePrimaryKey($contact['id']));
|
|
|
|
|
|
|
|
|
|
if(!$update_contact){
|
|
|
|
|
|
|
|
|
|
$update_contact = new ClientContact;
|
|
|
|
|
$update_contact->client_id = $client->id;
|
|
|
|
|
$update_contact->company_id = $client->company_id;
|
|
|
|
|
$update_contact->user_id = $client->user_id;
|
2019-10-02 19:40:32 +10:00
|
|
|
$update_contact->contact_key = Str::random(40);
|
2019-10-02 17:52:38 +10:00
|
|
|
}
|
2018-11-27 17:59:16 +11:00
|
|
|
|
|
|
|
|
$update_contact->fill($contact);
|
2019-05-10 16:08:33 +10:00
|
|
|
|
2018-11-27 17:59:16 +11:00
|
|
|
$update_contact->save();
|
|
|
|
|
});
|
|
|
|
|
|
2019-12-08 18:33:44 +11:00
|
|
|
|
|
|
|
|
|
2019-11-13 22:32:53 +11:00
|
|
|
//always made sure we have one blank contact to maintain state
|
|
|
|
|
if($contacts->count() == 0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$new_contact = new ClientContact;
|
|
|
|
|
$new_contact->client_id = $client->id;
|
|
|
|
|
$new_contact->company_id = $client->company_id;
|
|
|
|
|
$new_contact->user_id = $client->user_id;
|
|
|
|
|
$new_contact->contact_key = Str::random(40);
|
|
|
|
|
$new_contact->is_primary = true;
|
|
|
|
|
$new_contact->save();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 22:12:41 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|