addClassName('Service'); } public function getFormFields(): array { return [ "site", "location", "type", "clientinfo_uid", 'serverinfo_uid', "rack", "line", "start_at", "billing_at", "status", 'amount', "history", ]; } public function getFormFilters(): array { return [ 'site', 'location', 'type', 'clientinfo_uid', 'serverinfo_uid', 'rack', 'line', 'status', ]; } public function getIndexFields(): array { return [ 'site', 'location', 'type', 'clientinfo_uid', 'serverinfo_uid', 'amount', 'billing_at', 'status', 'start_at', 'updated_at', ]; } public function getIndexFilters(): array { return [ 'site', 'location', 'type', 'clientinfo_uid', 'serverinfo_uid', 'user_uid', //home의 최신신규서버현황에서 사용 'status', ]; } public function getBatchjobFields(): array { return ['site', 'location', 'type', 'clientinfo_uid', 'status']; } public function getFormRule(string $action, string $field): string { if (is_array($field)) { throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true)); } switch ($field) { case "serverinfo_uid": $rule = "required|numeric"; break;; default: $rule = parent::getFormRule($action, $field); break; } return $rule; } protected function getEntity_process(mixed $entity): ServiceEntity { if (!$entity instanceof ServiceEntity) { throw new \Exception(__METHOD__ . "에서 형식오류:ServiceEntity만 허용됩니다."); } //서버정보 정의 $serverEntity = $this->getServerService()->getEntity($entity->getServerInfoUID()); if ($serverEntity instanceof ServerEntity) { $entity->setServerEntity($serverEntity); } //결제정보 정의 if ($entity->getPaymentUID()) { $paymentEntity = $this->getPaymentService()->getEntity($entity->getPaymentUID()); if ($paymentEntity instanceof PaymentEntity) { $entity->setPaymentEntity($paymentEntity); } } return $entity; } final public function getPaymentService(): PaymentService { if (!$this->_paymentService) { $this->_paymentService = new PaymentService(); } return $this->_paymentService; } public function getServerService(): ServerService { if (!$this->_serverService) { $this->_serverService = new ServerService(); } return $this->_serverService; } final public function getServerPartService(): ServerPartService { if (!$this->_serverPartService) { $this->_serverPartService = new ServerPartService(); } return $this->_serverPartService; } //interval을 기준으로 최근 신규 서비스정보 가져오기 final public function getEntitiesByNewService(int $interval, string $status = ServiceEntity::DEFAULT_STATUS): array { return $this->getEntities(sprintf("start_at >= NOW()-INTERVAL {$interval} DAY AND status = '%s'", $status)); } final public function getTotalServiceCount(array $where = []): array { $rows = $this->getModel()->groupBy(['site', 'type'])->select("site,type, COUNT(CASE WHEN location = 'chiba' THEN 1 END) AS chiba, COUNT(CASE WHEN location = 'tokyo' THEN 1 END) AS tokyo, COUNT(CASE WHEN location IN ('chiba', 'tokyo') THEN 1 END) AS summary") ->where($where) ->get()->getResult(); $totalCounts = [ 'normal' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0], 'defence' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0], 'dedicated' => ['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], 'alternative' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0] ]; foreach ($rows as $row) { // echo "site:{$row->site},type:{$row->type}"; if (!array_key_exists($row->site, $totalCounts)) { $totalCounts[$row->site] = [ '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], 'vpn' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0], 'event' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0], 'test' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0], 'alternative' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0], ]; } if (!array_key_exists($row->type, $totalCounts[$row->site])) { $totalCounts[$row->site][$row->type] = []; } $totalCounts[$row->type]['chiba'] += $totalCounts[$row->site][$row->type]['chiba'] = $row->chiba; $totalCounts[$row->type]['tokyo'] += $totalCounts[$row->site][$row->type]['tokyo'] = $row->tokyo; $totalCounts[$row->type]['summary'] += $totalCounts[$row->site][$row->type]['summary'] = $row->summary; $totalCounts[$row->site]['chiba_summary'] += $row->chiba; $totalCounts[$row->site]['tokyo_summary'] += $row->tokyo; $totalCounts[$row->site]['all_summary'] = $totalCounts[$row->site]['chiba_summary'] + $totalCounts[$row->site]['tokyo_summary']; } // dd($totalCounts); return $totalCounts; } //서비스별 총 금액 final public function getTotalAmounts($where = []): array { $rows = $this->getModel()->groupBy('clientinfo_uid')->select("clientinfo_uid,SUM(amount) AS amount") ->where($where) ->get()->getResult(); $amounts = []; foreach ($rows as $row) { $amounts[$row->clientinfo_uid] = $row->amount; } return $amounts; } //다음 달로 결제일을 연장합니다. final public function extendBillingAt(string $billing_at, string $status): bool { $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 billing_at = ? AND status = ?"; return $this->getModel()->query($sql, [$billing_at, $status]); } final public function getCaculatedAmount(int $uid): int { $entity = $this->getEntity($uid); if (!$entity instanceof ServiceEntity) { throw new \Exception(__METHOD__ . "에서 오류발생:[{$uid}]에 대한 서비스정보를 찾을수 없습니다."); } $summary_amount = $entity->getRack() + $entity->getLine() + $entity->getServerEntity()->getPrice(); //기본:상면비+회선비+서버금액(price)+서버파트연결(월비용) //해당 서비스(서버) 관련 결제방식(Billing)이 Month인 ServerPart 전체를 다시 검사하여 월청구액을 합산한다. foreach ($this->getServerPartService()->getEntities(['serverinfo_uid' => $entity->getServerEntity()->getPK()]) as $serverPartEntity) { if ($serverPartEntity->getBilling() === PAYMENT['BILLING']['MONTH']) { //월비용일때만 적용 $summary_amount += $serverPartEntity->getCaculatedAmount(); //단가*Cnt } } return $summary_amount; } //기본 기능부분 //FieldForm관련용 public function getFormOption(string $field, array $options = []): array { switch ($field) { case 'serverinfo_uid': $options = $this->getServerService()->getEntities(); break; case 'CPU': case 'RAM': case 'DISK': case 'OS': case 'DB': case 'SOFTWARE': case 'SWITCH': case 'IP': case 'CS': $options = $this->getServerService()->getFormOption($field, $options); break; default: $options = parent::getFormOption($field, $options); break; } return $options; } //Action 기능 private function action_process(ServiceEntity $entity, string $action): ServiceEntity { //서버정보수정 $entity = $this->getServerService()->$action($entity); $entity = $this->getPaymentService()->$action($entity); //결제정보PK정의 $entity = parent::modify($entity, ['payment_uid' => $entity->getPaymentEntity()->getPK()]); return $entity; } //생성 public function create(array $formDatas): ServiceEntity { if (!array_key_exists('serverinfo_uid', $formDatas)) { throw new \Exception(__METHOD__ . "에서 오류발생: 서버가 지정되지 않았습니다."); } //신규등록(월청구액 전달값 그대로 사용) $entity = parent::create($formDatas); //후처리작업 return $this->action_process($entity, __FUNCTION__ . 'Service'); } //수정 public function modify(mixed $entity, array $formDatas): ServiceEntity { //기존 서버정보 해지 if (array_key_exists('serverinfo_uid', $formDatas) && $entity->getServerEntity()->getPK() != $formDatas['serverinfo_uid']) { $entity->getServerService()->deleteService($entity); } //수정작업(월청구액계산 확인 후 서비스정보에 수정) $entity = parent::modify( $entity, ['amount' => $this->getCaculatedAmount($entity->getPK())] ); //후처리작업 return $this->action_process($entity, __FUNCTION__ . 'Service'); } //삭제 public function delete(mixed $entity): ServiceEntity { //선처리작업 $this->action_process($entity, __FUNCTION__ . 'Service'); return parent::delete($entity); } }