59 lines
2.0 KiB
PHP
59 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace Modules\QualityManagement\Services;
|
|
|
|
use Modules\QualityManagement\Models\CapaAction;
|
|
use Modules\QualityManagement\Models\NcrReport;
|
|
|
|
class NcrWorkflowService
|
|
{
|
|
public function createNcr(int $businessId, array $data, QmsIntegrationService $integration): NcrReport
|
|
{
|
|
$ncr = NcrReport::create(array_merge($data, [
|
|
'business_id' => $businessId,
|
|
'ref_no' => $data['ref_no'] ?? $integration->nextRef($businessId, 'NCR'),
|
|
'reported_by' => $data['reported_by'] ?? auth()->id(),
|
|
'detected_at' => $data['detected_at'] ?? now()->toDateString(),
|
|
]));
|
|
|
|
return $ncr;
|
|
}
|
|
|
|
public function escalateToCapa(NcrReport $ncr, array $capaData, QmsIntegrationService $integration): CapaAction
|
|
{
|
|
$capa = CapaAction::create([
|
|
'business_id' => $ncr->business_id,
|
|
'ncr_id' => $ncr->id,
|
|
'ref_no' => $integration->nextRef($ncr->business_id, 'CAPA'),
|
|
'type' => $capaData['type'] ?? 'corrective',
|
|
'title' => $capaData['title'] ?? 'CAPA for '.$ncr->ref_no,
|
|
'root_cause' => $capaData['root_cause'] ?? null,
|
|
'action_plan' => $capaData['action_plan'] ?? null,
|
|
'owner_id' => $capaData['owner_id'] ?? auth()->id(),
|
|
'due_date' => $capaData['due_date'] ?? now()->addDays(30)->toDateString(),
|
|
'status' => 'planned',
|
|
]);
|
|
|
|
if ($ncr->status === 'open') {
|
|
$ncr->update(['status' => 'investigating']);
|
|
}
|
|
|
|
return $capa;
|
|
}
|
|
|
|
public function closeNcr(NcrReport $ncr): NcrReport
|
|
{
|
|
$openCapas = CapaAction::where('ncr_id', $ncr->id)
|
|
->whereNotIn('status', ['closed', 'cancelled'])
|
|
->count();
|
|
|
|
if ($openCapas > 0) {
|
|
throw new \RuntimeException(__('qualitymanagement::lang.open_capas_block_close'));
|
|
}
|
|
|
|
$ncr->update(['status' => 'closed', 'closed_at' => now()->toDateString()]);
|
|
|
|
return $ncr->fresh();
|
|
}
|
|
}
|