Files
sam-manage/database/seeders/DesignPriceSeeder.php
2026-02-25 11:45:01 +09:00

113 lines
3.7 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Price;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
/**
* 시뮬레이터용 단가 시더
*
* Design 시뮬레이터와 동일한 단가를 prices 테이블에 추가합니다.
* items.attributes.salesPrice의 값을 prices 테이블로 이전합니다.
*
* 사용법:
* php artisan db:seed --class=DesignPriceSeeder
*/
class DesignPriceSeeder extends Seeder
{
protected int $tenantId = 287;
public function run(): void
{
$this->command->info('시뮬레이터 단가 시딩 시작...');
DB::transaction(function () {
$this->seedPricesFromItems();
});
$count = Price::where('tenant_id', $this->tenantId)
->where('status', Price::STATUS_ACTIVE)
->count();
$this->command->info("단가 시딩 완료! (총 {$count}개 active)");
}
protected function seedPricesFromItems(): void
{
// 시뮬레이터용 품목 코드 패턴
$patterns = [
'SF-SCR-%', // 스크린 반제품
'SF-STL-%', // 철재 반제품
'SF-BND-%', // 절곡 반제품
'SM-%', // 부자재
'RM-%', // 원자재
];
$query = DB::table('items')
->where('tenant_id', $this->tenantId)
->whereNull('deleted_at')
->where(function ($q) use ($patterns) {
foreach ($patterns as $pattern) {
$q->orWhere('code', 'like', $pattern);
}
});
$items = $query->get();
$created = 0;
$skipped = 0;
foreach ($items as $item) {
$attributes = json_decode($item->attributes, true) ?? [];
$salesPrice = $attributes['salesPrice'] ?? 0;
if ($salesPrice <= 0) {
$skipped++;
continue;
}
// 기존 active 단가 확인
$existingPrice = Price::where('tenant_id', $this->tenantId)
->where('item_id', $item->id)
->where('item_type_code', $item->item_type)
->where('status', Price::STATUS_ACTIVE)
->first();
if ($existingPrice) {
// 기존 단가 업데이트
$existingPrice->update([
'sales_price' => $salesPrice,
'updated_by' => 1,
]);
$this->command->line(" 업데이트: {$item->code} => ".number_format($salesPrice));
} else {
// 새 단가 생성
Price::create([
'tenant_id' => $this->tenantId,
'item_type_code' => $item->item_type,
'item_id' => $item->id,
'client_group_id' => null,
'purchase_price' => $salesPrice * 0.7, // 매입가 추정 (70%)
'processing_cost' => 0,
'loss_rate' => 0,
'margin_rate' => 30, // 마진율 30%
'sales_price' => $salesPrice,
'rounding_rule' => Price::ROUNDING_ROUND,
'rounding_unit' => 100,
'effective_from' => now()->toDateString(),
'effective_to' => null,
'note' => 'Design 시뮬레이터 연동 데이터',
'status' => Price::STATUS_ACTIVE,
'is_final' => false,
'created_by' => 1,
]);
$created++;
$this->command->line(" 생성: {$item->code} => ".number_format($salesPrice));
}
}
$this->command->info(" 생성: {$created}개, 스킵(단가없음): {$skipped}");
}
}