dbmsv4/app/Services/Customer/Wallet/WalletService.php
2025-12-09 18:25:07 +09:00

95 lines
3.9 KiB
PHP

<?php
namespace App\Services\Customer\Wallet;
use App\Entities\Customer\ClientEntity;
use App\Entities\PaymentEntity;
use App\Models\CommonModel;
use App\Services\Customer\CustomerService;
use RuntimeException;
abstract class WalletService extends CustomerService
{
protected function __construct(CommonModel $model)
{
parent::__construct($model);
$this->addClassPaths('Wallet');
}
abstract protected function setWithdrawal_process(ClientEntity $clientEntity, int $amount): int;
private function setDeposit(int|ClientEntity $uid, string $title, string $key, int $value, string $status): ClientEntity
{
$entity = is_int($uid) ? $this->getEntity($uid) : $uid;
if (!$entity instanceof ClientEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 서비스정보를 찾을수 없습니다.");
}
$calculatedValue = null;
$balance = 0;
$field = "";
switch ($key) {
case PAYMENT['PAY']['ACCOUNT']:
$balance = $entity->getAccountBalance();
$field = "account_balance";
break;
case PAYMENT['PAY']['COUPON']:
$balance = $entity->getCouponBalance();
$field = "coupon_balance";
break;
case PAYMENT['PAY']['POINT']:
$balance = $entity->getPointBalance();
$field = "point_balance";
break;
}
//입금,추가 처리
if ($status === STATUS['DEPOSIT']) {
$calculatedValue = $balance + $value;
}
//출금,사용 처리
if ($status === STATUS['WITHDRAWAL']) {
if ($balance < $value) {
throw new RuntimeException(sprintf(
"%s 에서 오류발생: %s 고객의 %s[ %s ]이/가 사용한 %s 금액/수[ %s ]이/가 부족합니다.",
static::class . '->' . __FUNCTION__,
$entity->getTitle(),
$title,
number_format($balance),
$title,
number_format($value)
));
}
$calculatedValue = $balance - $value;
}
if (!is_int($calculatedValue) || $calculatedValue < 0) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 {$entity->getTitle()} 고객 {$title}의 계산결과 값이 NULL이거나 0보다 작은 값입니다.");
}
//balance 수정
$formDatas = [$field => $calculatedValue];
return parent::modify_process($entity, $formDatas);
}
//기본기능
//결제처리 관련
protected function createByPayment_process(PaymentEntity $paymentEntity, array $formDatas = []): array
{
//고객정보
$clientEntity = service('customer_clientservice')->getEntity($paymentEntity->getClientInfoUID());
if (!$clientEntity instanceof ClientEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$paymentEntity->getClientInfoUID()}에 해당하는 고객정보를 찾을수 없습니다.");
}
$formDatas['clientinfo_uid'] = $paymentEntity->getClientInfoUID();
$formDatas['title'] = $paymentEntity->getTitle();
$formDatas['issue_at'] = date('Y-m-d');
$formDatas['status'] = STATUS['WITHDRAWAL'];
$formDatas['balance'] = $this->setWithdrawal_process($clientEntity, $paymentEntity->getAmount());
return $formDatas;
}
final public function createByPayment(PaymentEntity $paymentEntity): void
{
$formDatas = $this->createByPayment_process($paymentEntity);
$fields = array_keys($formDatas);
$this->getFormService()->setFormFields($fields);
$this->getFormService()->setFormRules('create', $fields);
$this->create_process($formDatas);
}
}