246 lines
9.0 KiB
PHP
246 lines
9.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Customer;
|
|
|
|
use RuntimeException;
|
|
use DateTimeZone;
|
|
use DateTimeImmutable;
|
|
use App\Models\Customer\ServiceModel;
|
|
use App\Helpers\Customer\ServiceHelper;
|
|
use App\Forms\Customer\ServiceForm;
|
|
use App\Entities\Customer\ServiceEntity;
|
|
use App\Entities\CommonEntity;
|
|
use App\DTOs\Customer\ServiceDTO;
|
|
|
|
class ServiceService extends CustomerService
|
|
{
|
|
private $_form = null;
|
|
private $_helper = null;
|
|
public function __construct(ServiceModel $model)
|
|
{
|
|
parent::__construct($model);
|
|
$this->addClassPaths('Service');
|
|
}
|
|
protected function getDTOClass(): string
|
|
{
|
|
return ServiceDTO::class;
|
|
}
|
|
public function createDTO(array $formDatas): ServiceDTO
|
|
{
|
|
return new ServiceDTO($formDatas);
|
|
}
|
|
public function getEntityClass(): string
|
|
{
|
|
return ServiceEntity::class;
|
|
}
|
|
public function getFormService(): ServiceForm
|
|
{
|
|
if ($this->_form === null) {
|
|
$this->_form = new ServiceForm();
|
|
$this->_form->setAttributes([
|
|
'pk_field' => $this->model->getPKField(),
|
|
'title_field' => $this->model->getTitleField(),
|
|
'table' => $this->model->getTable(),
|
|
'useAutoIncrement' => $this->model->useAutoIncrement(),
|
|
'class_path' => $this->getClassPaths(false)
|
|
]);
|
|
}
|
|
return $this->_form;
|
|
}
|
|
public function getHelper(): ServiceHelper
|
|
{
|
|
if ($this->_helper === null) {
|
|
$this->_helper = new ServiceHelper();
|
|
$this->_helper->setAttributes([
|
|
'pk_field' => $this->model->getPKField(),
|
|
'title_field' => $this->model->getTitleField(),
|
|
'table' => $this->model->getTable(),
|
|
'useAutoIncrement' => $this->model->useAutoIncrement(),
|
|
'class_path' => $this->getClassPaths(false)
|
|
]);
|
|
}
|
|
return $this->_helper;
|
|
}
|
|
public function action_init_process(string $action, array $formDatas = []): void
|
|
{
|
|
$fields = [
|
|
"site",
|
|
"location",
|
|
"clientinfo_uid",
|
|
'serverinfo_uid',
|
|
"rack",
|
|
"line",
|
|
"title",
|
|
"start_at",
|
|
"billing_at",
|
|
"status",
|
|
'sale',
|
|
"history",
|
|
];
|
|
$filters = [
|
|
'site',
|
|
'location',
|
|
"rack",
|
|
"line",
|
|
'clientinfo_uid',
|
|
'serverinfo_uid',
|
|
'user_uid',
|
|
'status',
|
|
];
|
|
$indexFilter = $filters;
|
|
$batchjobFilters = [
|
|
'site',
|
|
'location',
|
|
'clientinfo_uid',
|
|
'status'
|
|
];
|
|
switch ($action) {
|
|
case 'create':
|
|
case 'create_form':
|
|
case 'modify':
|
|
case 'modify_form':
|
|
break;
|
|
case 'view':
|
|
$fields = [...$fields, 'created_at'];
|
|
break;
|
|
case 'index':
|
|
case 'download':
|
|
$fields = [
|
|
'site',
|
|
'location',
|
|
'clientinfo_uid',
|
|
'serverinfo_uid',
|
|
'sale',
|
|
'amount',
|
|
'billing_at',
|
|
'status',
|
|
'start_at',
|
|
'created_at'
|
|
];
|
|
break;
|
|
}
|
|
$this->getFormService()->setFormFields($fields);
|
|
$this->getFormService()->setFormRules($action, $fields);
|
|
$this->getFormService()->setFormFilters($filters);
|
|
$this->getFormService()->setFormOptions($action, $filters, $formDatas);
|
|
$this->getFormService()->setIndexFilters($indexFilter);
|
|
$this->getFormService()->setBatchjobFilters($batchjobFilters);
|
|
}
|
|
//추가 기능
|
|
//interval을 기준으로 최근 신규 서비스정보 가져오기
|
|
final public function getNewServiceEntities(int $interval, string $status = STATUS['AVAILABLE']): array
|
|
{
|
|
return $this->getEntities(["start_at >= NOW()-INTERVAL {$interval} DAY" => null, "status" => $status]);
|
|
}
|
|
//서비스별 총 금액
|
|
final public function getTotalAmounts($where = []): array
|
|
{
|
|
$rows = $this->model->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 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'));
|
|
// 현재 일(day)을 저장
|
|
$day = (int)$date->format('d');
|
|
// 다음달로 이동 (DateInterval 사용)
|
|
$date->modify('first day of next month');
|
|
// 다음달의 마지막 날 계산
|
|
$lastDayOfNextMonth = (int)$date->format('t');
|
|
// 현재 날짜가 다음달의 마지막 날보다 크면 -> 마지막 날로 설정
|
|
if ($day > $lastDayOfNextMonth) {
|
|
$day = $lastDayOfNextMonth;
|
|
}
|
|
// 일(day)을 설정
|
|
$date->setDate((int)$date->format('Y'), (int)$date->format('m'), $day);
|
|
// 최종 결과 리턴 (YYYY-MM-DD)
|
|
return $date->format('Y-m-d');
|
|
}
|
|
// 서비스금액관련처리
|
|
public function getCalculatedAmount(int $serverinfo_uid, int $rack, int $line, int $sale): int
|
|
{
|
|
$serverService = service('equipment_serverservice');
|
|
//기본:상면비+회선비+서버금액(price)+서버파트연결(월비용)-할인액
|
|
return $rack + $line + $serverService->getCalculatedAmount($serverinfo_uid) - $sale;
|
|
}
|
|
//기본 기능부분
|
|
protected function getEntity_process(mixed $entity): ServiceEntity
|
|
{
|
|
return $entity;
|
|
}
|
|
protected function create_process(array $formDatas): ServiceEntity
|
|
{
|
|
//필수항목검사
|
|
if (!array_key_exists('serverinfo_uid', $formDatas)) {
|
|
throw new RuntimeException(__METHOD__ . '에서 오류발생: 서버정보가 정의되지 않았습니다.');
|
|
}
|
|
//서비스코드생성
|
|
$formDatas['code'] = $formDatas['site'] . "_s" . uniqid();
|
|
//서비스 전체금액 구하기
|
|
$formDatas['amount'] = $this->getCalculatedAmount(
|
|
(int)$formDatas['serverinfo_uid'],
|
|
(int)$formDatas['rack'],
|
|
(int)$formDatas['line'],
|
|
(int)$formDatas['sale']
|
|
);
|
|
//서비스 생성
|
|
$entity = parent::create_process($formDatas);
|
|
if (!$entity instanceof ServiceEntity) {
|
|
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 ServiceEntity만 가능");
|
|
}
|
|
//서버정보 연결
|
|
service('equipment_serverservice')->attachToService($entity);
|
|
//결제정보 연결
|
|
service('paymentservice')->attachToService($entity);
|
|
return $entity;
|
|
}
|
|
protected function modify_process($entity, array $formDatas): ServiceEntity
|
|
{
|
|
//필수항목검사
|
|
if (!array_key_exists('serverinfo_uid', $formDatas)) {
|
|
throw new RuntimeException(__METHOD__ . '에서 오류발생: 서버정보가 정의되지 않았습니다.');
|
|
}
|
|
//수정전 서비스정보를 currentEntity 복사해준다.
|
|
if ($entity->getServerInfoUID() !== $formDatas['serverinfo_uid']) {
|
|
//서버정보 해지처리
|
|
service('equipment_serverservice')->detachFromService($entity);
|
|
//결제정보 해지(삭제)처리
|
|
service('paymentservice')->detachFromService($entity);
|
|
//서비스 전체금액 구하기
|
|
$formDatas['amount'] = $this->getCalculatedAmount(
|
|
(int)$formDatas['serverinfo_uid'],
|
|
(int)$formDatas['rack'],
|
|
(int)$formDatas['line'],
|
|
(int)$formDatas['sale']
|
|
);
|
|
}
|
|
//서비스 수정
|
|
$entity = parent::modify_process($entity, $formDatas);
|
|
if (!$entity instanceof ServiceEntity) {
|
|
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 ServiceEntity만 가능");
|
|
}
|
|
//서버정보 연결
|
|
service('equipment_serverservice')->attachToService($entity);
|
|
//결제정보 연결
|
|
service('paymentservice')->attachToService($entity);
|
|
return $entity;
|
|
}
|
|
//List 검색용
|
|
//FormFilter 조건절 처리
|
|
//검색어조건절처리
|
|
}
|