65 lines
1.9 KiB
PHP
65 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\HarimStates;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class Harim extends Model
|
|
{
|
|
use HasFactory;
|
|
protected $guarded = [];
|
|
|
|
public function histories(): HasMany
|
|
{
|
|
return $this->hasMany(HarimHistory::class);
|
|
}
|
|
|
|
protected function primaryArea(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn($value) => json_decode($value)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @throws Throwable
|
|
*/
|
|
public function updateStateWithHistory(int $action, string $description , array $data): void
|
|
{
|
|
DB::transaction(function () use ($action, $description, $data) {
|
|
$this->histories()->create([
|
|
'expert_id' => auth()->user()->id,
|
|
'previous_state_id' => $this->state_id,
|
|
'previous_state_name'=> $this->state_name,
|
|
'expert_description' => $description,
|
|
'action_id' => $action,
|
|
'action_name' => HarimAction::name($action),
|
|
]);
|
|
|
|
$this->update($data);
|
|
});
|
|
public function panjarehVahedHistories(): HasMany
|
|
{
|
|
return $this->hasMany(PanjarehVahedHistory::class);
|
|
}
|
|
|
|
protected function data(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
set: function ($value) {
|
|
$iv = config('harim_web_services.Harim_Info.iv');
|
|
$keyString = config('harim_web_services.Harim_Info.keyString');
|
|
$key = hash('sha256',$keyString, true);
|
|
$decrypted = openssl_decrypt($value,'aes-256-cbc',$key,false, $iv);
|
|
return json_decode($decrypted);
|
|
},
|
|
);
|
|
}
|
|
}
|