diff --git a/app/Helpers/CommonHelper.php b/app/Helpers/CommonHelper.php index 02dfcba..1e378db 100644 --- a/app/Helpers/CommonHelper.php +++ b/app/Helpers/CommonHelper.php @@ -185,7 +185,7 @@ abstract class CommonHelper case 'content': case 'detail': case 'history': - $value = html_entity_decode($value, ENT_QUOTES, 'UTF-8'); + $value = $value != null ? html_entity_decode($value, ENT_QUOTES, 'UTF-8') : ""; break; case 'issue_at': case 'expired_at': diff --git a/app/Services/Customer/ClientService.php b/app/Services/Customer/ClientService.php index e966d92..16e61c1 100644 --- a/app/Services/Customer/ClientService.php +++ b/app/Services/Customer/ClientService.php @@ -119,22 +119,18 @@ class ClientService extends CustomerService $calculatedValue = null; $balance = 0; - $title = ""; $field = ""; switch ($key) { case PAYMENT['PAY']['ACCOUNT']: $balance = $entity->getAccountBalance(); - $title = "예치금"; $field = "account_balance"; break; case PAYMENT['PAY']['COUPON']: $balance = $entity->getCouponBalance(); - $title = "쿠폰"; $field = "coupon_balance"; break; case PAYMENT['PAY']['POINT']: $balance = $entity->getPointBalance(); - $title = "포인트"; $field = "point_balance"; break; } @@ -161,41 +157,9 @@ class ClientService extends CustomerService throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 {$entity->getTitle()} 고객 {$title}의 계산결과 값이 NULL이거나 0보다 작은 값입니다."); } //balance 수정 - $formDatas = [$field => $calculatedValue, 'status' => $status]; + $formDatas = [$field => $calculatedValue]; return parent::modify_process($entity, $formDatas); } - //pay방식에따른 결제처리 - final public function updateWalletByPayment(PaymentEntity $entity): void - { - $service = null; - switch ($entity->getPay()) { - case PAYMENT['PAY']['ACCOUNT']: - $service = service('customer_wallet_accountservice'); - break; - case PAYMENT['PAY']['COUPON']: - $service = service('customer_wallet_couponservice'); - break; - case PAYMENT['PAY']['POINT']: - $service = service('customer_wallet_pointservice'); - break; - default: - throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getPay()}는 지정되지 않은 지불방식입니다."); - // break; - } - $formDatas = [ - 'clientinfo_uid' => $entity->getClientInfoUID(), - 'bank' => '결제차감', - 'title' => $entity->getTitle(), - 'alias' => '결제차감', - 'issue_at' => date('Y-m-d'), - 'amount' => $entity->getAmount(), - 'status' => STATUS['WITHDRAWAL'], - ]; - $fields = array_keys($formDatas); - $service->getFormService()->setFormFields($fields); - $service->getFormService()->setFormRules('create', $fields); - $service->create_process($formDatas); - } //기본 기능부분 protected function getEntity_process(mixed $entity): ClientEntity { diff --git a/app/Services/Customer/Wallet/AccountService.php b/app/Services/Customer/Wallet/AccountService.php index 64d7d6e..e512040 100644 --- a/app/Services/Customer/Wallet/AccountService.php +++ b/app/Services/Customer/Wallet/AccountService.php @@ -3,7 +3,9 @@ namespace App\Services\Customer\Wallet; use App\DTOs\Customer\Wallet\AccountDTO; +use App\Entities\Customer\ClientEntity; use App\Entities\Customer\Wallet\AccountEntity; +use App\Entities\PaymentEntity; use App\Forms\Customer\Wallet\AccountForm; use App\Helpers\Customer\Wallet\AccountHelper; use App\Models\Customer\Wallet\AccountModel; @@ -117,7 +119,6 @@ class AccountService extends WalletService protected function create_process(array $formDatas): AccountEntity { $entity = parent::create_process($formDatas); - service('customer_clientservice')->updateBalance($entity->getClientInfoUID(), "예치금", PAYMENT['PAY']['ACCOUNT'], $entity->getAmount(), $entity->getStatus()); return $entity; } protected function modify_process($entity, array $formDatas): AccountEntity @@ -132,4 +133,49 @@ class AccountService extends WalletService $this->model->orLike($this->model->getTable() . '.alias', $word, 'both'); parent::setSearchWord($word); } + //결제처리 관련 + protected function createByPayment_process(PaymentEntity $paymentEntity, array $formDatas = []): array + { + $formDatas['bank'] = $formDatas['alias'] = '결제차감'; + $formDatas['amount'] = $paymentEntity->getAmount(); + return parent::createByPayment_process($paymentEntity, $formDatas); + } + //입금처리 + protected function setDeposit_process(ClientEntity $clientEntity, int $amount): int + { + $calculatedBalance = $balance = $clientEntity->getAccountBalance(); + $field = "account_balance"; + if ($balance < $amount) { + throw new RuntimeException(sprintf( + "%s 에서 오류발생: %s 고객의 예치금액(%s원)이 출금액(%s원) 보다 부족합니다.", + static::class . '->' . __FUNCTION__, + $clientEntity->getTitle(), + number_format($balance), + number_format($amount) + )); + } + //최종처리 + $calculatedBalance = $balance - $amount; + service('customer_clientservice')->modify_process($clientEntity, ['account_balance' => $calculatedBalance]); + return $calculatedBalance; + } + + //출금처리 + protected function setWithdrawal_process(ClientEntity $clientEntity, int $amount): int + { + $calculatedBalance = $balance = $clientEntity->getAccountBalance(); + if ($balance < $amount) { + throw new RuntimeException(sprintf( + "%s 에서 오류발생: %s 고객의 예치금액(%s원)이 출금액(%s원) 보다 부족합니다.", + static::class . '->' . __FUNCTION__, + $clientEntity->getTitle(), + number_format($balance), + number_format($amount) + )); + } + //최종처리 + $calculatedBalance = $balance - $amount; + service('customer_clientservice')->modify_process($clientEntity, ['account_balance' => $calculatedBalance]); + return $calculatedBalance; + } } diff --git a/app/Services/Customer/Wallet/CouponService.php b/app/Services/Customer/Wallet/CouponService.php index 81cac35..4564ac9 100644 --- a/app/Services/Customer/Wallet/CouponService.php +++ b/app/Services/Customer/Wallet/CouponService.php @@ -2,12 +2,13 @@ namespace App\Services\Customer\Wallet; -use RuntimeException; -use App\Models\Customer\Wallet\CouponModel; -use App\Helpers\Customer\Wallet\CouponHelper; -use App\Forms\Customer\Wallet\CouponForm; -use App\Entities\Customer\Wallet\CouponEntity; use App\DTOs\Customer\Wallet\CouponDTO; +use App\Entities\Customer\Wallet\CouponEntity; +use App\Entities\PaymentEntity; +use App\Forms\Customer\Wallet\CouponForm; +use App\Helpers\Customer\Wallet\CouponHelper; +use App\Models\Customer\Wallet\CouponModel; +use RuntimeException; class CouponService extends WalletService { @@ -118,4 +119,11 @@ class CouponService extends WalletService //List 검색용 //FormFilter 조건절 처리 //검색어조건절처리 + + //결제처리 관련 + protected function createByPayment_process(PaymentEntity $paymentEntity, array $formDatas = []): array + { + $formDatas['cnt'] = $paymentEntity->getAmount(); + return parent::createByPayment_process($paymentEntity, $formDatas); + } } diff --git a/app/Services/Customer/Wallet/PointService.php b/app/Services/Customer/Wallet/PointService.php index 5ed4f61..2a8e0b9 100644 --- a/app/Services/Customer/Wallet/PointService.php +++ b/app/Services/Customer/Wallet/PointService.php @@ -2,12 +2,13 @@ namespace App\Services\Customer\Wallet; -use RuntimeException; -use App\Models\Customer\Wallet\PointModel; -use App\Helpers\Customer\Wallet\PointHelper; -use App\Forms\Customer\Wallet\PointForm; -use App\Entities\Customer\Wallet\PointEntity; use App\DTOs\Customer\Wallet\PointDTO; +use App\Entities\Customer\Wallet\PointEntity; +use App\Entities\PaymentEntity; +use App\Forms\Customer\Wallet\PointForm; +use App\Helpers\Customer\Wallet\PointHelper; +use App\Models\Customer\Wallet\PointModel; +use RuntimeException; class PointService extends WalletService { @@ -118,4 +119,11 @@ class PointService extends WalletService //List 검색용 //FormFilter 조건절 처리 //검색어조건절처리 + + //결제처리 관련 + protected function createByPayment_process(PaymentEntity $paymentEntity, array $formDatas = []): array + { + $formDatas['amount'] = $paymentEntity->getAmount(); + return parent::createByPayment_process($paymentEntity, $formDatas); + } } diff --git a/app/Services/Customer/Wallet/WalletService.php b/app/Services/Customer/Wallet/WalletService.php index fb10598..b23b0ad 100644 --- a/app/Services/Customer/Wallet/WalletService.php +++ b/app/Services/Customer/Wallet/WalletService.php @@ -2,8 +2,11 @@ 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 { @@ -12,4 +15,80 @@ abstract class WalletService extends CustomerService 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); + } } diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index 29eda9c..be173a1 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -209,13 +209,33 @@ class PaymentService extends CommonService } //지불 관련 + //pay방식에따른 결제처리 + private function updateWallet(PaymentEntity $entity): void + { + $service = null; + switch ($entity->getPay()) { + case PAYMENT['PAY']['ACCOUNT']: + $service = service('customer_wallet_accountservice'); + break; + case PAYMENT['PAY']['COUPON']: + $service = service('customer_wallet_couponservice'); + break; + case PAYMENT['PAY']['POINT']: + $service = service('customer_wallet_pointservice'); + break; + default: + throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getPay()}는 지정되지 않은 지불방식입니다."); + // break; + } + $service->createByPayment($entity); + } //일괄 지불용 protected function batchjob_process($entity, array $formDatas): object { // modify_process를 호출하여 로직 재사용 (PK 로드 및 PK 제거/방어 로직 포함) $entity = parent::batchjob_process($entity, $formDatas); //지불방식에 따른 고객 예치금,쿠폰,포인트 처리 - service('customer_clientservice')->updateWalletByPayment($entity); + $this->updateWallet($entity); return $entity; } //단일 지불용 @@ -232,7 +252,7 @@ class PaymentService extends CommonService //결제 완료 처리 후 추가정보 처리 $entity = parent::modify_process($entity, ['status' => STATUS['PAID']]); //지불방식에 따른 고객 예치금,쿠폰,포인트 처리 - service('customer_clientservice')->updateWalletByPayment($entity); + $this->updateWallet($entity); $db->transComplete(); return $entity; } catch (DatabaseException $e) {