Files
sam-api/app/Http/Resources/Api/V1/GuiderailModelResource.php

72 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Resources\Api\V1;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class GuiderailModelResource extends JsonResource
{
public function toArray(Request $request): array
{
$components = $this->getOption('components', []);
$materialSummary = $this->getOption('material_summary');
// material_summary가 없으면 components에서 계산
if (empty($materialSummary) && ! empty($components)) {
$materialSummary = $this->calcMaterialSummary($components);
}
return [
'id' => $this->id,
'code' => $this->code,
'name' => $this->name,
'item_type' => $this->item_type,
'item_category' => $this->item_category,
'is_active' => $this->is_active,
// 모델 속성
'model_name' => $this->getOption('model_name'),
'check_type' => $this->getOption('check_type'),
'rail_width' => $this->getOption('rail_width'),
'rail_length' => $this->getOption('rail_length'),
'finishing_type' => $this->getOption('finishing_type'),
'item_sep' => $this->getOption('item_sep'),
'model_UA' => $this->getOption('model_UA'),
'search_keyword' => $this->getOption('search_keyword'),
'author' => $this->getOption('author'),
'memo' => $this->getOption('memo'),
'registration_date' => $this->getOption('registration_date'),
// 케이스(SHUTTERBOX_MODEL) 전용
'exit_direction' => $this->getOption('exit_direction'),
'front_bottom_width' => $this->getOption('front_bottom_width'),
'box_width' => $this->getOption('box_width'),
'box_height' => $this->getOption('box_height'),
// 하단마감재(BOTTOMBAR_MODEL) 전용
'bar_width' => $this->getOption('bar_width'),
'bar_height' => $this->getOption('bar_height'),
// 부품 조합
'components' => $components,
'material_summary' => $materialSummary,
'component_count' => count($components),
// 메타
'created_at' => $this->created_at?->format('Y-m-d H:i:s'),
'updated_at' => $this->updated_at?->format('Y-m-d H:i:s'),
];
}
private function calcMaterialSummary(array $components): array
{
$summary = [];
foreach ($components as $comp) {
$material = $comp['material'] ?? null;
$widthSum = $comp['width_sum'] ?? 0;
$qty = $comp['quantity'] ?? 1;
if ($material && $widthSum) {
$summary[$material] = ($summary[$material] ?? 0) + ($widthSum * $qty);
}
}
return $summary;
}
}