Merge branch 'develop' of http://114.203.209.83:3000/SamProject/sam-api into develop

This commit is contained in:
김보곤
2026-02-25 14:10:53 +09:00
36 changed files with 3904 additions and 2779 deletions

View File

@@ -0,0 +1,631 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Attributes\AsCommand;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
#[AsCommand(name: 'migrate:5130-bending-stock', description: '5130 레거시 절곡품 코드 생성 + BD-* 전체 품목 초기 재고 셋팅')]
class Migrate5130BendingStock extends Command
{
protected $signature = 'migrate:5130-bending-stock
{--tenant_id=287 : Target tenant ID (default: 287 경동기업)}
{--dry-run : 실제 저장 없이 시뮬레이션만 수행}
{--min-stock=100 : 품목별 초기 재고 수량 (기본: 100)}
{--rollback : 초기 재고 셋팅 롤백 (init_stock 소스 데이터 삭제)}';
private string $sourceDb = 'chandj';
private string $targetDb = 'mysql';
// 5130 prod 코드 → 한글명
private array $prodNames = [
'R' => '가이드레일(벽면)', 'S' => '가이드레일(측면)',
'G' => '연기차단재', 'B' => '하단마감재(스크린)',
'T' => '하단마감재(철재)', 'L' => 'L-Bar', 'C' => '케이스',
];
// 5130 spec 코드 → 한글명
private array $specNames = [
'I' => '화이바원단', 'S' => 'SUS', 'U' => 'SUS2', 'E' => 'EGI',
'A' => '스크린용', 'D' => 'D형', 'C' => 'C형', 'M' => '본체',
'T' => '본체(철재)', 'B' => '후면코너부', 'L' => '린텔부',
'P' => '점검구', 'F' => '전면부',
];
// 5130 slength 코드 → 한글명
private array $slengthNames = [
'53' => 'W50×3000', '54' => 'W50×4000', '83' => 'W80×3000',
'84' => 'W80×4000', '12' => '1219mm', '24' => '2438mm',
'30' => '3000mm', '35' => '3500mm', '40' => '4000mm',
'41' => '4150mm', '42' => '4200mm', '43' => '4300mm',
];
private array $stats = [
'items_found' => 0,
'items_created_5130' => 0,
'items_category_updated' => 0,
'stocks_created' => 0,
'stocks_skipped' => 0,
'lots_created' => 0,
'transactions_created' => 0,
];
public function handle(): int
{
$tenantId = (int) $this->option('tenant_id');
$dryRun = $this->option('dry-run');
$rollback = $this->option('rollback');
$minStock = (int) $this->option('min-stock');
$this->info('=== BD-* 절곡품 초기 재고 셋팅 ===');
$this->info("Tenant ID: {$tenantId}");
$this->info('Mode: '.($dryRun ? 'DRY-RUN (시뮬레이션)' : 'LIVE'));
$this->info("초기 재고: {$minStock}개/품목");
$this->newLine();
if ($rollback) {
return $this->rollbackInitStock($tenantId, $dryRun);
}
// 0. 5130 레거시 데이터에서 BD-{PROD}{SPEC}-{SLENGTH} 아이템 생성
$this->info('📥 Step 0: 5130 레거시 코드 → BD 아이템 생성...');
$this->createLegacyItems($tenantId, $dryRun);
$this->newLine();
// 1. 전체 BD-* 아이템 조회 (기존 58개 + 5130 생성분)
$this->info('📥 Step 1: BD-* 절곡품 품목 조회...');
$items = DB::connection($this->targetDb)
->table('items')
->where('tenant_id', $tenantId)
->where('code', 'like', 'BD-%')
->whereNull('deleted_at')
->select('id', 'code', 'name', 'item_type', 'item_category', 'unit', 'options')
->orderBy('code')
->get();
$this->stats['items_found'] = $items->count();
$this->info(" - BD-* 품목: {$items->count()}");
if ($items->isEmpty()) {
$this->warn('BD-* 품목이 없습니다. 종료합니다.');
return self::SUCCESS;
}
// 2. item_category 미설정 품목 업데이트
$this->newLine();
$this->info('🏷️ Step 2: item_category 업데이트...');
$needsCategoryUpdate = $items->filter(fn ($item) => $item->item_category !== 'BENDING');
if ($needsCategoryUpdate->isNotEmpty()) {
$this->info(" - item_category 미설정/불일치: {$needsCategoryUpdate->count()}");
if (! $dryRun) {
DB::connection($this->targetDb)
->table('items')
->where('tenant_id', $tenantId)
->where('code', 'like', 'BD-%')
->whereNull('deleted_at')
->where(function ($q) {
$q->whereNull('item_category')
->orWhere('item_category', '!=', 'BENDING');
})
->update(['item_category' => 'BENDING', 'updated_at' => now()]);
}
$this->stats['items_category_updated'] = $needsCategoryUpdate->count();
} else {
$this->info(' - 모든 품목 BENDING 카테고리 설정 완료');
}
// 3. 현재 재고 현황 표시
$this->newLine();
$this->info('📊 Step 3: 현재 재고 현황...');
$this->showCurrentStockStatus($tenantId, $items);
// 4. 재고 셋팅 대상 확인
$this->newLine();
$this->info('📦 Step 4: 재고 셋팅 대상 확인...');
$itemsNeedingStock = $this->getItemsNeedingStock($tenantId, $items, $minStock);
if ($itemsNeedingStock->isEmpty()) {
$this->info(" - 모든 품목이 이미 {$minStock}개 이상 재고 보유. 추가 작업 불필요.");
$this->showStats();
return self::SUCCESS;
}
$this->info(" - 재고 셋팅 필요: {$itemsNeedingStock->count()}");
$this->table(
['코드', '품목명', '현재고', '목표', '추가수량'],
$itemsNeedingStock->map(fn ($item) => [
$item->code,
mb_strlen($item->name) > 30 ? mb_substr($item->name, 0, 30).'...' : $item->name,
number_format($item->current_qty),
number_format($minStock),
number_format($item->supplement_qty),
])->toArray()
);
if ($dryRun) {
$this->stats['stocks_created'] = $itemsNeedingStock->filter(fn ($i) => ! $i->has_stock)->count();
$this->stats['lots_created'] = $itemsNeedingStock->count();
$this->stats['transactions_created'] = $itemsNeedingStock->count();
$this->showStats();
$this->info('🔍 DRY RUN 완료. 실제 실행은 --dry-run 플래그를 제거하세요.');
return self::SUCCESS;
}
if (! $this->confirm('초기 재고를 셋팅하시겠습니까?')) {
$this->info('취소되었습니다.');
return self::SUCCESS;
}
// 5. 실행
$this->newLine();
$this->info('🚀 Step 5: 초기 재고 셋팅 실행...');
DB::connection($this->targetDb)->transaction(function () use ($tenantId, $itemsNeedingStock, $minStock) {
$this->executeStockSetup($tenantId, $itemsNeedingStock, $minStock);
});
$this->newLine();
$this->showStats();
$this->info('✅ 초기 재고 셋팅 완료!');
return self::SUCCESS;
}
/**
* 현재 재고 현황 표시
*/
private function showCurrentStockStatus(int $tenantId, \Illuminate\Support\Collection $items): void
{
$itemIds = $items->pluck('id');
$stocks = DB::connection($this->targetDb)
->table('stocks')
->where('tenant_id', $tenantId)
->whereIn('item_id', $itemIds)
->whereNull('deleted_at')
->get()
->keyBy('item_id');
$hasStock = 0;
$noStock = 0;
foreach ($items as $item) {
$stock = $stocks->get($item->id);
if ($stock && (float) $stock->stock_qty > 0) {
$hasStock++;
} else {
$noStock++;
}
}
$this->info(" - 재고 있음: {$hasStock}");
$this->info(" - 재고 없음: {$noStock}");
}
/**
* 재고 셋팅이 필요한 품목 목록 조회
*/
private function getItemsNeedingStock(int $tenantId, \Illuminate\Support\Collection $items, int $minStock): \Illuminate\Support\Collection
{
$itemIds = $items->pluck('id');
$stocks = DB::connection($this->targetDb)
->table('stocks')
->where('tenant_id', $tenantId)
->whereIn('item_id', $itemIds)
->whereNull('deleted_at')
->get()
->keyBy('item_id');
$result = collect();
foreach ($items as $item) {
$stock = $stocks->get($item->id);
$currentQty = $stock ? (float) $stock->stock_qty : 0;
if ($currentQty >= $minStock) {
$this->stats['stocks_skipped']++;
continue;
}
$supplementQty = $minStock - $currentQty;
$item->has_stock = (bool) $stock;
$item->stock_id = $stock?->id;
$item->current_qty = $currentQty;
$item->supplement_qty = $supplementQty;
$result->push($item);
}
return $result;
}
/**
* 초기 재고 셋팅 실행
*/
private function executeStockSetup(int $tenantId, \Illuminate\Support\Collection $items, int $minStock): void
{
foreach ($items as $item) {
$stockId = $item->stock_id;
// Stock 레코드가 없으면 생성
if (! $item->has_stock) {
$stockId = DB::connection($this->targetDb)->table('stocks')->insertGetId([
'tenant_id' => $tenantId,
'item_id' => $item->id,
'item_code' => $item->code,
'item_name' => $item->name,
'item_type' => 'bent_part',
'unit' => $item->unit ?? 'EA',
'stock_qty' => 0,
'safety_stock' => 0,
'reserved_qty' => 0,
'available_qty' => 0,
'lot_count' => 0,
'status' => 'out',
'created_at' => now(),
'updated_at' => now(),
]);
$this->stats['stocks_created']++;
$this->line(" + Stock 생성: {$item->code}");
}
// FIFO 순서 계산
$maxFifo = DB::connection($this->targetDb)
->table('stock_lots')
->where('stock_id', $stockId)
->max('fifo_order');
$nextFifo = ($maxFifo ?? 0) + 1;
// LOT 번호 생성
$lotNo = 'INIT-'.now()->format('ymd').'-'.str_replace(['-', ' ', '*'], ['', '', 'x'], $item->code);
// 중복 체크
$existingLot = DB::connection($this->targetDb)
->table('stock_lots')
->where('tenant_id', $tenantId)
->where('stock_id', $stockId)
->where('lot_no', $lotNo)
->whereNull('deleted_at')
->first();
if ($existingLot) {
$this->warn(" ⚠️ 이미 LOT 존재 (skip): {$lotNo}");
continue;
}
$supplementQty = $item->supplement_qty;
// StockLot 생성
$stockLotId = DB::connection($this->targetDb)->table('stock_lots')->insertGetId([
'tenant_id' => $tenantId,
'stock_id' => $stockId,
'lot_no' => $lotNo,
'fifo_order' => $nextFifo,
'receipt_date' => now()->toDateString(),
'qty' => $supplementQty,
'reserved_qty' => 0,
'available_qty' => $supplementQty,
'unit' => $item->unit ?? 'EA',
'status' => 'available',
'created_at' => now(),
'updated_at' => now(),
]);
$this->stats['lots_created']++;
// StockTransaction 생성
DB::connection($this->targetDb)->table('stock_transactions')->insert([
'tenant_id' => $tenantId,
'stock_id' => $stockId,
'stock_lot_id' => $stockLotId,
'type' => 'IN',
'qty' => $supplementQty,
'balance_qty' => 0,
'reference_type' => 'init_stock',
'reference_id' => 0,
'lot_no' => $lotNo,
'reason' => 'receiving',
'remark' => "절곡품 초기 재고 셋팅 (min-stock={$minStock})",
'item_code' => $item->code,
'item_name' => $item->name,
'created_at' => now(),
]);
$this->stats['transactions_created']++;
// Stock 집계 갱신
$this->refreshStockFromLots($stockId, $tenantId);
$this->line("{$item->code}: 0 → {$supplementQty} (+{$supplementQty})");
}
}
/**
* Stock 집계 갱신 (LOT 기반)
*/
private function refreshStockFromLots(int $stockId, int $tenantId): void
{
$lotStats = DB::connection($this->targetDb)
->table('stock_lots')
->where('stock_id', $stockId)
->where('tenant_id', $tenantId)
->whereNull('deleted_at')
->selectRaw('
COALESCE(SUM(qty), 0) as total_qty,
COALESCE(SUM(reserved_qty), 0) as total_reserved,
COALESCE(SUM(available_qty), 0) as total_available,
COUNT(*) as lot_count,
MIN(receipt_date) as oldest_lot_date,
MAX(receipt_date) as latest_receipt_date
')
->first();
$stockQty = (float) $lotStats->total_qty;
DB::connection($this->targetDb)
->table('stocks')
->where('id', $stockId)
->update([
'stock_qty' => $stockQty,
'reserved_qty' => (float) $lotStats->total_reserved,
'available_qty' => (float) $lotStats->total_available,
'lot_count' => (int) $lotStats->lot_count,
'oldest_lot_date' => $lotStats->oldest_lot_date,
'last_receipt_date' => $lotStats->latest_receipt_date,
'status' => $stockQty > 0 ? 'normal' : 'out',
'updated_at' => now(),
]);
}
/**
* 롤백: init_stock 참조 데이터 삭제
*/
private function rollbackInitStock(int $tenantId, bool $dryRun): int
{
$this->warn('⚠️ 롤백: 초기 재고 셋팅 데이터를 삭제합니다.');
// init_stock으로 생성된 트랜잭션
$txCount = DB::connection($this->targetDb)
->table('stock_transactions')
->where('tenant_id', $tenantId)
->where('reference_type', 'init_stock')
->count();
// init_stock 트랜잭션에 연결된 LOT
$lotIds = DB::connection($this->targetDb)
->table('stock_transactions')
->where('tenant_id', $tenantId)
->where('reference_type', 'init_stock')
->whereNotNull('stock_lot_id')
->pluck('stock_lot_id')
->unique();
// 5130으로 생성된 아이템
$legacyItemCount = DB::connection($this->targetDb)
->table('items')
->where('tenant_id', $tenantId)
->where('options->source', '5130_migration')
->whereNull('deleted_at')
->count();
$this->info(' 삭제 대상:');
$this->info(" - stock_transactions (reference_type=init_stock): {$txCount}");
$this->info(" - stock_lots (연결 LOT): {$lotIds->count()}");
$this->info(" - items (source=5130_migration): {$legacyItemCount}");
if ($dryRun) {
$this->info('DRY RUN - 실제 삭제 없음');
return self::SUCCESS;
}
if (! $this->confirm('정말 롤백하시겠습니까? 되돌릴 수 없습니다.')) {
return self::SUCCESS;
}
DB::connection($this->targetDb)->transaction(function () use ($tenantId, $lotIds) {
// 1. 트랜잭션 삭제
DB::connection($this->targetDb)
->table('stock_transactions')
->where('tenant_id', $tenantId)
->where('reference_type', 'init_stock')
->delete();
// 2. LOT에서 stock_id 목록 수집 (집계 갱신용)
$affectedStockIds = collect();
if ($lotIds->isNotEmpty()) {
$affectedStockIds = DB::connection($this->targetDb)
->table('stock_lots')
->whereIn('id', $lotIds)
->pluck('stock_id')
->unique();
// LOT 삭제
DB::connection($this->targetDb)
->table('stock_lots')
->whereIn('id', $lotIds)
->delete();
}
// 3. 영향받은 Stock 집계 갱신
foreach ($affectedStockIds as $stockId) {
$this->refreshStockFromLots($stockId, $tenantId);
}
// 4. 5130 migration으로 생성된 아이템 + 연결 stocks 삭제
$migrationItemIds = DB::connection($this->targetDb)
->table('items')
->where('tenant_id', $tenantId)
->where('options->source', '5130_migration')
->whereNull('deleted_at')
->pluck('id');
if ($migrationItemIds->isNotEmpty()) {
$migrationStockIds = DB::connection($this->targetDb)
->table('stocks')
->where('tenant_id', $tenantId)
->whereIn('item_id', $migrationItemIds)
->pluck('id');
if ($migrationStockIds->isNotEmpty()) {
DB::connection($this->targetDb)
->table('stock_lots')
->whereIn('stock_id', $migrationStockIds)
->delete();
DB::connection($this->targetDb)
->table('stocks')
->whereIn('id', $migrationStockIds)
->delete();
}
DB::connection($this->targetDb)
->table('items')
->whereIn('id', $migrationItemIds)
->delete();
}
});
$this->info('✅ 롤백 완료');
return self::SUCCESS;
}
/**
* 5130 레거시 데이터에서 BD-{PROD}{SPEC}-{SLENGTH} 아이템 생성
*/
private function createLegacyItems(int $tenantId, bool $dryRun): void
{
// 5130 lot 테이블에서 고유 prod+spec+slength 조합 추출
$lots = DB::connection($this->sourceDb)
->table('lot')
->where(function ($q) {
$q->whereNull('is_deleted')
->orWhere('is_deleted', 0);
})
->whereNotNull('prod')
->where('prod', '!=', '')
->whereNotNull('surang')
->where('surang', '>', 0)
->select('prod', 'spec', 'slength')
->distinct()
->get();
// bending_work_log 테이블에서도 추출 (lot에 없는 조합 포함)
$workLogs = DB::connection($this->sourceDb)
->table('bending_work_log')
->where(function ($q) {
$q->whereNull('is_deleted')
->orWhere('is_deleted', 0);
})
->whereNotNull('prod_code')
->where('prod_code', '!=', '')
->select('prod_code as prod', 'spec_code as spec', 'slength_code as slength')
->distinct()
->get();
$allRecords = $lots->merge($workLogs);
if ($allRecords->isEmpty()) {
$this->info(' - 5130 데이터 없음');
return;
}
// 고유 제품 조합 추출
$uniqueProducts = [];
foreach ($allRecords as $row) {
$key = trim($row->prod).'-'.trim($row->spec ?? '').'-'.trim($row->slength ?? '');
if (! isset($uniqueProducts[$key])) {
$uniqueProducts[$key] = [
'prod' => trim($row->prod),
'spec' => trim($row->spec ?? ''),
'slength' => trim($row->slength ?? ''),
];
}
}
$this->info(" - 5130 고유 제품 조합: ".count($uniqueProducts).'개');
$created = 0;
$skipped = 0;
foreach ($uniqueProducts as $data) {
$itemCode = "BD-{$data['prod']}{$data['spec']}-{$data['slength']}";
$prodName = $this->prodNames[$data['prod']] ?? $data['prod'];
$specName = $this->specNames[$data['spec']] ?? $data['spec'];
$slengthName = $this->slengthNames[$data['slength']] ?? $data['slength'];
$itemName = implode(' ', array_filter([$prodName, $specName, $slengthName]));
// 이미 존재하는지 확인
$existing = DB::connection($this->targetDb)
->table('items')
->where('tenant_id', $tenantId)
->where('code', $itemCode)
->whereNull('deleted_at')
->first();
if ($existing) {
$skipped++;
continue;
}
if (! $dryRun) {
DB::connection($this->targetDb)->table('items')->insert([
'tenant_id' => $tenantId,
'code' => $itemCode,
'name' => $itemName,
'item_type' => 'PT',
'item_category' => 'BENDING',
'unit' => 'EA',
'options' => json_encode([
'source' => '5130_migration',
'lot_managed' => true,
'consumption_method' => 'auto',
'production_source' => 'self_produced',
'input_tracking' => true,
'legacy_prod' => $data['prod'],
'legacy_spec' => $data['spec'],
'legacy_slength' => $data['slength'],
]),
'is_active' => true,
'created_at' => now(),
'updated_at' => now(),
]);
}
$created++;
}
$this->stats['items_created_5130'] = $created;
$this->info(" - 신규 생성: {$created}건, 기존 존재 (skip): {$skipped}");
}
/**
* 통계 출력
*/
private function showStats(): void
{
$this->newLine();
$this->info('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
$this->info('📊 실행 통계');
$this->info('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
$this->info(" 5130 아이템 생성: {$this->stats['items_created_5130']}");
$this->info(" BD-* 품목 수 (전체): {$this->stats['items_found']}");
$this->info(" 카테고리 업데이트: {$this->stats['items_category_updated']}");
$this->info(" Stock 레코드 생성: {$this->stats['stocks_created']}");
$this->info(" 기존 재고 충분 (skip): {$this->stats['stocks_skipped']}");
$this->info(" StockLot 생성: {$this->stats['lots_created']}");
$this->info(" 입고 트랜잭션: {$this->stats['transactions_created']}");
$this->info('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
}
}

View File

@@ -0,0 +1,137 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Attributes\AsCommand;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
#[AsCommand(name: 'bending:validate-items', description: 'BD-* 절곡 세부품목 마스터 데이터 검증 (prefix × lengthCode 전 조합)')]
class ValidateBendingItems extends Command
{
protected $signature = 'bending:validate-items
{--tenant_id=287 : Target tenant ID (default: 287 경동기업)}';
/**
* prefix별 유효 길이코드 정의
*
* 가이드레일: 30, 35, 40, 43 (벽면/측면 공통)
* 하단마감재: 30, 40
* 셔터박스: 12, 24, 30, 35, 40, 41
* 연기차단재: 53, 54, 83, 84 (W50/W80 전용 코드)
* XX: 12, 24, 30, 35, 40, 41, 43 (하부BASE + 셔터 상부/마구리)
* YY: 30, 35, 40, 43 (별도 SUS 마감)
* HH: 30, 40 (보강평철)
*/
private function getPrefixLengthCodes(): array
{
$guideRailCodes = ['30', '35', '40', '43'];
$guideRailCodesWithExtra = ['24', '30', '35', '40', '43']; // RT/ST는 적은 종류
$bottomBarCodes = ['30', '40'];
$shutterBoxCodes = ['12', '24', '30', '35', '40', '41'];
return [
// 가이드레일 벽면형
'RS' => $guideRailCodes, // 벽면 SUS 마감재
'RM' => ['24', '30', '35', '40', '42', '43'], // 벽면 본체 (EGI)
'RC' => ['24', '30', '35', '40', '42', '43'], // 벽면 C형
'RD' => ['24', '30', '35', '40', '42', '43'], // 벽면 D형
'RT' => ['30', '43'], // 벽면 본체 (철재)
// 가이드레일 측면형
'SS' => ['30', '35', '40'], // 측면 SUS 마감재
'SM' => ['24', '30', '35', '40', '43'], // 측면 본체 (EGI)
'SC' => ['24', '30', '35', '40', '43'], // 측면 C형
'SD' => ['24', '30', '35', '40', '43'], // 측면 D형
'ST' => ['43'], // 측면 본체 (철재)
'SU' => ['30', '35', '40', '43'], // 측면 SUS (SUS2)
// 하단마감재
'BE' => $bottomBarCodes, // EGI 마감
'BS' => ['24', '30', '35', '40', '43'], // SUS 마감
'TS' => ['43'], // 철재 SUS
'LA' => $bottomBarCodes, // L-Bar
// 셔터박스
'CF' => $shutterBoxCodes, // 전면부
'CL' => $shutterBoxCodes, // 린텔부
'CP' => $shutterBoxCodes, // 점검구
'CB' => $shutterBoxCodes, // 후면코너부
// 연기차단재
'GI' => ['53', '54', '83', '84', '30', '35', '40'], // W50/W80 + 일반
// 공용/기타
'XX' => ['12', '24', '30', '35', '40', '41', '43'], // 하부BASE/셔터 상부/마구리
'YY' => ['30', '35', '40', '43'], // 별도 SUS 마감
'HH' => ['30', '40'], // 보강평철
];
}
public function handle(): int
{
$tenantId = (int) $this->option('tenant_id');
$this->info("=== BD-* 절곡 세부품목 마스터 검증 (tenant: {$tenantId}) ===");
$this->newLine();
// DB에서 전체 BD-* 품목 조회
$existingItems = DB::table('items')
->where('tenant_id', $tenantId)
->where('code', 'like', 'BD-%')
->whereNull('deleted_at')
->pluck('code')
->toArray();
$existingSet = array_flip($existingItems);
$this->info('현재 등록된 BD-* 품목: '.count($existingItems).'개');
$this->newLine();
$prefixMap = $this->getPrefixLengthCodes();
$totalExpected = 0;
$missing = [];
$found = 0;
foreach ($prefixMap as $prefix => $codes) {
$prefixMissing = [];
foreach ($codes as $code) {
$itemCode = "BD-{$prefix}-{$code}";
$totalExpected++;
if (isset($existingSet[$itemCode])) {
$found++;
} else {
$prefixMissing[] = $itemCode;
$missing[] = $itemCode;
}
}
$status = empty($prefixMissing) ? '✅' : '❌';
$countStr = count($codes) - count($prefixMissing).'/'.count($codes);
$this->line(" {$status} BD-{$prefix}: {$countStr}");
if (! empty($prefixMissing)) {
foreach ($prefixMissing as $m) {
$this->line(" ⚠️ 누락: {$m}");
}
}
}
$this->newLine();
$this->info('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
$this->info("검증 결과: {$found}/{$totalExpected} 등록 완료");
if (empty($missing)) {
$this->info('✅ All items registered — 누락 0건');
return self::SUCCESS;
}
$this->warn('❌ 누락 항목: '.count($missing).'건');
$this->newLine();
$this->table(['누락 품목코드'], array_map(fn ($m) => [$m], $missing));
return self::FAILURE;
}
}

View File

@@ -0,0 +1,101 @@
<?php
namespace App\DTOs\Production;
use InvalidArgumentException;
/**
* dynamic_bom JSON 항목 DTO
*
* work_order_items.options.dynamic_bom 배열의 각 엔트리를 표현
*/
class DynamicBomEntry
{
public function __construct(
public readonly int $child_item_id,
public readonly string $child_item_code,
public readonly string $lot_prefix,
public readonly string $part_type,
public readonly string $category,
public readonly string $material_type,
public readonly int $length_mm,
public readonly int|float $qty,
) {}
/**
* 배열에서 DTO 생성
*/
public static function fromArray(array $data): self
{
self::validate($data);
return new self(
child_item_id: (int) $data['child_item_id'],
child_item_code: (string) $data['child_item_code'],
lot_prefix: (string) $data['lot_prefix'],
part_type: (string) $data['part_type'],
category: (string) $data['category'],
material_type: (string) $data['material_type'],
length_mm: (int) $data['length_mm'],
qty: $data['qty'],
);
}
/**
* DTO → 배열 변환 (JSON 저장용)
*/
public function toArray(): array
{
return [
'child_item_id' => $this->child_item_id,
'child_item_code' => $this->child_item_code,
'lot_prefix' => $this->lot_prefix,
'part_type' => $this->part_type,
'category' => $this->category,
'material_type' => $this->material_type,
'length_mm' => $this->length_mm,
'qty' => $this->qty,
];
}
/**
* 필수 필드 검증
*
* @throws InvalidArgumentException
*/
public static function validate(array $data): bool
{
$required = ['child_item_id', 'child_item_code', 'lot_prefix', 'part_type', 'category', 'material_type', 'length_mm', 'qty'];
foreach ($required as $field) {
if (! array_key_exists($field, $data) || $data[$field] === null) {
throw new InvalidArgumentException("DynamicBomEntry: '{$field}' is required");
}
}
if ((int) $data['child_item_id'] <= 0) {
throw new InvalidArgumentException('DynamicBomEntry: child_item_id must be positive');
}
$validCategories = ['guideRail', 'bottomBar', 'shutterBox', 'smokeBarrier'];
if (! in_array($data['category'], $validCategories, true)) {
throw new InvalidArgumentException('DynamicBomEntry: category must be one of: '.implode(', ', $validCategories));
}
if ($data['qty'] <= 0) {
throw new InvalidArgumentException('DynamicBomEntry: qty must be positive');
}
return true;
}
/**
* DynamicBomEntry 배열 → JSON 저장용 배열 변환
*
* @param DynamicBomEntry[] $entries
*/
public static function toArrayList(array $entries): array
{
return array_map(fn (self $e) => $e->toArray(), $entries);
}
}

View File

@@ -6,6 +6,7 @@
use App\Http\Controllers\Controller;
use App\Http\Requests\Order\CreateFromQuoteRequest;
use App\Http\Requests\Order\CreateProductionOrderRequest;
use App\Http\Requests\Order\OrderBulkDeleteRequest;
use App\Http\Requests\Order\StoreOrderRequest;
use App\Http\Requests\Order\UpdateOrderRequest;
use App\Http\Requests\Order\UpdateOrderStatusRequest;
@@ -66,6 +67,21 @@ public function update(UpdateOrderRequest $request, int $id)
}, __('message.order.updated'));
}
/**
* 일괄 삭제
*/
public function bulkDestroy(OrderBulkDeleteRequest $request)
{
return ApiResponse::handle(function () use ($request) {
$validated = $request->validated();
return $this->service->bulkDestroy(
$validated['ids'],
$validated['force'] ?? false
);
}, __('message.order.bulk_deleted'));
}
/**
* 삭제
*/
@@ -119,12 +135,25 @@ public function revertOrderConfirmation(int $id)
}
/**
* 생산지시 되돌리기 (작업지시 및 관련 데이터 삭제)
* 절곡 BOM 품목 재고 확인
*/
public function revertProductionOrder(int $id)
public function checkBendingStock(int $id)
{
return ApiResponse::handle(function () use ($id) {
return $this->service->revertProductionOrder($id);
return $this->service->checkBendingStockForOrder($id);
}, __('message.fetched'));
}
/**
* 생산지시 되돌리기 (작업지시 및 관련 데이터 삭제)
*/
public function revertProductionOrder(Request $request, int $id)
{
$force = $request->boolean('force', false);
$reason = $request->input('reason');
return ApiResponse::handle(function () use ($id, $force, $reason) {
return $this->service->revertProductionOrder($id, $force, $reason);
}, __('message.order.production_order_reverted'));
}
}

