Mass changes
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
use App\Models\FieldJob;
|
||||
use App\Models\FieldJobSetting;
|
||||
use App\Models\User;
|
||||
use Exception;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Inertia\Inertia;
|
||||
@@ -83,48 +85,51 @@ public function assign(Request $request)
|
||||
'assigned_user_id' => 'required|integer|exists:users,id',
|
||||
]);
|
||||
|
||||
$setting = FieldJobSetting::query()->latest('id')->first();
|
||||
if (! $setting) {
|
||||
return back()->withErrors(['setting' => 'No Field Job Setting found. Create one in Settings → Field Job Settings.']);
|
||||
try {
|
||||
DB::transaction(function () use ($data) {
|
||||
$setting = FieldJobSetting::query()->latest('id')->first();
|
||||
|
||||
if (! $setting) {
|
||||
throw new Exception('No Field Job Setting found. Create one in Settings → Field Job Settings.');
|
||||
}
|
||||
|
||||
$contract = Contract::query()->where('uuid', $data['contract_uuid'])->firstOrFail();
|
||||
|
||||
$job = FieldJob::create([
|
||||
'field_job_setting_id' => $setting->id,
|
||||
'assigned_user_id' => $data['assigned_user_id'],
|
||||
'contract_id' => $contract->id,
|
||||
'assigned_at' => now(),
|
||||
]);
|
||||
// Create an activity for the assignment
|
||||
if ($setting->action_id && $setting->assign_decision_id) {
|
||||
$assigneeName = User::query()->where('id', $data['assigned_user_id'])->value('name');
|
||||
// Localized note: "Terensko opravilo dodeljeno" + assignee when present
|
||||
$note = 'Terensko opravilo dodeljeno'.($assigneeName ? ' uporabniku '.$assigneeName : '');
|
||||
Activity::create([
|
||||
'due_date' => null,
|
||||
'amount' => null,
|
||||
'note' => $note,
|
||||
'action_id' => $setting->action_id,
|
||||
'decision_id' => $setting->assign_decision_id,
|
||||
'client_case_id' => $contract->client_case_id,
|
||||
'contract_id' => $contract->id,
|
||||
]);
|
||||
|
||||
// Move contract to the configured segment for field jobs
|
||||
$job->moveContractToSegment($setting->segment_id);
|
||||
} else {
|
||||
throw new Exception('The current Field Job Setting is missing an action or assign decision. Please update it in Settings → Field Job Settings.');
|
||||
}
|
||||
});
|
||||
|
||||
return back()->with('success', 'Field job assigned.');
|
||||
} catch (QueryException $e) {
|
||||
return back()->withErrors(['database' => 'Database error: '.$e->getMessage()]);
|
||||
} catch (Exception $e) {
|
||||
return back()->withErrors(['error' => 'Error: '.$e->getMessage()]);
|
||||
}
|
||||
|
||||
$contract = Contract::query()->where('uuid', $data['contract_uuid'])->firstOrFail();
|
||||
|
||||
$job = FieldJob::create([
|
||||
'field_job_setting_id' => $setting->id,
|
||||
'assigned_user_id' => $data['assigned_user_id'],
|
||||
'contract_id' => $contract->id,
|
||||
'assigned_at' => now(),
|
||||
]);
|
||||
|
||||
// Create an activity for the assignment
|
||||
// Find the first action linked to the assign decision via pivot; also prefer actions within the same segment as the setting
|
||||
$decisionId = $setting->assign_decision_id;
|
||||
$actionId = null;
|
||||
if ($decisionId) {
|
||||
// Strictly use the action_decision pivot: take the first action mapped to this decision
|
||||
$actionId = DB::table('action_decision')
|
||||
->where('decision_id', $decisionId)
|
||||
->orderBy('id')
|
||||
->value('action_id');
|
||||
}
|
||||
|
||||
if ($actionId) {
|
||||
$assigneeName = User::query()->where('id', $data['assigned_user_id'])->value('name');
|
||||
// Localized note: "Terensko opravilo dodeljeno" + assignee when present
|
||||
$note = 'Terensko opravilo dodeljeno'.($assigneeName ? ' uporabniku '.$assigneeName : '');
|
||||
Activity::create([
|
||||
'due_date' => null,
|
||||
'amount' => null,
|
||||
'note' => $note,
|
||||
'action_id' => $actionId,
|
||||
'decision_id' => $decisionId,
|
||||
'client_case_id' => $contract->client_case_id,
|
||||
'contract_id' => $contract->id,
|
||||
]);
|
||||
}
|
||||
|
||||
return back()->with('success', 'Field job assigned.');
|
||||
}
|
||||
|
||||
public function cancel(Request $request)
|
||||
@@ -149,24 +154,22 @@ public function cancel(Request $request)
|
||||
// Create an activity for the cancellation, mirroring the assign flow
|
||||
// Prefer the job's setting for a consistent decision
|
||||
$job->loadMissing('setting');
|
||||
$actionId = optional($job->setting)->action_id;
|
||||
$decisionId = optional($job->setting)->cancel_decision_id;
|
||||
if ($decisionId) {
|
||||
$actionId = DB::table('action_decision')
|
||||
->where('decision_id', $decisionId)
|
||||
->orderBy('id')
|
||||
->value('action_id');
|
||||
|
||||
if ($actionId) {
|
||||
Activity::create([
|
||||
'due_date' => null,
|
||||
'amount' => null,
|
||||
'note' => 'Terensko opravilo preklicano',
|
||||
'action_id' => $actionId,
|
||||
'decision_id' => $decisionId,
|
||||
'client_case_id' => $contract->client_case_id,
|
||||
'contract_id' => $contract->id,
|
||||
]);
|
||||
}
|
||||
// If no decision configured, skip logging
|
||||
if ($actionId && $decisionId) {
|
||||
|
||||
Activity::create([
|
||||
'due_date' => null,
|
||||
'amount' => null,
|
||||
'note' => 'Terensko opravilo preklicano',
|
||||
'action_id' => $actionId,
|
||||
'decision_id' => $decisionId,
|
||||
'client_case_id' => $contract->client_case_id,
|
||||
'contract_id' => $contract->id,
|
||||
]);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,13 +186,7 @@ public function complete(Request $request, \App\Models\ClientCase $clientCase)
|
||||
}
|
||||
|
||||
$decisionId = $setting->complete_decision_id;
|
||||
$actionId = null;
|
||||
if ($decisionId) {
|
||||
$actionId = DB::table('action_decision')
|
||||
->where('decision_id', $decisionId)
|
||||
->orderBy('id')
|
||||
->value('action_id');
|
||||
}
|
||||
$actionId = $setting->action_id;
|
||||
|
||||
// Find all active jobs for this case for the current user
|
||||
$jobs = FieldJob::query()
|
||||
|
||||
Reference in New Issue
Block a user