109 lines
2.6 KiB
PHP
109 lines
2.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models\Credit;
|
||
|
|
|
||
|
|
use App\Enums\Credit\BalanceOperation;
|
||
|
|
use App\Enums\Credit\EstimationStatus;
|
||
|
|
use App\Models\Ticket;
|
||
|
|
use App\Models\User;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Validation\ValidationException;
|
||
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
||
|
|
|
||
|
|
class Estimation extends Model
|
||
|
|
{
|
||
|
|
use CastsEnums;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The attributes that should be cast to Enums.
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $enumCasts = [
|
||
|
|
'status' => EstimationStatus::class
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The table associated with the model.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $table = 'credit_estimations';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The attributes that are mass assignable.
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $fillable = [
|
||
|
|
'amount', 'status', 'credit_account_id'
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Perform any actions required after the model boots.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
protected static function booted()
|
||
|
|
{
|
||
|
|
static::updating(function (self $model) {
|
||
|
|
if (!$model->original['validator_id'] !== $model->validator_id) {
|
||
|
|
$log = new CreditLog();
|
||
|
|
$log->account()->associate($model->account);
|
||
|
|
$log->type = BalanceOperation::debit();
|
||
|
|
$log->amount = $model->amount;
|
||
|
|
$log->saveOrFail();
|
||
|
|
$log->estimation()->sync([$model->id]);
|
||
|
|
}
|
||
|
|
if ($model->status->equals(EstimationStatus::pending())) {
|
||
|
|
throw ValidationException::withMessages([trans('Impossible de modifier une estimation')]);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// * RELATIONSHIPS
|
||
|
|
|
||
|
|
/**
|
||
|
|
* BelongsTo Ticket relation
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||
|
|
*/
|
||
|
|
public function ticket()
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Ticket::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* BelongsTo Credit\Account relation
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||
|
|
*/
|
||
|
|
public function account()
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Account::class, 'credit_account_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* BelongsTo User relation
|
||
|
|
* The use who created the estimate
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||
|
|
*/
|
||
|
|
public function estimator()
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class, 'estimator_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* BelongsTo User relation (nullable)
|
||
|
|
* The use who validated the estimate
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||
|
|
*/
|
||
|
|
public function validator()
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class, 'validator_id');
|
||
|
|
}
|
||
|
|
}
|