231 lines
12 KiB
PHP
231 lines
12 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
|
|
{
|
|
//청구방식에 따른 결제일 설정
|
|
$billing_at = null;
|
|
if ($billing === PAYMENT['BILLING']['MONTH']) {
|
|
$billing_at = $serviceEntity->getBillingAt();
|
|
} else if ($billing === PAYMENT['BILLING']['ONETIME']) {
|
|
$billing_at = date("Y-m-d");
|
|
} else {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$billing}에 해당하는 청구방식을 찾을수 없습니다.");
|
|
}
|
|
return $billing_at;
|
|
}
|
|
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__ . "에서 오류발생: 청구방법이 지정되지 않았습니다.");
|
|
}
|
|
// 키가 없거나(Undefined), null, '', 0, false 중 하나라도 해당되면 실행
|
|
if (empty($formDatas['part_uid'])) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 파트번호가 지정되지 않았습니다.");
|
|
}
|
|
//해당 파트정보의 Title을 파트정보의 Title로 설정
|
|
$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;
|
|
}
|
|
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설정
|
|
$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()) {
|
|
$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']) {
|
|
service('customer_serviceservice')->updateAmount($serviceEntity);
|
|
}
|
|
//Billing형식이 Onetime이면 일회성결제 생성
|
|
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설정
|
|
$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은 PaymentEntity만 가능");
|
|
}
|
|
//해당 파트정보가 변경
|
|
$this->getPartService($entity->getType())->modifyServerPart($oldEntity, $entity);
|
|
//서비스가 정의 되어 있으면
|
|
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']) {
|
|
service('customer_serviceservice')->updateAmount($serviceEntity);
|
|
}
|
|
//Billing형식이 Onetime이면 일회성결제 수정
|
|
if ($entity->getBilling() == PAYMENT['BILLING']['ONETIME']) {
|
|
service('paymentservice')->modifyByServerPart($oldEntity, $entity);
|
|
}
|
|
}
|
|
return $entity;
|
|
}
|
|
protected function delete_process($entity): CommonEntity
|
|
{
|
|
$this->getPartService($entity->getType())->detachFromServerPart($entity);
|
|
$entity = parent::delete_process($entity);
|
|
if (!$entity instanceof ServerPartEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 PaymentEntity만 가능");
|
|
}
|
|
//서비스가 정의 되어 있으면
|
|
//월비용 서버파트 인경우 서비스 금액 재설정
|
|
if ($entity->getServiceInfoUid() !== null && $entity->getBilling() == PAYMENT['BILLING']['MONTH']) {
|
|
//서비스정보 가져오기
|
|
$serviceEntity = service('customer_serviceservice')->getEntity($entity->getServiceInfoUid());
|
|
if (!$serviceEntity instanceof ServiceEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getServiceInfoUid()}에 해당하는 서비스정보를 찾을 수 없습니다.");
|
|
}
|
|
service('customer_serviceservice')->updateAmount($serviceEntity);
|
|
}
|
|
return $entity;
|
|
}
|
|
//List 검색용
|
|
//FormFilter 조건절 처리
|
|
//검색어조건절처리
|
|
|
|
//서버추가시 기본파트 자동추가용
|
|
public function attachToServer(ServerEntity $serverEntity): void
|
|
{
|
|
$chassisEntity = service("equipment_chassisservice")->getEntity($serverEntity->getChassisInfoUid());
|
|
//해당 서버의 chassis_uid에 해당하는 Default값이 있는지 체크 후 서버파트 추가
|
|
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);
|
|
}
|
|
}
|
|
public function detachFromServer(ServerEntity $serverEntity): void
|
|
{
|
|
//서버정보에 해당하는 ServerPart정보 상태가 기본인것 제외한 모두 회수처리.
|
|
foreach ($this->getEntities(['serverinfo_uid' => $serverEntity->getPK(), "billing !=" => PAYMENT['BILLING']['BASE']]) as $entity) {
|
|
$this->getPartService($entity->getType())->detachFromServerPart($entity);
|
|
parent::delete_process($entity);
|
|
}
|
|
}
|
|
}
|