ldc-crm/app/Models/Customer.php
2021-08-23 22:47:42 +02:00

191 lines
5.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class Customer extends Model
{
use HasFactory;
public const FIRST_OR_NULL = 0;
public const FIRST_OR_NEW = 1;
public const FIRST_OR_FAIL = 2;
protected $fillable = [
'email'
];
/**
* CustomerInfo hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function infos()
{
return $this->hasMany(CustomerInfo::class);
}
/**
* Get the CustomerInfo from a specific brand
* or create a New one if $firstOrNewOrFail is 1
* throw model not found exception if firstOrNewOrFail = 2
*
* @param Brand $brand
* @param int $firstOrNewOrFail
* @return \Illuminate\Database\Eloquent\Builder
*/
public function info(Brand $brand, $firstOrNewOrFail = self::FIRST_OR_NULL): ?CustomerInfo
{
if (isset($this->relations['infos'])) {
/** @var \App\Models\CustomerInfo $info */
foreach ($this->relations['infos'] as $info) {
if ($info->brand_id === $brand->id) {
return $info;
}
}
}
$query = CustomerInfo::whereHas('brand', function (Builder $query) use ($brand) {
$query->where('id', $brand->id);
})->whereHas('customer', function (Builder $query) {
$query->where('id', $this->id);
});
if ($firstOrNewOrFail === self::FIRST_OR_NEW) {
return $query->firstOrNew();
}
if ($firstOrNewOrFail === self::FIRST_OR_FAIL) {
return $query->firstOrFail();
}
return $query->first();
}
/**
* CustomerLogin hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function logins()
{
return $this->hasMany(CustomerLogin::class);
}
/**
* Get the CustomerLogin from a specific brand
* or create a New one if $firstOrNewOrFail is 1
* throw model not found exception if firstOrNewOrFail = 2
*
* @param Brand $brand
* @param int $firstOrNewOrFail
* @return \Illuminate\Database\Eloquent\Builder
*/
public function login(Brand $brand, $firstOrNewOrFail = self::FIRST_OR_NULL): ?CustomerLogin
{
if (isset($this->relations['logins'])) {
/** @var \App\Models\CustomerLogin $login */
foreach ($this->relations['logins'] as $login) {
if ($login->brand_id === $brand->id) {
return $login;
}
}
}
$query = CustomerLogin::whereHas('brand', function (Builder $query) use ($brand) {
$query->where('id', $brand->id);
})->whereHas('customer', function (Builder $query) use ($brand) {
$query->where('id', $this->id);
});
if ($firstOrNewOrFail === self::FIRST_OR_NEW) {
return $query->firstOrNew();
}
if ($firstOrNewOrFail === self::FIRST_OR_FAIL) {
return $query->firstOrFail();
}
return $query->first();
}
/**
* Load login and brand for this customer
*
* @param Brand $brand
* @return self
*/
public function loadBrandData(Brand $brand, bool $orFail = false): self
{
$this->load([
'logins' => function ($q) use ($brand) {
$q->where('brand_id', $brand->id);
},
'infos' => function ($q) use ($brand) {
$q->where('brand_id', $brand->id);
}
]);
if ($orFail and !isset($this->relations['logins'][0])) {
throw (new ModelNotFoundException())->setModel(CustomerLogin::class);
}
if ($orFail and !isset($this->relations['infos'][0])) {
throw (new ModelNotFoundException())->setModel(CustomerInfo::class);
}
return $this;
}
/**
* CustomerOptInHistory hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function optInHistory()
{
return $this->hasMany(CustomerOptInHistory::class);
}
/**
* Customer Brand belongsToMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\belongsToMany
*/
public function brands()
{
return $this->belongsToMany(Brand::class, CustomerInfo::class);
}
/**
* Answers belongsToMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function answers()
{
return $this->belongsToMany(Answer::class, 'customer_answer');
}
public function setOptIn(Brand $brand, bool $optIn)
{
// Ajout dans la table d'historique des opt-in de la nouvelle valeur
$optInHistory = new CustomerOptInHistory();
$optInHistory->customer_id = $this->id;
$optInHistory->brand_id = $brand->id;
$optInHistory->new_value = $optIn;
$optInHistory->saveOrFail();
}
/**
* Get All answers given by this customer
* for a specific service
*
* @param Service $service
* @return Collection
*/
public function getAnswers(Service $service): Collection
{
return Answer::whereHas('customers', function ($q) {
$q->whereId($this->id);
})->whereHas('question', function ($q) use ($service) {
$q->whereHas('brand', function ($q) use ($service) {
$q->whereId($service->brand->id);
});
})->get();
}
}