- TenantSetting CRUD API 추가 - Calendar, Entertainment, VAT 서비스 개선 - 5130 BOM 계산 로직 수정 - quote_items에 item_type 컬럼 추가 - tenant_settings 테이블 마이그레이션 - Swagger 문서 업데이트
206 lines
5.5 KiB
PHP
206 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Tenants\TenantSetting;
|
|
use Illuminate\Support\Collection;
|
|
|
|
/**
|
|
* 테넌트 설정 서비스
|
|
*
|
|
* 테넌트별 설정값 관리
|
|
* - 재고관리 품목유형
|
|
* - 안전재고 기본값
|
|
* - 알림 설정 등
|
|
*/
|
|
class TenantSettingService extends Service
|
|
{
|
|
/**
|
|
* 설정 그룹 상수
|
|
*/
|
|
public const GROUP_STOCK = 'stock';
|
|
|
|
public const GROUP_ORDER = 'order';
|
|
|
|
public const GROUP_PRODUCTION = 'production';
|
|
|
|
public const GROUP_NOTIFICATION = 'notification';
|
|
|
|
/**
|
|
* 설정 키 상수
|
|
*/
|
|
public const KEY_STOCK_ITEM_TYPES = 'stock_item_types';
|
|
|
|
public const KEY_DEFAULT_SAFETY_STOCK = 'default_safety_stock';
|
|
|
|
public const KEY_LOW_STOCK_ALERT = 'low_stock_alert';
|
|
|
|
/**
|
|
* 기본 설정값
|
|
*/
|
|
private array $defaults = [
|
|
self::GROUP_STOCK => [
|
|
self::KEY_STOCK_ITEM_TYPES => ['RM', 'SM', 'CS', 'PT', 'SF'],
|
|
self::KEY_DEFAULT_SAFETY_STOCK => 10,
|
|
self::KEY_LOW_STOCK_ALERT => true,
|
|
],
|
|
];
|
|
|
|
/**
|
|
* 특정 그룹의 모든 설정 조회
|
|
*/
|
|
public function getByGroup(string $group): Collection
|
|
{
|
|
return TenantSetting::where('tenant_id', $this->tenantId())
|
|
->group($group)
|
|
->get()
|
|
->mapWithKeys(function ($setting) {
|
|
return [$setting->setting_key => $setting->setting_value];
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 모든 설정 조회 (그룹별 구조화)
|
|
*/
|
|
public function getAll(): array
|
|
{
|
|
$settings = TenantSetting::where('tenant_id', $this->tenantId())
|
|
->get()
|
|
->groupBy('setting_group')
|
|
->map(function ($group) {
|
|
return $group->mapWithKeys(function ($setting) {
|
|
return [$setting->setting_key => [
|
|
'value' => $setting->setting_value,
|
|
'description' => $setting->description,
|
|
'updated_at' => $setting->updated_at?->toISOString(),
|
|
]];
|
|
});
|
|
})
|
|
->toArray();
|
|
|
|
return $settings;
|
|
}
|
|
|
|
/**
|
|
* 설정값 조회 (기본값 포함)
|
|
*/
|
|
public function get(string $group, string $key, $default = null)
|
|
{
|
|
$value = TenantSetting::getValue($this->tenantId(), $group, $key);
|
|
|
|
if ($value !== null) {
|
|
return $value;
|
|
}
|
|
|
|
// 기본값에서 찾기
|
|
if ($default === null && isset($this->defaults[$group][$key])) {
|
|
return $this->defaults[$group][$key];
|
|
}
|
|
|
|
return $default;
|
|
}
|
|
|
|
/**
|
|
* 설정값 저장
|
|
*/
|
|
public function set(string $group, string $key, $value, ?string $description = null): TenantSetting
|
|
{
|
|
return TenantSetting::setValue(
|
|
$this->tenantId(),
|
|
$group,
|
|
$key,
|
|
$value,
|
|
$description,
|
|
$this->apiUserId()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 여러 설정값 일괄 저장
|
|
*/
|
|
public function setMany(string $group, array $settings): array
|
|
{
|
|
$results = [];
|
|
|
|
foreach ($settings as $key => $data) {
|
|
$value = is_array($data) && isset($data['value']) ? $data['value'] : $data;
|
|
$description = is_array($data) && isset($data['description']) ? $data['description'] : null;
|
|
|
|
$results[$key] = $this->set($group, $key, $value, $description);
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* 설정 삭제
|
|
*/
|
|
public function delete(string $group, string $key): bool
|
|
{
|
|
return TenantSetting::where('tenant_id', $this->tenantId())
|
|
->where('setting_group', $group)
|
|
->where('setting_key', $key)
|
|
->delete() > 0;
|
|
}
|
|
|
|
/**
|
|
* 재고관리 품목유형 조회
|
|
*/
|
|
public function getStockItemTypes(): array
|
|
{
|
|
return $this->get(self::GROUP_STOCK, self::KEY_STOCK_ITEM_TYPES, ['RM', 'SM', 'CS', 'PT', 'SF']);
|
|
}
|
|
|
|
/**
|
|
* 기본 안전재고 조회
|
|
*/
|
|
public function getDefaultSafetyStock(): int
|
|
{
|
|
return (int) $this->get(self::GROUP_STOCK, self::KEY_DEFAULT_SAFETY_STOCK, 10);
|
|
}
|
|
|
|
/**
|
|
* 재고부족 알림 활성화 여부
|
|
*/
|
|
public function isLowStockAlertEnabled(): bool
|
|
{
|
|
return (bool) $this->get(self::GROUP_STOCK, self::KEY_LOW_STOCK_ALERT, true);
|
|
}
|
|
|
|
/**
|
|
* 기본 설정으로 초기화
|
|
*/
|
|
public function initializeDefaults(): array
|
|
{
|
|
$results = [];
|
|
|
|
foreach ($this->defaults as $group => $settings) {
|
|
foreach ($settings as $key => $value) {
|
|
// 기존 설정이 없을 때만 생성
|
|
$existing = TenantSetting::getValue($this->tenantId(), $group, $key);
|
|
if ($existing === null) {
|
|
$results["{$group}.{$key}"] = $this->set($group, $key, $value, $this->getDefaultDescription($group, $key));
|
|
}
|
|
}
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* 기본 설명 가져오기
|
|
*/
|
|
private function getDefaultDescription(string $group, string $key): string
|
|
{
|
|
$descriptions = [
|
|
self::GROUP_STOCK => [
|
|
self::KEY_STOCK_ITEM_TYPES => '재고관리 대상 품목유형 (FG 완제품 제외)',
|
|
self::KEY_DEFAULT_SAFETY_STOCK => '안전재고 기본값',
|
|
self::KEY_LOW_STOCK_ALERT => '재고부족 알림 활성화',
|
|
],
|
|
];
|
|
|
|
return $descriptions[$group][$key] ?? '';
|
|
}
|
|
}
|