2021-04-20 11:16:34 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2025-11-12 16:35:16 +01:00
|
|
|
use App\Models\Panier;
|
2025-11-18 18:56:19 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2021-04-20 11:16:34 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2025-03-23 12:53:40 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2021-04-20 11:16:34 +02:00
|
|
|
|
|
|
|
|
class Customers extends Model
|
|
|
|
|
{
|
2025-11-18 18:56:19 +01:00
|
|
|
use HasFactory;
|
|
|
|
|
|
2025-03-23 12:53:40 +01:00
|
|
|
/**
|
|
|
|
|
* @var string $primaryKey
|
|
|
|
|
*/
|
2026-10-28 13:23:49 +01:00
|
|
|
|
2021-08-13 11:48:06 +02:00
|
|
|
protected $primaryKey = 'hashcode';
|
2025-03-23 12:53:40 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string $keyType
|
|
|
|
|
*/
|
2026-10-28 13:23:49 +01:00
|
|
|
|
2021-08-20 11:46:17 +02:00
|
|
|
protected $keyType = 'string';
|
2021-04-20 19:51:30 +02:00
|
|
|
|
2025-03-23 12:53:40 +01:00
|
|
|
/**
|
|
|
|
|
* @var array $fillable
|
|
|
|
|
*/
|
2026-10-28 13:23:49 +01:00
|
|
|
|
2021-08-13 11:48:06 +02:00
|
|
|
protected $fillable = ['hashcode', 'email', 'country'];
|
2021-04-20 19:51:30 +02:00
|
|
|
|
2025-03-23 12:53:40 +01:00
|
|
|
/**
|
|
|
|
|
* booted method
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-04-20 19:51:30 +02:00
|
|
|
protected static function booted()
|
|
|
|
|
{
|
2026-10-28 12:27:46 +01:00
|
|
|
static::creating(function ($customer): void {
|
|
|
|
|
$customer->country = new Customers()->getIsoCountry($customer->country);
|
2021-04-20 19:51:30 +02:00
|
|
|
});
|
|
|
|
|
|
2026-10-28 12:27:46 +01:00
|
|
|
static::updating(function ($customer): void {
|
|
|
|
|
$customer->country = new Customers()->getIsoCountry($customer->country);
|
2021-04-20 19:51:30 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-23 12:53:40 +01:00
|
|
|
/**
|
|
|
|
|
* getIsoCountry method
|
|
|
|
|
* Default is : France
|
|
|
|
|
*
|
|
|
|
|
* @param string $country
|
|
|
|
|
* @return integer
|
|
|
|
|
*/
|
2021-04-27 17:29:47 +02:00
|
|
|
private function getIsoCountry($country)
|
2021-04-20 19:51:30 +02:00
|
|
|
{
|
2021-04-27 17:29:47 +02:00
|
|
|
$country = trim(ucfirst(strtolower($country)));
|
2026-10-28 12:27:46 +01:00
|
|
|
$country = match ($country) {
|
|
|
|
|
'Belgique' => 56,
|
|
|
|
|
'Luxembourg' => 442,
|
|
|
|
|
default => 250,
|
|
|
|
|
};
|
2025-03-23 12:53:40 +01:00
|
|
|
return $country;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-14 17:05:09 +02:00
|
|
|
* Printed Coupons Relation
|
2025-03-23 12:53:40 +01:00
|
|
|
*
|
|
|
|
|
* @return HasMany
|
|
|
|
|
*/
|
2025-08-14 17:05:09 +02:00
|
|
|
public function printedCoupons()
|
2025-03-23 12:53:40 +01:00
|
|
|
{
|
2025-08-14 17:05:09 +02:00
|
|
|
return $this->hasMany(PrintedCoupons::class, 'customer_id', 'id');
|
2021-04-20 19:51:30 +02:00
|
|
|
}
|
2025-08-05 14:47:02 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Basket model relation
|
|
|
|
|
* @return HasMany
|
|
|
|
|
*/
|
|
|
|
|
public function baskets()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Basket::class, 'customer_id', 'id');
|
|
|
|
|
}
|
2025-08-14 17:05:09 +02:00
|
|
|
|
|
|
|
|
/**
|
2025-10-29 12:41:39 +01:00
|
|
|
* Panier model relation
|
2025-08-14 17:05:09 +02:00
|
|
|
* @return HasMany
|
|
|
|
|
*/
|
|
|
|
|
public function paniers()
|
|
|
|
|
{
|
2025-10-29 12:41:39 +01:00
|
|
|
return $this->hasMany(Panier::class, 'panier_contactId', 'id');
|
2025-08-14 17:05:09 +02:00
|
|
|
}
|
2021-04-20 11:16:34 +02:00
|
|
|
}
|