Changes 0228092025 Laptop

This commit is contained in:
2025-09-28 14:51:02 +02:00
parent 765beb78b7
commit b40ee9dcde
36 changed files with 2099 additions and 65 deletions
+6
View File
@@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Laravel\Scout\Searchable;
class Action extends Model
@@ -26,4 +27,9 @@ public function segment(): BelongsTo
return $this->belongsTo(\App\Models\Segment::class);
}
public function activities(): HasMany
{
return $this->hasMany(\App\Models\Activity::class);
}
}
+68
View File
@@ -65,4 +65,72 @@ public function objects(): HasMany
return $this->hasMany(\App\Models\CaseObject::class, 'contract_id');
}
protected static function booted(): void
{
static::created(function (Contract $contract): void {
// Only apply configs when type and client case are set and no segments are already attached
if (empty($contract->type_id) || empty($contract->client_case_id)) {
return;
}
$existing = \DB::table('contract_segment')
->where('contract_id', $contract->id)
->count();
if ($existing > 0) {
// Respect pre-attached segments (e.g. custom import logic)
return;
}
$configs = ContractConfig::query()
->where('contract_type_id', $contract->type_id)
->where('active', true)
->get(['segment_id', 'is_initial']);
if ($configs->isEmpty()) {
return;
}
foreach ($configs as $cfg) {
// Ensure the segment is attached to the client case and active
$attached = \DB::table('client_case_segment')
->where('client_case_id', $contract->client_case_id)
->where('segment_id', $cfg->segment_id)
->first();
if (!$attached) {
\DB::table('client_case_segment')->insert([
'client_case_id' => $contract->client_case_id,
'segment_id' => $cfg->segment_id,
'active' => true,
'created_at' => now(),
'updated_at' => now(),
]);
} elseif (!$attached->active) {
\DB::table('client_case_segment')
->where('id', $attached->id)
->update(['active' => true, 'updated_at' => now()]);
}
// Attach to contract; mark active if initial, otherwise inactive
$pivot = \DB::table('contract_segment')
->where('contract_id', $contract->id)
->where('segment_id', $cfg->segment_id)
->first();
$activeFlag = $cfg->is_initial ? true : false;
if ($pivot) {
\DB::table('contract_segment')
->where('id', $pivot->id)
->update(['active' => $activeFlag, 'updated_at' => now()]);
} else {
\DB::table('contract_segment')->insert([
'contract_id' => $contract->id,
'segment_id' => $cfg->segment_id,
'active' => $activeFlag,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
});
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ContractConfig extends Model
{
use HasFactory;
protected $fillable = [
'contract_type_id',
'segment_id',
'is_initial',
'active',
];
protected function casts(): array
{
return [
'active' => 'boolean',
'is_initial' => 'boolean',
];
}
public function type(): BelongsTo
{
return $this->belongsTo(ContractType::class, 'contract_type_id');
}
public function segment(): BelongsTo
{
return $this->belongsTo(Segment::class, 'segment_id');
}
}
+5
View File
@@ -24,4 +24,9 @@ public function events(): BelongsToMany
{
return $this->belongsToMany(\App\Models\Event::class);
}
public function activities(): HasMany
{
return $this->hasMany(\App\Models\Activity::class);
}
}
+25 -4
View File
@@ -5,21 +5,32 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class FieldJob extends Model
{
use HasFactory;
use SoftDeletes;
protected $fillable = [
'field_job_setting_id',
'asigned_user_id',
'start_date',
'end_date',
'user_id',
'contract_id',
'assigned_at',
'completed_at',
'cancelled_at',
'priority',
'notes',
'address_snapshot ',
];
protected $casts = [
'start_date' => 'date',
'end_date' => 'date',
'assigned_at' => 'date',
'completed_at' => 'date',
'cancelled_at' => 'date',
'priority' => 'boolean',
'address_snapshot ' => 'array',
];
protected static function booted(){
@@ -39,4 +50,14 @@ public function assignedUser(): BelongsTo
{
return $this->belongsTo(User::class, 'asigned_user_id');
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
public function contract(): BelongsTo
{
return $this->belongsTo(Contract::class, 'contract_id');
}
}
+6
View File
@@ -13,6 +13,7 @@ class FieldJobSetting extends Model
protected $fillable = [
'segment_id',
'initial_decision_id',
'asign_decision_id',
'complete_decision_id',
];
@@ -27,6 +28,11 @@ public function asignDecision(): BelongsTo
return $this->belongsTo(Decision::class, 'asign_decision_id');
}
public function initialDecision(): BelongsTo
{
return $this->belongsTo(Decision::class, 'initial_decision_id');
}
public function completeDecision(): BelongsTo
{
return $this->belongsTo(Decision::class, 'complete_decision_id');
+13
View File
@@ -11,6 +11,19 @@ class Segment extends Model
/** @use HasFactory<\Database\Factories\SegmentFactory> */
use HasFactory;
protected $fillable = [
'name',
'description',
'active',
];
protected function casts(): array
{
return [
'active' => 'boolean',
];
}
public function contracts(): BelongsToMany {
return $this->belongsToMany(\App\Models\Contract::class);
}