View File

@@ -22,6 +22,7 @@ public function index(Request $request): JsonResponse
$params = $request->only([
'search',
'item_type',
'item_category',
'status',
'location',
'sort_by',

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Http\Requests\Order;
use Illuminate\Foundation\Http\FormRequest;
class OrderBulkDeleteRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'ids' => 'required|array|min:1',
'ids.*' => 'required|integer',
'force' => 'sometimes|boolean',
];
}
}

View File

@@ -24,8 +24,13 @@ public function rules(): array
'order_qty' => ['nullable', 'numeric', 'min:0'],
'order_unit' => ['nullable', 'string', 'max:20'],
'due_date' => ['nullable', 'date'],
'receiving_qty' => ['nullable', 'numeric', 'min:0'],
'receiving_date' => ['nullable', 'date'],
'lot_no' => ['nullable', 'string', 'max:50'],
'status' => ['nullable', 'string', 'in:order_completed,shipping,inspection_pending,receiving_pending'],
'remark' => ['nullable', 'string', 'max:1000'],
'manufacturer' => ['nullable', 'string', 'max:100'],
'material_no' => ['nullable', 'string', 'max:50'],
];
}

View File

@@ -23,7 +23,7 @@ public function rules(): array
'order_qty' => ['sometimes', 'numeric', 'min:0'],
'order_unit' => ['nullable', 'string', 'max:20'],
'due_date' => ['nullable', 'date'],
'status' => ['sometimes', 'string', 'in:order_completed,shipping,inspection_pending,receiving_pending,completed'],
'status' => ['sometimes', 'string', 'in:order_completed,shipping,inspection_pending,receiving_pending,completed,inspection_completed'],
'remark' => ['nullable', 'string', 'max:1000'],
'receiving_qty' => ['nullable', 'numeric', 'min:0'],
'receiving_date' => ['nullable', 'date'],
@@ -31,6 +31,8 @@ public function rules(): array
'inspection_status' => ['nullable', 'string', 'max:10'],
'inspection_date' => ['nullable', 'date'],
'inspection_result' => ['nullable', 'string', 'max:20'],
'manufacturer' => ['nullable', 'string', 'max:100'],
'material_no' => ['nullable', 'string', 'max:50'],
];
}

