ldc-boutique-cnc-sable/app/Models/Customers.php
2025-01-23 15:35:17 +01:00

140 lines
3.7 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;
use Carbon\Carbon;
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();
}
}
/**
* HasMany orders relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function orders()
{
return $this->hasMany(Order::class, 'customer_id');
}
/**
* Check if customer is no longer employed
*
* @return boolean
*/
public function isNoLongerEmployed()
{
if (!$this->salarie or !$this->salarie->end_date) {
return false;
}
return Carbon::parse($this->salarie->end_date)->lessThanOrEqualTo(Carbon::now());
}
}