authorizeSupplyChain('supplychain.scorecard.view');
$businessId = $this->supplyChainUtil->getBusinessId();
if ($request->ajax()) {
$scorecards = SupplierScorecard::forBusiness($businessId)
->with('contact')
->orderByDesc('period_end');
return DataTables::of($scorecards)
->addColumn('supplier_name', fn ($row) => $row->contact?->name ?? '—')
->addColumn('period', fn ($row) => $row->period_start->format('Y-m-d').' — '.$row->period_end->format('Y-m-d'))
->addColumn('risk_badge', function ($row) {
$class = $this->supplyChainUtil->riskBadgeClass($row->risk_level);
return ''.__('supplychain::lang.risk_'.$row->risk_level).'';
})
->addColumn('action', function ($row) {
return '';
})
->rawColumns(['risk_badge', 'action'])
->make(true);
}
return view('supplychain::supplier_scorecards.index');
}
public function show($id)
{
$this->authorizeSupplyChain('supplychain.scorecard.view');
$businessId = $this->supplyChainUtil->getBusinessId();
$scorecard = SupplierScorecard::forBusiness($businessId)->with('contact')->findOrFail($id);
return view('supplychain::supplier_scorecards.show', compact('scorecard'));
}
public function calculate(Request $request)
{
$this->authorizeSupplyChain('supplychain.scorecard.create');
$businessId = $this->supplyChainUtil->getBusinessId();
$data = $request->validate([
'period_start' => 'nullable|date',
'period_end' => 'nullable|date|after_or_equal:period_start',
]);
$start = ! empty($data['period_start']) ? Carbon::parse($data['period_start']) : null;
$end = ! empty($data['period_end']) ? Carbon::parse($data['period_end']) : null;
$results = $this->scorecardService->calculateForBusiness($businessId, $start, $end);
return $this->supplyChainUtil->jsonOrRedirect(
$request,
true,
__('supplychain::lang.scorecards_calculated', ['count' => count($results)]),
action([self::class, 'index'])
);
}
public function calculateSupplier(Request $request, $contactId)
{
$this->authorizeSupplyChain('supplychain.scorecard.create');
$businessId = $this->supplyChainUtil->getBusinessId();
$this->scorecardService->calculateForSupplier($businessId, (int) $contactId);
return $this->supplyChainUtil->jsonOrRedirect(
$request,
true,
__('supplychain::lang.scorecard_calculated')
);
}
public function contactScorecards($contactId)
{
$this->authorizeSupplyChain('supplychain.scorecard.view');
$businessId = $this->supplyChainUtil->getBusinessId();
$scorecards = SupplierScorecard::forBusiness($businessId)
->where('contact_id', (int) $contactId)
->orderByDesc('period_end')
->limit(12)
->get();
return response()->json([
'scorecards' => $scorecards->map(function ($sc) {
return [
'period' => $sc->period_start->format('Y-m-d').' — '.$sc->period_end->format('Y-m-d'),
'overall_score' => number_format($sc->overall_score, 1),
'otif_score' => number_format($sc->otif_score, 1),
'quality_score' => number_format($sc->quality_score, 1),
'risk_class' => $this->supplyChainUtil->riskBadgeClass($sc->risk_level),
'risk_label' => __('supplychain::lang.risk_'.$sc->risk_level),
];
}),
]);
}
}