201 lines
8.0 KiB
PHP
201 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin\Customer;
|
|
|
|
use App\Entities\Customer\ClientEntity;
|
|
use App\Entities\Customer\ServiceEntity;
|
|
use App\Entities\Customer\ServicePaymentEntity;
|
|
use App\Helpers\Customer\ServicePaymentHelper;
|
|
use App\Libraries\LogCollector;
|
|
|
|
use App\Services\Customer\ServicePaymentService;
|
|
use App\Services\Customer\ServiceService;
|
|
use App\Services\Customer\ClientService;
|
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class ServicePaymentController extends CustomerController
|
|
{
|
|
private ?ServiceService $_serviceService = null;
|
|
private ?ClientService $_clientService = 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;
|
|
}
|
|
public function getClientService(): ClientService
|
|
{
|
|
if (!$this->_clientService) {
|
|
$this->_clientService = new ClientService($this->request);
|
|
}
|
|
return $this->_clientService;
|
|
}
|
|
protected function getResultSuccess(string $message = MESSAGES["SUCCESS"], ?string $actionTemplate = null): RedirectResponse|string
|
|
{
|
|
switch ($this->getAction()) {
|
|
case 'invoice':
|
|
$this->control = $this->getControlDatas();
|
|
$this->getHelper()->setViewDatas($this->getViewDatas());
|
|
$actionTemplate = $this->request->getVar('ActionTemplate') ?? $actionTemplate ?? 'payment';
|
|
if ($actionTemplate) {
|
|
$view_file = $this->view_path . $actionTemplate . DIRECTORY_SEPARATOR . $this->getAction();
|
|
} else {
|
|
$view_file = $this->view_path . $this->getAction();
|
|
}
|
|
$result = view($view_file, ['viewDatas' => $this->getViewDatas()]);
|
|
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관련
|
|
//수정관련
|
|
protected function modify_process(mixed $entity, array $formDatas): mixed
|
|
{
|
|
//수정자 정보 자동추가용
|
|
$formDatas['user_uid'] = $this->getMyAuth()->getUIDByAuthInfo();
|
|
return parent::modify_process($entity, $formDatas);
|
|
}
|
|
//View 관련
|
|
protected function view_process(mixed $entity): mixed
|
|
{
|
|
//LINE,IP,SERVER등 추가 FilterOption 셋팅용
|
|
$this->setFilterOptionsByItemType();
|
|
return parent::view_process($entity);
|
|
}
|
|
//List 관련
|
|
protected function setOrderByForList(): void
|
|
{
|
|
//OrderBy 처리
|
|
$this->getService()->getModel()->orderBy('billing_at', 'ASC', false);
|
|
parent::setOrderByForList();
|
|
}
|
|
protected function index_process(): array
|
|
{
|
|
//LINE,IP,SERVER등 추가 FilterOption 셋팅용
|
|
$this->setFilterOptionsByItemType();
|
|
return parent::index_process();
|
|
}
|
|
//Invoice 관련
|
|
private function getOwnersForInvoice(ClientEntity $ownerEntity): array
|
|
{
|
|
$temps = [
|
|
'name' => $ownerEntity->getName(),
|
|
'total_amount' => 0,
|
|
'services' => []
|
|
];
|
|
return $temps;
|
|
}
|
|
private function getServicesForInvoice(ServiceEntity $serviceEntity): array
|
|
{
|
|
$temps = [
|
|
'code' => $serviceEntity->getTitle(),
|
|
'billing_at' => $serviceEntity->getBillingAt(),
|
|
'items' => []
|
|
];
|
|
return $temps;
|
|
}
|
|
private function getItemsForInvoice(ServicePaymentEntity $entity): array
|
|
{
|
|
$partEntites = $this->getFilterFieldOption($entity->getItemType());
|
|
// dd($partEntites);
|
|
$temps = [
|
|
'item_type' => SERVICE_ITEM_TYPES[$entity->getItemType()],
|
|
'item_uid' => $partEntites[$entity->getItemUid()]->getTitle(),
|
|
'amount' => $entity->getAmount()
|
|
];
|
|
return $temps;
|
|
}
|
|
private function invoice_process(): array
|
|
{
|
|
//변경할 UIDS
|
|
$uids = $this->request->getPost('batchjob_uids[]');
|
|
if (!is_array($uids) || !count($uids)) {
|
|
throw new \Exception("청구서에 적용될 리스트를 선택하셔야합니다.");
|
|
}
|
|
$owner_pk = false;
|
|
$entities = [];
|
|
foreach ($uids as $uid) {
|
|
//기존 Entity 가져오기
|
|
$entity = $this->getService()->getEntity($uid);
|
|
if (!$entity) {
|
|
LogCollector::debug(__METHOD__ . "에서 {$uid}에 대한 결제정보를 찾을수 없습니다.");
|
|
}
|
|
//entities에 관리자 설정
|
|
$ownerEntity = $this->getClientService()->getEntity($entity->getOwnerUID());
|
|
if (!$ownerEntity) {
|
|
LogCollector::debug(__METHOD__ . "에서 {$entity->getOwnerUID()}에 대한 관라자정보를 찾을수 없습니다.");
|
|
}
|
|
if ($ownerEntity->getPK() !== $owner_pk) {
|
|
if (!array_key_exists($ownerEntity->getPK(), $entities)) {
|
|
$entities[$ownerEntity->getPK()] = $this->getOwnersForInvoice($ownerEntity);
|
|
}
|
|
$owner_pk = $ownerEntity->getPK();
|
|
}
|
|
//entities에 서비스 설정
|
|
$serviceEntity = $this->getServiceService()->getEntity($entity->getServiceUid());
|
|
if (!$serviceEntity) {
|
|
LogCollector::debug(__METHOD__ . "에서 {$entity->getServiceUid()}에 대한 서비스정보를 찾을수 없습니다.");
|
|
}
|
|
if (!array_key_exists($serviceEntity->getPK(), $entities[$owner_pk]['services'])) {
|
|
$entities[$owner_pk]['services'][$serviceEntity->getPK()] = $this->getServicesForInvoice($serviceEntity);
|
|
}
|
|
//entities에 서비스 Item Type,Item 설정
|
|
if (!array_key_exists($entity->getPK(), $entities[$owner_pk]['services'][$serviceEntity->getPK()])) {
|
|
$entities[$owner_pk]['services'][$serviceEntity->getPK()]['items'][] = $this->getItemsForInvoice($entity);
|
|
}
|
|
//entities에 총 결제금액 설정
|
|
$entities[$ownerEntity->getPK()]['total_amount'] += $entity->getAmount();
|
|
}
|
|
// dd($entities);
|
|
return $entities;
|
|
}
|
|
public function invoice(): RedirectResponse|string
|
|
{
|
|
try {
|
|
$this->initAction(__FUNCTION__);
|
|
//LINE,IP,SERVER등 추가 FilterOption 셋팅용
|
|
foreach (SERVICE_ITEM_TYPES as $item_type => $label) {
|
|
$options = $this->getService()->getServiceItemLinkService($item_type)->getEntities();
|
|
$this->setFilterFieldOption($item_type, $options);
|
|
}
|
|
$this->entities = $this->invoice_process();
|
|
return $this->getResultSuccess();
|
|
} catch (\Exception $e) {
|
|
return $this->getResultFail($e->getMessage());
|
|
}
|
|
}
|
|
}
|