changes 0328092025

This commit is contained in:
Simon Pocrnjič
2025-09-28 22:36:47 +02:00
parent b40ee9dcde
commit 7e8e0a479b
61 changed files with 4306 additions and 654 deletions
+87 -4
View File
@@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
class FieldJob extends Model
{
@@ -14,7 +15,7 @@ class FieldJob extends Model
protected $fillable = [
'field_job_setting_id',
'asigned_user_id',
'assigned_user_id',
'user_id',
'contract_id',
'assigned_at',
@@ -33,12 +34,43 @@ class FieldJob extends Model
'address_snapshot ' => 'array',
];
protected static function booted(){
protected static function booted()
{
static::creating(function (FieldJob $fieldJob) {
if(!isset($fieldJob->user_id)){
if (! isset($fieldJob->user_id)) {
$fieldJob->user_id = auth()->id();
}
});
static::updated(function (FieldJob $fieldJob): void {
// If job was just completed or cancelled, move contract to configured segment
$completedChanged = $fieldJob->wasChanged('completed_at') && ! is_null($fieldJob->completed_at);
$cancelledChanged = $fieldJob->wasChanged('cancelled_at') && ! is_null($fieldJob->cancelled_at);
if (! $completedChanged && ! $cancelledChanged) {
return;
}
if (! $fieldJob->relationLoaded('setting')) {
$fieldJob->load('setting');
}
if ($cancelledChanged) {
// On cancel: redirect to queue segment
$segmentId = $fieldJob->setting?->queue_segment_id;
$fieldJob->moveContractToSegment($segmentId);
return;
}
if ($completedChanged) {
// On complete: redirect to return segment
$segmentId = $fieldJob->setting?->return_segment_id;
$fieldJob->moveContractToSegment($segmentId);
return;
}
});
}
public function setting(): BelongsTo
@@ -48,7 +80,7 @@ public function setting(): BelongsTo
public function assignedUser(): BelongsTo
{
return $this->belongsTo(User::class, 'asigned_user_id');
return $this->belongsTo(User::class, 'assigned_user_id');
}
public function user(): BelongsTo
@@ -60,4 +92,55 @@ public function contract(): BelongsTo
{
return $this->belongsTo(Contract::class, 'contract_id');
}
/**
* Set/ensure the contract has the return segment marked active based on the field job setting.
*/
/**
* Ensure the contract has the provided segment marked active.
*/
public function moveContractToSegment(?int $segmentId): void
{
if (empty($segmentId) || empty($this->contract_id)) {
return;
}
// First, deactivate any currently active segments for this contract
DB::table('contract_segment')
->where('contract_id', $this->contract_id)
->where('active', true)
->update(['active' => false, 'updated_at' => now()]);
// Then activate (or create) the target segment pivot
$pivot = DB::table('contract_segment')
->where('contract_id', $this->contract_id)
->where('segment_id', $segmentId)
->first();
if ($pivot) {
DB::table('contract_segment')
->where('id', $pivot->id)
->update(['active' => true, 'updated_at' => now()]);
} else {
DB::table('contract_segment')->insert([
'contract_id' => $this->contract_id,
'segment_id' => $segmentId,
'active' => true,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
/**
* Back-compat convenience: move to configured return segment.
*/
public function returnContractToConfiguredSegment(): void
{
if (! $this->relationLoaded('setting')) {
$this->load('setting');
}
$this->moveContractToSegment($this->setting?->return_segment_id);
}
}