field job added option to add multiple contracts to user at once
This commit is contained in:
@@ -132,6 +132,77 @@ public function assign(Request $request)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk assign multiple contracts to a single user.
|
||||
*/
|
||||
public function assignBulk(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'contract_uuids' => 'required|array|min:1',
|
||||
'contract_uuids.*' => 'required|string|distinct|exists:contracts,uuid',
|
||||
'assigned_user_id' => 'required|integer|exists:users,id',
|
||||
]);
|
||||
|
||||
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.');
|
||||
}
|
||||
|
||||
if (! ($setting->action_id && $setting->assign_decision_id)) {
|
||||
throw new Exception('The current Field Job Setting is missing an action or assign decision. Please update it in Settings → Field Job Settings.');
|
||||
}
|
||||
|
||||
$assigneeName = User::query()->where('id', $data['assigned_user_id'])->value('name');
|
||||
$noteBase = 'Terensko opravilo dodeljeno'.($assigneeName ? ' uporabniku '.$assigneeName : '');
|
||||
|
||||
// Load all contracts in one query
|
||||
$contracts = Contract::query()->whereIn('uuid', $data['contract_uuids'])->get();
|
||||
|
||||
foreach ($contracts as $contract) {
|
||||
// Skip if already has an active job
|
||||
$hasActive = FieldJob::query()
|
||||
->where('contract_id', $contract->id)
|
||||
->whereNull('completed_at')
|
||||
->whereNull('cancelled_at')
|
||||
->exists();
|
||||
|
||||
if ($hasActive) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$job = FieldJob::create([
|
||||
'field_job_setting_id' => $setting->id,
|
||||
'assigned_user_id' => $data['assigned_user_id'],
|
||||
'contract_id' => $contract->id,
|
||||
'assigned_at' => now(),
|
||||
]);
|
||||
|
||||
Activity::create([
|
||||
'due_date' => null,
|
||||
'amount' => null,
|
||||
'note' => $noteBase,
|
||||
'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);
|
||||
}
|
||||
});
|
||||
|
||||
return back()->with('success', 'Field jobs assigned.');
|
||||
} catch (QueryException $e) {
|
||||
return back()->withErrors(['database' => 'Database error: '.$e->getMessage()]);
|
||||
} catch (Exception $e) {
|
||||
return back()->withErrors(['error' => 'Error: '.$e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
public function cancel(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
|
||||
Reference in New Issue
Block a user