317 lines
13 KiB
PHP
317 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Equipment;
|
|
|
|
use App\DTOs\Equipment\ServerPartDTO;
|
|
use App\Entities\CommonEntity;
|
|
use App\Entities\Customer\ServiceEntity;
|
|
use App\Entities\Equipment\ServerEntity;
|
|
use App\Entities\Equipment\ServerPartEntity;
|
|
use App\Entities\Part\PartEntity;
|
|
use App\Forms\Equipment\ServerPartForm;
|
|
use App\Helpers\Equipment\ServerPartHelper;
|
|
use App\Models\Equipment\ServerPartModel;
|
|
use App\Services\Part\PartService;
|
|
use RuntimeException;
|
|
|
|
class ServerPartService extends EquipmentService
|
|
{
|
|
protected string $formClass = ServerPartForm::class;
|
|
protected string $helperClass = ServerPartHelper::class;
|
|
|
|
public function __construct(ServerPartModel $model)
|
|
{
|
|
parent::__construct($model);
|
|
$this->addClassPaths('ServerPart');
|
|
}
|
|
|
|
public function getDTOClass(): string
|
|
{
|
|
return ServerPartDTO::class;
|
|
}
|
|
|
|
public function createDTO(array $formDatas): ServerPartDTO
|
|
{
|
|
return new ServerPartDTO($formDatas);
|
|
}
|
|
|
|
public function getEntityClass(): string
|
|
{
|
|
return ServerPartEntity::class;
|
|
}
|
|
|
|
private function getPartService(string $type): PartService
|
|
{
|
|
return service('part_' . strtolower($type) . 'service');
|
|
}
|
|
|
|
protected function getEntity_process(mixed $entity): ServerPartEntity
|
|
{
|
|
return $entity;
|
|
}
|
|
|
|
private function getBillingAtByServiceEntity(ServiceEntity $serviceEntity, string $billing): string
|
|
{
|
|
if ($billing === PAYMENT['BILLING']['MONTH']) {
|
|
return $serviceEntity->getBillingAt();
|
|
} elseif ($billing === PAYMENT['BILLING']['ONETIME']) {
|
|
return date("Y-m-d");
|
|
}
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$billing}에 해당하는 청구방식을 찾을수 없습니다.");
|
|
}
|
|
|
|
private function setPartTitleByPartEntity(array $formDatas): string
|
|
{
|
|
$partEntity = $this->getPartService($formDatas['type'])->getEntity($formDatas['part_uid']);
|
|
if (!$partEntity instanceof PartEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['part_uid']}에 해당하는 파트정보을 찾을수 없습니다.");
|
|
}
|
|
return $partEntity->getTitle();
|
|
}
|
|
|
|
private function getFormDatasForServerPart(array $formDatas, ServerEntity $serverEntity): array
|
|
{
|
|
if (empty($formDatas['serverinfo_uid'])) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 서버번호가 지정되지 않았습니다.");
|
|
}
|
|
if (empty($formDatas['type'])) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 부품형식이 지정되지 않았습니다.");
|
|
}
|
|
if (empty($formDatas['billing'])) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 청구방법이 지정되지 않았습니다.");
|
|
}
|
|
if (empty($formDatas['part_uid'])) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 파트번호가 지정되지 않았습니다.");
|
|
}
|
|
|
|
$formDatas['title'] = $this->setPartTitleByPartEntity($formDatas);
|
|
|
|
$serviceEntity = null;
|
|
if ($serverEntity->getServiceInfoUid()) {
|
|
$serviceEntity = service('customer_serviceservice')->getEntity($serverEntity->getServiceInfoUid());
|
|
}
|
|
|
|
$formDatas['billing_at'] = null;
|
|
if ($serviceEntity instanceof ServiceEntity) {
|
|
$formDatas['billing_at'] = $this->getBillingAtByServiceEntity($serviceEntity, $formDatas['billing']);
|
|
}
|
|
|
|
$formDatas['clientinfo_uid'] = $serverEntity->getClientInfoUid();
|
|
$formDatas['serviceinfo_uid'] = $serverEntity->getServiceInfoUid();
|
|
|
|
return $formDatas;
|
|
}
|
|
|
|
/**
|
|
* ✅ MONTH 파트 변경 시:
|
|
* - "서비스가 유지중"이면: amount 재계산 + 월 Payment upsert (internal)
|
|
* - "서버가 서비스에서 분리된 상태(server.serviceinfo_uid==null)"이면: amount 재계산만 + 월 Payment는 TERMINATED 처리
|
|
*/
|
|
private function syncMonthlyServiceAndPayment(?int $serviceUid, ?ServiceEntity $oldServiceSnapshot = null, bool $serverDetached = false): void
|
|
{
|
|
if (!$serviceUid)
|
|
return;
|
|
|
|
$svcService = service('customer_serviceservice');
|
|
|
|
$current = $svcService->getEntity($serviceUid);
|
|
if (!$current instanceof ServiceEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$serviceUid}에 해당하는 서비스정보을 찾을수 없습니다.");
|
|
}
|
|
|
|
$old = $oldServiceSnapshot instanceof ServiceEntity ? $oldServiceSnapshot : clone $current;
|
|
|
|
if ($serverDetached) {
|
|
// ✅ 해지 케이스: payment upsert 금지(0원화 방지), 월 미납은 TERMINATED
|
|
$svcService->recalcAmountInternal($current);
|
|
service('paymentservice')->terminateUnpaidMonthlyByService($old);
|
|
return;
|
|
}
|
|
|
|
// ✅ 일반 케이스: amount + payment upsert
|
|
$svcService->recalcAmountAndSyncPaymentInternal($old, $current);
|
|
}
|
|
|
|
protected function create_process(array $formDatas): CommonEntity
|
|
{
|
|
$serverEntity = service('equipment_serverservice')->getEntity($formDatas['serverinfo_uid']);
|
|
if (!$serverEntity instanceof ServerEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['serverinfo_uid']}에 해당하는 서버정보을 찾을수 없습니다.");
|
|
}
|
|
|
|
$formDatas = $this->getFormDatasForServerPart($formDatas, $serverEntity);
|
|
|
|
$entity = parent::create_process($formDatas);
|
|
if (!$entity instanceof ServerPartEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: Return Type은 ServerPartEntity만 가능");
|
|
}
|
|
|
|
$this->getPartService($entity->getType())->attachToServerPart($entity);
|
|
|
|
if ($entity->getServiceInfoUid()) {
|
|
if ($entity->getBilling() == PAYMENT['BILLING']['MONTH']) {
|
|
$this->syncMonthlyServiceAndPayment($entity->getServiceInfoUid(), null, false);
|
|
}
|
|
|
|
if ($entity->getBilling() == PAYMENT['BILLING']['ONETIME']) {
|
|
service('paymentservice')->createByServerPart($entity);
|
|
}
|
|
}
|
|
|
|
return $entity;
|
|
}
|
|
|
|
protected function modify_process($entity, array $formDatas): CommonEntity
|
|
{
|
|
$serverEntity = service('equipment_serverservice')->getEntity($formDatas['serverinfo_uid']);
|
|
if (!$serverEntity instanceof ServerEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['serverinfo_uid']}에 해당하는 서버정보을 찾을수 없습니다.");
|
|
}
|
|
|
|
$formDatas = $this->getFormDatasForServerPart($formDatas, $serverEntity);
|
|
|
|
$oldEntity = clone $entity;
|
|
|
|
$entity = parent::modify_process($entity, $formDatas);
|
|
if (!$entity instanceof ServerPartEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: Return Type은 ServerPartEntity만 가능");
|
|
}
|
|
|
|
$this->getPartService($entity->getType())->modifyServerPart($oldEntity, $entity);
|
|
|
|
$serviceUidsToSync = [];
|
|
|
|
if ($oldEntity->getServiceInfoUid() && $oldEntity->getBilling() == PAYMENT['BILLING']['MONTH']) {
|
|
$serviceUidsToSync[(int) $oldEntity->getServiceInfoUid()] = true;
|
|
}
|
|
if ($entity->getServiceInfoUid() && $entity->getBilling() == PAYMENT['BILLING']['MONTH']) {
|
|
$serviceUidsToSync[(int) $entity->getServiceInfoUid()] = true;
|
|
}
|
|
|
|
foreach (array_keys($serviceUidsToSync) as $svcUid) {
|
|
$this->syncMonthlyServiceAndPayment((int) $svcUid, null, false);
|
|
}
|
|
|
|
if ($entity->getBilling() == PAYMENT['BILLING']['ONETIME']) {
|
|
service('paymentservice')->modifyByServerPart($oldEntity, $entity);
|
|
}
|
|
|
|
return $entity;
|
|
}
|
|
|
|
protected function delete_process($entity): CommonEntity
|
|
{
|
|
if (!$entity instanceof ServerPartEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: ServerPartEntity만 가능");
|
|
}
|
|
|
|
$old = clone $entity;
|
|
|
|
$this->getPartService($entity->getType())->detachFromServerPart($entity);
|
|
|
|
$entity = parent::delete_process($entity);
|
|
if (!$entity instanceof ServerPartEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: Return Type은 ServerPartEntity만 가능");
|
|
}
|
|
|
|
if ($old->getServiceInfoUid() !== null && $old->getBilling() == PAYMENT['BILLING']['MONTH']) {
|
|
$this->syncMonthlyServiceAndPayment($old->getServiceInfoUid(), null, false);
|
|
}
|
|
|
|
// ✅ ONETIME 미납 결제 삭제는 "보류" (여기서는 아무것도 안함)
|
|
return $entity;
|
|
}
|
|
|
|
//서버추가시 기본파트 자동추가용
|
|
public function attachToServer(ServerEntity $serverEntity): void
|
|
{
|
|
$chassisEntity = service("equipment_chassisservice")->getEntity($serverEntity->getChassisInfoUid());
|
|
|
|
foreach (SERVERPART['SERVER_PARTTYPES'] as $parttype) {
|
|
$uid_function = "get{$parttype}InfoUid";
|
|
$cnt_function = "get{$parttype}Cnt";
|
|
|
|
$uid = $chassisEntity->$uid_function();
|
|
$cnt = $chassisEntity->$cnt_function();
|
|
if ($uid === null)
|
|
continue;
|
|
|
|
$partEntity = $this->getPartService($parttype)->getEntity($uid);
|
|
if (!$partEntity instanceof PartEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 {$parttype} 파트정보를 찾을수 없습니다.");
|
|
}
|
|
|
|
$formDatas = [];
|
|
$formDatas['serverinfo_uid'] = $serverEntity->getPK();
|
|
$formDatas["part_uid"] = $partEntity->getPK();
|
|
$formDatas['billing'] = PAYMENT['BILLING']['BASE'];
|
|
$formDatas['type'] = $parttype;
|
|
$formDatas['title'] = $partEntity->getTitle();
|
|
$formDatas['amount'] = $partEntity->getPrice();
|
|
$formDatas['cnt'] = $cnt;
|
|
|
|
$this->create_process($formDatas);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* ✅ 서버 해지/분리 시 서버파트 회수 처리
|
|
* - MONTH 파트 삭제되면:
|
|
* - server.serviceinfo_uid == null (분리 완료) 인 경우에만:
|
|
* - 월 미납 payment는 삭제/0원수정 금지, status=TERMINATED
|
|
* - service.amount는 재계산만(결제 upsert 금지)
|
|
* - ONETIME 미납 삭제는 보류
|
|
*
|
|
* @param ServiceEntity|null $oldServiceEntity 분리 전 서비스 스냅샷(필수 권장)
|
|
*/
|
|
public function detachFromServer(ServerEntity $serverEntity, ?ServiceEntity $oldServiceEntity = null): void
|
|
{
|
|
// 서버가 분리되기 전 serviceinfo_uid를 잃을 수 있으므로, oldServiceEntity로 serviceUid 확보
|
|
$serviceUid = $oldServiceEntity instanceof ServiceEntity ? $oldServiceEntity->getPK() : $serverEntity->getServiceInfoUid();
|
|
|
|
$monthlyChanged = false;
|
|
|
|
foreach ($this->getEntities([
|
|
'serverinfo_uid' => $serverEntity->getPK(),
|
|
"billing !=" => PAYMENT['BILLING']['BASE']
|
|
]) as $entity) {
|
|
|
|
if (!$entity instanceof ServerPartEntity)
|
|
continue;
|
|
|
|
$this->getPartService($entity->getType())->detachFromServerPart($entity);
|
|
|
|
if ($entity->getBilling() == PAYMENT['BILLING']['MONTH']) {
|
|
$monthlyChanged = true;
|
|
}
|
|
|
|
// ✅ 일괄 삭제(추가 동기화는 아래에서 한 번만)
|
|
parent::delete_process($entity);
|
|
}
|
|
|
|
if (!$monthlyChanged || !$serviceUid) {
|
|
return;
|
|
}
|
|
|
|
// ✅ "서버가 서비스에서 분리된 경우"에만 payment TERMINATED 처리
|
|
$serverDetached = ($serverEntity->getServiceInfoUid() === null);
|
|
|
|
if ($serverDetached) {
|
|
// oldServiceEntity가 없으면(호환 호출), 여기서라도 스냅샷 시도(가능하면)
|
|
if (!$oldServiceEntity instanceof ServiceEntity) {
|
|
$svc = service('customer_serviceservice')->getEntity($serviceUid);
|
|
if ($svc instanceof ServiceEntity) {
|
|
$oldServiceEntity = clone $svc;
|
|
}
|
|
}
|
|
if ($oldServiceEntity instanceof ServiceEntity) {
|
|
$this->syncMonthlyServiceAndPayment($serviceUid, $oldServiceEntity, true);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// ✅ 서비스 유지중인 경우: amount + payment upsert
|
|
$this->syncMonthlyServiceAndPayment($serviceUid, null, false);
|
|
}
|
|
}
|