View File

@@ -101,6 +101,8 @@ class WorkOrder extends Model
public const STATUS_SHIPPED = 'shipped'; // 출하
public const STATUS_CANCELLED = 'cancelled'; // 취소
public const STATUSES = [
self::STATUS_UNASSIGNED,
self::STATUS_PENDING,
@@ -108,6 +110,7 @@ class WorkOrder extends Model
self::STATUS_IN_PROGRESS,
self::STATUS_COMPLETED,
self::STATUS_SHIPPED,
self::STATUS_CANCELLED,
];
/**

View File

@@ -254,7 +254,7 @@ public function scopeFinalized($query)
public function scopeConverted($query)
{
return $query->where('status', self::STATUS_CONVERTED);
return $query->whereNotNull('order_id');
}
/**
@@ -339,12 +339,29 @@ public function isEditable(): bool
return true;
}
/**
* 상태 접근자: order_id가 존재하면 자동으로 'converted' 반환
* DB에 status='converted'를 저장하지 않고, 수주 존재 여부로 판별
*/
public function getStatusAttribute($value): string
{
if ($this->order_id) {
return self::STATUS_CONVERTED;
}
return $value;
}
/**
* 삭제 가능 여부 확인
*/
public function isDeletable(): bool
{
return ! in_array($this->status, [self::STATUS_FINALIZED, self::STATUS_CONVERTED]);
if ($this->order_id) {
return false;
}
return $this->getRawOriginal('status') !== self::STATUS_FINALIZED;
}
/**

View File

@@ -28,6 +28,7 @@ class StockLot extends Model
'location',
'status',
'receiving_id',
'work_order_id',
'created_by',
'updated_by',
'deleted_by',
@@ -41,6 +42,7 @@ class StockLot extends Model
'available_qty' => 'decimal:3',
'stock_id' => 'integer',
'receiving_id' => 'integer',
'work_order_id' => 'integer',
];
/**
@@ -68,6 +70,14 @@ public function receiving(): BelongsTo
return $this->belongsTo(Receiving::class);
}
/**
* 작업지시 관계 (생산입고)
*/
public function workOrder(): BelongsTo
{
return $this->belongsTo(\App\Models\Production\WorkOrder::class);
}
/**
* 생성자 관계
*/

View File

@@ -48,12 +48,15 @@ class StockTransaction extends Model
public const REASON_ORDER_CANCEL = 'order_cancel';
public const REASON_PRODUCTION_OUTPUT = 'production_output';
public const REASONS = [
self::REASON_RECEIVING => '입고',
self::REASON_WORK_ORDER_INPUT => '생산투입',
self::REASON_SHIPMENT => '출하',
self::REASON_ORDER_CONFIRM => '수주확정',
self::REASON_ORDER_CANCEL => '수주취소',
self::REASON_PRODUCTION_OUTPUT => '생산입고',
];
protected $fillable = [
@@ -111,4 +114,4 @@ public function getReasonLabelAttribute(): string
{
return self::REASONS[$this->reason] ?? ($this->reason ?? '-');
}
}
}

View File

