ldc-supportweb/app/Models/Credit/Account.php

145 lines
3.7 KiB
PHP
Raw Normal View History

2022-05-31 14:06:07 +02:00
<?php
namespace App\Models\Credit;
use App\Enums\Credit\AccountType;
use App\Models\Brand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
use Kwaadpepper\CrudPolicies\Enums\CrudType;
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
use Kwaadpepper\Enum\Traits\CastsEnums;
class Account extends Model
{
use CastsEnums;
use IsCrudModel;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'credit_accounts';
/**
* Crud Label
*
* @var string
*/
protected $crudLabelColumn = 'label';
/**
* Crud Value
*
* @var string
*/
protected $crudValueColumn = 'id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'label', 'type',
'balance', 'brand_id'
];
/**
* The attributes that should be cast to Enums.
*
* @var array
*/
protected $enumCasts = [
'type' => AccountType::class
];
/**
* Account
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
self::$editableProperties = [
'label' => [
'label' => trans('Label'),
'placeholder' => trans('Nom du compte'),
'type' => CrudType::string(),
'validate' => [
'string',
'max:255'
],
'required' => true,
'nullable' => false,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'type' => [
'label' => trans('Type'),
'type' => CrudType::enum(),
'enum' => AccountType::class,
'required' => true,
'nullable' => false,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'balance' => [
'label' => trans('Solde'),
'type' => CrudType::float(),
'required' => false,
'nullable' => false,
'readonly' => true,
'step' => 0.5,
'validate' => [
'numeric',
function ($attribute, $value, $fail) {
if (($value * 10) % 5 !== 0) {
$fail("$attribute doit être divisible par 0.5 jour/homme");
}
},
],
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'brand' => [
'label' => Str::singular(trans('models.classes.brands')),
'type' => CrudType::belongsTo(),
'belongsTo' => Brand::class,
'required' => false,
'nullable' => false,
'actions' => [
CrudAction::viewAny()
]
]
];
parent::__construct($attributes);
}
// * RELATIONSHIPS
/**
* BelongsTo Brand relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand()
{
return $this->belongsTo(Brand::class);
}
}