dbmsv2/app/Services/PaymentService.php
2025-10-01 11:32:56 +09:00

268 lines
11 KiB
PHP

<?php
namespace App\Services;
use App\Entities\Customer\ServiceEntity;
use App\Entities\Equipment\ServerPartEntity;
use App\Entities\PaymentEntity;
use App\Helpers\PaymentHelper;
use App\Interfaces\Customer\ServiceInterface;
use App\Interfaces\Equipment\ServerPartInterface;
use App\Models\PaymentModel;
use App\Services\CommonService;
use App\Services\Customer\ClientService;
use App\Services\Customer\ServiceService;
use App\Services\Equipment\ServerPartService;
use App\Services\UserService;
class PaymentService extends CommonService implements ServiceInterface, ServerPartInterface
{
private ?UserService $_userService = null;
private ?ClientService $_clientService = null;
private ?ServiceService $_serviceService = null;
private ?ServerPartService $_serverPartService = null;
public function __construct()
{
parent::__construct(new PaymentModel(), new PaymentHelper());
$this->addClassName('Payment');
}
final public function getFormFields(): array
{
return [
"serviceinfo_uid",
"title",
"amount",
"billing",
"billing_at",
"content"
];
}
final public function getFormFilters(): array
{
return [
'clientinfo_uid',
"serviceinfo_uid",
'billing',
'pay',
'status',
'user_uid',
];
}
final public function getIndexFields(): array
{
return [
'clientinfo_uid',
"serviceinfo_uid",
'billing',
'title',
'amount',
'billing_at',
'pay',
'status',
'updated_at',
'countdown',
'user_uid',
];
}
final public function getBatchjobFields(): array
{
return ['pay', 'status'];
}
final public function getBatchjobButtons(): array
{
return [
'batchjob' => '일괄 결제 ',
'invoice' => '청구서 발행',
];
}
final public function getClientService(): ClientService
{
if (!$this->_clientService) {
$this->_clientService = new ClientService();
}
return $this->_clientService;
}
final public function getUSerService(): UserService
{
if (!$this->_userService) {
$this->_userService = new UserService();
}
return $this->_userService;
}
final public function getServiceService(): ServiceService
{
if ($this->_serviceService === null) {
$this->_serviceService = new ServiceService();
}
return $this->_serviceService;
}
final public function getServerPartService(): ServerPartService
{
if (!$this->_serverPartService) {
$this->_serverPartService = new ServerPartService();
}
return $this->_serverPartService;
}
//총 미납건수, 금액
final public function getUnPaids(string $group, array $where = []): array
{
$rows = $this->getModel()->groupBy($group)
->select("{$group},COUNT(uid) as cnt, SUM(amount) as amount")
->where(['status' => STATUS['UNPAID']])
->where($where)
->get()->getResult();
$unPaids = [];
foreach ($rows as $row) {
$unPaids[$row->$group] = ['cnt' => $row->cnt, 'amount' => $row->amount];
}
return $unPaids;
}
//결제금액설정
final public function setAmount(PaymentEntity $entity, int $amount): PaymentEntity
{
return parent::modify($entity, ['amount' => $amount]);
}
//기본 기능부분
//FieldForm관련용
final public function getFormOption(string $field, array $options = []): array
{
switch ($field) {
case 'serviceinfo_uid':
$options = $this->getServiceService()->getEntities();
break;
default:
$options = parent::getFormOption($field, $options);
break;
}
return $options;
}
//Action 기능
public function create(array $formDatas): PaymentEntity
{
if (!array_key_exists('serviceinfo_uid', $formDatas)) {
throw new \Exception("서비스정보가 없습니다.");
}
//서비스정보 가져오기
$serviceEntity = $this->getServiceService()->getEntity($formDatas['serviceinfo_uid']);
if (!$serviceEntity instanceof ServiceEntity) {
throw new \Exception(__METHOD__ . "에서 오류발생: 서비스정보[{$formDatas['serviceinfo_uid']}]를 찾을수 없습니다.");
}
$formDatas['clientinfo_uid'] = $serviceEntity->getClientInfoUID();
return parent::create($formDatas);
}
//List 검색용
//OrderBy 처리
final public function setOrderBy(mixed $field = null, mixed $value = null): void
{
$this->getModel()->orderBy('billing_at ASC');
parent::setOrderBy($field, $value);
}
//서비스관련 작업
public function setService(ServiceEntity $serviceEntity, array $serviceDatas): ServiceEntity
{
$formDatas = [];
$formDatas['clientinfo_uid'] = $serviceEntity->getClientInfoUID();
$formDatas['serviceinfo_uid'] = $serviceEntity->getPK();
$formDatas['serverinfo_uid'] = $serviceEntity->getServerInfoUID();
$formDatas['title'] = $serviceEntity->getServerEntity()->getTitle();
$formDatas['amount'] = $serviceEntity->getAmount();
$formDatas['billing'] = PAYMENT['BILLING']['MONTH'];
$formDatas['billing_at'] = $serviceEntity->getBillingAt();
//필수정보처리 후 결제정보등록 후 서비스정보 Entity에 결제정보 설정
return $serviceEntity->setPaymentEntity($this->getModel()->create($formDatas));
}
public function changeService(ServiceEntity $oldServiceEntity, ServiceEntity $serviceEntity, array $serviceDatas): ServiceEntity
{
//결제정보 가져오기
$entity = $serviceEntity->getPaymentEntity();
if (!$entity instanceof PaymentEntity) {
throw new \Exception(__METHOD__ . "에서 오류발생: 서비스정보[{$serviceEntity->getPK()}]에 해당하는 결제정보를 찾을수 없습니다.");
}
//미납상태확인
if ($entity->getStatus() !== STATUS['UNPAID']) {
throw new \Exception(__METHOD__ . "에서 오류발생: 완료된 결제는 수정이 불가합니다.");
}
$formDatas = [];
$formDatas['clientinfo_uid'] = $serviceEntity->getClientInfoUID();
$formDatas['serviceinfo_uid'] = $serviceEntity->getPK();
$formDatas['serverinfo_uid'] = $serviceEntity->getServerInfoUID();
$formDatas['title'] = $serviceEntity->getServerEntity()->getTitle();
$formDatas['amount'] = $serviceEntity->getAmount();
$formDatas['billing'] = PAYMENT['BILLING']['MONTH'];
$formDatas['billing_at'] = $serviceEntity->getBillingAt();
//필수정보처리 후 결제정보수정 후 서비스정보 Entity에 결제정보 설정
return $serviceEntity->setPaymentEntity($this->getModel()->modify($entity, $formDatas));
}
public function unsetService(ServiceEntity $serviceEntity, array $serviceDatas): ServiceEntity
{
//아무것도 하지 않는다.
return $serviceEntity;
}
//서버파트관련 작업(1회성만처리)
public function setServerPart(ServerPartEntity $serverPartEntity, array $serverPartDatas): ServerPartEntity
{
if ($serverPartEntity->getBilling() !== PAYMENT['BILLING']['ONETIME']) {
throw new \Exception("[{$serverPartEntity->getBilling()}],청구방식이 일회성이 아닙니다.");
}
if ($serverPartEntity->getServiceInfoUID() === null) {
throw new \Exception(__METHOD__ . "에서 오류발생: 서비스정보가 정의된 후에만 가능합니다.");
}
$formDatas = [];
$formDatas['clientinfo_uid'] = $serverPartEntity->getClientInfoUID();
$formDatas['serviceinfo_uid'] = $serverPartEntity->getServiceInfoUID();
$formDatas['serverinfo_uid'] = $serverPartEntity->getServerInfoUID(); //서버연결정보 수정시에 필요함
//타이틀은 기타의 경우 직접작성한 제목을 등록하고 아닌경우는 Part의 Title을 사용한다.
$formDatas['title'] = $serverPartEntity->getPartEntity()->getTitle();
$formDatas['amount'] = $serverPartEntity->getTotalAmount(); //단가*cnt
$formDatas['billing'] = $serverPartEntity->getBilling();
//당일결체일로 설정
$formDatas['billing_at'] = date("Y-m-d");
//결제정보등록
$entity = $this->getModel()->create($formDatas);
//서버연결정보 Entity에 결제정보 설정
return $serverPartEntity->setPaymentEntity($entity);
}
public function changeServerPart(ServerPartEntity $oldServerPartEntity, ServerPartEntity $serverPartEntity, array $serverPartDatas): ServerPartEntity
{
if ($serverPartEntity->getBilling() !== PAYMENT['BILLING']['ONETIME']) {
throw new \Exception("[{$serverPartEntity->getBilling()}],청구방식이 일회성이 아닙니다.");
}
//기존 결제정보 가져오기
$entity = $serverPartEntity->getPaymentEntity();
//기존 결제정보가 없다면 신규입력으로 처리
if (!$entity instanceof PaymentEntity) {
return $this->setServerPart($serverPartEntity, $serverPartDatas);
}
//미납상태확인
if ($entity->getStatus() !== STATUS['UNPAID']) {
throw new \Exception(__METHOD__ . "에서 오류발생: 완료된 결제는 수정이 불가합니다.");
}
if ($serverPartEntity->getServiceInfoUID() === null) {
throw new \Exception(__METHOD__ . "에서 오류발생: 서비스정보가 정의된 후에만 가능합니다.");
}
$formDatas = [];
$formDatas['clientinfo_uid'] = $serverPartEntity->getClientInfoUID();
$formDatas['serviceinfo_uid'] = $serverPartEntity->getServiceInfoUID();
$formDatas['serverinfo_uid'] = $serverPartEntity->getServerInfoUID(); //서버연결정보 수정시에 필요함
//타이틀은 기타의 경우 직접작성한 제목을 등록하고 아닌경우는 Part의 Title을 사용한다.
$formDatas['title'] = $serverPartEntity->getPartEntity()->getTitle();
$formDatas['amount'] = $serverPartEntity->getTotalAmount(); //단가*cnt
$formDatas['billing'] = $serverPartEntity->getBilling();
//당일결체일로 설정
$formDatas['billing_at'] = date("Y-m-d");
//결제정보수정
$entity = $this->getModel()->modify($entity, $formDatas);
//서버연결정보 Entity에 결제정보 설정
$serverPartEntity->setPaymentEntity($entity);
return $serverPartEntity;
}
public function unsetServerPart(ServerPartEntity $serverPartEntity, array $serverPartDatas): ServerPartEntity
{
//삭제시에는 아무것도 하지 않는다.
return $serverPartEntity;
}
}