2022-05-31 14:06:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models\Credit;
|
|
|
|
|
|
2023-08-14 09:23:39 +00:00
|
|
|
use App\Enums\Credit\BalanceOperationEnum;
|
|
|
|
|
use App\Enums\Credit\EstimationStatusEnum;
|
2022-05-31 14:06:07 +02:00
|
|
|
use App\Models\Ticket;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
|
|
|
|
|
|
class Estimation extends Model
|
|
|
|
|
{
|
|
|
|
|
/**
|
2023-08-14 09:23:39 +00:00
|
|
|
* The attributes that should be cast to native types.
|
2022-05-31 14:06:07 +02:00
|
|
|
*
|
2025-04-08 11:47:49 +02:00
|
|
|
* @var array<string, string>
|
2022-05-31 14:06:07 +02:00
|
|
|
*/
|
2023-08-14 09:23:39 +00:00
|
|
|
protected $casts = [
|
|
|
|
|
'status' => EstimationStatusEnum::class
|
2022-05-31 14:06:07 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The table associated with the model.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $table = 'credit_estimations';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
2025-04-08 11:47:49 +02:00
|
|
|
* @var list<string>
|
2022-05-31 14:06:07 +02:00
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'amount', 'status', 'credit_account_id'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-04-08 11:47:49 +02:00
|
|
|
protected static function booted(): void
|
2022-05-31 14:06:07 +02:00
|
|
|
{
|
|
|
|
|
static::updating(function (self $model) {
|
|
|
|
|
if (!$model->original['validator_id'] !== $model->validator_id) {
|
|
|
|
|
$log = new CreditLog();
|
|
|
|
|
$log->account()->associate($model->account);
|
2023-08-14 09:23:39 +00:00
|
|
|
$log->type = BalanceOperationEnum::debit;
|
2022-05-31 14:06:07 +02:00
|
|
|
$log->amount = $model->amount;
|
|
|
|
|
$log->saveOrFail();
|
|
|
|
|
$log->estimation()->sync([$model->id]);
|
|
|
|
|
}
|
2023-08-14 09:23:39 +00:00
|
|
|
if ($model->status->equals(EstimationStatusEnum::pending)) {
|
2022-05-31 14:06:07 +02:00
|
|
|
throw ValidationException::withMessages([trans('Impossible de modifier une estimation')]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
2025-04-08 11:47:49 +02:00
|
|
|
* BelongsTo Ticket relation.
|
2022-05-31 14:06:07 +02:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
2025-04-08 11:47:49 +02:00
|
|
|
public function ticket(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2022-05-31 14:06:07 +02:00
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Ticket::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-04-08 11:47:49 +02:00
|
|
|
* BelongsTo Credit\Account relation.
|
2022-05-31 14:06:07 +02:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
2025-04-08 11:47:49 +02:00
|
|
|
public function account(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2022-05-31 14:06:07 +02:00
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Account::class, 'credit_account_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* BelongsTo User relation
|
2025-04-08 11:47:49 +02:00
|
|
|
* The use who created the estimate.
|
2022-05-31 14:06:07 +02:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
2025-04-08 11:47:49 +02:00
|
|
|
public function estimator(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2022-05-31 14:06:07 +02:00
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'estimator_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* BelongsTo User relation (nullable)
|
2025-04-08 11:47:49 +02:00
|
|
|
* The use who validated the estimate.
|
2022-05-31 14:06:07 +02:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
2025-04-08 11:47:49 +02:00
|
|
|
public function validator(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2022-05-31 14:06:07 +02:00
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'validator_id');
|
|
|
|
|
}
|
|
|
|
|
}
|