ldc-crm/app/Models/Customer.php

192 lines
5.7 KiB
PHP
Raw Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Models;
2021-03-14 10:45:42 +01:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
2020-12-01 17:02:10 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2021-08-23 22:47:42 +02:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
2020-12-01 17:02:10 +01:00
class Customer extends Model
{
use HasFactory;
public const FIRST_OR_NULL = 0;
public const FIRST_OR_NEW = 1;
public const FIRST_OR_FAIL = 2;
2021-03-14 10:45:42 +01:00
protected $fillable = [
'email'
];
/**
* CustomerInfo hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function infos()
{
return $this->hasMany(CustomerInfo::class);
2020-12-01 17:02:10 +01:00
}
2021-03-14 10:45:42 +01:00
/**
* Get the CustomerInfo from a specific brand
* or create a New one if $firstOrNewOrFail is 1
* throw model not found exception if firstOrNewOrFail = 2
2021-03-14 10:45:42 +01:00
*
* @param Brand $brand
* @param int $firstOrNewOrFail
2021-03-14 10:45:42 +01:00
* @return \Illuminate\Database\Eloquent\Builder
*/
public function info(Brand $brand, $firstOrNewOrFail = self::FIRST_OR_NULL): ?CustomerInfo
2021-03-14 10:45:42 +01:00
{
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) {
2021-03-14 10:45:42 +01:00
$query->where('id', $brand->id);
2021-08-23 22:47:42 +02:00
})->whereHas('customer', function (Builder $query) {
2021-03-14 10:45:42 +01:00
$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();
2021-03-14 10:45:42 +01:00
}
/**
* CustomerLogin hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function logins()
{
return $this->hasMany(CustomerLogin::class);
2020-12-01 17:02:10 +01:00
}
2021-03-14 10:45:42 +01:00
/**
* Get the CustomerLogin from a specific brand
* or create a New one if $firstOrNewOrFail is 1
* throw model not found exception if firstOrNewOrFail = 2
2021-03-14 10:45:42 +01:00
*
* @param Brand $brand
* @param int $firstOrNewOrFail
2021-03-14 10:45:42 +01:00
* @return \Illuminate\Database\Eloquent\Builder
*/
public function login(Brand $brand, $firstOrNewOrFail = self::FIRST_OR_NULL): ?CustomerLogin
2021-03-14 10:45:42 +01:00
{
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) {
2021-03-14 10:45:42 +01:00
$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
*/
2021-08-23 22:47:42 +02:00
public function loadBrandData(Brand $brand, bool $orFail = false): self
{
2021-08-23 22:47:42 +02:00
$this->load([
'logins' => function ($q) use ($brand) {
$q->where('brand_id', $brand->id);
},
'infos' => function ($q) use ($brand) {
$q->where('brand_id', $brand->id);
}
]);
2021-08-23 22:47:42 +02:00
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;
2021-03-14 10:45:42 +01:00
}
/**
* CustomerOptInHistory hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function optInHistory()
{
return $this->hasMany(CustomerOptInHistory::class);
2020-12-01 17:02:10 +01:00
}
2021-03-14 10:45:42 +01:00
/**
* 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');
}
2021-03-14 10:45:42 +01:00
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();
}
2020-12-01 17:02:10 +01:00
}