198 lines
8.2 KiB
PHP
198 lines
8.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Customer;
|
|
|
|
use App\DTOs\Customer\ServiceDTO;
|
|
use App\Entities\Customer\ServiceEntity;
|
|
use App\Forms\Customer\ServiceForm;
|
|
use App\Helpers\Customer\ServiceHelper;
|
|
use App\Models\Customer\ServiceModel;
|
|
use DateTimeImmutable;
|
|
use DateTimeZone;
|
|
use RuntimeException;
|
|
|
|
class ServiceService extends CustomerService
|
|
{
|
|
private $_form = null;
|
|
private $_helper = null;
|
|
public function __construct(ServiceModel $model)
|
|
{
|
|
parent::__construct($model);
|
|
$this->addClassPaths('Service');
|
|
}
|
|
public 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->getPKField(),
|
|
'title_field' => $this->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->getPKField(),
|
|
'title_field' => $this->getTitleField(),
|
|
'table' => $this->model->getTable(),
|
|
'useAutoIncrement' => $this->model->useAutoIncrement(),
|
|
'class_path' => $this->getClassPaths(false)
|
|
]);
|
|
}
|
|
return $this->_helper;
|
|
}
|
|
//추가 기능
|
|
//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;
|
|
}
|
|
//다음 달로 결제일 가져오기.(CLI용)
|
|
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');
|
|
}
|
|
// 서비스금액관련처리
|
|
private function getCalculatedAmount(int $rack_price, int $line_price, int $sale_price, $serverinfo_uid): int
|
|
{
|
|
//총 서비스금액 구하기
|
|
$server_amount = service('equipment_serverservice')->getCalculatedAmount($serverinfo_uid);
|
|
return (int)$server_amount + $rack_price + $line_price - $sale_price;
|
|
}
|
|
final public function updateAmount(ServiceEntity $entity): ServiceEntity
|
|
{
|
|
//기본:서버금액(서버비+서버파트(월비용))+상면비+회선비-할인액
|
|
$entity->amount = $this->getCalculatedAmount(
|
|
$entity->getRack(),
|
|
$entity->getLine(),
|
|
$entity->getSale(),
|
|
$entity->getServerInfoUID()
|
|
);
|
|
//총 서비스금액 설정 및 저장
|
|
if (!$this->model->save($entity)) {
|
|
// 저장 실패 시 예외 처리
|
|
$errors = $this->model->errors();
|
|
throw new RuntimeException("금액 업데이트 중 DB 저장 오류: " . implode(', ', $errors));
|
|
}
|
|
return $entity;
|
|
}
|
|
// 서비스금액관련처리
|
|
final public function updateBillingAt($uid, string $billing_at): ServiceEntity
|
|
{
|
|
$entity = $this->getEntity($uid);
|
|
if (!$entity instanceof ServiceEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:{$uid}에 해당하는 서비스정보를 찾을 수 업습니다.");
|
|
}
|
|
$entity->billing_at = $billing_at;
|
|
if (!$this->model->save($entity)) {
|
|
// 저장 실패 시 예외 처리
|
|
$errors = $this->model->errors();
|
|
throw new RuntimeException("금액 업데이트 중 DB 저장 오류: " . implode(', ', $errors));
|
|
}
|
|
return $entity;
|
|
}
|
|
//기본 기능부분
|
|
protected function getEntity_process(mixed $entity): ServiceEntity
|
|
{
|
|
return $entity;
|
|
}
|
|
protected function create_process(array $formDatas): ServiceEntity
|
|
{
|
|
//생성폼에는 없는 필수항목 지정용(code)
|
|
$formDatas['code'] = $formDatas['site'] . "_s" . uniqid();
|
|
//총서비스금액
|
|
$formDatas['amount'] = $this->getCalculatedAmount($formDatas['rack'], $formDatas['line'], $formDatas['sale'], $formDatas['serverinfo_uid']);
|
|
//서비스 생성
|
|
$entity = parent::create_process($formDatas);
|
|
if (!$entity instanceof ServiceEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServiceEntity만 가능");
|
|
}
|
|
//서버정보 연결
|
|
service('equipment_serverservice')->attatchToService($entity, $entity->getServerInfoUID());
|
|
//서비스비용 설정
|
|
$entity = $this->updateAmount($entity);
|
|
//결제정보 생성
|
|
service('paymentservice')->createByService($entity);
|
|
return $entity;
|
|
}
|
|
protected function modify_process($entity, array $formDatas): ServiceEntity
|
|
{
|
|
//변경전 정보
|
|
$oldEntity = clone $entity;
|
|
|
|
//수정폼에는 없는 필수항목 지정용(code,amount)
|
|
$formDatas['code'] = $entity->getCode();
|
|
$formDatas['amount'] = $entity->getAmount();
|
|
//서비스 수정
|
|
$entity = parent::modify_process($entity, $formDatas);
|
|
if (!$entity instanceof ServiceEntity) {
|
|
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServiceEntity만 가능");
|
|
}
|
|
//신규 서비스 서버의 비용으로 재계산 서비스 금액 설정
|
|
$entity = $this->updateAmount($entity);
|
|
//기존 서비스용 결제비용 과 신규 서버스용으로 결제비용이 다르면 수정
|
|
service('paymentservice')->modifyByService($oldEntity, $entity);
|
|
//서버정보 연결 신규서버로 변경
|
|
if ($oldEntity->getServerInfoUID() !== $entity->getServerInfoUID()) {
|
|
// throw new RuntimeException("{$oldEntity->getServerInfoUID()} !== {$entity->getServerInfoUID()}");
|
|
service('equipment_serverservice')->modifyByService($oldEntity, $entity);
|
|
}
|
|
return $entity;
|
|
}
|
|
//List 검색용
|
|
//FormFilter 조건절 처리
|
|
//검색어조건절처리
|
|
}
|