123 lines
3 KiB
PHP
123 lines
3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models\Credit;
|
||
|
|
|
||
|
|
use App\Enums\Credit\BalanceOperation;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
|
||
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudType;
|
||
|
|
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
|
||
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
||
|
|
|
||
|
|
class CreditLog extends Model
|
||
|
|
{
|
||
|
|
use CastsEnums;
|
||
|
|
use IsCrudModel;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Crud Label
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $crudLabelColumn = 'type';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Crud Value
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $crudValueColumn = 'id';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Indicates if the model should be timestamped.
|
||
|
|
*
|
||
|
|
* @var boolean
|
||
|
|
*/
|
||
|
|
public $timestamps = false;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The attributes that should be cast to Enums.
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $enumCasts = [
|
||
|
|
'type' => BalanceOperation::class
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Account
|
||
|
|
*
|
||
|
|
* @param array $attributes
|
||
|
|
*/
|
||
|
|
public function __construct(array $attributes = [])
|
||
|
|
{
|
||
|
|
self::$editableProperties = [
|
||
|
|
'amount' => [
|
||
|
|
'label' => trans('Montant'),
|
||
|
|
'type' => CrudType::float(),
|
||
|
|
'actions' => [
|
||
|
|
CrudAction::viewAny()
|
||
|
|
]
|
||
|
|
],
|
||
|
|
'type' => [
|
||
|
|
'label' => trans('Opération'),
|
||
|
|
'type' => CrudType::enum(),
|
||
|
|
'enum' => BalanceOperation::class,
|
||
|
|
'actions' => [
|
||
|
|
CrudAction::viewAny()
|
||
|
|
]
|
||
|
|
],
|
||
|
|
'created_at' => [
|
||
|
|
'label' => trans('Date'),
|
||
|
|
'type' => CrudType::datetime(),
|
||
|
|
'actions' => [
|
||
|
|
CrudAction::viewAny()
|
||
|
|
]
|
||
|
|
]
|
||
|
|
];
|
||
|
|
parent::__construct($attributes);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Perform any actions required after the model boots.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
protected static function booted()
|
||
|
|
{
|
||
|
|
static::creating(function (self $model) {
|
||
|
|
$model->created_at = now();
|
||
|
|
if ($model->type->equals(BalanceOperation::debit())) {
|
||
|
|
$model->account->balance = $model->account->balance - $model->amount;
|
||
|
|
}
|
||
|
|
if ($model->type->equals(BalanceOperation::credit())) {
|
||
|
|
$model->account->balance = $model->account->balance + $model->amount;
|
||
|
|
}
|
||
|
|
$model->account->saveOrFail();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// * RELATIONSHIPS
|
||
|
|
|
||
|
|
/**
|
||
|
|
* BelongsToMany estimation relation (1 nullable)
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||
|
|
*/
|
||
|
|
public function estimation()
|
||
|
|
{
|
||
|
|
return $this->belongsToMany(Estimation::class, EstimationLog::class, 'credit_log_id', 'credit_estimation_id')
|
||
|
|
->withTimestamps();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* BelongsTo account relation
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||
|
|
*/
|
||
|
|
public function account()
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Account::class, 'credit_account_id');
|
||
|
|
}
|
||
|
|
}
|