@@ -46,9 +46,8 @@ class AppServiceProvider extends ServiceProvider
public function register(): void
{
// 개발환경 + API 라우트에서만 쿼리 로그 수집
if (app()->environment('local')) {
// 콘솔/큐 등 non-HTTP 컨텍스트 보호
if (function_exists('request') && request() && request()->is('api/*')) {
if (app()->environment('local') && !app()->runningInConsole()) {
if (request()->is('api/*')) {
DB::enableQueryLog();
}
}

View File

@@ -25,6 +25,19 @@ public function __construct(
private NumberingService $numberingService
) {}
/**
* 상세 조회용 공통 relations (show와 동일한 구조 보장)
*/
private function loadDetailRelations(Order $order): Order
{
return $order->load([
'client:id,name,contact_person,phone,email,manager_name',
'items' => fn ($q) => $q->orderBy('sort_order'),
'rootNodes' => fn ($q) => $q->withRecursiveChildren(),
'quote:id,quote_number,site_name,calculation_inputs',
]);
}
/**
* 목록 조회 (검색/필터링/페이징)
*/
@@ -131,20 +144,13 @@ public function show(int $id)
{
$tenantId = $this->tenantId();
$order = Order::where('tenant_id', $tenantId)
->with([
'client:id,name,contact_person,phone,email,manager_name',
'items' => fn ($q) => $q->orderBy('sort_order'),
'rootNodes' => fn ($q) => $q->withRecursiveChildren(),
'quote:id,quote_number,site_name,calculation_inputs',
])
->find($id);
$order = Order::where('tenant_id', $tenantId)->find($id);
if (! $order) {
throw new NotFoundHttpException(__('error.not_found'));
}
return $order;
return $this->loadDetailRelations($order);
}
/**
@@ -222,6 +228,19 @@ public function store(array $data)
}
// 품목 저장
// sort_order 기반 분배 준비
$locationCount = count($productItems);
$itemsPerLocation = ($locationCount > 1)
? intdiv(count($items), $locationCount)
: 0;
// floor/code 조합이 개소별로 고유한지 확인 (모두 동일하면 매칭 무의미)
$uniqueLocations = collect($productItems)
->map(fn ($p) => ($p['floor'] ?? '').'-'.($p['code'] ?? ''))
->unique()
->count();
$canMatchByFloorCode = $uniqueLocations > 1;
foreach ($items as $index => $item) {
$item['tenant_id'] = $tenantId;
$item['serial_no'] = $index + 1; // 1부터 시작하는 순번
@@ -239,18 +258,32 @@ public function store(array $data)
}
}
// floor_code/symbol_code로 노드 매칭
// 노드 매칭 (개소 분배)
if (! empty($nodeMap) && ! empty($productItems)) {
$floorCode = $item['floor_code'] ?? null;
$symbolCode = $item['symbol_code'] ?? null;
if ($floorCode && $symbolCode) {
foreach ($productItems as $pidx => $pItem) {
if (($pItem['floor'] ?? '') === $floorCode && ($pItem['code'] ?? '') === $symbolCode) {
$item['order_node_id'] = $nodeMap[$pidx]->id ?? null;
break;
$locIdx = 0;
$matched = false;
// 1순위: floor_code/symbol_code로 매칭 (개소별 고유값이 있는 경우만)
if ($canMatchByFloorCode) {
$floorCode = $item['floor_code'] ?? null;
$symbolCode = $item['symbol_code'] ?? null;
if ($floorCode && $symbolCode) {
foreach ($productItems as $pidx => $pItem) {
if (($pItem['floor'] ?? '') === $floorCode && ($pItem['code'] ?? '') === $symbolCode) {
$locIdx = $pidx;
$matched = true;
break;
}
}
}
}
// 2순위: sort_order 기반 균등 분배
if (! $matched && $itemsPerLocation > 0) {
$locIdx = min(intdiv($index, $itemsPerLocation), $locationCount - 1);
}
$item['order_node_id'] = $nodeMap[$locIdx]->id ?? null;
}
$order->items()->create($item);
@@ -260,7 +293,7 @@ public function store(array $data)
$order->refresh();
$order->recalculateTotals()->save();
return $order->load(['client:id,name', 'items']);
return $this->loadDetailRelations($order);
});
}
@@ -326,7 +359,7 @@ public function update(int $id, array $data)
$order->recalculateTotals()->save();
}
return $order->load(['client:id,name', 'items']);
return $this->loadDetailRelations($order);
});
}
@@ -402,6 +435,167 @@ public function destroy(int $id)
});
}
/**
* 일괄 삭제
*/
public function bulkDestroy(array $ids, bool $force = false): array
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
// force=true는 운영 환경에서 차단
if ($force && app()->environment('production')) {
throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException(__('error.forbidden'));
}
$orders = Order::where('tenant_id', $tenantId)
->whereIn('id', $ids)
->get();
$deletedCount = 0;
$skippedIds = [];
return DB::transaction(function () use ($orders, $force, $userId, $tenantId, &$deletedCount, &$skippedIds) {
foreach ($orders as $order) {
if ($force) {
// force=true (개발환경 완전삭제): 모든 상태 허용, 연관 데이터 모두 삭제
$this->forceDeleteWorkOrders($order, $tenantId);
} else {
// 일반 삭제: 상태/작업지시/출하 검증
if (! in_array($order->status_code, [
Order::STATUS_DRAFT,
Order::STATUS_CONFIRMED,
Order::STATUS_CANCELLED,
])) {
$skippedIds[] = $order->id;
continue;
}
if ($order->workOrders()->exists()) {
$skippedIds[] = $order->id;
continue;
}
if ($order->shipments()->exists()) {
$skippedIds[] = $order->id;
continue;
}
}
// 견적 연결 해제
if ($order->quote_id) {
Quote::withoutGlobalScopes()
->where('id', $order->quote_id)
->where('order_id', $order->id)
->update([
'order_id' => null,
'status' => Quote::STATUS_FINALIZED,
]);
}
if ($force) {
// hard delete: 컴포넌트 → 품목 → 노드 → 마스터
foreach ($order->items as $item) {
$item->components()->forceDelete();
}
$order->items()->forceDelete();
$order->nodes()->forceDelete();
$order->forceDelete();
} else {
// soft delete: 기존 destroy() 로직과 동일
foreach ($order->items as $item) {
$item->components()->update(['deleted_by' => $userId]);
$item->components()->delete();
}
$order->items()->update(['deleted_by' => $userId]);
$order->items()->delete();
$order->nodes()->update(['deleted_by' => $userId]);
$order->nodes()->delete();
$order->deleted_by = $userId;
$order->save();
$order->delete();
}
$deletedCount++;
}
return [
'deleted_count' => $deletedCount,
'skipped_count' => count($skippedIds),
'skipped_ids' => $skippedIds,
];
});
}
/**
* 작업지시 및 연관 데이터 강제 삭제 (개발환경 완전삭제용)
*/
private function forceDeleteWorkOrders(Order $order, int $tenantId): void
{
$workOrderIds = WorkOrder::where('tenant_id', $tenantId)
->where('sales_order_id', $order->id)
->pluck('id')
->toArray();
if (empty($workOrderIds)) {
return;
}
// 1. 자재 투입 재고 복구 + 삭제
$materialInputs = WorkOrderMaterialInput::whereIn('work_order_id', $workOrderIds)->get();
if ($materialInputs->isNotEmpty()) {
$stockService = app(StockService::class);
foreach ($materialInputs as $input) {
try {
$stockService->increaseToLot(
stockLotId: $input->stock_lot_id,
qty: (float) $input->qty,
reason: 'work_order_input_cancel',
referenceId: $input->work_order_id
);
} catch (\Exception $e) {
Log::warning('완전삭제: 재고 복원 실패', [
'input_id' => $input->id,
'stock_lot_id' => $input->stock_lot_id,
'error' => $e->getMessage(),
]);
}
}
WorkOrderMaterialInput::whereIn('work_order_id', $workOrderIds)->delete();
}
// 2. 문서 삭제
$documentIds = Document::where('linkable_type', 'work_order')
->whereIn('linkable_id', $workOrderIds)
->pluck('id')
->toArray();
if (! empty($documentIds)) {
DocumentData::whereIn('document_id', $documentIds)->delete();
DocumentApproval::whereIn('document_id', $documentIds)->delete();
Document::whereIn('id', $documentIds)->forceDelete();
}
// 3. 출하 참조 해제
DB::table('shipments')
->whereIn('work_order_id', $workOrderIds)
->update(['work_order_id' => null]);
// 4. 부속 데이터 삭제
DB::table('work_order_step_progress')->whereIn('work_order_id', $workOrderIds)->delete();
DB::table('work_order_assignees')->whereIn('work_order_id', $workOrderIds)->delete();
DB::table('work_order_bending_details')->whereIn('work_order_id', $workOrderIds)->delete();
DB::table('work_order_issues')->whereIn('work_order_id', $workOrderIds)->delete();
DB::table('work_results')->whereIn('work_order_id', $workOrderIds)->delete();
// 5. 작업지시 품목 → 작업지시 삭제
DB::table('work_order_items')->whereIn('work_order_id', $workOrderIds)->delete();
WorkOrder::whereIn('id', $workOrderIds)->forceDelete();
}
/**
* 상태 변경
*/
@@ -457,7 +651,7 @@ public function updateStatus(int $id, string $status)
$order->updated_by = $userId;
$order->save();
$result = $order->load(['client:id,name', 'items']);
$result = $this->loadDetailRelations($order);
// 매출이 생성된 경우 응답에 포함
if ($createdSale) {
@@ -755,14 +949,13 @@ public function createFromQuote(int $quoteId, array $data = [])
$order->refresh();
$order->recalculateTotals()->save();
// 견적 상태를 '수주전환완료'로 변경
// 견적에 수주 연결 (status는 accessor가 자동으로 'converted' 반환)
$quote->update([
'status' => Quote::STATUS_CONVERTED,
'order_id' => $order->id,
'updated_by' => $userId,
]);
return $order->load(['client:id,name', 'items', 'quote:id,quote_number']);
return $this->loadDetailRelations($order);
});
}
@@ -974,7 +1167,7 @@ public function syncFromQuote(Quote $quote, int $revision): ?Order
'created_by' => $userId,
]);
return $order->load(['client:id,name', 'items', 'quote:id,quote_number']);
return $this->loadDetailRelations($order);
});
}
@@ -1143,9 +1336,9 @@ public function createProductionOrder(int $orderId, array $data)
->values()
->all();
$bendingInfo = app(BendingInfoBuilder::class)->build($order, $processId, $nodeIds ?: null);
if ($bendingInfo) {
$workOrderOptions = ['bending_info' => $bendingInfo];
$buildResult = app(BendingInfoBuilder::class)->build($order, $processId, $nodeIds ?: null);
if ($buildResult) {
$workOrderOptions = ['bending_info' => $buildResult['bending_info']];
}
}
@@ -1212,17 +1405,33 @@ public function createProductionOrder(int $orderId, array $data)
$slatInfo['joint_bar'] = (2 + (int) floor(((float) $woWidth - 500) / 1000)) * $qty;
}
$woHeight = $nodeOptions['height'] ?? $nodeOptions['open_height'] ?? null;
$woItemOptions = array_filter([
'floor' => $orderItem->floor_code,
'code' => $orderItem->symbol_code,
'width' => $woWidth,
'height' => $nodeOptions['height'] ?? $nodeOptions['open_height'] ?? null,
'height' => $woHeight,
'cutting_info' => $nodeOptions['cutting_info'] ?? null,
'slat_info' => $slatInfo,
'bending_info' => $nodeOptions['bending_info'] ?? null,
'wip_info' => $nodeOptions['wip_info'] ?? null,
], fn ($v) => $v !== null);
// 절곡 공정: 개소별 dynamic_bom 생성
if (! empty($buildResult['context']) && $woWidth && $woHeight) {
$dynamicBom = app(BendingInfoBuilder::class)->buildDynamicBomForItem(
$buildResult['context'],
(int) $woWidth,
(int) $woHeight,
(int) ($orderItem->quantity ?? 1),
$tenantId,
);
if (! empty($dynamicBom)) {
$woItemOptions['dynamic_bom'] = $dynamicBom;
}
}
DB::table('work_order_items')->insert([
'tenant_id' => $tenantId,
'work_order_id' => $workOrder->id,
@@ -1251,7 +1460,7 @@ public function createProductionOrder(int $orderId, array $data)
return [
'work_orders' => $workOrders,
'work_order' => $workOrders[0] ?? null, // 하위 호환성
'order' => $order->load(['client:id,name', 'items']),
'order' => $this->loadDetailRelations($order),
];
});
}
@@ -1383,7 +1592,7 @@ public function revertOrderConfirmation(int $orderId): array
$order->save();
return [
'order' => $order->load(['client:id,name', 'items']),
'order' => $this->loadDetailRelations($order),
'previous_status' => $previousStatus,
'deleted_sale_id' => $deletedSaleId,
];
@@ -1391,13 +1600,26 @@ public function revertOrderConfirmation(int $orderId): array
}
/**
* 생산지시 되돌리기 (작업지시 및 관련 데이터 삭제)
* 생산지시 되돌리기
*
* force=true: 개발 모드 - 모든 데이터 hard delete + 재고 복구 (운영환경 차단)
* force=false: 운영 모드 - 작업지시 취소 상태 변경 + 재고 역분개 (데이터 보존)
*/
public function revertProductionOrder(int $orderId): array
public function revertProductionOrder(int $orderId, bool $force = false, ?string $reason = null): array
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
// force=true는 운영 환경에서 차단
if ($force && app()->environment('production')) {
throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException(__('error.forbidden'));
}
// 운영 모드에서는 reason 필수
if (! $force && empty($reason)) {
throw new BadRequestHttpException(__('error.order.cancel_reason_required'));
}
// 수주 조회
$order = Order::where('tenant_id', $tenantId)
->find($orderId);
@@ -1411,8 +1633,19 @@ public function revertProductionOrder(int $orderId): array
throw new BadRequestHttpException(__('error.order.cannot_revert_completed'));
}
if ($force) {
return $this->revertProductionOrderForce($order, $tenantId, $userId);
}
return $this->revertProductionOrderCancel($order, $tenantId, $userId, $reason);
}
/**
* 생산지시 되돌리기 - 개발 모드 (hard delete)
*/
private function revertProductionOrderForce(Order $order, int $tenantId, int $userId): array
{
return DB::transaction(function () use ($order, $tenantId, $userId) {
// 관련 작업지시 ID 조회
$workOrderIds = WorkOrder::where('tenant_id', $tenantId)
->where('sales_order_id', $order->id)
->pluck('id')
@@ -1509,10 +1742,161 @@ public function revertProductionOrder(int $orderId): array
$order->save();
return [
'order' => $order->load(['client:id,name', 'items']),
'order' => $this->loadDetailRelations($order),
'deleted_counts' => $deletedCounts,
'previous_status' => $previousStatus,
];
});
}
/**
* 생산지시 되돌리기 - 운영 모드 (취소 상태 변경, 데이터 보존)
*/
private function revertProductionOrderCancel(Order $order, int $tenantId, int $userId, string $reason): array
{
return DB::transaction(function () use ($order, $tenantId, $userId, $reason) {
$workOrders = WorkOrder::where('tenant_id', $tenantId)
->where('sales_order_id', $order->id)
->get();
$cancelledCount = 0;
$skippedIds = [];
$stockService = app(StockService::class);
foreach ($workOrders as $workOrder) {
// completed/shipped 상태는 취소 거부
if (in_array($workOrder->status, [WorkOrder::STATUS_COMPLETED, WorkOrder::STATUS_SHIPPED])) {
$skippedIds[] = $workOrder->id;
continue;
}
// 작업지시 상태를 cancelled로 변경
$workOrder->status = WorkOrder::STATUS_CANCELLED;
$workOrder->updated_by = $userId;
// options에 취소 정보 기록
$options = $workOrder->options ?? [];
$options['cancelled_at'] = now()->toIso8601String();
$options['cancelled_by'] = $userId;
$options['cancel_reason'] = $reason;
$workOrder->options = $options;
$workOrder->save();
// 자재 투입분 재고 역분개
$materialInputs = WorkOrderMaterialInput::where('work_order_id', $workOrder->id)->get();
foreach ($materialInputs as $input) {
try {
$stockService->increaseToLot(
stockLotId: $input->stock_lot_id,
qty: (float) $input->qty,
reason: 'work_order_cancel',
referenceId: $workOrder->id
);
} catch (\Exception $e) {
Log::warning('생산지시 취소: 재고 복원 실패', [
'input_id' => $input->id,
'stock_lot_id' => $input->stock_lot_id,
'error' => $e->getMessage(),
]);
}
}
$cancelledCount++;
}
// 수주 상태를 CONFIRMED로 복원
$previousStatus = $order->status_code;
$order->status_code = Order::STATUS_CONFIRMED;
$order->updated_by = $userId;
$order->save();
return [
'order' => $this->loadDetailRelations($order),
'cancelled_count' => $cancelledCount,
'skipped_count' => count($skippedIds),
'skipped_ids' => $skippedIds,
'previous_status' => $previousStatus,
];
});
}
/**
* 수주의 절곡 BOM 품목별 재고 현황 조회
*
* order_items에서 item_category='BENDING'인 품목을 추출하고
* 각 품목의 재고 가용량/부족량을 반환합니다.
*/
public function checkBendingStockForOrder(int $orderId): array
{
$tenantId = $this->tenantId();
$order = Order::where('tenant_id', $tenantId)
->with(['items'])
->find($orderId);
if (! $order) {
throw new NotFoundHttpException(__('error.not_found'));
}
// order_items에서 item_id가 있는 품목의 ID 수집 + 수량 합산
$itemQtyMap = []; // item_id => total_qty
foreach ($order->items as $orderItem) {
$itemId = $orderItem->item_id;
if (! $itemId) {
continue;
}
$qty = (float) ($orderItem->quantity ?? 0);
if ($qty <= 0) {
continue;
}
$itemQtyMap[$itemId] = ($itemQtyMap[$itemId] ?? 0) + $qty;
}
if (empty($itemQtyMap)) {
return [];
}
// items 테이블에서 item_category = 'BENDING'인 것만 필터
$bendingItems = DB::table('items')
->where('tenant_id', $tenantId)
->whereIn('id', array_keys($itemQtyMap))
->where('item_category', 'BENDING')
->whereNull('deleted_at')
->select('id', 'code', 'name', 'unit')
->get();
if ($bendingItems->isEmpty()) {
return [];
}
$stockService = app(StockService::class);
$result = [];
foreach ($bendingItems as $item) {
$neededQty = $itemQtyMap[$item->id];
$stockInfo = $stockService->getAvailableStock($item->id);
$availableQty = $stockInfo ? (float) $stockInfo['available_qty'] : 0;
$reservedQty = $stockInfo ? (float) $stockInfo['reserved_qty'] : 0;
$stockQty = $stockInfo ? (float) $stockInfo['stock_qty'] : 0;
$shortfallQty = max(0, $neededQty - $availableQty);
$result[] = [
'item_id' => $item->id,
'item_code' => $item->code,
'item_name' => $item->name,
'unit' => $item->unit,
'needed_qty' => $neededQty,
'stock_qty' => $stockQty,
'reserved_qty' => $reservedQty,
'available_qty' => $availableQty,
'shortfall_qty' => $shortfallQty,
'status' => $shortfallQty > 0 ? 'insufficient' : 'sufficient',
];
}
return $result;
}
}

View File

