diff --git a/app/Services/Equipment/ServerPartService.php b/app/Services/Equipment/ServerPartService.php index 40693c3..97cb4eb 100644 --- a/app/Services/Equipment/ServerPartService.php +++ b/app/Services/Equipment/ServerPartService.php @@ -185,10 +185,9 @@ class ServerPartService extends EquipmentService } //서버추가시 기본파트 자동추가용 - public function attachToServer(ServerEntity $serverEntity): void + public function initToServer(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"; diff --git a/app/Services/Equipment/ServerService.php b/app/Services/Equipment/ServerService.php index 359617b..6034409 100644 --- a/app/Services/Equipment/ServerService.php +++ b/app/Services/Equipment/ServerService.php @@ -114,21 +114,27 @@ class ServerService extends EquipmentService return $caculatedAmount; } + //서버관련 정보가 변경되었을때 호출(금액이나,서버파트정보가 변경되었을때) public function recalcAmount(ServerEntity $entity): void { - if ($entity->getServiceInfoUid()) { - $serviceEntity = service('customer_serviceservice')->getEntity($entity->getServiceInfoUid()); - if (!$serviceEntity instanceof ServiceEntity) { - throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getServiceInfoUid()} 서비스정보를 찾을수 없습니다."); - } - // ✅ 핵심 추가: 서비스금액 변경 및 payment Sync용 - if ($serviceEntity->getServerInfoUid() === $entity->getServiceInfoUid()) { - $oldServiceEntity = clone $serviceEntity; - $serviceEntity = service('customer_serviceservice')->recalcAmount($serviceEntity); - //결제비 설정 - service('paymentservice')->modifyByService($oldServiceEntity, $serviceEntity); - } + if (!$entity->getServiceInfoUid()) { + return; } + $serviceEntity = service('customer_serviceservice')->getEntity($entity->getServiceInfoUid()); + if (!$serviceEntity instanceof ServiceEntity) { + throw new RuntimeException( + static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getServiceInfoUid()} 서비스정보를 찾을수 없습니다." + ); + } + //해당 서비스의 메인서버가 아닌경우 변경없음 + if ($serviceEntity->getServerInfoUid() !== $entity->getPK()) { + return; + } + //서비스의 금액을 변경 + $oldServiceEntity = clone $serviceEntity; + $serviceEntity = service('customer_serviceservice')->recalcAmount($serviceEntity); + //서비스의 금액을 변경 적용 후 결제정보를 sync하기 위해 + service('paymentservice')->modifyByService($oldServiceEntity, $serviceEntity); } protected function create_process(array $formDatas): ServerEntity @@ -144,7 +150,7 @@ class ServerService extends EquipmentService service('part_switchservice')->attachToServer($entity); } service('equipment_chassisservice')->attachToServer($entity); - service('equipment_serverpartservice')->attachToServer($entity); + service('equipment_serverpartservice')->initToServer($entity); return $entity; } diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index dc143b4..3b8eee7 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -193,11 +193,13 @@ class PaymentService extends CommonService return $rows; } - //서비스관련 + // ------------------------------ + // 서비스정보 기준 월 결제 관련 + // ------------------------------ private function getFormDatasFromService(ServiceEntity $serviceEntity, array $formDatas = []): array { $formDatas['serviceinfo_uid'] = $serviceEntity->getPK(); - $formDatas["clientinfo_uid"] = $serviceEntity->getClientInfoUid(); + $formDatas['clientinfo_uid'] = $serviceEntity->getClientInfoUid(); $formDatas['amount'] = $serviceEntity->getAmount(); $formDatas['billing'] = $formDatas['billing'] ?? PAYMENT['BILLING']['MONTH']; $formDatas['billing_at'] = $serviceEntity->getBillingAt(); @@ -208,87 +210,166 @@ class PaymentService extends CommonService $formDatas['title'] ?? $serviceEntity->getTitle(), DateTime::createFromFormat('Y-m-d', $formDatas['billing_at'])->format('Y년 m월') ); + return $formDatas; } + private function getPaymentEntityByService(ServiceEntity $serviceEntity): ?PaymentEntity + { + $entity = $this->getEntity([ + 'serviceinfo_uid' => $serviceEntity->getPK(), + 'billing' => PAYMENT['BILLING']['MONTH'], + 'billing_at' => $serviceEntity->getBillingAt(), + ]); + + if ($entity === null) { + return null; + } + + if (!$entity instanceof PaymentEntity) { + throw new RuntimeException( + static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 PaymentEntity만 가능" + ); + } + + return $entity; + } + + /** + * 서비스 기준 결제정보 생성/수정 공통 처리 + * - 기존 결제정보가 있으면 수정 + * - 없으면 생성 + * - 조회는 존재 여부 기준 + * - status는 조회 후 수정 가능 여부 판단 + */ + private function saveProcessByService(ServiceEntity $targetServiceEntity, ServiceEntity $serviceEntity): PaymentEntity + { + $entity = $this->getPaymentEntityByService($targetServiceEntity); + $formDatas = $this->getFormDatasFromService($serviceEntity); + + if ($entity === null) { + return parent::create_process($formDatas); + } + + if ($entity->getStatus() !== STATUS['UNPAID']) { + throw new RuntimeException( + static::class . '->' . __FUNCTION__ . "에서 오류발생: 이미 생성된 결제정보가 있으며 수정 가능한 상태가 아닙니다." + ); + } + + return parent::modify_process($entity, $formDatas); + } + public function createByService(ServiceEntity $serviceEntity): PaymentEntity { - $formDatas = $this->getFormDatasFromService($serviceEntity); - return $this->create_process($formDatas); + return $this->saveProcessByService($serviceEntity, $serviceEntity); } - //서비스정보로 결제정보 생성 또는 수정 (일반 운영용: upsert 유지) + public function modifyByService(ServiceEntity $oldServiceEntity, ServiceEntity $serviceEntity): PaymentEntity { - $entity = $this->getEntity([ - 'serviceinfo_uid' => $oldServiceEntity->getPK(), - 'billing' => PAYMENT['BILLING']['MONTH'], - 'billing_at' => $oldServiceEntity->getBillingAt(), - 'status' => STATUS['UNPAID'] - ]); - if (!$entity instanceof PaymentEntity) { - throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServiceEntity만 가능"); - } - //매칭되는게 있으면(기존 서비스인경우) 아니면 신규등록 - $formDatas = $this->getFormDatasFromService($serviceEntity); - return $this->modify_process($entity, $formDatas); + return $this->saveProcessByService($oldServiceEntity, $serviceEntity); } - //서버파트별 일회성 관련 + // ------------------------------ + // 서버파트 기준 일회성 결제 관련 + // ------------------------------ private function getFormDatasFromServerPart(ServerPartEntity $serverPartEntity, array $formDatas = []): array { if ($serverPartEntity->getServiceInfoUid() === null) { - throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 서비스정보가 정의되지 않아 일회성 상품을 설정하실수 없습니다."); + throw new RuntimeException( + static::class . '->' . __FUNCTION__ . "에서 오류발생: 서비스정보가 정의되지 않아 일회성 상품을 설정하실수 없습니다." + ); } + $formDatas['serviceinfo_uid'] = $serverPartEntity->getServiceInfoUid(); - $formDatas["clientinfo_uid"] = $serverPartEntity->getClientInfoUid(); - $formDatas["serverpartinfo_uid"] = $serverPartEntity->getPK(); + $formDatas['clientinfo_uid'] = $serverPartEntity->getClientInfoUid(); + $formDatas['serverpartinfo_uid'] = $serverPartEntity->getPK(); $formDatas['amount'] = $serverPartEntity->getCalculatedAmount(); $formDatas['billing'] = $formDatas['billing'] ?? PAYMENT['BILLING']['ONETIME']; $formDatas['billing_at'] = $serverPartEntity->getBillingAt(); $formDatas['pay'] = $formDatas['pay'] ?? PAYMENT['PAY']['ACCOUNT']; $formDatas['status'] = $formDatas['status'] ?? STATUS['UNPAID']; - $formDatas['title'] = sprintf("%s 일회성비용", $formDatas['title'] ?? $serverPartEntity->getCustomTitle()); + $formDatas['title'] = sprintf( + "%s 일회성비용", + $formDatas['title'] ?? $serverPartEntity->getCustomTitle() + ); + return $formDatas; } + private function getPaymentEntityByServerPart(ServerPartEntity $serverPartEntity): ?PaymentEntity + { + $entity = $this->getEntity([ + 'serverpartinfo_uid' => $serverPartEntity->getPK(), + 'serviceinfo_uid' => $serverPartEntity->getServiceInfoUid(), + 'billing' => PAYMENT['BILLING']['ONETIME'], + 'billing_at' => $serverPartEntity->getBillingAt(), + ]); + + if ($entity === null) { + return null; + } + + if (!$entity instanceof PaymentEntity) { + throw new RuntimeException( + static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 PaymentEntity만 가능" + ); + } + + return $entity; + } + + /** + * 서버파트 기준 결제정보 생성/수정 공통 처리 + * - 기존 결제정보가 있으면 수정 + * - 없으면 생성 + * - 조회는 존재 여부 기준 + * - status는 조회 후 수정 가능 여부 판단 + */ + private function saveProcessByServerPart(ServerPartEntity $targetServerPartEntity, ServerPartEntity $serverPartEntity): PaymentEntity + { + $entity = $this->getPaymentEntityByServerPart($targetServerPartEntity); + $formDatas = $this->getFormDatasFromServerPart($serverPartEntity); + + if ($entity === null) { + return parent::create_process($formDatas); + } + + if ($entity->getStatus() !== STATUS['UNPAID']) { + throw new RuntimeException( + static::class . '->' . __FUNCTION__ . "에서 오류발생: 이미 생성된 서버파트 결제정보가 있으며 수정 가능한 상태가 아닙니다." + ); + } + + return parent::modify_process($entity, $formDatas); + } + public function createByServerPart(ServerPartEntity $serverPartEntity): PaymentEntity { - $formDatas = $this->getFormDatasFromServerPart($serverPartEntity); - return parent::create_process($formDatas); + return $this->saveProcessByServerPart($serverPartEntity, $serverPartEntity); } public function modifyByServerPart(ServerPartEntity $oldServerPartEntity, ServerPartEntity $serverPartEntity): PaymentEntity { - $entity = $this->getEntity([ - 'serverpartinfo_uid' => $oldServerPartEntity->getPK(), - 'serviceinfo_uid' => $oldServerPartEntity->getServiceInfoUid(), - 'billing' => $oldServerPartEntity->getBilling(), - 'billing_at' => $oldServerPartEntity->getBillingAt(), - 'status' => STATUS['UNPAID'] - ]); - - if (!$entity instanceof PaymentEntity) { - throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 기존 서버파트정보의 {$oldServerPartEntity->getTitle()}에 해당하는 결제정보가 존재하지 않습니다."); - } - - $formDatas = $this->getFormDatasFromServerPart($serverPartEntity); - return parent::modify_process($entity, $formDatas); + return $this->saveProcessByServerPart($oldServerPartEntity, $serverPartEntity); } - // ✅ 서비스 해지(서버 분리) 시: 월비용 파트정보는 고객만 연결 + /** + * 서비스 해지(서버 분리) 시: 월비용 파트정보는 고객만 연결 + */ public function terminateByServerPart(ServiceEntity $oldServiceEntity): void { - $entity = $this->getEntity([ - 'serviceinfo_uid' => $oldServiceEntity->getPK(), - 'billing' => PAYMENT['BILLING']['MONTH'], - 'billing_at' => $oldServiceEntity->getBillingAt(), - 'status' => STATUS['UNPAID'], - ]); + $entity = $this->getPaymentEntityByService($oldServiceEntity); + if (!$entity instanceof PaymentEntity) { return; } - //매칭되는게 있으면(기존 파트정보서비스인경우) + + if ($entity->getStatus() !== STATUS['UNPAID']) { + return; + } + parent::modify_process($entity, ['status' => STATUS['TERMINATED']]); } }