dbmsv4 init...5

This commit is contained in:
최준흠 2026-02-04 15:32:47 +09:00
parent e7d439b1cf
commit fbc62329ae
5 changed files with 342 additions and 296 deletions

View File

@ -17,12 +17,12 @@ return [
'deleted_at' => "삭제일", 'deleted_at' => "삭제일",
'countdown' => "납부기한/완료", 'countdown' => "납부기한/완료",
], ],
"BILLING" => [ "BILLING" => [
PAYMENT['BILLING']['MONTH'] => "매월", PAYMENT['BILLING']['MONTH'] => "매월",
PAYMENT['BILLING']['ONETIME'] => "일회성", PAYMENT['BILLING']['ONETIME'] => "일회성",
PAYMENT['BILLING']['PREPAYMENT'] => "선결제", PAYMENT['BILLING']['PREPAYMENT'] => "선결제",
], ],
"PAY" => [ "PAY" => [
PAYMENT['PAY']['ACCOUNT'] => "예치금", PAYMENT['PAY']['ACCOUNT'] => "예치금",
PAYMENT['PAY']['COUPON'] => "쿠폰", PAYMENT['PAY']['COUPON'] => "쿠폰",
PAYMENT['PAY']['POINT'] => "포인트", PAYMENT['PAY']['POINT'] => "포인트",
@ -30,5 +30,6 @@ return [
"STATUS" => [ "STATUS" => [
STATUS['UNPAID'] => "미지급", STATUS['UNPAID'] => "미지급",
STATUS['PAID'] => "지급완료", STATUS['PAID'] => "지급완료",
STATUS['TERMINATED'] => "해지",
], ],
]; ];

View File

@ -21,106 +21,124 @@ class ServiceService extends CustomerService
parent::__construct($model); parent::__construct($model);
$this->addClassPaths('Service'); $this->addClassPaths('Service');
} }
public function getDTOClass(): string public function getDTOClass(): string
{ {
return ServiceDTO::class; return ServiceDTO::class;
} }
public function createDTO(array $formDatas): ServiceDTO public function createDTO(array $formDatas): ServiceDTO
{ {
return new ServiceDTO($formDatas); return new ServiceDTO($formDatas);
} }
public function getEntityClass(): string public function getEntityClass(): string
{ {
return ServiceEntity::class; return ServiceEntity::class;
} }
//추가 기능
//interval을 기준으로 최근 신규 서비스정보 가져오기
final public function getNewServiceEntities(int $interval, string $status = STATUS['AVAILABLE']): array final public function getNewServiceEntities(int $interval, string $status = STATUS['AVAILABLE']): array
{ {
return $this->getEntities(["start_at >= NOW()-INTERVAL {$interval} DAY" => null, "status" => $status]); return $this->getEntities(["start_at >= NOW()-INTERVAL {$interval} DAY" => null, "status" => $status]);
} }
//서비스별 총 금액
final public function getTotalAmounts($where = []): array final public function getTotalAmounts($where = []): array
{ {
$rows = $this->model->groupBy('clientinfo_uid')->select("clientinfo_uid,SUM(amount) AS amount") $rows = $this->model->groupBy('clientinfo_uid')->select("clientinfo_uid,SUM(amount) AS amount")
->where($where) ->where($where)
->get()->getResult(); ->get()->getResult();
$amounts = []; $amounts = [];
foreach ($rows as $row) { foreach ($rows as $row) {
$amounts[$row->clientinfo_uid] = $row->amount; $amounts[$row->clientinfo_uid] = $row->amount;
} }
return $amounts; return $amounts;
} }
//다음 달로 결제일 가져오기.(CLI용)
final public function getNextMonthDate(ServiceEntity $entity): string final public function getNextMonthDate(ServiceEntity $entity): string
{ {
// $sql = "UPDATE serviceinfo SET billing_at =
// IF(DAY(billing_at) > DAY(LAST_DAY(billing_at)),
// LAST_DAY(DATE_ADD(billing_at, INTERVAL 1 MONTH)),
// DATE_ADD(billing_at, INTERVAL 1 MONTH)
// ) WHERE uid = ?";
// return $this->model->query($sql, [$entity->getPK()]);
// 입력된 날짜를 DateTime 객체로 변환
$date = new DateTimeImmutable($entity->getBillingAt(), new DateTimeZone('Asia/Tokyo')); $date = new DateTimeImmutable($entity->getBillingAt(), new DateTimeZone('Asia/Tokyo'));
// 현재 일(day)을 저장
$day = (int) $date->format('d'); $day = (int) $date->format('d');
// 다음달로 이동 (DateInterval 사용)
$date->modify('first day of next month'); $date = $date->modify('first day of next month');
// 다음달의 마지막 날 계산
$lastDayOfNextMonth = (int) $date->format('t'); $lastDayOfNextMonth = (int) $date->format('t');
// 현재 날짜가 다음달의 마지막 날보다 크면 -> 마지막 날로 설정
if ($day > $lastDayOfNextMonth) { if ($day > $lastDayOfNextMonth) {
$day = $lastDayOfNextMonth; $day = $lastDayOfNextMonth;
} }
// 일(day)을 설정
$date->setDate((int) $date->format('Y'), (int) $date->format('m'), $day); $date = $date->setDate((int) $date->format('Y'), (int) $date->format('m'), $day);
// 최종 결과 리턴 (YYYY-MM-DD)
return $date->format('Y-m-d'); return $date->format('Y-m-d');
} }
// 서비스금액관련처리
private function getCalculatedAmount(int $rack_price, int $line_price, int $sale_price, int $serverinfo_uid): int private function getCalculatedAmount(int $rack_price, int $line_price, int $sale_price, int $serverinfo_uid): int
{ {
//총 서비스금액 구하기
$server_amount = service('equipment_serverservice')->getCalculatedAmount($serverinfo_uid); $server_amount = service('equipment_serverservice')->getCalculatedAmount($serverinfo_uid);
return (int) $server_amount + $rack_price + $line_price - $sale_price; return (int) $server_amount + $rack_price + $line_price - $sale_price;
} }
final public function updateAmount(ServiceEntity $entity): ServiceEntity
final protected function updateAmount(ServiceEntity $entity): ServiceEntity
{ {
//기본:서버금액(서버비+서버파트(월비용))+상면비+회선비-할인액
$entity->amount = $this->getCalculatedAmount( $entity->amount = $this->getCalculatedAmount(
$entity->getRack(), $entity->getRack(),
$entity->getLine(), $entity->getLine(),
$entity->getSale(), $entity->getSale(),
$entity->getServerInfoUid() $entity->getServerInfoUid()
); );
//총 서비스금액 설정 및 저장
if (!$this->model->save($entity)) { if (!$this->model->save($entity)) {
// 저장 실패 시 예외 처리
$errors = $this->model->errors(); $errors = $this->model->errors();
throw new RuntimeException("금액 업데이트 중 DB 저장 오류: " . implode(', ', $errors)); throw new RuntimeException("금액 업데이트 중 DB 저장 오류: " . implode(', ', $errors));
} }
return $entity; return $entity;
} }
// 서비스금액관련처리
final public function updateBillingAt($uid, string $billing_at): ServiceEntity final public function updateBillingAt($uid, string $billing_at): ServiceEntity
{ {
$entity = $this->getEntity($uid); $entity = $this->getEntity($uid);
if (!$entity instanceof ServiceEntity) { if (!$entity instanceof ServiceEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:{$uid}에 해당하는 서비스정보를 찾을 수 업습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:{$uid}에 해당하는 서비스정보를 찾을 수 업습니다.");
} }
$entity->billing_at = $billing_at; $entity->billing_at = $billing_at;
if (!$this->model->save($entity)) { if (!$this->model->save($entity)) {
// 저장 실패 시 예외 처리
$errors = $this->model->errors(); $errors = $this->model->errors();
throw new RuntimeException("금액 업데이트 중 DB 저장 오류: " . implode(', ', $errors)); throw new RuntimeException("금액 업데이트 중 DB 저장 오류: " . implode(', ', $errors));
} }
return $entity; return $entity;
} }
//기본 기능부분
protected function getEntity_process(mixed $entity): ServiceEntity protected function getEntity_process(mixed $entity): ServiceEntity
{ {
return $entity; return $entity;
} }
// ✅ (internal) payment까지 동기화하는 일반용 로직 (트랜잭션 없음)
final public function recalcAmountAndSyncPaymentInternal(ServiceEntity $old, ServiceEntity $current): ServiceEntity
{
$current = $this->updateAmount($current);
service('paymentservice')->upsertByService($old, $current);
return $current;
}
// ✅ (internal) amount만 재계산 (해지 시 0원될 수 있으나 payment 건드리지 않음)
final public function recalcAmountInternal(ServiceEntity $current): ServiceEntity
{
return $this->updateAmount($current);
}
// ✅ (public) dbTransaction 보류: 내부 로직만 호출
final public function recalcAmountAndSyncPayment(ServiceEntity $serviceEntity): ServiceEntity
{
$old = clone $serviceEntity;
return $this->recalcAmountAndSyncPaymentInternal($old, $serviceEntity);
}
final public function recalcAmountAndSyncPaymentByOld(ServiceEntity $old, ServiceEntity $current): ServiceEntity
{
return $this->recalcAmountAndSyncPaymentInternal($old, $current);
}
protected function create_process(array $formDatas): ServiceEntity protected function create_process(array $formDatas): ServiceEntity
{ {
if (empty($formDatas['site'])) { if (empty($formDatas['site'])) {
@ -129,22 +147,20 @@ class ServiceService extends CustomerService
if (empty($formDatas['serverinfo_uid'])) { if (empty($formDatas['serverinfo_uid'])) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 서버가 지정되지 않았습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 서버가 지정되지 않았습니다.");
} }
//생성폼에는 없는 필수항목 지정용(code)
$formDatas['code'] = $formDatas['site'] . "_s" . uniqid(); $formDatas['code'] = $formDatas['site'] . "_s" . uniqid();
$formDatas['amount'] = 0; //임시정의 후 Update $formDatas['amount'] = 0;
//서비스 생성
$entity = parent::create_process($formDatas); $entity = parent::create_process($formDatas);
if (!$entity instanceof ServiceEntity) { if (!$entity instanceof ServiceEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServiceEntity만 가능"); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServiceEntity만 가능");
} }
//서버정보 연결
service('equipment_serverservice')->attatchToService($entity, $entity->getServerInfoUid()); service('equipment_serverservice')->attatchToService($entity, $entity->getServerInfoUid());
//서비스비용 설정
$entity = $this->updateAmount($entity); return $this->recalcAmountAndSyncPayment($entity);
//결제정보 생성
service('paymentservice')->createByService($entity);
return $entity;
} }
protected function modify_process($entity, array $formDatas): ServiceEntity protected function modify_process($entity, array $formDatas): ServiceEntity
{ {
if (empty($formDatas['site'])) { if (empty($formDatas['site'])) {
@ -153,27 +169,21 @@ class ServiceService extends CustomerService
if (empty($formDatas['serverinfo_uid'])) { if (empty($formDatas['serverinfo_uid'])) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 서버가 지정되지 않았습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 서버가 지정되지 않았습니다.");
} }
//변경전 정보
$oldEntity = clone $entity; $oldEntity = clone $entity;
//수정폼에는 없는 필수항목 지정용(code)
$formDatas['code'] = $entity->getCode(); $formDatas['code'] = $entity->getCode();
$formDatas['amount'] = 0; //임시정의 후 Update $formDatas['amount'] = 0;
//서비스 수정
$entity = parent::modify_process($entity, $formDatas); $entity = parent::modify_process($entity, $formDatas);
if (!$entity instanceof ServiceEntity) { if (!$entity instanceof ServiceEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServiceEntity만 가능"); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServiceEntity만 가능");
} }
//서버정보 연결 신규서버로 변경
if ($oldEntity->getServerInfoUid() !== $entity->getServerInfoUid()) { if ($oldEntity->getServerInfoUid() !== $entity->getServerInfoUid()) {
service('equipment_serverservice')->modifyByService($oldEntity, $entity); service('equipment_serverservice')->modifyByService($oldEntity, $entity);
} }
//서비스비용 설정
$entity = $this->updateAmount($entity); return $this->recalcAmountAndSyncPaymentByOld($oldEntity, $entity);
//기존 서비스용 결제비용 과 신규 서버스용으로 결제비용이 다르면 수정
service('paymentservice')->modifyByService($oldEntity, $entity);
return $entity;
} }
//List 검색용
//FormFilter 조건절 처리
//검색어조건절처리
} }

