fix: ItemService update 시 attributes 머지 로직 추가

- 기존: attributes 전체 덮어쓰기 → 수정: 기존 값 보존 후 새 값만 머지
- 품목 수정 시 레거시 속성(sales_price, legacy 정보 등) 유실 방지

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-04 20:30:07 +09:00
parent d27061bbdc
commit a7975f7270
2 changed files with 20 additions and 32 deletions

View File

@@ -7,6 +7,8 @@
use App\Models\ItemMaster\ItemPage;
use App\Models\Items\Item;
use App\Models\Items\ItemDetail;
use App\Models\Products\CommonCode;
use App\Models\Scopes\TenantScope;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -425,7 +427,18 @@ public function index(array $params): LengthAwarePaginator
}
}
$paginator = $query->orderBy('id', 'desc')->paginate($size);
// common_codes에서 item_type 한글명 조회 (서브쿼리로 컬럼 충돌 방지)
$tableName = $query->getModel()->getTable();
$query->addSelect([
'item_type_name' => CommonCode::withoutGlobalScope(TenantScope::class)
->select('name')
->whereColumn('code', "{$tableName}.item_type")
->where('code_group', 'item_type')
->where('is_active', true)
->limit(1),
]);
$paginator = $query->orderBy("{$tableName}.id", 'desc')->paginate($size);
// 날짜 형식 변환, files 그룹화, options 펼침, code → item_code
$paginator->setCollection(
@@ -702,6 +715,12 @@ public function update(int $id, array $data): Model
$this->validateBom($data['bom'], $id);
}
// attributes 병합 (기존 값 보존, 새 값으로 덮어쓰기)
if (isset($data['attributes']) && is_array($data['attributes'])) {
$existingAttributes = is_array($item->attributes) ? $item->attributes : [];
$data['attributes'] = array_merge($existingAttributes, $data['attributes']);
}
// 테이블 업데이트
$itemData = array_intersect_key($data, array_flip([
'item_type', 'code', 'name', 'unit', 'category_id',