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
+21 -7
View File
@@ -6,11 +6,13 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
class Activity extends Model
{
/** @use HasFactory<\Database\Factories\ActivityFactory> */
use HasFactory;
use SoftDeletes;
protected $fillable = [
@@ -20,7 +22,8 @@ class Activity extends Model
'action_id',
'user_id',
'decision_id',
'contract_id'
'contract_id',
'client_case_id',
];
protected $hidden = [
@@ -28,19 +31,25 @@ class Activity extends Model
'decision_id',
'client_case_id',
'user_id',
'contract_id'
'contract_id',
];
protected static function booted(){
protected static function booted()
{
static::creating(function (Activity $activity) {
if(!isset($activity->user_id)){
if (! isset($activity->user_id)) {
$activity->user_id = auth()->id();
}
// If an activity with a due date is added for a contract, update the related account's promise_date
if (! empty($activity->contract_id) && ! empty($activity->due_date)) {
DB::table('accounts')
->where('contract_id', $activity->contract_id)
->update(['promise_date' => $activity->due_date, 'updated_at' => now()]);
}
});
}
public function action(): BelongsTo
{
return $this->belongsTo(\App\Models\Action::class);
@@ -56,8 +65,13 @@ public function clientCase(): BelongsTo
return $this->belongsTo(\App\Models\ClientCase::class);
}
public function contract(): BelongsTo|null
public function contract(): ?BelongsTo
{
return $this->belongsTo(\App\Models\Contract::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(\App\Models\User::class);
}
}
+10 -8
View File
@@ -3,34 +3,35 @@
namespace App\Models;
use App\Traits\Uuid;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Builder;
use Laravel\Scout\Searchable;
class ClientCase extends Model
{
/** @use HasFactory<\Database\Factories\ClientCaseFactory> */
use HasFactory;
use Uuid;
use Searchable;
use Uuid;
protected $fillable = [
'client_id',
'person_id'
'person_id',
];
protected $hidden = [
'id',
'client_id',
'person_id'
'person_id',
];
protected function makeAllSearchableUsing(Builder $query): Builder
protected function makeAllSearchableUsing(Builder $query): Builder
{
return $query->with('person');
}
@@ -39,11 +40,11 @@ public function toSearchableArray(): array
{
return [
'person.full_name' => ''
'person.full_name' => '',
];
}
public function client(): BelongsTo
public function client(): BelongsTo
{
return $this->belongsTo(\App\Models\Client::class);
}
@@ -64,7 +65,8 @@ public function activities(): HasMany
return $this->hasMany(\App\Models\Activity::class);
}
public function segments(): BelongsToMany {
public function segments(): BelongsToMany
{
return $this->belongsToMany(\App\Models\Segment::class)->withTimestamps();
}
+16 -12
View File
@@ -3,24 +3,22 @@
namespace App\Models;
use App\Traits\Uuid;
use Illuminate\Database\Eloquent\Factories\BelongsToManyRelationship;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOneOrManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Contract extends Model
{
/** @use HasFactory<\Database\Factories\ContractFactory> */
use HasFactory;
use Uuid;
use SoftDeletes;
use Uuid;
protected $fillable = [
'reference',
@@ -28,13 +26,13 @@ class Contract extends Model
'end_date',
'client_case_id',
'type_id',
'description'
'description',
];
protected $hidden = [
'id',
'client_case_id',
'type_id'
'type_id',
];
public function type(): BelongsTo
@@ -47,8 +45,9 @@ public function clientCase(): BelongsTo
return $this->belongsTo(\App\Models\ClientCase::class)
->with(['person']);
}
public function segments(): BelongsToMany {
public function segments(): BelongsToMany
{
return $this->belongsToMany(\App\Models\Segment::class)
->withPivot('active', 'created_at')
->wherePivot('active', true);
@@ -65,6 +64,11 @@ public function objects(): HasMany
return $this->hasMany(\App\Models\CaseObject::class, 'contract_id');
}
public function documents(): MorphMany
{
return $this->morphMany(\App\Models\Document::class, 'documentable');
}
protected static function booted(): void
{
static::created(function (Contract $contract): void {
@@ -96,7 +100,7 @@ protected static function booted(): void
->where('client_case_id', $contract->client_case_id)
->where('segment_id', $cfg->segment_id)
->first();
if (!$attached) {
if (! $attached) {
\DB::table('client_case_segment')->insert([
'client_case_id' => $contract->client_case_id,
'segment_id' => $cfg->segment_id,
@@ -104,7 +108,7 @@ protected static function booted(): void
'created_at' => now(),
'updated_at' => now(),
]);
} elseif (!$attached->active) {
} elseif (! $attached->active) {
\DB::table('client_case_segment')
->where('id', $attached->id)
->update(['active' => true, 'updated_at' => now()]);
+12 -1
View File
@@ -13,8 +13,8 @@
class Document extends Model
{
use HasFactory;
use Uuid;
use SoftDeletes;
use Uuid;
protected $fillable = [
'uuid',
@@ -80,4 +80,15 @@ protected static function booted(): void
}
});
}
/**
* Include soft-deleted documents when resolving by route key (e.g. {document:uuid}).
*/
public function resolveRouteBinding($value, $field = null)
{
// Always include trashed so deep-linking to older documents works
return static::withTrashed()
->where($field ?? $this->getRouteKeyName(), $value)
->firstOrFail();
}
}
+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);
}
}
+21 -3
View File
@@ -14,8 +14,11 @@ class FieldJobSetting extends Model
protected $fillable = [
'segment_id',
'initial_decision_id',
'asign_decision_id',
'assign_decision_id',
'complete_decision_id',
'cancel_decision_id',
'return_segment_id',
'queue_segment_id',
];
public function segment(): BelongsTo
@@ -23,9 +26,9 @@ public function segment(): BelongsTo
return $this->belongsTo(Segment::class);
}
public function asignDecision(): BelongsTo
public function assignDecision(): BelongsTo
{
return $this->belongsTo(Decision::class, 'asign_decision_id');
return $this->belongsTo(Decision::class, 'assign_decision_id');
}
public function initialDecision(): BelongsTo
@@ -38,6 +41,21 @@ public function completeDecision(): BelongsTo
return $this->belongsTo(Decision::class, 'complete_decision_id');
}
public function cancelDecision(): BelongsTo
{
return $this->belongsTo(Decision::class, 'cancel_decision_id');
}
public function returnSegment(): BelongsTo
{
return $this->belongsTo(Segment::class, 'return_segment_id');
}
public function queueSegment(): BelongsTo
{
return $this->belongsTo(Segment::class, 'queue_segment_id');
}
public function fieldJobs(): HasMany
{
return $this->hasMany(FieldJob::class);