View File

@ -24,41 +24,42 @@ class ServerPartService extends EquipmentService
parent::__construct($model); parent::__construct($model);
$this->addClassPaths('ServerPart'); $this->addClassPaths('ServerPart');
} }
public function getDTOClass(): string public function getDTOClass(): string
{ {
return ServerPartDTO::class; return ServerPartDTO::class;
} }
public function createDTO(array $formDatas): ServerPartDTO public function createDTO(array $formDatas): ServerPartDTO
{ {
return new ServerPartDTO($formDatas); return new ServerPartDTO($formDatas);
} }
public function getEntityClass(): string public function getEntityClass(): string
{ {
return ServerPartEntity::class; return ServerPartEntity::class;
} }
//각 파트별 서비스
private function getPartService(string $type): PartService private function getPartService(string $type): PartService
{ {
return service('part_' . strtolower($type) . 'service'); return service('part_' . strtolower($type) . 'service');
} }
//기본 기능부분
protected function getEntity_process(mixed $entity): ServerPartEntity protected function getEntity_process(mixed $entity): ServerPartEntity
{ {
return $entity; return $entity;
} }
private function getBillingAtByServiceEntity(ServiceEntity $serviceEntity, string $billing): string private function getBillingAtByServiceEntity(ServiceEntity $serviceEntity, string $billing): string
{ {
//청구방식에 따른 결제일 설정
$billing_at = null;
if ($billing === PAYMENT['BILLING']['MONTH']) { if ($billing === PAYMENT['BILLING']['MONTH']) {
$billing_at = $serviceEntity->getBillingAt(); return $serviceEntity->getBillingAt();
} else if ($billing === PAYMENT['BILLING']['ONETIME']) { } elseif ($billing === PAYMENT['BILLING']['ONETIME']) {
$billing_at = date("Y-m-d"); return date("Y-m-d");
} else {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$billing}에 해당하는 청구방식을 찾을수 없습니다.");
} }
return $billing_at; throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$billing}에 해당하는 청구방식을 찾을수 없습니다.");
} }
private function setPartTitleByPartEntity(array $formDatas): string private function setPartTitleByPartEntity(array $formDatas): string
{ {
$partEntity = $this->getPartService($formDatas['type'])->getEntity($formDatas['part_uid']); $partEntity = $this->getPartService($formDatas['type'])->getEntity($formDatas['part_uid']);
@ -67,6 +68,7 @@ class ServerPartService extends EquipmentService
} }
return $partEntity->getTitle(); return $partEntity->getTitle();
} }
private function getFormDatasForServerPart(array $formDatas, ServerEntity $serverEntity): array private function getFormDatasForServerPart(array $formDatas, ServerEntity $serverEntity): array
{ {
if (empty($formDatas['serverinfo_uid'])) { if (empty($formDatas['serverinfo_uid'])) {
@ -78,153 +80,237 @@ class ServerPartService extends EquipmentService
if (empty($formDatas['billing'])) { if (empty($formDatas['billing'])) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 청구방법이 지정되지 않았습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 청구방법이 지정되지 않았습니다.");
} }
// 키가 없거나(Undefined), null, '', 0, false 중 하나라도 해당되면 실행
if (empty($formDatas['part_uid'])) { if (empty($formDatas['part_uid'])) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 파트번호가 지정되지 않았습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 파트번호가 지정되지 않았습니다.");
} }
//해당 파트정보의 Title을 파트정보의 Title로 설정
$formDatas['title'] = $this->setPartTitleByPartEntity($formDatas); $formDatas['title'] = $this->setPartTitleByPartEntity($formDatas);
//청구일 설정
//서비스정보 가져오기
$serviceEntity = null; $serviceEntity = null;
if ($serverEntity->getServiceInfoUid()) { if ($serverEntity->getServiceInfoUid()) {
$serviceEntity = service('customer_serviceservice')->getEntity($serverEntity->getServiceInfoUid()); $serviceEntity = service('customer_serviceservice')->getEntity($serverEntity->getServiceInfoUid());
} }
$formDatas['billing_at'] = null; $formDatas['billing_at'] = null;
if ($serviceEntity instanceof ServiceEntity) { if ($serviceEntity instanceof ServiceEntity) {
$formDatas['billing_at'] = $this->getBillingAtByServiceEntity($serviceEntity, $formDatas['billing']); $formDatas['billing_at'] = $this->getBillingAtByServiceEntity($serviceEntity, $formDatas['billing']);
} }
//해당 파트정보에 고객번호,서비스번호 설정
$formDatas['clientinfo_uid'] = $serverEntity->getClientInfoUid(); $formDatas['clientinfo_uid'] = $serverEntity->getClientInfoUid();
$formDatas['serviceinfo_uid'] = $serverEntity->getServiceInfoUid(); $formDatas['serviceinfo_uid'] = $serverEntity->getServiceInfoUid();
return $formDatas; 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 protected function create_process(array $formDatas): CommonEntity
{ {
//서버정보 가져오기
$serverEntity = service('equipment_serverservice')->getEntity($formDatas['serverinfo_uid']); $serverEntity = service('equipment_serverservice')->getEntity($formDatas['serverinfo_uid']);
if (!$serverEntity instanceof ServerEntity) { if (!$serverEntity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['serverinfo_uid']}에 해당하는 서버정보을 찾을수 없습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['serverinfo_uid']}에 해당하는 서버정보을 찾을수 없습니다.");
} }
//생성을 위한 추가 FormDatas설정
$formDatas = $this->getFormDatasForServerPart($formDatas, $serverEntity); $formDatas = $this->getFormDatasForServerPart($formDatas, $serverEntity);
//서버파트 생성
$entity = parent::create_process($formDatas); $entity = parent::create_process($formDatas);
if (!$entity instanceof ServerPartEntity) { if (!$entity instanceof ServerPartEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServerPartEntity만 가능"); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: Return Type은 ServerPartEntity만 가능");
} }
//해당 파트정보를 서버파트정보에 추가
$this->getPartService($entity->getType())->attachToServerPart($entity); $this->getPartService($entity->getType())->attachToServerPart($entity);
//서비스가 정의 되어 있으면
if ($entity->getServiceInfoUid()) { if ($entity->getServiceInfoUid()) {
$serviceEntity = service('customer_serviceservice')->getEntity($entity->getServiceInfoUid());
if (!$serviceEntity instanceof ServiceEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getServiceInfoUid()}에 해당하는 서비스정보을 찾을수 없습니다.");
}
//Billing형식이 Month이면 서비스 금액설정 호출
if ($entity->getBilling() == PAYMENT['BILLING']['MONTH']) { if ($entity->getBilling() == PAYMENT['BILLING']['MONTH']) {
service('customer_serviceservice')->updateAmount($serviceEntity); $this->syncMonthlyServiceAndPayment($entity->getServiceInfoUid(), null, false);
} }
//Billing형식이 Onetime이면 일회성결제 생성
if ($entity->getBilling() == PAYMENT['BILLING']['ONETIME']) { if ($entity->getBilling() == PAYMENT['BILLING']['ONETIME']) {
service('paymentservice')->createByServerPart($entity); service('paymentservice')->createByServerPart($entity);
} }
} }
return $entity; return $entity;
} }
protected function modify_process($entity, array $formDatas): CommonEntity protected function modify_process($entity, array $formDatas): CommonEntity
{ {
//서버정보 가져오기
$serverEntity = service('equipment_serverservice')->getEntity($formDatas['serverinfo_uid']); $serverEntity = service('equipment_serverservice')->getEntity($formDatas['serverinfo_uid']);
if (!$serverEntity instanceof ServerEntity) { if (!$serverEntity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['serverinfo_uid']}에 해당하는 서버정보을 찾을수 없습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['serverinfo_uid']}에 해당하는 서버정보을 찾을수 없습니다.");
} }
//수정을 위한 추가 FormDatas설정
$formDatas = $this->getFormDatasForServerPart($formDatas, $serverEntity); $formDatas = $this->getFormDatasForServerPart($formDatas, $serverEntity);
//서버파트 수정
$oldEntity = clone $entity; $oldEntity = clone $entity;
$entity = parent::modify_process($entity, $formDatas); $entity = parent::modify_process($entity, $formDatas);
if (!$entity instanceof ServerPartEntity) { if (!$entity instanceof ServerPartEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 PaymentEntity만 가능"); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: Return Type은 ServerPartEntity만 가능");
} }
//해당 파트정보가 변경
$this->getPartService($entity->getType())->modifyServerPart($oldEntity, $entity); $this->getPartService($entity->getType())->modifyServerPart($oldEntity, $entity);
//서비스가 정의 되어 있으면
if ($entity->getServiceInfoUid()) { $serviceUidsToSync = [];
$serviceEntity = service('customer_serviceservice')->getEntity($entity->getServiceInfoUid());
if (!$serviceEntity instanceof ServiceEntity) { if ($oldEntity->getServiceInfoUid() && $oldEntity->getBilling() == PAYMENT['BILLING']['MONTH']) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getServiceInfoUid()}에 해당하는 서비스정보을 찾을수 없습니다."); $serviceUidsToSync[(int) $oldEntity->getServiceInfoUid()] = true;
}
//Billing형식이 Month이면 서비스 금액설정 호출
if ($entity->getBilling() == PAYMENT['BILLING']['MONTH']) {
service('customer_serviceservice')->updateAmount($serviceEntity);
}
//Billing형식이 Onetime이면 일회성결제 수정
if ($entity->getBilling() == PAYMENT['BILLING']['ONETIME']) {
service('paymentservice')->modifyByServerPart($oldEntity, $entity);
}
} }
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; return $entity;
} }
protected function delete_process($entity): CommonEntity 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); $this->getPartService($entity->getType())->detachFromServerPart($entity);
$entity = parent::delete_process($entity); $entity = parent::delete_process($entity);
if (!$entity instanceof ServerPartEntity) { if (!$entity instanceof ServerPartEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 PaymentEntity만 가능"); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: Return Type은 ServerPartEntity만 가능");
} }
//서비스가 정의 되어 있으면
//월비용 서버파트 인경우 서비스 금액 재설정 if ($old->getServiceInfoUid() !== null && $old->getBilling() == PAYMENT['BILLING']['MONTH']) {
if ($entity->getServiceInfoUid() !== null && $entity->getBilling() == PAYMENT['BILLING']['MONTH']) { $this->syncMonthlyServiceAndPayment($old->getServiceInfoUid(), null, false);
//서비스정보 가져오기
$serviceEntity = service('customer_serviceservice')->getEntity($entity->getServiceInfoUid());
if (!$serviceEntity instanceof ServiceEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getServiceInfoUid()}에 해당하는 서비스정보를 찾을 수 없습니다.");
}
service('customer_serviceservice')->updateAmount($serviceEntity);
} }
// ✅ ONETIME 미납 결제 삭제는 "보류" (여기서는 아무것도 안함)
return $entity; return $entity;
} }
//List 검색용
//FormFilter 조건절 처리
//검색어조건절처리
//서버추가시 기본파트 자동추가용 //서버추가시 기본파트 자동추가용
public function attachToServer(ServerEntity $serverEntity): void public function attachToServer(ServerEntity $serverEntity): void
{ {
$chassisEntity = service("equipment_chassisservice")->getEntity($serverEntity->getChassisInfoUid()); $chassisEntity = service("equipment_chassisservice")->getEntity($serverEntity->getChassisInfoUid());
//해당 서버의 chassis_uid에 해당하는 Default값이 있는지 체크 후 서버파트 추가
foreach (SERVERPART['SERVER_PARTTYPES'] as $parttype) { foreach (SERVERPART['SERVER_PARTTYPES'] as $parttype) {
$uid_function = "get{$parttype}InfoUid"; $uid_function = "get{$parttype}InfoUid";
$cnt_function = "get{$parttype}Cnt"; $cnt_function = "get{$parttype}Cnt";
$uid = $chassisEntity->$uid_function(); $uid = $chassisEntity->$uid_function();
$cnt = $chassisEntity->$cnt_function(); $cnt = $chassisEntity->$cnt_function();
if ($uid === null) { if ($uid === null)
continue; continue;
}
//해당 파트정보 가져오기
$partEntity = $this->getPartService($parttype)->getEntity($uid); $partEntity = $this->getPartService($parttype)->getEntity($uid);
if (!$partEntity instanceof PartEntity) { if (!$partEntity instanceof PartEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 {$parttype} 파트정보를 찾을수 없습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 {$parttype} 파트정보를 찾을수 없습니다.");
} }
//서버파트정보 생성
$formDatas = []; $formDatas = [];
$formDatas['serverinfo_uid'] = $serverEntity->getPK(); $formDatas['serverinfo_uid'] = $serverEntity->getPK();
$formDatas["part_uid"] = $partEntity->getPK(); $formDatas["part_uid"] = $partEntity->getPK();
$formDatas['billing'] = PAYMENT['BILLING']['BASE']; $formDatas['billing'] = PAYMENT['BILLING']['BASE'];
$formDatas['type'] = $parttype; $formDatas['type'] = $parttype;
$formDatas['title'] = $partEntity->getTitle(); //파트 제목 $formDatas['title'] = $partEntity->getTitle();
$formDatas['amount'] = $partEntity->getPrice(); //파트 금액 $formDatas['amount'] = $partEntity->getPrice();
$formDatas['cnt'] = $cnt; $formDatas['cnt'] = $cnt;
$this->create_process($formDatas); $this->create_process($formDatas);
} }
} }
public function detachFromServer(ServerEntity $serverEntity): void
/**
* 서버 해지/분리 서버파트 회수 처리
* - 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
{ {
//서버정보에 해당하는 ServerPart정보 상태가 기본인것 제외한 모두 회수처리. // 서버가 분리되기 전 serviceinfo_uid를 잃을 수 있으므로, oldServiceEntity로 serviceUid 확보
foreach ($this->getEntities(['serverinfo_uid' => $serverEntity->getPK(), "billing !=" => PAYMENT['BILLING']['BASE']]) as $entity) { $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); $this->getPartService($entity->getType())->detachFromServerPart($entity);
if ($entity->getBilling() == PAYMENT['BILLING']['MONTH']) {
$monthlyChanged = true;
}
// ✅ 일괄 삭제(추가 동기화는 아래에서 한 번만)
parent::delete_process($entity); 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);
} }
} }

View File

@ -20,86 +20,22 @@ class ServerService extends EquipmentService
parent::__construct($model); parent::__construct($model);
$this->addClassPaths('Server'); $this->addClassPaths('Server');
} }
public function getDTOClass(): string public function getDTOClass(): string
{ {
return ServerDTO::class; return ServerDTO::class;
} }
public function createDTO(array $formDatas): ServerDTO public function createDTO(array $formDatas): ServerDTO
{ {
return new ServerDTO($formDatas); return new ServerDTO($formDatas);
} }
public function getEntityClass(): string public function getEntityClass(): string
{ {
return ServerEntity::class; return ServerEntity::class;
} }
final public function getTotalServiceCount(array $where = []): array
{
$totalCounts = [
'chiba_summary' => 0,
'tokyo_summary' => 0,
'all_summary' => 0,
'normal' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
'defence' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
'dedicated' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
'alternative' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
'vpn' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
'event' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
'test' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
];
$builder = $this->model->select("serverinfo.type,
COUNT(CASE WHEN serviceinfo.location = 'chiba' THEN 1 END) AS chiba,
COUNT(CASE WHEN serviceinfo.location = 'tokyo' THEN 1 END) AS tokyo,
COUNT(CASE WHEN serviceinfo.location IN ('chiba', 'tokyo') THEN 1 END) AS summary")
->join('serviceinfo', 'serviceinfo.uid = serverinfo.serviceinfo_uid')
->where($where)
->groupBy('serverinfo.type')
->builder();
// echo $builder->getCompiledSelect(false) . "<BR>"; //초기화 없이 SQL만 보고 싶을 때: getCompiledSelect(false) ← 꼭 false!
// dd($rows);
foreach ($builder->get()->getResult() as $row) {
$totalCounts[$row->type]['chiba'] = $row->chiba;
$totalCounts[$row->type]['tokyo'] = $row->tokyo;
$totalCounts[$row->type]['summary'] += $row->summary;
$totalCounts['chiba_summary'] += $row->chiba;
$totalCounts['tokyo_summary'] += $row->tokyo;
$totalCounts['all_summary'] = $totalCounts['chiba_summary'] + $totalCounts['tokyo_summary'];
}
// dd($totalCounts);
return $totalCounts;
}
//전체 검색어에 따른 서버정보를 검색 후 해당하는 서비스리스트를 가져온다.
final public function getSearchServices(string $keyword): array
{
$builder = $this->model->distinct()->select('serverinfo.serviceinfo_uid AS serviceinfo_uid')
->join('clientinfo', 'clientinfo.uid = serverinfo.clientinfo_uid')
->join('serverpartinfo', 'serverpartinfo.clientinfo_uid = clientinfo.uid', 'left')
->groupStart()
->like('clientinfo.name', $keyword, 'both', null, true) // escape=true
->orLike('serverinfo.code', $keyword, 'both', null, true)
->orLike('serverinfo.ip', $keyword, 'both', null, true)
->orLike('serverinfo.title', $keyword, 'both', null, true)
->orLike('serverpartinfo.title', $keyword, 'both', null, true)
->groupEnd()
->builder();
// echo $builder->getCompiledSelect(false); //초기화 없이 SQL만 보고 싶을 때: getCompiledSelect(false) ← 꼭 false!
$rows = $builder->get()->getResult();
if (!count($rows)) {
return [];
}
return $rows;
}
//서버 Title별 카운트수
final public function getStockCount(): array
{
$builder = $this->model->select('title,COUNT(*) AS cnt')->groupBy('title')->builder();
// echo $builder->getCompiledSelect(false); //초기화 없이 SQL만 보고 싶을 때: getCompiledSelect(false) ← 꼭 false!
// dd($builder->get()->getResult());
$rows = [];
foreach ($builder->get()->getResult() as $row) {
$rows[$row->title] = $row->cnt;
}
return $rows;
}
//총서버금액 //총서버금액
final public function getCalculatedAmount(int $uid): int final public function getCalculatedAmount(int $uid): int
{ {
@ -107,159 +43,150 @@ class ServerService extends EquipmentService
if (!$entity instanceof ServerEntity) { if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid} 서버 정보를 찾을수 없습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid} 서버 정보를 찾을수 없습니다.");
} }
$serverPartService = service('equipment_serverpartservice'); $serverPartService = service('equipment_serverpartservice');
$caculatedAmount = $entity->getPrice(); $caculatedAmount = $entity->getPrice();
//해당 서비스(서버) 관련 결제방식(Billing)이 Month인 ServerPart찾아서 월청구액에 합산한다.
foreach ($serverPartService->getEntities(['serverinfo_uid' => $entity->getPK(), 'billing' => PAYMENT['BILLING']['MONTH']]) as $serverPartEntity) { foreach ($serverPartService->getEntities(['serverinfo_uid' => $entity->getPK(), 'billing' => PAYMENT['BILLING']['MONTH']]) as $serverPartEntity) {
$caculatedAmount += $serverPartEntity->getCalculatedAmount(); //단가*Cnt $caculatedAmount += $serverPartEntity->getCalculatedAmount();
} }
return $caculatedAmount; return $caculatedAmount;
} }
//기본 기능부분
protected function getEntity_process(mixed $entity): ServerEntity protected function getEntity_process(mixed $entity): ServerEntity
{ {
return $entity; return $entity;
} }
protected function action_process_fieldhook(string $field, $value, array $formDatas): array
{
switch ($field) {
default:
$formDatas = parent::action_process_fieldhook($field, $value, $formDatas);
break;
}
return $formDatas;
}
protected function create_process(array $formDatas): ServerEntity protected function create_process(array $formDatas): ServerEntity
{ {
$entity = parent::create_process($formDatas); $entity = parent::create_process($formDatas);
if (!$entity instanceof ServerEntity) { if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServerEntity만 가능"); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServerEntity만 가능");
} }
//새로운 IP 추가 (IP가 정의 되어 있으면)
if ($entity->getIP()) { if ($entity->getIP()) {
service('part_ipservice')->attachToServer($entity); service('part_ipservice')->attachToServer($entity);
} }
//새로운 Switch 추가 (Switch가 정의 되어 있으면)
if ($entity->getSwitchInfoUid()) { if ($entity->getSwitchInfoUid()) {
service('part_switchservice')->attachToServer($entity); service('part_switchservice')->attachToServer($entity);
} }
//새로운 Chassis 추가 (Chassis가 정의 되어 있으면)
service('equipment_chassisservice')->attachToServer($entity); service('equipment_chassisservice')->attachToServer($entity);
//새로운 ServerPart 추가 (ServerPart가 정의 되어 있으면)
service('equipment_serverpartservice')->attachToServer($entity); service('equipment_serverpartservice')->attachToServer($entity);
return $entity; return $entity;
} }
protected function modify_process($entity, array $formDatas): ServerEntity protected function modify_process($entity, array $formDatas): ServerEntity
{ {
//필수항목검사
if (!array_key_exists('chassisinfo_uid', $formDatas)) { if (!array_key_exists('chassisinfo_uid', $formDatas)) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . '에서 오류발생: 샷시정보가 정의되지 않았습니다.'); throw new RuntimeException(static::class . '->' . __FUNCTION__ . '에서 오류발생: 샷시정보가 정의되지 않았습니다.');
} }
//변경전 정보
$oldEntity = clone $entity; $oldEntity = clone $entity;
$entity = parent::modify_process($entity, $formDatas); $entity = parent::modify_process($entity, $formDatas);
if (!$entity instanceof ServerEntity) { if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServerEntity만 가능"); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServerEntity만 가능");
} }
//IP변경
if ($oldEntity->getIP() !== $entity->getIP()) { if ($oldEntity->getIP() !== $entity->getIP()) {
//기존 IP 제거
service('part_ipservice')->detachFromServer($oldEntity); service('part_ipservice')->detachFromServer($oldEntity);
//새로운 IP 추가 (IP가 정의 되어 있으면)
if ($entity->getIP() !== null) { if ($entity->getIP() !== null) {
service('part_ipservice')->attachToServer($entity); service('part_ipservice')->attachToServer($entity);
} }
} }
//SWITCH변경
if ($oldEntity->getSwitchInfoUid() !== $entity->getSwitchInfoUid()) { if ($oldEntity->getSwitchInfoUid() !== $entity->getSwitchInfoUid()) {
service('part_switchservice')->detachFromServer($oldEntity); service('part_switchservice')->detachFromServer($oldEntity);
//새로운 Switch 추가 (Switch가 정의 되어 있으면)
if ($entity->getSwitchInfoUid() !== null) { if ($entity->getSwitchInfoUid() !== null) {
service('part_switchservice')->attachToServer($entity); service('part_switchservice')->attachToServer($entity);
} }
} }
//샷시변경
if ($oldEntity->getChassisInfoUid() !== $entity->getChassisInfoUid()) { if ($oldEntity->getChassisInfoUid() !== $entity->getChassisInfoUid()) {
service('equipment_chassisservice')->detachFromServer($oldEntity); service('equipment_chassisservice')->detachFromServer($oldEntity);
//새로운 Chassis 추가 (Chassis가 정의 되어 있으면)
if ($entity->getChassisInfoUid() !== null) { if ($entity->getChassisInfoUid() !== null) {
service('equipment_chassisservice')->attachToServer($entity); service('equipment_chassisservice')->attachToServer($entity);
} }
} }
//서비스변경
if ($entity->getServiceInfoUid() !== null) { //서비스가 정의 되어 있으면 // ✅ 서비스 유지중이면 정상 동기화 (해지 시는 deatchFromService에서 따로 처리)
if ($entity->getServiceInfoUid() !== null) {
$serviceEntity = service('customer_serviceservice')->getEntity($entity->getServiceInfoUid()); $serviceEntity = service('customer_serviceservice')->getEntity($entity->getServiceInfoUid());
if (!$serviceEntity instanceof ServiceEntity) { if (!$serviceEntity instanceof ServiceEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getServiceInfoUid()}에 해당하는 서비스정보을 찾을수 없습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getServiceInfoUid()}에 해당하는 서비스정보을 찾을수 없습니다.");
} }
service('customer_serviceservice')->updateAmount($serviceEntity); service('customer_serviceservice')->recalcAmountAndSyncPayment($serviceEntity);
} }
return $entity; return $entity;
} }
//List 검색용
//FormFilter 조건절 처리
//검색어조건절처리
public function setSearchWord(string $word): void public function setSearchWord(string $word): void
{ {
$this->model->orLike($this->model->getTable() . '.ip', $word, 'both'); $this->model->orLike($this->model->getTable() . '.ip', $word, 'both');
$this->model->orLike($this->model->getTable() . '.ilo_ip', $word, 'both'); $this->model->orLike($this->model->getTable() . '.ilo_ip', $word, 'both');
parent::setSearchWord($word); parent::setSearchWord($word);
} }
//OrderBy 처리
//서비스관련
public function attatchToService(ServiceEntity $serviceEntity, $uid, array $formDatas = []): void public function attatchToService(ServiceEntity $serviceEntity, $uid, array $formDatas = []): void
{ {
$entity = $this->getEntity($uid); $entity = $this->getEntity($uid);
if (!$entity instanceof ServerEntity) { if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 서버정보을 찾을수 없습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 서버정보을 찾을수 없습니다.");
} }
$formDatas['serviceinfo_uid'] = $serviceEntity->getPK(); $formDatas['serviceinfo_uid'] = $serviceEntity->getPK();
$formDatas["clientinfo_uid"] = $serviceEntity->getClientInfoUid(); $formDatas["clientinfo_uid"] = $serviceEntity->getClientInfoUid();
$formDatas['status'] = $formDatas['status'] ?? STATUS['OCCUPIED']; $formDatas['status'] = $formDatas['status'] ?? STATUS['OCCUPIED'];
parent::modify_process($entity, $formDatas); parent::modify_process($entity, $formDatas);
} }
public function modifyByService(ServiceENtity $oldServiceEntity, ServiceEntity $serviceEntity): void
{ //해지처리 (중요: server.serviceinfo_uid == null 이후에 ServerPart detach 호출)
//기존 메인서버 정보
$oldEntity = $this->getEntity($oldServiceEntity->getServerInfoUid());
if (!$oldEntity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$oldServiceEntity->getServerInfoUid()}에 해당하는 서비스의 기존 메인 서버정보을 찾을수 없습니다.");
}
//신규 메인서버 정보
$entity = $this->getEntity($serviceEntity->getServerInfoUid());
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$serviceEntity->getServerInfoUid()}에 해당하는 서비스의 신규 메인 서버정보을 찾을수 없습니다.");
}
//신규 메인서버는 기존 메인서버의 서버타입을 전달받아 설정 메인서버로 추가
$this->attatchToService($serviceEntity, $entity->getPK(), ['type' => $oldEntity->getType()]);
//기존서버는 대체서버로 변경 대체서버로 유지시킴
parent::modify_process($oldEntity, ['type' => 'alternative']);
}
//해지처리
public function deatchFromService($uid, array $formDatas = []): void public function deatchFromService($uid, array $formDatas = []): void
{ {
$entity = $this->getEntity($uid); $entity = $this->getEntity($uid);
if (!$entity instanceof ServerEntity) { if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 서버정보을 찾을수 없습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 서버정보을 찾을수 없습니다.");
} }
//서버파트정보 해제
// ✅ 분리 전 서비스 스냅샷
$oldServiceEntity = null;
$serviceUid = $entity->getServiceInfoUid();
if ($serviceUid) {
$svc = service('customer_serviceservice')->getEntity($serviceUid);
if ($svc instanceof ServiceEntity) {
$oldServiceEntity = clone $svc;
}
}
// 파트 detach(네트워크 자원)
if ($entity->getIP()) { if ($entity->getIP()) {
service('part_ipservice')->detachFromServer($entity); service('part_ipservice')->detachFromServer($entity);
} }
if ($entity->getSwitchInfoUid()) { if ($entity->getSwitchInfoUid()) {
service('part_switchservice')->detachFromServer($entity); service('part_switchservice')->detachFromServer($entity);
} }
service('equipment_serverpartservice')->detachFromServer($entity);
//서버정보 초기화 // ✅ 먼저 서버를 서비스에서 분리 (serviceinfo_uid=null)
$formDatas['serviceinfo_uid'] = null; $formDatas['serviceinfo_uid'] = null;
$formDatas["clientinfo_uid"] = null; $formDatas["clientinfo_uid"] = null;
$formDatas["switchinfo_uid"] = null; $formDatas["switchinfo_uid"] = null;
$formDatas["ip"] = null; $formDatas["ip"] = null;
$formDatas["switchinfo_uid"] = null;
$formDatas['status'] = $formDatas['status'] ?? STATUS['AVAILABLE']; $formDatas['status'] = $formDatas['status'] ?? STATUS['AVAILABLE'];
parent::modify_process($entity, $formDatas);
// throw new RuntimeException(var_export($formDatas, true) . var_export($entity, true)); $entity = parent::modify_process($entity, $formDatas);
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServerEntity만 가능");
}
// ✅ 이제 server.serviceinfo_uid == null 상태이므로 정책 발동:
// - MONTH 파트 삭제
// - 월 미납 payment는 삭제/0원수정 금지, status=TERMINATED
// - ONETIME 미납 삭제는 보류
service('equipment_serverpartservice')->detachFromServer($entity, $oldServiceEntity);
} }
} }

View File

@ -11,7 +11,6 @@ use App\Entities\PaymentEntity;
use App\Forms\PaymentForm; use App\Forms\PaymentForm;
use App\Helpers\PaymentHelper; use App\Helpers\PaymentHelper;
use App\Models\PaymentModel; use App\Models\PaymentModel;
use App\Services\Customer\Wallet\WalletService; use App\Services\Customer\Wallet\WalletService;
use DateTime; use DateTime;
use RuntimeException; use RuntimeException;
@ -26,18 +25,22 @@ class PaymentService extends CommonService
parent::__construct($model); parent::__construct($model);
$this->addClassPaths('Payment'); $this->addClassPaths('Payment');
} }
public function getDTOClass(): string public function getDTOClass(): string
{ {
return PaymentDTO::class; return PaymentDTO::class;
} }
public function getEntityClass(): string public function getEntityClass(): string
{ {
return PaymentEntity::class; return PaymentEntity::class;
} }
public function createDTO(array $formDatas): PaymentDTO public function createDTO(array $formDatas): PaymentDTO
{ {
return new PaymentDTO($formDatas); return new PaymentDTO($formDatas);
} }
//총 미납건수, 금액 //총 미납건수, 금액
final public function getUnPaids(string $group, array $where = []): array final public function getUnPaids(string $group, array $where = []): array
{ {
@ -46,26 +49,24 @@ class PaymentService extends CommonService
->where(['status' => STATUS['UNPAID']]) ->where(['status' => STATUS['UNPAID']])
->where($where) ->where($where)
->builder(); ->builder();
// echo $builder->getCompiledSelect(false); //초기화 없이 SQL만 보고 싶을 때: getCompiledSelect(false) ← 꼭 false!
$unPaids = []; $unPaids = [];
foreach ($builder->get()->getResult() as $row) { foreach ($builder->get()->getResult() as $row) {
$unPaids[$row->$group] = ['cnt' => $row->cnt, 'amount' => $row->amount]; $unPaids[$row->$group] = ['cnt' => $row->cnt, 'amount' => $row->amount];
} }
return $unPaids; return $unPaids;
} }
//기본 기능부분
protected function getEntity_process(mixed $entity): PaymentEntity protected function getEntity_process(mixed $entity): PaymentEntity
{ {
return $entity; return $entity;
} }
//List 검색용
//FormFilter 조건절 처리 //FormFilter 조건절 처리
public function setFilter(string $field, mixed $filter_value): void public function setFilter(string $field, mixed $filter_value): void
{ {
switch ($field) { switch ($field) {
case 'role': case 'role':
//FIND_IN_SET()은 MySQL 함수이므로 CodeIgniter가 이를 일반 컬럼명으로 착각하고 escape하게 되면 오류가 발생
// 따라서 ->where($sql, null, false)로 명시하여 escape를 꺼줘야 정상 작동
$where = "FIND_IN_SET(" . $this->model->escape($filter_value) . ", {$this->model->getTable()}.{$field}) > 0"; $where = "FIND_IN_SET(" . $this->model->escape($filter_value) . ", {$this->model->getTable()}.{$field}) > 0";
$this->model->where($where, null, false); $this->model->where($where, null, false);
break; break;
@ -74,6 +75,7 @@ class PaymentService extends CommonService
break; break;
} }
} }
//검색어조건절처리 //검색어조건절처리
public function setSearchWord(string $word): void public function setSearchWord(string $word): void
{ {
@ -81,7 +83,7 @@ class PaymentService extends CommonService
$this->model->orLike($this->model->getTable() . '.email', $word, 'both'); $this->model->orLike($this->model->getTable() . '.email', $word, 'both');
parent::setSearchWord($word); parent::setSearchWord($word);
} }
//추가기능
//pay방식에따른 WalletService 등록 //pay방식에따른 WalletService 등록
private function getWalletService($pay): WalletService private function getWalletService($pay): WalletService
{ {
@ -98,34 +100,73 @@ class PaymentService extends CommonService
break; break;
default: default:
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$pay}는 지정되지 않은 지불방식입니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$pay}는 지정되지 않은 지불방식입니다.");
// break;
} }
return $walletService; return $walletService;
} }
// ✅ 서비스 해지(서버 분리) 시: 월 미납 청구는 "삭제/0원수정" 금지, 상태만 TERMINATED
public function terminateUnpaidMonthlyByService(ServiceEntity $oldServiceEntity): void
{
$entity = $this->getEntity([
'serviceinfo_uid' => $oldServiceEntity->getPK(),
'billing' => PAYMENT['BILLING']['MONTH'],
'billing_at' => $oldServiceEntity->getBillingAt(),
'status' => STATUS['UNPAID'],
]);
if (!$entity instanceof PaymentEntity) {
return;
}
// amount/title 건드리지 않고 status만 변경
parent::modify_process($entity, ['status' => STATUS['TERMINATED']]);
}
//서비스정보로 결제정보 생성 또는 수정 (일반 운영용: upsert 유지)
public function upsertByService(ServiceEntity $oldServiceEntity, ServiceEntity $serviceEntity): PaymentEntity
{
$entity = $this->getEntity([
'serviceinfo_uid' => $oldServiceEntity->getPK(),
'billing' => PAYMENT['BILLING']['MONTH'],
'billing_at' => $oldServiceEntity->getBillingAt(),
'status' => STATUS['UNPAID']
]);
$formDatas = $this->getFormDatasFromService($serviceEntity);
if (!$entity instanceof PaymentEntity) {
return $this->create_process($formDatas);
}
return $this->modify_process($entity, $formDatas);
}
//일회성,선결제,쿠폰,포인트 입력 관련 //일회성,선결제,쿠폰,포인트 입력 관련
protected function create_process(array $formDatas): PaymentEntity protected function create_process(array $formDatas): PaymentEntity
{ {
if (!array_key_exists('serviceinfo_uid', $formDatas)) { if (!array_key_exists('serviceinfo_uid', $formDatas)) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 서비스가 정의되지 않았습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 서비스가 정의되지 않았습니다.");
} }
$serviceEntity = service('customer_serviceservice')->getEntity($formDatas['serviceinfo_uid']); $serviceEntity = service('customer_serviceservice')->getEntity($formDatas['serviceinfo_uid']);
if (!$serviceEntity instanceof ServiceEntity) { if (!$serviceEntity instanceof ServiceEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['serviceinfo_uid']}에 해당하는 서비스정보를 찾을 수 없습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['serviceinfo_uid']}에 해당하는 서비스정보를 찾을 수 없습니다.");
} }
$formDatas['clientinfo_uid'] = $serviceEntity->getClientInfoUid(); $formDatas['clientinfo_uid'] = $serviceEntity->getClientInfoUid();
$entity = parent::create_process($formDatas); $entity = parent::create_process($formDatas);
if (!$entity instanceof PaymentEntity) { if (!$entity instanceof PaymentEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 PaymentEntity만 가능"); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 PaymentEntity만 가능");
} }
//지불이 완료된경우 지불처리
if ($entity->getStatus() === STATUS['PAID']) { if ($entity->getStatus() === STATUS['PAID']) {
$this->getWalletService($entity->getPay())->withdrawalByPayment($entity); $this->getWalletService($entity->getPay())->withdrawalByPayment($entity);
;
} }
//선결제인경우 서비스정보에 결제일 변경용
if ($entity->getBilling() === PAYMENT['BILLING']['PREPAYMENT']) { if ($entity->getBilling() === PAYMENT['BILLING']['PREPAYMENT']) {
service('customer_serviceservice')->updateBillingAt($entity->getServiceInfoUid(), $entity->getBillingAt()); service('customer_serviceservice')->updateBillingAt($entity->getServiceInfoUid(), $entity->getBillingAt());
} }
return $entity; return $entity;
} }
@ -134,26 +175,28 @@ class PaymentService extends CommonService
if ($entity->getStatus() === STATUS['PAID']) { if ($entity->getStatus() === STATUS['PAID']) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 이미 지불된 결제정보는 수정이 불가합니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 이미 지불된 결제정보는 수정이 불가합니다.");
} }
$serviceEntity = service('customer_serviceservice')->getEntity($entity->getServiceInfoUid()); $serviceEntity = service('customer_serviceservice')->getEntity($entity->getServiceInfoUid());
if (!$serviceEntity instanceof ServiceEntity) { if (!$serviceEntity instanceof ServiceEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getServiceInfoUid()}에 해당하는 서비스정보를 찾을 수 없습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getServiceInfoUid()}에 해당하는 서비스정보를 찾을 수 없습니다.");
} }
//수정된 결제정보
$entity = parent::modify_process($entity, $formDatas); $entity = parent::modify_process($entity, $formDatas);
if (!$entity instanceof PaymentEntity) { if (!$entity instanceof PaymentEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 PaymentEntity만 가능"); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 PaymentEntity만 가능");
} }
//지불이 된경우 지불처리
if ($entity->getStatus() === STATUS['PAID']) { if ($entity->getStatus() === STATUS['PAID']) {
$this->getWalletService($entity->getPay())->withdrawalByPayment($entity); $this->getWalletService($entity->getPay())->withdrawalByPayment($entity);
;
} }
//선결제인경우 서비스정보에 결제일 변경용
if ($entity->getBilling() === PAYMENT['BILLING']['PREPAYMENT']) { if ($entity->getBilling() === PAYMENT['BILLING']['PREPAYMENT']) {
service('customer_serviceservice')->updateBillingAt($entity->getServiceInfoUid(), $entity->getBillingAt()); service('customer_serviceservice')->updateBillingAt($entity->getServiceInfoUid(), $entity->getBillingAt());
} }
return $entity; return $entity;
} }
protected function delete_process($entity): PaymentEntity protected function delete_process($entity): PaymentEntity
{ {
if ($entity->getStatus() === STATUS['PAID']) { if ($entity->getStatus() === STATUS['PAID']) {
@ -172,6 +215,7 @@ class PaymentService extends CommonService
'services' => [], 'services' => [],
]; ];
} }
if (!array_key_exists($serviceEntity->getPK(), $rows[$clientEntity->getPK()]['services'])) { if (!array_key_exists($serviceEntity->getPK(), $rows[$clientEntity->getPK()]['services'])) {
$serverEntity = service('equipment_serverservice')->getEntity($serviceEntity->getServerInfoUid()); $serverEntity = service('equipment_serverservice')->getEntity($serviceEntity->getServerInfoUid());
if (!$serverEntity instanceof ServerEntity) { if (!$serverEntity instanceof ServerEntity) {
@ -184,13 +228,15 @@ class PaymentService extends CommonService
'items' => [], 'items' => [],
]; ];
} }
//entities에 총 결제금액 설정
$rows[$clientEntity->getPK()]['services'][$serviceEntity->getPK()]['items'][] = [ $rows[$clientEntity->getPK()]['services'][$serviceEntity->getPK()]['items'][] = [
'title' => $entity->getTitle(), 'title' => $entity->getTitle(),
'amount' => $entity->getAmount() 'amount' => $entity->getAmount()
]; ];
$rows[$clientEntity->getPK()]['services'][$serviceEntity->getPK()]['amount'] += $entity->getAmount(); $rows[$clientEntity->getPK()]['services'][$serviceEntity->getPK()]['amount'] += $entity->getAmount();
$rows[$clientEntity->getPK()]['total_amount'] += $entity->getAmount(); $rows[$clientEntity->getPK()]['total_amount'] += $entity->getAmount();
return $rows; return $rows;
} }
@ -211,33 +257,13 @@ class PaymentService extends CommonService
); );
return $formDatas; return $formDatas;
} }
public function createByService(ServiceEntity $serviceEntity): PaymentEntity public function createByService(ServiceEntity $serviceEntity): PaymentEntity
{ {
$formDatas = $this->getFormDatasFromService($serviceEntity); $formDatas = $this->getFormDatasFromService($serviceEntity);
return parent::create_process($formDatas); return $this->create_process($formDatas);
}
public function modifyByService(ServiceEntity $oldServiceEntity, ServiceEntity $serviceEntity): PaymentEntity
{
//기존 서비스정보의 청구방식이 Month이고 지급기한일과 같고,상태가 UNPAID인 결제정보 가져와서 결제정보 수정
$entity = $this->getEntity([
'serviceinfo_uid' => $oldServiceEntity->getPK(),
'billing' => PAYMENT['BILLING']['MONTH'],
'billing_at' => $oldServiceEntity->getBillingAt(),
'status' => STATUS['UNPAID']
]);
if (!$entity instanceof PaymentEntity) { //해당조건에 맞는게 없으면 생성
log_message('error', sprintf(
"\n------Last Query (%s)-----\nQuery: %s\n------------------------------\n",
static::class . '->' . __FUNCTION__,
$this->model->getLastQuery() ?? "No Query Available",
));
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 기존 서비스정보의 {$oldServiceEntity->getTitle()}에 해당하는 결제정보가 존재하지 않습니다.");
}
//신규 서비스정보에 맞게 수정
$formDatas = $this->getFormDatasFromService($serviceEntity);
$entity = parent::modify_process($entity, $formDatas);
return $entity;
} }
//서버파트별 일회성 관련 //서버파트별 일회성 관련
private function getFormDatasFromServerPart(ServerPartEntity $serverPartEntity, array $formDatas = []): array private function getFormDatasFromServerPart(ServerPartEntity $serverPartEntity, array $formDatas = []): array
{ {
@ -255,20 +281,15 @@ class PaymentService extends CommonService
$formDatas['title'] = sprintf("%s 일회성비용", $formDatas['title'] ?? $serverPartEntity->getCustomTitle()); $formDatas['title'] = sprintf("%s 일회성비용", $formDatas['title'] ?? $serverPartEntity->getCustomTitle());
return $formDatas; return $formDatas;
} }
public function createByServerPart(ServerPartEntity $serverPartEntity): PaymentEntity public function createByServerPart(ServerPartEntity $serverPartEntity): PaymentEntity
{ {
if ($serverPartEntity->getServiceInfoUid() === null) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 서비스정보가 정의되지 않아 일회성 상품을 설정하실수 없습니다.");
}
$formDatas = $this->getFormDatasFromServerPart($serverPartEntity); $formDatas = $this->getFormDatasFromServerPart($serverPartEntity);
return parent::create_process($formDatas); return parent::create_process($formDatas);
} }
public function modifyByServerPart(ServerPartEntity $oldServerPartEntity, ServerPartEntity $serverPartEntity): PaymentEntity public function modifyByServerPart(ServerPartEntity $oldServerPartEntity, ServerPartEntity $serverPartEntity): PaymentEntity
{ {
if ($serverPartEntity->getServiceInfoUid() === null) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 서비스정보가 정의되지 않아 일회성 상품을 설정하실수 없습니다.");
}
//서버파트정보의 서비스번호가 같고, 청구방식이 onetime이고 상태가 UNPAID인 결제정보 가져와서 결제정보 수정
$entity = $this->getEntity([ $entity = $this->getEntity([
'serverpartinfo_uid' => $oldServerPartEntity->getPK(), 'serverpartinfo_uid' => $oldServerPartEntity->getPK(),
'serviceinfo_uid' => $oldServerPartEntity->getServiceInfoUid(), 'serviceinfo_uid' => $oldServerPartEntity->getServiceInfoUid(),
@ -276,7 +297,8 @@ class PaymentService extends CommonService
'billing_at' => $oldServerPartEntity->getBillingAt(), 'billing_at' => $oldServerPartEntity->getBillingAt(),
'status' => STATUS['UNPAID'] 'status' => STATUS['UNPAID']
]); ]);
if (!$entity instanceof PaymentEntity) { //해당조건에 맞는게 없으면 생성
if (!$entity instanceof PaymentEntity) {
log_message('error', sprintf( log_message('error', sprintf(
"\n------Last Query (%s)-----\nQuery: %s\n------------------------------\n", "\n------Last Query (%s)-----\nQuery: %s\n------------------------------\n",
static::class . '->' . __FUNCTION__, static::class . '->' . __FUNCTION__,
@ -284,8 +306,8 @@ class PaymentService extends CommonService
)); ));
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 기존 서버파트정보의 {$oldServerPartEntity->getTitle()}에 해당하는 결제정보가 존재하지 않습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 기존 서버파트정보의 {$oldServerPartEntity->getTitle()}에 해당하는 결제정보가 존재하지 않습니다.");
} }
$formDatas = $this->getFormDatasFromServerPart($serverPartEntity); $formDatas = $this->getFormDatasFromServerPart($serverPartEntity);
$entity = parent::modify_process($entity, $formDatas); return parent::modify_process($entity, $formDatas);
return $entity;
} }
} }