feat: [stock] 재고 조정 API 추가
- GET /stocks/{id}/adjustments: 조정 이력 조회
- POST /stocks/{id}/adjustments: 조정 등록 (증감 수량 + 사유)
- StockTransaction에 adjustment reason 추가
- StoreStockAdjustmentRequest 검증 추가
This commit is contained in:
@@ -191,6 +191,77 @@ public function show(int $id): Item
|
||||
->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 재고 조정 이력 조회
|
||||
*/
|
||||
public function adjustments(int $stockId): array
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
$stock = Stock::where('tenant_id', $tenantId)->findOrFail($stockId);
|
||||
|
||||
$transactions = StockTransaction::where('tenant_id', $tenantId)
|
||||
->where('stock_id', $stock->id)
|
||||
->where('reason', StockTransaction::REASON_ADJUSTMENT)
|
||||
->with('creator:id,name')
|
||||
->orderByDesc('created_at')
|
||||
->get();
|
||||
|
||||
return $transactions->map(fn ($t) => [
|
||||
'id' => $t->id,
|
||||
'adjusted_at' => $t->created_at->format('Y-m-d H:i'),
|
||||
'quantity' => (float) $t->qty,
|
||||
'balance_qty' => (float) $t->balance_qty,
|
||||
'remark' => $t->remark,
|
||||
'inspector' => $t->creator?->name ?? '-',
|
||||
])->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 재고 조정 등록
|
||||
*/
|
||||
public function createAdjustment(int $stockId, array $data): array
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
return DB::transaction(function () use ($stockId, $data, $tenantId, $userId) {
|
||||
$stock = Stock::where('tenant_id', $tenantId)->findOrFail($stockId);
|
||||
|
||||
$qty = (float) $data['quantity'];
|
||||
|
||||
// 재고량 직접 조정
|
||||
$stock->stock_qty += $qty;
|
||||
$stock->available_qty += $qty;
|
||||
$stock->status = $stock->calculateStatus();
|
||||
$stock->updated_by = $userId;
|
||||
$stock->save();
|
||||
|
||||
// 거래 유형: 양수 → IN, 음수 → OUT
|
||||
$type = $qty >= 0 ? StockTransaction::TYPE_IN : StockTransaction::TYPE_OUT;
|
||||
|
||||
// 거래 이력 기록
|
||||
$this->recordTransaction(
|
||||
stock: $stock,
|
||||
type: $type,
|
||||
qty: $qty,
|
||||
reason: StockTransaction::REASON_ADJUSTMENT,
|
||||
referenceType: 'stock',
|
||||
referenceId: $stock->id,
|
||||
remark: $data['remark'] ?? null
|
||||
);
|
||||
|
||||
return [
|
||||
'id' => $stock->id,
|
||||
'adjusted_at' => now()->format('Y-m-d H:i'),
|
||||
'quantity' => $qty,
|
||||
'balance_qty' => (float) $stock->stock_qty,
|
||||
'remark' => $data['remark'] ?? null,
|
||||
'inspector' => \App\Models\Members\User::find($userId)?->name ?? '-',
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 품목코드로 재고 조회 (Item 기준)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user