dbms/app/Controllers/Admin/Customer/ServicePaymentController.php
2025-06-13 16:44:28 +09:00

141 lines
5.1 KiB
PHP

<?php
namespace App\Controllers\Admin\Customer;
use App\Helpers\Customer\ServicePaymentHelper;
use App\Libraries\LogCollector;
use App\Services\Customer\ServicePaymentService;
use App\Services\Customer\ServiceService;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class ServicePaymentController extends CustomerController
{
private ?ServiceService $_serviceService = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->content_title = lang("{$this->getService()->getClassName()}.title");
$this->class_path .= $this->getService()->getClassName();
$this->uri_path .= strtolower($this->getService()->getClassName('/')) . '/';
// $this->view_path .= strtolower($this->getService()->getClassName()) . DIRECTORY_SEPARATOR;
}
public function getService(): ServicePaymentService
{
if (!$this->_service) {
$this->_service = new ServicePaymentService($this->request);
}
return $this->_service;
}
public function getHelper(): ServicePaymentHelper
{
if (!$this->_helper) {
$this->_helper = new ServicePaymentHelper($this->request);
}
return $this->_helper;
}
public function getServiceService(): ServiceService
{
if (!$this->_serviceService) {
$this->_serviceService = new ServiceService($this->request);
}
return $this->_serviceService;
}
protected function initAction(string $action): void
{
//$item_type(CPU,RAM,STORAGE등)에 따라 선언된 getFormFieldOption용 사용됨 (initAction보다 먼저 호출해야 됨)
$this->initServiceItemOptions();
parent::initAction($action);
}
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
case 'serviceinfo_uid':
foreach ($this->getServiceService()->getEntities() as $entity) {
$options[$entity->getPK()] = $entity->getTitle();
}
break;
case 'item_uid':
$options = [];
break;
default:
$options = parent::getFormFieldOption($field, $options);
break;
}
return $options;
}
protected function getResultSuccess(string $message = MESSAGES["SUCCESS"], ?string $actionTemplate = null): RedirectResponse|string
{
switch ($this->getAction()) {
case 'invoice_email':
$result = parent::getResultSuccess($message, 'invoice_email');
break;
case 'invoice':
$result = parent::getResultSuccess($message, 'invoice');
break;
case 'index':
$result = parent::getResultSuccess($message, $this->request->getVar('ActionTemplate') ?? $actionTemplate ?? 'payment');
break;
default:
$result = parent::getResultSuccess($message, $actionTemplate);
break;
}
return $result;
}
//Index,FieldForm관련
private function setService(int $uid): void
{
$this->getServiceService()->getEntity($uid);
$entity = $this->getServiceService()->getEntity($uid);
if (!$entity) {
throw new \Exception("서비스 정보가 존재하지 않습니다. uid: {$uid}");
}
$this->_services[$uid] = $entity;
}
private function invoice_process(): array
{
//변경할 UIDS
$uids = $this->request->getPost('batchjob_uids[]');
if (!is_array($uids) || !count($uids)) {
throw new \Exception("청구서에 적용될 리스트를 선택하셔야합니다.");
}
$this->item_fields = ['item_type', 'ammount', 'biiling_cycle'];
$entities = [];
foreach ($uids as $uid) {
//기존 Entity 가져오기
$entity = $this->getService()->getEntity($uid);
if (!$entity) {
LogCollector::debug(__METHOD__ . "에서 {$uid}에 대한 정보를 찾을수 없습니다.");
}
//서비스 정보 추가
$this->addService($entity->getServiceUid());
$entities[] = $entity;
}
return $entities;
}
public function invoice_email(): RedirectResponse|string
{
try {
$this->setAction(__FUNCTION__);
$this->entities = $this->invoice_process();
return $this->getResultSuccess();
} catch (\Exception $e) {
return $this->getResultFail($e->getMessage());
}
}
public function invoice_mobile(): RedirectResponse|string
{
try {
$this->setAction(__FUNCTION__);
$this->entities = $this->invoice_process();
return $this->getResultSuccess();
} catch (\Exception $e) {
return $this->getResultFail($e->getMessage());
}
}
}