@@ -2,15 +2,17 @@
namespace App\Services\Production;
use App\DTOs\Production\DynamicBomEntry;
use App\Models\Orders\Order;
use App\Models\Process;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
/**
* 수주 → 생산지시 시 절곡 공정용 bending_info JSON 자동 생성
*
* 입력: Order (rootNodes eager loaded) + processId
* 출력: BendingInfoExtended 구조의 array (work_orders.options.bending_info에 저장)
* 출력: ['bending_info' => array, 'context' => array] — bending_info + dynamic_bom 생성 컨텍스트
*
* @see react/src/components/production/WorkOrders/documents/bending/types.ts
*/
@@ -18,6 +20,7 @@ class BendingInfoBuilder
{
// 표준 원자재 길이 버킷 (5130 레거시 write_form.php 기준)
private const GUIDE_RAIL_LENGTHS = [2438, 3000, 3500, 4000, 4300];
private const SHUTTER_BOX_LENGTHS = [1219, 2438, 3000, 3500, 4000, 4150];
/**
@@ -65,7 +68,307 @@ public function build(Order $order, int $processId, ?array $nodeIds = null): ?ar
$aggregated = $this->aggregateNodes($nodes);
// 6. bending_info 조립
return $this->assembleBendingInfo($productInfo, $materials, $aggregated);
$bendingInfo = $this->assembleBendingInfo($productInfo, $materials, $aggregated);
// 7. 셔터박스 크기 추출 (dynamic_bom 컨텍스트용)
$caseBom = $aggregated['bomCategories']['shutterBox_case'] ?? null;
$motorBom = $aggregated['bomCategories']['motor'] ?? null;
$boxSize = null;
if ($caseBom) {
$motorCapacity = $this->extractMotorCapacity($motorBom);
$boxSize = $motorCapacity ? $this->getShutterBoxSize($motorCapacity) : null;
if (! $boxSize) {
$boxSize = str_replace('BD-케이스-', '', $caseBom['item_code'] ?? '');
}
}
return [
'bending_info' => $bendingInfo,
'context' => [
'productCode' => $productInfo['productCode'],
'guideType' => $productInfo['guideType'],
'finishMaterial' => $productInfo['finishMaterial'],
'materials' => $materials,
'boxSize' => $boxSize,
'hasSmokeRail' => isset($aggregated['bomCategories']['smokeBarrier_rail']),
'hasSmokeCase' => isset($aggregated['bomCategories']['smokeBarrier_case']),
],
];
}
/**
* 개소(work_order_item) 단위 dynamic_bom 생성
*
* @param array $context build() 반환값의 'context'
* @param int $width 개소의 오픈폭 (mm)
* @param int $height 개소의 오픈높이 (mm)
* @param int $qty 개소 수량
* @param int $tenantId 테넌트 ID (item 조회용)
* @return array DynamicBomEntry::toArray() 배열
*/
public function buildDynamicBomForItem(array $context, int $width, int $height, int $qty, int $tenantId = 287): array
{
$resolver = new PrefixResolver;
$entries = [];
$productCode = $context['productCode'];
$guideType = $context['guideType'];
$finishMaterial = $context['finishMaterial'];
$materials = $context['materials'];
$boxSize = $context['boxSize'];
$hasExtraFinish = ! empty($materials['guideRailExtraFinish']);
// ─── 1. 가이드레일 세부품목 ───
$dimGroups = [['height' => $height, 'width' => $width, 'qty' => $qty]];
$heightData = $this->heightLengthData($dimGroups);
// 가이드레일은 개구부 양쪽 2개이므로 수량 ×2
foreach ($heightData as &$entry) {
$entry['quantity'] *= 2;
}
unset($entry);
$guideTypes = match ($guideType) {
'혼합형' => ['wall', 'side'],
'측면형' => ['side'],
default => ['wall'],
};
$guidePartTypes = ['finish', 'body', 'c_type', 'd_type'];
if ($hasExtraFinish) {
$guidePartTypes[] = 'extra_finish';
}
$guidePartTypes[] = 'base';
foreach ($guideTypes as $gType) {
foreach ($heightData as $ld) {
foreach ($guidePartTypes as $partType) {
$prefix = $resolver->resolveGuideRailPrefix($partType, $gType, $productCode);
if (empty($prefix)) {
continue;
}
$itemCode = $resolver->buildItemCode($prefix, $ld['length']);
if (! $itemCode) {
Log::warning('BendingInfoBuilder: lengthCode 변환 실패', ['prefix' => $prefix, 'length' => $ld['length']]);
continue;
}
$itemId = $resolver->resolveItemId($itemCode, $tenantId);
if (! $itemId) {
Log::warning('BendingInfoBuilder: 미등록 품목', ['code' => $itemCode]);
continue;
}
$entries[] = new DynamicBomEntry(
child_item_id: $itemId,
child_item_code: $itemCode,
lot_prefix: $prefix,
part_type: PrefixResolver::partTypeName($partType),
category: 'guideRail',
material_type: $this->resolvePartMaterial($partType, $gType, $materials),
length_mm: $ld['length'],
qty: $ld['quantity'],
);
}
}
}
// ─── 2. 하단마감재 세부품목 ───
[$qty3000, $qty4000] = $this->bottomBarDistribution($width);
$bottomLengths = array_filter([3000 => $qty3000 * $qty, 4000 => $qty4000 * $qty]);
$bottomPartTypes = ['main', 'lbar', 'reinforce'];
$hasBottomExtra = ! empty($materials['bottomBarExtraFinish']) && $materials['bottomBarExtraFinish'] !== '없음';
if ($hasBottomExtra) {
$bottomPartTypes[] = 'extra';
}
foreach ($bottomLengths as $length => $lengthQty) {
if ($lengthQty <= 0) {
continue;
}
foreach ($bottomPartTypes as $partType) {
$prefix = $resolver->resolveBottomBarPrefix($partType, $productCode, $finishMaterial);
$itemCode = $resolver->buildItemCode($prefix, $length);
if (! $itemCode) {
continue;
}
$itemId = $resolver->resolveItemId($itemCode, $tenantId);
if (! $itemId) {
Log::warning('BendingInfoBuilder: 미등록 하단마감재', ['code' => $itemCode]);
continue;
}
$entries[] = new DynamicBomEntry(
child_item_id: $itemId,
child_item_code: $itemCode,
lot_prefix: $prefix,
part_type: PrefixResolver::partTypeName($partType),
category: 'bottomBar',
material_type: $materials['bottomBarFinish'],
length_mm: $length,
qty: $lengthQty,
);
}
}
// ─── 3. 셔터박스 세부품목 ───
if ($boxSize) {
$isStandard = $boxSize === '500*380';
$dist = $this->shutterBoxDistribution($width);
$shutterPartTypes = ['front', 'lintel', 'inspection', 'rear_corner', 'top_cover', 'fin_cover'];
foreach ($dist as $length => $count) {
$totalCount = $count * $qty;
if ($totalCount <= 0) {
continue;
}
foreach ($shutterPartTypes as $partType) {
$prefix = $resolver->resolveShutterBoxPrefix($partType, $isStandard);
$itemCode = $resolver->buildItemCode($prefix, $length);
if (! $itemCode) {
continue;
}
$itemId = $resolver->resolveItemId($itemCode, $tenantId);
if (! $itemId) {
Log::warning('BendingInfoBuilder: 미등록 셔터박스', ['code' => $itemCode]);
continue;
}
$entries[] = new DynamicBomEntry(
child_item_id: $itemId,
child_item_code: $itemCode,
lot_prefix: $prefix,
part_type: PrefixResolver::partTypeName($partType),
category: 'shutterBox',
material_type: 'EGI',
length_mm: $length,
qty: $totalCount,
);
}
}
// 상부덮개 수량: ceil(width / 1219) × qty (1219mm 단위)
$coverQty = (int) ceil($width / 1219) * $qty;
if ($coverQty > 0) {
$coverPrefix = $resolver->resolveShutterBoxPrefix('top_cover', $isStandard);
$coverCode = $resolver->buildItemCode($coverPrefix, 1219);
if ($coverCode) {
$coverId = $resolver->resolveItemId($coverCode, $tenantId);
if ($coverId) {
$entries[] = new DynamicBomEntry(
child_item_id: $coverId,
child_item_code: $coverCode,
lot_prefix: $coverPrefix,
part_type: PrefixResolver::partTypeName('top_cover'),
category: 'shutterBox',
material_type: 'EGI',
length_mm: 1219,
qty: $coverQty,
);
}
}
}
// 마구리 수량: qty × 2
$finQty = $qty * 2;
if ($finQty > 0) {
$finPrefix = $resolver->resolveShutterBoxPrefix('fin_cover', $isStandard);
// 마구리는 박스 높이 기준이므로 셔터박스 최소 단위 사용
$finCode = $resolver->buildItemCode($finPrefix, 1219);
if ($finCode) {
$finId = $resolver->resolveItemId($finCode, $tenantId);
if ($finId) {
$entries[] = new DynamicBomEntry(
child_item_id: $finId,
child_item_code: $finCode,
lot_prefix: $finPrefix,
part_type: PrefixResolver::partTypeName('fin_cover'),
category: 'shutterBox',
material_type: 'EGI',
length_mm: 1219,
qty: $finQty,
);
}
}
}
}
// ─── 4. 연기차단재 세부품목 ───
if ($context['hasSmokeRail'] || $context['hasSmokeCase']) {
$smokePrefix = $resolver->resolveSmokeBarrierPrefix();
// W50 (레일용): open_height + 250 → 표준 길이
if ($context['hasSmokeRail']) {
$col24 = $height + 250;
$w50Length = $this->bucketToStandardLength($col24, [2438, 3000, 3500, 4000, 4300]);
if ($w50Length && $col24 <= 4300) {
$w50Code = $resolver->buildItemCode($smokePrefix, $w50Length, 'w50');
if ($w50Code) {
$w50Id = $resolver->resolveItemId($w50Code, $tenantId);
if ($w50Id) {
$entries[] = new DynamicBomEntry(
child_item_id: $w50Id,
child_item_code: $w50Code,
lot_prefix: $smokePrefix,
part_type: '연기차단재(W50)',
category: 'smokeBarrier',
material_type: 'GI',
length_mm: $w50Length,
qty: 2 * $qty,
);
}
}
}
}
// W80 (케이스용): floor((width+240)*2/3000 + 1) × qty
if ($context['hasSmokeCase']) {
$col38 = $width + 240;
$w80PerNode = (int) floor(($col38 * 2 / 3000) + 1);
$w80Qty = $w80PerNode * $qty;
if ($w80Qty > 0) {
// W80은 3000mm 기본 (레거시 동일)
$w80Code = $resolver->buildItemCode($smokePrefix, 3000, 'w80');
if ($w80Code) {
$w80Id = $resolver->resolveItemId($w80Code, $tenantId);
if ($w80Id) {
$entries[] = new DynamicBomEntry(
child_item_id: $w80Id,
child_item_code: $w80Code,
lot_prefix: $smokePrefix,
part_type: '연기차단재(W80)',
category: 'smokeBarrier',
material_type: 'GI',
length_mm: 3000,
qty: $w80Qty,
);
}
}
}
}
}
return DynamicBomEntry::toArrayList($entries);
}
/**
* 파트타입 + 가이드타입 → 실제 재질 결정
*/
private function resolvePartMaterial(string $partType, string $guideType, array $materials): string
{
return match ($partType) {
'finish' => $materials['guideRailFinish'],
'extra_finish' => $materials['guideRailExtraFinish'],
'body', 'c_type', 'd_type' => $materials['bodyMaterial'],
'base' => 'EGI',
default => '',
};
}
/**

View File

@@ -0,0 +1,307 @@
<?php
namespace App\Services\Production;
use Illuminate\Support\Facades\DB;
/**
* 절곡 세부품목 LOT Prefix 결정 및 BD-XX-NN 코드 생성
*
* 제품코드 + 마감재질 + 가이드타입 → LOT prefix → BD-XX-NN 코드 → items.id
*/
class PrefixResolver
{
// ─────────────────────────────────────────────────
// 가이드레일 Prefix 맵
// ─────────────────────────────────────────────────
/** 벽면형(Wall) prefix: partType → prefix (finish는 productCode별 분기) */
private const WALL_PREFIXES = [
'finish' => ['KSS' => 'RS', 'KQTS' => 'RS', 'KSE' => 'RE', 'KWE' => 'RE', 'KTE' => 'RS'],
'body' => 'RM',
'c_type' => 'RC',
'd_type' => 'RD',
'extra_finish' => 'YY',
'base' => 'XX',
];
/** 측면형(Side) prefix */
private const SIDE_PREFIXES = [
'finish' => ['KSS' => 'SS', 'KQTS' => 'SS', 'KSE' => 'SE', 'KWE' => 'SE', 'KTE' => 'SS'],
'body' => 'SM',
'c_type' => 'SC',
'd_type' => 'SD',
'extra_finish' => 'YY',
'base' => 'XX',
];
/** 철재(KTE01) body 오버라이드 */
private const STEEL_BODY_OVERRIDES = [
'wall' => 'RT',
'side' => 'ST',
];
// ─────────────────────────────────────────────────
// 하단마감재 Prefix 맵
// ─────────────────────────────────────────────────
/** 하단마감재 main prefix: finishMaterial 기반 */
private const BOTTOM_BAR_MAIN = [
'EGI' => 'BE',
'SUS' => 'BS',
'STEEL' => 'TS',
];
// ─────────────────────────────────────────────────
// 셔터박스 Prefix 맵
// ─────────────────────────────────────────────────
/** 표준 사이즈(500*380) 셔터박스 prefix */
private const SHUTTER_STANDARD = [
'front' => 'CF',
'lintel' => 'CL',
'inspection' => 'CP',
'rear_corner' => 'CB',
'top_cover' => 'XX',
'fin_cover' => 'XX',
];
// ─────────────────────────────────────────────────
// 길이코드 매핑
// ─────────────────────────────────────────────────
private const LENGTH_TO_CODE = [
1219 => '12',
2438 => '24',
3000 => '30',
3500 => '35',
4000 => '40',
4150 => '41',
4200 => '42',
4300 => '43',
];
/** 연기차단재 전용 길이코드 */
private const SMOKE_LENGTH_TO_CODE = [
'w50' => [3000 => '53', 4000 => '54'],
'w80' => [3000 => '83', 4000 => '84'],
];
/** 파트타입 한글명 */
private const PART_TYPE_NAMES = [
'finish' => '마감재',
'body' => '본체',
'c_type' => 'C형',
'd_type' => 'D형',
'extra_finish' => '별도마감',
'base' => '하부BASE',
'main' => '메인',
'lbar' => 'L-Bar',
'reinforce' => '보강평철',
'extra' => '별도마감',
'front' => '전면부',
'lintel' => '린텔부',
'inspection' => '점검구',
'rear_corner' => '후면코너부',
'top_cover' => '상부덮개',
'fin_cover' => '마구리',
'smoke' => '연기차단재',
];
/** items.id 캐시: code → id */
private array $itemIdCache = [];
// ─────────────────────────────────────────────────
// 가이드레일
// ─────────────────────────────────────────────────
/**
* 가이드레일 세부품목의 prefix 결정
*
* @param string $partType 'finish', 'body', 'c_type', 'd_type', 'extra_finish', 'base'
* @param string $guideType 'wall', 'side'
* @param string $productCode 'KSS01', 'KSE01', 'KWE01', 'KTE01', 'KQTS01'
* @return string prefix (빈 문자열이면 해당 파트 없음)
*/
public function resolveGuideRailPrefix(string $partType, string $guideType, string $productCode): string
{
$prefixMap = $guideType === 'wall' ? self::WALL_PREFIXES : self::SIDE_PREFIXES;
$codePrefix = $this->extractCodePrefix($productCode);
$isSteel = $codePrefix === 'KTE';
// body: 철재 오버라이드
if ($partType === 'body' && $isSteel) {
return self::STEEL_BODY_OVERRIDES[$guideType] ?? '';
}
// finish: productCode별 분기
if ($partType === 'finish') {
$finishMap = $prefixMap['finish'] ?? [];
return $finishMap[$codePrefix] ?? '';
}
// extra_finish, base, c_type, d_type, body: 고정 prefix
return $prefixMap[$partType] ?? '';
}
// ─────────────────────────────────────────────────
// 하단마감재
// ─────────────────────────────────────────────────
/**
* 하단마감재 세부품목의 prefix 결정
*
* @param string $partType 'main', 'lbar', 'reinforce', 'extra'
* @param string $productCode 'KSS01', 'KSE01', etc.
* @param string $finishMaterial 'EGI마감', 'SUS마감'
* @return string prefix
*/
public function resolveBottomBarPrefix(string $partType, string $productCode, string $finishMaterial): string
{
if ($partType === 'lbar') {
return 'LA';
}
if ($partType === 'reinforce') {
return 'HH';
}
if ($partType === 'extra') {
return 'YY';
}
// main: 재질 기반
$codePrefix = $this->extractCodePrefix($productCode);
$isSteel = $codePrefix === 'KTE';
if ($isSteel) {
return 'TS';
}
$isSUS = in_array($codePrefix, ['KSS', 'KQTS']);
return $isSUS ? 'BS' : 'BE';
}
// ─────────────────────────────────────────────────
// 셔터박스
// ─────────────────────────────────────────────────
/**
* 셔터박스 세부품목의 prefix 결정
*
* @param string $partType 'front', 'lintel', 'inspection', 'rear_corner', 'top_cover', 'fin_cover'
* @param bool $isStandardSize 500*380인지
* @return string prefix
*/
public function resolveShutterBoxPrefix(string $partType, bool $isStandardSize): string
{
if (! $isStandardSize) {
return 'XX';
}
return self::SHUTTER_STANDARD[$partType] ?? 'XX';
}
// ─────────────────────────────────────────────────
// 연기차단재
// ─────────────────────────────────────────────────
/**
* 연기차단재 세부품목의 prefix 결정 (항상 GI)
*/
public function resolveSmokeBarrierPrefix(): string
{
return 'GI';
}
// ─────────────────────────────────────────────────
// 코드 생성 및 조회
// ─────────────────────────────────────────────────
/**
* prefix + 길이(mm) → BD-XX-NN 코드 생성
*
* @param string $prefix LOT prefix (RS, RM, etc.)
* @param int $lengthMm 길이 (mm)
* @param string|null $smokeCategory 연기차단재 카테고리 ('w50', 'w80')
* @return string|null BD 코드 (길이코드 변환 실패 시 null)
*/
public function buildItemCode(string $prefix, int $lengthMm, ?string $smokeCategory = null): ?string
{
$lengthCode = self::lengthToCode($lengthMm, $smokeCategory);
if ($lengthCode === null) {
return null;
}
return "BD-{$prefix}-{$lengthCode}";
}
/**
* BD-XX-NN 코드 → items.id 조회 (캐시)
*
* @return int|null items.id (미등록 시 null)
*/
public function resolveItemId(string $itemCode, int $tenantId = 287): ?int
{
$cacheKey = "{$tenantId}:{$itemCode}";
if (isset($this->itemIdCache[$cacheKey])) {
return $this->itemIdCache[$cacheKey];
}
$id = DB::table('items')
->where('tenant_id', $tenantId)
->where('code', $itemCode)
->whereNull('deleted_at')
->value('id');
$this->itemIdCache[$cacheKey] = $id;
return $id;
}
/**
* 길이(mm) → 길이코드 변환
*
* @param int $lengthMm 길이 (mm)
* @param string|null $smokeCategory 연기차단재 카테고리 ('w50', 'w80')
* @return string|null 길이코드 (변환 불가 시 null)
*/
public static function lengthToCode(int $lengthMm, ?string $smokeCategory = null): ?string
{
// 연기차단재 전용 코드
if ($smokeCategory && isset(self::SMOKE_LENGTH_TO_CODE[$smokeCategory][$lengthMm])) {
return self::SMOKE_LENGTH_TO_CODE[$smokeCategory][$lengthMm];
}
return self::LENGTH_TO_CODE[$lengthMm] ?? null;
}
/**
* 파트타입 한글명 반환
*/
public static function partTypeName(string $partType): string
{
return self::PART_TYPE_NAMES[$partType] ?? $partType;
}
/**
* 캐시 초기화 (테스트 용)
*/
public function clearCache(): void
{
$this->itemIdCache = [];
}
// ─────────────────────────────────────────────────
// private
// ─────────────────────────────────────────────────
/**
* 'KSS01' → 'KSS', 'KQTS01' → 'KQTS' 등 제품코드 prefix 추출
*/
private function extractCodePrefix(string $productCode): string
{
return preg_replace('/\d+$/', '', $productCode);
}
}

View File

@@ -1662,11 +1662,11 @@ private function calculateTenantBom(
$weightFormula = 'AREA × 25';
$weightCalc = "{$area} × 25";
} elseif ($productType === 'steel') {
// 철재: W1 × (H1 + 550) / 1M, 중량 = 면적 × 25
$area = ($W1 * ($H1 + 550)) / 1000000;
// 철재: W1 × H1 / 1M, 중량 = 면적 × 25 (레거시 Slat_updateCol12and13 동일)
$area = ($W1 * $H1) / 1000000;
$weight = $area * 25;
$areaFormula = '(W1 × (H1 + 550)) / 1,000,000';
$areaCalc = "({$W1} × ({$H1} + 550)) / 1,000,000";
$areaFormula = '(W1 × H1) / 1,000,000';
$areaCalc = "({$W1} × {$H1}) / 1,000,000";
$weightFormula = 'AREA × 25';
$weightCalc = "{$area} × 25";
} else {

View File

@@ -73,9 +73,11 @@ public function index(array $params): LengthAwarePaginator
$query->where('quote_type', $quoteType);
}
// 상태 필터
if ($status) {
$query->where('status', $status);
// 상태 필터 (converted는 order_id 기반으로 판별)
if ($status === Quote::STATUS_CONVERTED) {
$query->whereNotNull('order_id');
} elseif ($status) {
$query->where('status', $status)->whereNull('order_id');
}
// 제품 카테고리 필터
@@ -592,12 +594,12 @@ public function cancelFinalize(int $id): Quote
throw new NotFoundHttpException(__('error.quote_not_found'));
}
if ($quote->status !== Quote::STATUS_FINALIZED) {
throw new BadRequestHttpException(__('error.quote_not_finalized'));
if ($quote->order_id) {
throw new BadRequestHttpException(__('error.quote_already_converted'));
}
if ($quote->status === Quote::STATUS_CONVERTED) {
throw new BadRequestHttpException(__('error.quote_already_converted'));
if ($quote->getRawOriginal('status') !== Quote::STATUS_FINALIZED) {
throw new BadRequestHttpException(__('error.quote_not_finalized'));
}
$quote->update([
@@ -687,11 +689,23 @@ public function convertToOrder(int $id): Quote
}
// 수주 상세 품목 생성 (노드 연결 포함)
// formula_source 없는 레거시 데이터용: sort_order 기반 분배 준비
$locationCount = count($productItems);
$hasFormulaSource = $quote->items->contains(fn ($item) => ! empty($item->formula_source));
$itemsPerLocation = (! $hasFormulaSource && $locationCount > 1)
? intdiv($quote->items->count(), $locationCount)
: 0;
$serialIndex = 1;
foreach ($quote->items as $quoteItem) {
foreach ($quote->items as $index => $quoteItem) {
$productMapping = $this->resolveLocationMapping($quoteItem, $productItems);
$locIdx = $this->resolveLocationIndex($quoteItem, $productItems);
// sort_order 기반 분배 (formula_source/note 매칭 모두 실패 시)
if ($locIdx === 0 && $itemsPerLocation > 0) {
$locIdx = min(intdiv($index, $itemsPerLocation), $locationCount - 1);
}
$productMapping['order_node_id'] = isset($nodeMap[$locIdx]) ? $nodeMap[$locIdx]->id : null;
$orderItem = OrderItem::createFromQuoteItem($quoteItem, $order->id, $serialIndex, $productMapping);
@@ -705,9 +719,8 @@ public function convertToOrder(int $id): Quote
$order->recalculateTotals();
$order->save();
// 견적 상태 변경
// 견적에 수주 연결 (status는 accessor가 자동으로 'converted' 반환)
$quote->update([
'status' => Quote::STATUS_CONVERTED,
'order_id' => $order->id,
'updated_by' => $userId,
]);

View File

@@ -194,10 +194,13 @@ public function store(array $data): Receiving
$receiving->item_name = $data['item_name'];
$receiving->specification = $data['specification'] ?? null;
$receiving->supplier = $data['supplier'];
$receiving->order_qty = $data['order_qty'];
$receiving->order_qty = $data['order_qty'] ?? null;
$receiving->order_unit = $data['order_unit'] ?? 'EA';
$receiving->due_date = $data['due_date'] ?? null;
$receiving->status = $data['status'] ?? 'order_completed';
$receiving->receiving_qty = $data['receiving_qty'] ?? null;
$receiving->receiving_date = $data['receiving_date'] ?? null;
$receiving->lot_no = $data['lot_no'] ?? $this->generateLotNo();
$receiving->status = $data['status'] ?? 'receiving_pending';
$receiving->remark = $data['remark'] ?? null;
// options 필드 처리 (제조사, 수입검사 등 확장 필드)

View File

@@ -23,7 +23,7 @@ public function aggregateDaily(int $tenantId, Carbon $date): int
COALESCE(SUM(total_amount), 0) as total_amount,
SUM(CASE WHEN status = 'approved' THEN 1 ELSE 0 END) as approved_count,
SUM(CASE WHEN status = 'rejected' THEN 1 ELSE 0 END) as rejected_count,
SUM(CASE WHEN status = 'converted' THEN 1 ELSE 0 END) as conversion_count
SUM(CASE WHEN order_id IS NOT NULL THEN 1 ELSE 0 END) as conversion_count
")
->first();

View File

@@ -3,6 +3,8 @@
namespace App\Services;
use App\Models\Items\Item;
use App\Models\Production\WorkOrder;
use App\Models\Production\WorkOrderItem;
use App\Models\Tenants\Receiving;
use App\Models\Tenants\Stock;
use App\Models\Tenants\StockLot;
@@ -67,6 +69,11 @@ public function index(array $params): LengthAwarePaginator
$query->where('items.item_type', strtoupper($params['item_type']));
}
// 품목 카테고리 필터 (Item.item_category: BENDING, SCREEN, STEEL 등)
if (! empty($params['item_category'])) {
$query->where('items.item_category', strtoupper($params['item_category']));
}
// 재고 상태 필터 (Stock.status)
if (! empty($params['status'])) {
$query->whereHas('stock', function ($q) use ($params) {
@@ -313,6 +320,99 @@ public function increaseFromReceiving(Receiving $receiving): StockLot
});
}
/**
* 생산 완료 시 완성품 재고 입고
*
* increaseFromReceiving()을 기반으로 구현.
* 선생산(수주 없는 작업지시) 완료 시 양품을 재고로 적재.
*
* @param WorkOrder $workOrder 선생산 작업지시
* @param WorkOrderItem $woItem 작업지시 품목
* @param float $goodQty 양품 수량
* @param string $lotNo LOT 번호
* @return StockLot 생성된 StockLot
*/
public function increaseFromProduction(
WorkOrder $workOrder,
WorkOrderItem $woItem,
float $goodQty,
string $lotNo
): StockLot {
if (! $woItem->item_id) {
throw new \Exception(__('error.stock.item_id_required'));
}
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
return DB::transaction(function () use ($workOrder, $woItem, $goodQty, $lotNo, $tenantId, $userId) {
// 1. Stock 조회 또는 생성
$stock = $this->getOrCreateStock($woItem->item_id);
// 2. FIFO 순서 계산
$fifoOrder = $this->getNextFifoOrder($stock->id);
// 3. StockLot 생성
$stockLot = new StockLot;
$stockLot->tenant_id = $tenantId;
$stockLot->stock_id = $stock->id;
$stockLot->lot_no = $lotNo;
$stockLot->fifo_order = $fifoOrder;
$stockLot->receipt_date = now()->toDateString();
$stockLot->qty = $goodQty;
$stockLot->reserved_qty = 0;
$stockLot->available_qty = $goodQty;
$stockLot->unit = $woItem->unit ?? 'EA';
$stockLot->supplier = null;
$stockLot->supplier_lot = null;
$stockLot->po_number = null;
$stockLot->location = null;
$stockLot->status = 'available';
$stockLot->receiving_id = null;
$stockLot->work_order_id = $workOrder->id;
$stockLot->created_by = $userId;
$stockLot->updated_by = $userId;
$stockLot->save();
// 4. Stock 합계 갱신
$stock->refreshFromLots();
// 5. 거래 이력 기록
$this->recordTransaction(
stock: $stock,
type: StockTransaction::TYPE_IN,
qty: $goodQty,
reason: StockTransaction::REASON_PRODUCTION_OUTPUT,
referenceType: 'work_order',
referenceId: $workOrder->id,
lotNo: $lotNo,
stockLotId: $stockLot->id
);
// 6. 감사 로그 기록
$this->logStockChange(
stock: $stock,
action: 'production_in',
reason: 'production_output',
referenceType: 'work_order',
referenceId: $workOrder->id,
qtyChange: $goodQty,
lotNo: $lotNo
);
Log::info('Stock increased from production', [
'work_order_id' => $workOrder->id,
'item_id' => $woItem->item_id,
'stock_id' => $stock->id,
'stock_lot_id' => $stockLot->id,
'qty' => $goodQty,
'lot_no' => $lotNo,
]);
return $stockLot;
});
}
/**
* 입고 수정 시 재고 조정 (차이만큼 증감)
*
@@ -425,18 +525,41 @@ public function getOrCreateStock(int $itemId, ?Receiving $receiving = null): Sto
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$stock = Stock::where('tenant_id', $tenantId)
$item = Item::where('tenant_id', $tenantId)
->findOrFail($itemId);
// 1차: item_id로 조회 (SoftDeletes 포함)
$stock = Stock::withTrashed()
->where('tenant_id', $tenantId)
->where('item_id', $itemId)
->first();
// 2차: item_code로 조회 (unique key 기준, item_id가 다를 수 있음)
if (! $stock) {
$stock = Stock::withTrashed()
->where('tenant_id', $tenantId)
->where('item_code', $item->code)
->first();
// item_id가 변경된 경우 업데이트
if ($stock && $stock->item_id !== $itemId) {
$stock->item_id = $itemId;
}
}
if ($stock) {
if ($stock->trashed()) {
$stock->restore();
$stock->status = 'out';
}
$stock->item_name = $item->name;
$stock->updated_by = $userId;
$stock->save();
return $stock;
}
// Stock이 없으면 새로 생성
$item = Item::where('tenant_id', $tenantId)
->findOrFail($itemId);
$stock = new Stock;
$stock->tenant_id = $tenantId;
$stock->item_id = $itemId;

View File

@@ -24,7 +24,8 @@ class WorkOrderService extends Service
private const AUDIT_TARGET = 'work_order';
public function __construct(
private readonly AuditLogger $auditLogger
private readonly AuditLogger $auditLogger,
private readonly StockService $stockService
) {}
/**
@@ -587,15 +588,62 @@ public function updateStatus(int $id, string $status, ?array $resultData = null)
// 연결된 수주(Order) 상태 동기화
$this->syncOrderStatus($workOrder, $tenantId);
// 작업완료 시 자동 출하 생성
// 작업완료 시: 수주 연동 → 출하 생성 / 선생산 → 재고 입고
if ($status === WorkOrder::STATUS_COMPLETED) {
$this->createShipmentFromWorkOrder($workOrder, $tenantId, $userId);
if ($workOrder->sales_order_id) {
$this->createShipmentFromWorkOrder($workOrder, $tenantId, $userId);
} else {
$this->stockInFromProduction($workOrder);
}
}
return $workOrder->load(['assignee:id,name', 'assignees.user:id,name', 'team:id,name', 'process:id,process_name,process_code']);
});
}
/**
* 선생산 작업지시 완료 시 완성품을 재고로 입고
*
* 수주 없는 작업지시(sales_order_id = null)가 완료되면
* 각 품목의 양품 수량을 재고 시스템에 입고 처리합니다.
*/
private function stockInFromProduction(WorkOrder $workOrder): void
{
$workOrder->loadMissing('items.item');
foreach ($workOrder->items as $woItem) {
if ($this->shouldStockIn($woItem)) {
$resultData = $woItem->options['result'] ?? [];
$goodQty = (float) ($resultData['good_qty'] ?? $woItem->quantity);
$lotNo = $resultData['lot_no'] ?? '';
if ($goodQty > 0 && $lotNo) {
$this->stockService->increaseFromProduction(
$workOrder, $woItem, $goodQty, $lotNo
);
}
}
}
}
/**
* 품목이 생산입고 대상인지 판단
*
* items.options의 production_source와 lot_managed 속성으로 판단.
*/
private function shouldStockIn(WorkOrderItem $woItem): bool
{
$item = $woItem->item;
if (! $item) {
return false;
}
$options = $item->options ?? [];
return ($options['production_source'] ?? null) === 'self_produced'
&& ($options['lot_managed'] ?? false) === true;
}
/**
* 작업지시 완료 시 자동 출하 생성
*
@@ -1144,13 +1192,69 @@ public function getMaterials(int $workOrderId): array
throw new NotFoundHttpException(__('error.not_found'));
}
// Phase 1: 작업지시 품목들에서 유니크 자재 목록 수집 (item_id 기준 합산)
// ── Step 1: dynamic_bom 대상 item_id 일괄 수집 (N+1 방지) ──
$allDynamicItemIds = [];
foreach ($workOrder->items as $woItem) {
$options = is_string($woItem->options) ? json_decode($woItem->options, true) : ($woItem->options ?? []);
$dynamicBom = $options['dynamic_bom'] ?? null;
if ($dynamicBom && is_array($dynamicBom)) {
$allDynamicItemIds = array_merge($allDynamicItemIds, array_column($dynamicBom, 'child_item_id'));
}
}
// 배치 조회 (dynamic_bom 품목)
$dynamicItems = [];
if (! empty($allDynamicItemIds)) {
$dynamicItems = \App\Models\Items\Item::where('tenant_id', $tenantId)
->whereIn('id', array_unique($allDynamicItemIds))
->get()
->keyBy('id');
}
// ── Step 2: 유니크 자재 목록 수집 ──
// 키: dynamic_bom → "{item_id}_{woItem_id}", 기존 BOM → "{item_id}"
$uniqueMaterials = [];
foreach ($workOrder->items as $woItem) {
$options = is_string($woItem->options) ? json_decode($woItem->options, true) : ($woItem->options ?? []);
$dynamicBom = $options['dynamic_bom'] ?? null;
// dynamic_bom 우선 — 있으면 BOM 무시
if ($dynamicBom && is_array($dynamicBom)) {
foreach ($dynamicBom as $bomEntry) {
$childItemId = $bomEntry['child_item_id'] ?? null;
if (! $childItemId || ! isset($dynamicItems[$childItemId])) {
continue;
}
// 합산 키: (item_id, work_order_item_id) 쌍
$key = $childItemId.'_'.$woItem->id;
// dynamic_bom.qty는 아이템 수량이 곱해져 있으므로 나눠서 개소당 수량 산출
$bomQty = (float) ($bomEntry['qty'] ?? 1);
$woItemQty = max(1, (float) ($woItem->quantity ?? 1));
$perNodeQty = $bomQty / $woItemQty;
if (isset($uniqueMaterials[$key])) {
$uniqueMaterials[$key]['required_qty'] += $perNodeQty;
} else {
$uniqueMaterials[$key] = [
'item' => $dynamicItems[$childItemId],
'bom_qty' => $perNodeQty,
'required_qty' => $perNodeQty,
'work_order_item_id' => $woItem->id,
'lot_prefix' => $bomEntry['lot_prefix'] ?? null,
'part_type' => $bomEntry['part_type'] ?? null,
'category' => $bomEntry['category'] ?? null,
];
}
}
continue; // dynamic_bom이 있으면 기존 BOM fallback 건너뜀
}
// 기존 BOM 로직 (하위 호환)
$materialItems = [];
// BOM이 있으면 자식 품목들을 자재로 사용
if ($woItem->item_id) {
$item = \App\Models\Items\Item::where('tenant_id', $tenantId)
->find($woItem->item_id);
@@ -1189,7 +1293,7 @@ public function getMaterials(int $workOrderId): array
];
}
// 유니크 자재 수집 (같은 item_id면 required_qty 합산)
// 기존 방식: item_id 기준 합산
foreach ($materialItems as $matInfo) {
$itemId = $matInfo['item']->id;
if (isset($uniqueMaterials[$itemId])) {
@@ -1200,30 +1304,67 @@ public function getMaterials(int $workOrderId): array
}
}
// Phase 2: 유니크 자재별로 StockLot 조회
// ── Step 3: 유니크 자재별로 StockLot 조회 ──
// 배치 조회를 위해 전체 item_id 수집
$allItemIds = [];
foreach ($uniqueMaterials as $matInfo) {
$allItemIds[] = $matInfo['item']->id;
}
$allItemIds = array_unique($allItemIds);
// Stock 배치 조회 (N+1 방지)
$stockMap = [];
if (! empty($allItemIds)) {
$stocks = \App\Models\Tenants\Stock::where('tenant_id', $tenantId)
->whereIn('item_id', $allItemIds)
->get();
foreach ($stocks as $stock) {
$stockMap[$stock->item_id] = $stock;
}
}
// StockLot 배치 조회 (N+1 방지)
$lotsByStockId = [];
$stockIds = array_map(fn ($s) => $s->id, $stockMap);
if (! empty($stockIds)) {
$allLots = \App\Models\Tenants\StockLot::where('tenant_id', $tenantId)
->whereIn('stock_id', $stockIds)
->where('status', 'available')
->where('available_qty', '>', 0)
->orderBy('fifo_order', 'asc')
->get();
foreach ($allLots as $lot) {
$lotsByStockId[$lot->stock_id][] = $lot;
}
}
$materials = [];
$rank = 1;
foreach ($uniqueMaterials as $matInfo) {
$materialItem = $matInfo['item'];
$stock = \App\Models\Tenants\Stock::where('tenant_id', $tenantId)
->where('item_id', $materialItem->id)
->first();
$stock = $stockMap[$materialItem->id] ?? null;
$lotsFound = false;
// 공통 필드 (dynamic_bom 추가 필드 포함)
$extraFields = [];
if (isset($matInfo['work_order_item_id'])) {
$extraFields = [
'work_order_item_id' => $matInfo['work_order_item_id'],
'lot_prefix' => $matInfo['lot_prefix'],
'part_type' => $matInfo['part_type'],
'category' => $matInfo['category'],
];
}
if ($stock) {
$lots = \App\Models\Tenants\StockLot::where('tenant_id', $tenantId)
->where('stock_id', $stock->id)
->where('status', 'available')
->where('available_qty', '>', 0)
->orderBy('fifo_order', 'asc')
->get();
$lots = $lotsByStockId[$stock->id] ?? [];
foreach ($lots as $lot) {
$lotsFound = true;
$materials[] = [
$materials[] = array_merge([
'stock_lot_id' => $lot->id,
'item_id' => $materialItem->id,
'lot_no' => $lot->lot_no,
@@ -1239,13 +1380,13 @@ public function getMaterials(int $workOrderId): array
'receipt_date' => $lot->receipt_date,
'supplier' => $lot->supplier,
'fifo_rank' => $rank++,
];
], $extraFields);
}
}
// 가용 로트가 없는 경우 자재 정보만 반환 (재고 없음 표시)
if (! $lotsFound) {
$materials[] = [
$materials[] = array_merge([
'stock_lot_id' => null,
'item_id' => $materialItem->id,
'lot_no' => null,
@@ -1261,7 +1402,7 @@ public function getMaterials(int $workOrderId): array
'receipt_date' => null,
'supplier' => null,
'fifo_rank' => $rank++,
];
], $extraFields);
}
}
@@ -1289,11 +1430,50 @@ public function registerMaterialInput(int $workOrderId, array $inputs): array
throw new NotFoundHttpException(__('error.not_found'));
}
return DB::transaction(function () use ($inputs, $tenantId, $userId, $workOrderId) {
// work_order_item_id가 있는 항목은 registerMaterialInputForItem()으로 위임
$groupedByItem = [];
$noItemInputs = [];
foreach ($inputs as $input) {
$woItemId = $input['work_order_item_id'] ?? null;
if ($woItemId) {
$groupedByItem[$woItemId][] = $input;
} else {
$noItemInputs[] = $input;
}
}
// work_order_item_id가 있는 항목 → 개소별 투입으로 위임
$delegatedResults = [];
foreach ($groupedByItem as $woItemId => $itemInputs) {
$delegatedResults[] = $this->registerMaterialInputForItem($workOrderId, $woItemId, $itemInputs);
}
// work_order_item_id가 없는 항목 → 기존 방식 + WorkOrderMaterialInput 레코드 생성
if (empty($noItemInputs)) {
// 전부 위임된 경우
$totalCount = array_sum(array_column($delegatedResults, 'material_count'));
$allResults = array_merge(...array_map(fn ($r) => $r['input_results'], $delegatedResults));
return [
'work_order_id' => $workOrderId,
'material_count' => $totalCount,
'input_results' => $allResults,
'input_at' => now()->toDateTimeString(),
];
}
// fallback: 첫 번째 work_order_item_id로 매핑
$fallbackWoItemId = WorkOrderItem::where('tenant_id', $tenantId)
->where('work_order_id', $workOrderId)
->orderBy('id')
->value('id');
return DB::transaction(function () use ($noItemInputs, $tenantId, $userId, $workOrderId, $fallbackWoItemId, $delegatedResults) {
$stockService = app(StockService::class);
$inputResults = [];
foreach ($inputs as $input) {
foreach ($noItemInputs as $input) {
$stockLotId = $input['stock_lot_id'] ?? null;
$qty = (float) ($input['qty'] ?? 0);
@@ -1309,6 +1489,21 @@ public function registerMaterialInput(int $workOrderId, array $inputs): array
referenceId: $workOrderId
);
// WorkOrderMaterialInput 레코드 생성 (이력 통일)
$lot = \App\Models\Tenants\StockLot::with('stock')->find($stockLotId);
$lotItemId = $lot?->stock?->item_id;
WorkOrderMaterialInput::create([
'tenant_id' => $tenantId,
'work_order_id' => $workOrderId,
'work_order_item_id' => $fallbackWoItemId,
'stock_lot_id' => $stockLotId,
'item_id' => $lotItemId ?? 0,
'qty' => $qty,
'input_by' => $userId,
'input_at' => now(),
]);
$inputResults[] = [
'stock_lot_id' => $stockLotId,
'qty' => $qty,
@@ -1325,17 +1520,23 @@ public function registerMaterialInput(int $workOrderId, array $inputs): array
'material_input',
null,
[
'inputs' => $inputs,
'inputs' => $noItemInputs,
'input_results' => $inputResults,
'input_by' => $userId,
'input_at' => now()->toDateTimeString(),
]
);
// 위임된 결과와 합산
$allResults = $inputResults;
foreach ($delegatedResults as $dr) {
$allResults = array_merge($allResults, $dr['input_results']);
}
return [
'work_order_id' => $workOrderId,
'material_count' => count($inputResults),
'input_results' => $inputResults,
'material_count' => count($allResults),
'input_results' => $allResults,
'input_at' => now()->toDateTimeString(),
];
});
@@ -2193,8 +2394,8 @@ public function getInspectionReport(int $workOrderId): array
$tenantId = $this->tenantId();
$workOrder = WorkOrder::where('tenant_id', $tenantId)
->with(['order', 'items' => function ($q) {
$q->ordered();
->with(['salesOrder', 'items' => function ($q) {
$q->ordered()->with('sourceOrderItem');
}])
->find($workOrderId);
@@ -2202,18 +2403,61 @@ public function getInspectionReport(int $workOrderId): array
throw new NotFoundHttpException(__('error.not_found'));
}
$items = $workOrder->items->map(function ($item) {
return [
'id' => $item->id,
'item_name' => $item->item_name,
'specification' => $item->specification,
'quantity' => $item->quantity,
'sort_order' => $item->sort_order,
'status' => $item->status,
'options' => $item->options,
'inspection_data' => $item->getInspectionData(),
// 개소(order_node_id)별 그룹핑 — WorkerScreen과 동일한 구조
$grouped = $workOrder->items->groupBy(
fn ($item) => $item->sourceOrderItem?->order_node_id ?? 'unassigned'
);
$nodeIds = $grouped->keys()->filter(fn ($k) => $k !== 'unassigned')->values()->all();
$nodes = ! empty($nodeIds)
? \App\Models\Orders\OrderNode::whereIn('id', $nodeIds)->get()->keyBy('id')
: collect();
$nodeGroups = [];
foreach ($grouped as $nodeId => $groupItems) {
$node = $nodeId !== 'unassigned' ? $nodes->get($nodeId) : null;
$nodeOpts = $node?->options ?? [];
$firstItem = $groupItems->first();
$soi = $firstItem->sourceOrderItem;
$floorCode = $soi?->floor_code ?? '-';
$symbolCode = $soi?->symbol_code ?? '-';
$floorLabel = collect([$floorCode, $symbolCode])
->filter(fn ($v) => $v && $v !== '-')->join('/');
$nodeGroups[] = [
'node_id' => $nodeId !== 'unassigned' ? (int) $nodeId : null,
'node_name' => $floorLabel ?: ($node?->name ?? '미지정'),
'floor' => $nodeOpts['floor'] ?? $floorCode,
'code' => $nodeOpts['symbol'] ?? $symbolCode,
'width' => $nodeOpts['width'] ?? 0,
'height' => $nodeOpts['height'] ?? 0,
'total_quantity' => $groupItems->sum('quantity'),
'options' => $nodeOpts,
'items' => $groupItems->map(fn ($item) => [
'id' => $item->id,
'item_name' => $item->item_name,
'specification' => $item->specification,
'quantity' => $item->quantity,
'sort_order' => $item->sort_order,
'status' => $item->status,
'options' => $item->options,
'inspection_data' => $item->getInspectionData(),
])->values()->all(),
];
});
}
// 플랫 아이템 목록 (summary 계산용)
$items = $workOrder->items->map(fn ($item) => [
'id' => $item->id,
'item_name' => $item->item_name,
'specification' => $item->specification,
'quantity' => $item->quantity,
'sort_order' => $item->sort_order,
'status' => $item->status,
'options' => $item->options,
'inspection_data' => $item->getInspectionData(),
]);
return [
'work_order' => [
@@ -2223,13 +2467,14 @@ public function getInspectionReport(int $workOrderId): array
'planned_date' => $workOrder->planned_date,
'due_date' => $workOrder->due_date,
],
'order' => $workOrder->order ? [
'id' => $workOrder->order->id,
'order_no' => $workOrder->order->order_no,
'client_name' => $workOrder->order->client_name ?? null,
'site_name' => $workOrder->order->site_name ?? null,
'order_date' => $workOrder->order->order_date ?? null,
'order' => $workOrder->salesOrder ? [
'id' => $workOrder->salesOrder->id,
'order_no' => $workOrder->salesOrder->order_no,
'client_name' => $workOrder->salesOrder->client_name ?? null,
'site_name' => $workOrder->salesOrder->site_name ?? null,
'order_date' => $workOrder->salesOrder->order_date ?? null,
] : null,
'node_groups' => $nodeGroups,
'items' => $items,
'summary' => [
'total_items' => $items->count(),
@@ -2604,7 +2849,45 @@ public function getMaterialsForItem(int $workOrderId, int $itemId): array
// 해당 개소의 BOM 기반 자재 추출
$materialItems = [];
if ($woItem->item_id) {
// ① dynamic_bom 우선 체크 (절곡 등 동적 BOM 사용 공정)
$options = is_string($woItem->options) ? json_decode($woItem->options, true) : ($woItem->options ?? []);
$dynamicBom = $options['dynamic_bom'] ?? null;
if ($dynamicBom && is_array($dynamicBom)) {
// dynamic_bom child_item_id 배치 조회 (N+1 방지)
$childItemIds = array_filter(array_column($dynamicBom, 'child_item_id'));
$childItems = [];
if (! empty($childItemIds)) {
$childItems = \App\Models\Items\Item::where('tenant_id', $tenantId)
->whereIn('id', array_unique($childItemIds))
->get()
->keyBy('id');
}
foreach ($dynamicBom as $bomEntry) {
$childItemId = $bomEntry['child_item_id'] ?? null;
if (! $childItemId || ! isset($childItems[$childItemId])) {
continue;
}
// dynamic_bom.qty는 아이템 수량이 곱해져 있으므로 나눠서 개소당 수량 산출
// (작업일지 bendingInfo와 동일한 수량)
$bomQty = (float) ($bomEntry['qty'] ?? 1);
$woItemQty = max(1, (float) ($woItem->quantity ?? 1));
$perNodeQty = $bomQty / $woItemQty;
$materialItems[] = [
'item' => $childItems[$childItemId],
'bom_qty' => $perNodeQty,
'required_qty' => $perNodeQty,
'lot_prefix' => $bomEntry['lot_prefix'] ?? null,
'part_type' => $bomEntry['part_type'] ?? null,
'category' => $bomEntry['category'] ?? null,
];
}
}
// ② dynamic_bom이 없으면 정적 BOM fallback
if (empty($materialItems) && $woItem->item_id) {
$item = \App\Models\Items\Item::where('tenant_id', $tenantId)
->find($woItem->item_id);
@@ -2693,6 +2976,9 @@ public function getMaterialsForItem(int $workOrderId, int $itemId): array
'receipt_date' => $lot->receipt_date,
'supplier' => $lot->supplier,
'fifo_rank' => $rank++,
'lot_prefix' => $matInfo['lot_prefix'] ?? null,
'part_type' => $matInfo['part_type'] ?? null,
'category' => $matInfo['category'] ?? null,
];
}
}
@@ -2716,6 +3002,9 @@ public function getMaterialsForItem(int $workOrderId, int $itemId): array
'receipt_date' => null,
'supplier' => null,
'fifo_rank' => $rank++,
'lot_prefix' => $matInfo['lot_prefix'] ?? null,
'part_type' => $matInfo['part_type'] ?? null,
'category' => $matInfo['category'] ?? null,
];
}
}
@@ -2764,9 +3053,9 @@ public function registerMaterialInputForItem(int $workOrderId, int $itemId, arra
referenceId: $workOrderId
);
// 로트의 품목 ID 조회
$lot = \App\Models\Tenants\StockLot::find($stockLotId);
$lotItemId = $lot ? ($lot->stock->item_id ?? null) : null;
// 로트의 품목 ID 조회 (Eager Loading으로 N+1 방지)
$lot = \App\Models\Tenants\StockLot::with('stock')->find($stockLotId);
$lotItemId = $lot?->stock?->item_id;
// 개소별 매핑 레코드 생성
WorkOrderMaterialInput::create([

View File

@@ -153,6 +153,23 @@
*
* @OA\Property(property="status", type="string", enum={"DRAFT", "CONFIRMED", "IN_PROGRESS", "COMPLETED", "CANCELLED"}, example="CONFIRMED")
* )
*
* @OA\Schema(
* schema="OrderBulkDeleteRequest",
* type="object",
* required={"ids"},
*
* @OA\Property(property="ids", type="array", @OA\Items(type="integer"), example={1, 2, 3}),
* @OA\Property(property="force", type="boolean", description="강제 삭제 여부 (진행중 수주 포함)", example=false)
* )
*
* @OA\Schema(
* schema="OrderRevertProductionRequest",
* type="object",
*
* @OA\Property(property="force", type="boolean", description="강제 되돌리기 (물리 삭제, 기본값 false)", example=false),
* @OA\Property(property="reason", type="string", description="되돌리기 사유 (운영 모드 시 필수)", example="고객 요청에 의한 생산지시 취소")
* )
*/
class OrderApi
{
@@ -364,4 +381,86 @@ public function destroy() {}
* )
*/
public function updateStatus() {}
/**
* @OA\Delete(
* path="/api/v1/orders/bulk",
* tags={"Order"},
* summary="수주 일괄 삭제",
* description="여러 수주를 일괄 삭제합니다 (Soft Delete). 진행중/완료 수주는 건너뜁니다.",
* security={{"ApiKeyAuth": {}}, {"BearerAuth": {}}},
*
* @OA\RequestBody(
* required=true,
*
* @OA\JsonContent(ref="#/components/schemas/OrderBulkDeleteRequest")
* ),
*
* @OA\Response(
* response=200,
* description="성공",
*
* @OA\JsonContent(
*
* @OA\Property(property="success", type="boolean", example=true),
* @OA\Property(property="message", type="string"),
* @OA\Property(property="data", type="object",
* @OA\Property(property="deleted_count", type="integer", example=3),
* @OA\Property(property="skipped_count", type="integer", example=1),
* @OA\Property(property="skipped_ids", type="array", @OA\Items(type="integer"), example={5})
* )
* )
* ),
*
* @OA\Response(response=422, description="유효성 검증 실패")
* )
*/
public function bulkDestroy() {}
/**
* @OA\Post(
* path="/api/v1/orders/{id}/revert-production",
* tags={"Order"},
* summary="생산지시 되돌리기",
* description="생산지시를 되돌립니다. 기본 모드(force=false)에서는 작업지시를 취소 처리하며, 강제 모드(force=true)에서는 물리 삭제합니다.",
* security={{"ApiKeyAuth": {}}, {"BearerAuth": {}}},
*
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
*
* @OA\RequestBody(
* required=false,
*
* @OA\JsonContent(ref="#/components/schemas/OrderRevertProductionRequest")
* ),
*
* @OA\Response(
* response=200,
* description="성공",
*
* @OA\JsonContent(
*
* @OA\Property(property="success", type="boolean", example=true),
* @OA\Property(property="message", type="string"),
* @OA\Property(property="data", type="object",
* @OA\Property(property="order", ref="#/components/schemas/Order"),
* @OA\Property(property="deleted_counts", type="object",
* @OA\Property(property="work_results", type="integer", example=0),
* @OA\Property(property="work_order_items", type="integer", example=5),
* @OA\Property(property="work_orders", type="integer", example=2)
* ),
* @OA\Property(property="cancelled_counts", type="object", nullable=true,
* @OA\Property(property="work_orders", type="integer", example=2),
* @OA\Property(property="work_order_items", type="integer", example=5)
* ),
* @OA\Property(property="previous_status", type="string", example="IN_PROGRESS")
* )
* )
* ),
*
* @OA\Response(response=400, description="되돌리기 불가 상태 (수주확정/수주등록 상태)"),
* @OA\Response(response=404, description="수주를 찾을 수 없음"),
* @OA\Response(response=422, description="운영 모드에서 사유 미입력")
* )
*/
public function revertProductionOrder() {}
}