feat: H-2 재고 현황 API 구현
- StockController: 재고 조회 및 통계 API - StockService: 재고 비즈니스 로직 - Stock, StockLot 모델: 재고/로트 관리 - Swagger 문서화 - stocks, stock_lots 테이블 마이그레이션 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
147
app/Models/Tenants/Stock.php
Normal file
147
app/Models/Tenants/Stock.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Tenants;
|
||||
|
||||
use App\Traits\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Stock extends Model
|
||||
{
|
||||
use BelongsToTenant, SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'tenant_id',
|
||||
'item_code',
|
||||
'item_name',
|
||||
'item_type',
|
||||
'specification',
|
||||
'unit',
|
||||
'stock_qty',
|
||||
'safety_stock',
|
||||
'reserved_qty',
|
||||
'available_qty',
|
||||
'lot_count',
|
||||
'oldest_lot_date',
|
||||
'location',
|
||||
'status',
|
||||
'last_receipt_date',
|
||||
'last_issue_date',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
'deleted_by',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'stock_qty' => 'decimal:3',
|
||||
'safety_stock' => 'decimal:3',
|
||||
'reserved_qty' => 'decimal:3',
|
||||
'available_qty' => 'decimal:3',
|
||||
'lot_count' => 'integer',
|
||||
'oldest_lot_date' => 'date',
|
||||
'last_receipt_date' => 'date',
|
||||
'last_issue_date' => 'date',
|
||||
];
|
||||
|
||||
/**
|
||||
* 품목 유형 목록
|
||||
*/
|
||||
public const ITEM_TYPES = [
|
||||
'raw_material' => '원자재',
|
||||
'bent_part' => '절곡부품',
|
||||
'purchased_part' => '구매부품',
|
||||
'sub_material' => '부자재',
|
||||
'consumable' => '소모품',
|
||||
];
|
||||
|
||||
/**
|
||||
* 재고 상태 목록
|
||||
*/
|
||||
public const STATUSES = [
|
||||
'normal' => '정상',
|
||||
'low' => '부족',
|
||||
'out' => '없음',
|
||||
];
|
||||
|
||||
/**
|
||||
* LOT 관계
|
||||
*/
|
||||
public function lots(): HasMany
|
||||
{
|
||||
return $this->hasMany(StockLot::class)->orderBy('fifo_order');
|
||||
}
|
||||
|
||||
/**
|
||||
* 생성자 관계
|
||||
*/
|
||||
public function creator(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Members\User::class, 'created_by');
|
||||
}
|
||||
|
||||
/**
|
||||
* 품목유형 라벨
|
||||
*/
|
||||
public function getItemTypeLabelAttribute(): string
|
||||
{
|
||||
return self::ITEM_TYPES[$this->item_type] ?? $this->item_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 상태 라벨
|
||||
*/
|
||||
public function getStatusLabelAttribute(): string
|
||||
{
|
||||
return self::STATUSES[$this->status] ?? $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 경과일 계산 (가장 오래된 LOT 기준)
|
||||
*/
|
||||
public function getDaysElapsedAttribute(): int
|
||||
{
|
||||
if (! $this->oldest_lot_date) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->oldest_lot_date->diffInDays(now());
|
||||
}
|
||||
|
||||
/**
|
||||
* 재고 상태 계산
|
||||
*/
|
||||
public function calculateStatus(): string
|
||||
{
|
||||
if ($this->stock_qty <= 0) {
|
||||
return 'out';
|
||||
}
|
||||
|
||||
if ($this->stock_qty < $this->safety_stock) {
|
||||
return 'low';
|
||||
}
|
||||
|
||||
return 'normal';
|
||||
}
|
||||
|
||||
/**
|
||||
* 재고 정보 업데이트 (LOT 기반)
|
||||
*/
|
||||
public function refreshFromLots(): void
|
||||
{
|
||||
$lots = $this->lots()->where('status', '!=', 'used')->get();
|
||||
|
||||
$this->lot_count = $lots->count();
|
||||
$this->stock_qty = $lots->sum('qty');
|
||||
$this->reserved_qty = $lots->sum('reserved_qty');
|
||||
$this->available_qty = $lots->sum('available_qty');
|
||||
|
||||
$oldestLot = $lots->sortBy('receipt_date')->first();
|
||||
$this->oldest_lot_date = $oldestLot?->receipt_date;
|
||||
$this->last_receipt_date = $lots->max('receipt_date');
|
||||
|
||||
$this->status = $this->calculateStatus();
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user