116 lines
3.2 KiB
PHP
116 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Http\Resources\CustomerCrmResource;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use App\Models\Salarie;
|
|
use LDC\LDCCRM\Exceptions\CrmException;
|
|
use LDC\LDCCRM\Models\Customer as CrmCustomer;
|
|
|
|
class Customers extends Authenticatable
|
|
{
|
|
use Notifiable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'email',
|
|
'username'
|
|
];
|
|
|
|
/** @var string */
|
|
protected $guard = 'customer';
|
|
|
|
|
|
/**
|
|
* hasOne salarie relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function salarie()
|
|
{
|
|
return $this->hasOne(Salarie::class, 'customer_id');
|
|
}
|
|
|
|
/**
|
|
* Get the crm Data of a customer
|
|
*
|
|
* @return \LDC\LDCCRM\Models\Customer
|
|
* @throws \LDC\LDCCRM\Exceptions\CrmException If cannot fetch data from crm.
|
|
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
|
|
*/
|
|
public function getApiDataAttribute(): CrmCustomer
|
|
{
|
|
/** @var \LDC\LDCCRM\CrmApiV2 */
|
|
$crm = app('crmAPI');
|
|
$crmCustomer = $crm->getCustomer($this->hashcode);
|
|
if (!$crmCustomer) {
|
|
throw new CrmException('CRM get Customer returned null');
|
|
}
|
|
return $crmCustomer;
|
|
}
|
|
|
|
/**
|
|
* Set the crm Data of a customer
|
|
* @param CustomerCrmResource $resource
|
|
*
|
|
* @return CrmCustomer
|
|
*
|
|
* @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
|
|
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
|
|
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
|
|
* @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
|
|
*/
|
|
public function updateApiData(CustomerCrmResource $resource): CrmCustomer
|
|
{
|
|
$data = $resource->toArray(null);
|
|
|
|
/** @var \LDC\LDCCRM\CrmApiV2 */
|
|
$crm = app('crmAPI');
|
|
$crmCustomer = $crm->getCustomer($data['hashcode']);
|
|
if (!$crmCustomer) {
|
|
throw new CrmException('CRM get Customer returned null');
|
|
}
|
|
|
|
$crmCustomer->fill($data);
|
|
$crmCustomer->save();
|
|
|
|
return $crmCustomer;
|
|
}
|
|
|
|
/**
|
|
* This mutate Customer attributes to array
|
|
* including only non critical properties and crm values
|
|
* (One that can be send to front office without security breach)
|
|
*
|
|
* @return array
|
|
*/
|
|
public function toArray()
|
|
{
|
|
if ($this->hashcode) {
|
|
return collect(array_merge(
|
|
parent::toArray(),
|
|
(array)$this->apiData
|
|
))->intersectByKeys([
|
|
'email' => '',
|
|
'firstname' => '',
|
|
'lastname' => '',
|
|
'address' => '',
|
|
'address2' => '',
|
|
'postcode' => '',
|
|
'birth_date' => '',
|
|
'city' => '',
|
|
'country' => '',
|
|
'opt_in' => '',
|
|
'phone' => ''
|
|
])->toArray();
|
|
} else {
|
|
return parent::toArray();
|
|
}
|
|
}
|
|
}
|