ldc-crm/app/Models/CustomerInfo.php
Alexis Gousseau 303834bbca
Crm V4
- Make all tests pass Client (V4),CRMV2,CRMV4
- Simplify client submodule path
- Move App_url to .env.testing
2023-10-06 16:37:29 +02:00

146 lines
4.4 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Country;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property integer $id Id.
* @property integer $customer_id Foreign key Customer.
* @property integer $brand_id Foreign key Brand.
* @property string $firstname Firstname.
* @property string $lastname Lastname.
* @property \Carbon\Carbon|null $birth_date Birth_date.
* @property string|null $address Address.
* @property string|null $address2 Address2.
* @property string $phone Phone.
* @property \App\Enums\Country $country Country.
* @property string|null $city City.
* @property string|null $postcode Postcode.
* @property boolean $opt_in Opt_in.
* @property string $hashcode Hashcode.
* @property \Illuminate\Support\Carbon $created_at Created date.
* @property \Illuminate\Support\Carbon $updated_at Updated date.
*
* @property-read \App\Models\Customer $customer Customer BelongsTo relation.
* @property-read \App\Models\Brand $brand Brand BelongsTo relation.
*/
class CustomerInfo extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'firstname',
'lastname',
'birth_date',
'address',
'address2',
'phone',
'country',
'city',
'postcode',
'opt_in'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'country' => Country::class,
'opt_in' => 'bool',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $model) {
$model->hashcode = Str::uuid();
});
static::deleting(function (self $model) {
CustomerOptInHistory::query()->whereHas('customer', function (Builder $q) use ($model) {
$q->where('id', $model->customer->id);
})->whereHas('brand', function (Builder $q) use ($model) {
$q->where('id', $model->brand->id);
})->delete();
});
}
// * RELATIONS
/**
* Customer BelongsTo relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function customer(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Customer::class);
}
/**
* Brand BelongsTo relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Brand::class);
}
// * METHODS
/**
* Get Login.
*
* @return \App\Models\CustomerLogin
*/
public function login(): \App\Models\CustomerLogin
{
return $this->customer->login($this->brand);
}
/**
* Get customer info from email.
*
* @param \App\Models\Brand $brand
* @param string $email
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function getFromEmail(Brand $brand, string $email): \Illuminate\Database\Eloquent\Builder
{
return CustomerInfo::query()->whereHas('brand', function (Builder $q) use ($brand) {
$q->where('id', $brand->id);
})->whereHas('customer', function (Builder $q) use ($email) {
$q->where('email', $email);
});
}
/**
* Get customer info from hashcode.
*
* @param \App\Models\Brand $brand
* @param string $hashcode
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function getFromHashcode(Brand $brand, string $hashcode): \Illuminate\Database\Eloquent\Builder
{
return CustomerInfo::query()->whereHas('brand', function (Builder $q) use ($brand) {
$q->where('id', $brand->id);
})->where('hashcode', $hashcode);
}
}