vhost/app/Controllers/Admin/ProductController.php
2024-05-20 19:18:29 +09:00

186 lines
7.3 KiB
PHP

<?php
namespace App\Controllers\Admin;
use App\Controllers\Trait\UpDownloadTrait;
use App\Entities\ProductEntity;
use App\Models\DeviceModel;
use App\Models\ProductDeviceModel;
use App\Models\ProductModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class ProductController extends AdminController
{
use UpDownloadTrait;
private $_deviceModel = null;
private $_productDeviceModel = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->initModel(new ProductModel());
$this->_viewDatas['className'] = 'Product';
$this->_viewPath .= strtolower($this->_viewDatas['className']);
$this->_viewDatas['title'] = lang($this->_viewDatas['className'] . '.title');
$this->_viewDatas['class_icon'] = CLASS_ICONS[strtoupper($this->_viewDatas['className'])];
helper($this->_viewDatas['className']);
}
final protected function getDeviceModel(): DeviceModel
{
return $this->_deviceModel = $this->_deviceModel ?: new DeviceModel();
}
final protected function getProductDeviceModel(): ProductDeviceModel
{
return $this->_productDeviceModel = $this->_productDeviceModel ?: new ProductDeviceModel();
}
public function getFields(string $action = ""): array
{
$fields = ["category", 'type', 'name', "photo", "device", "cost", "sale", "stock", "view_cnt", "status", "content",];
switch ($action) {
case "index":
case "excel":
return ["category", "user_uid", 'type', 'name', "cost", "sale", "price", "stock", "view_cnt", "status", "created_at"];
break;
case "view":
return [...$fields, "created_at"];
break;
default:
return $fields;
break;
}
}
public function getFieldFilters(): array
{
return ["category", "user_uid", 'type', "status"];
}
public function getFieldBatchFilters(): array
{
return parent::getFieldBatchFilters();
}
//Field별 Form Datas 처리용
protected function getFieldFormData(string $field, $entity = null): array
{
switch ($field) {
case 'photo':
$file = $this->upload_image_procedure($field);
if (!is_null($file)) {
$this->_viewDatas['fieldDatas'][$field] = $file;
}
break;
default:
return parent::getFieldFormData($field, $entity);
break;
}
return $this->_viewDatas['fieldDatas'];
}
private function calculate_price(): int
{
if ($this->_viewDatas['fieldDatas']['cost'] < $this->_viewDatas['fieldDatas']['sale']) {
throw new \Exception(sprintf(
"%s가[%s] %s[%s]보다 작습니다.",
lang($this->_viewDatas['className'] . '.label.cost'),
number_format($this->_viewDatas['fieldDatas']['cost']),
lang($this->_viewDatas['className'] . '.label.sale'),
number_format($this->_viewDatas['fieldDatas']['sale']),
));
}
return $this->_viewDatas['fieldDatas']['cost'] - $this->_viewDatas['fieldDatas']['sale'];
}
//가상서버
protected function virtual_process(ProductEntity $entity): array
{
//가상서버정보
$protudctDatas = array(
'category' => 'virtual',
'name' => '',
'content' => '',
'cost' => $this->_viewDatas['fieldDatas']['price'],
'price' => $this->_viewDatas['fieldDatas']['price'],
'sale' => 0,
'stock' => 1,
'view_cnt' => 1,
'status' => 'use',
);
//서버부품정보검증
$titles = array('가상서버');
//foreach (Product['parts']['virtual']['category'] as $category => $attrs) {
foreach ([] as $category => $attrs) {
if (!$this->_viewDatas['fieldDatas'][$category]) {
throw new \Exception($category . "의 값이 지정되지 않았습니다.");
} else {
$protudctDatas[$category . "_model"] = $attrs['label'];
$protudctDatas[$category . "_cnt"] = $this->_viewDatas['fieldDatas'][$category];
array_push(
$titles,
sprintf(
"%s * %s%s,",
$protudctDatas[$category . "_model"],
$protudctDatas[$category . "_cnt"],
$attrs['unit'],
),
);
}
}
$protudctDatas['name'] = implode(" ", $titles);
$protudctDatas['content'] = implode("\n", $titles);
$product = $this->getModel()->create($protudctDatas);
return array();
//return $this->add_procedure($product, 1, $this->_viewDatas['fieldDatas']['paymentday']);
}
//주문처리
protected function device_process(ProductEntity $entity): array
{
switch ($this->_viewDatas['fieldDatas']['category']) {
case 'virtual':
return $this->virtual_process($entity);
break;
case 'beremetal':
//상품관련 tw_product_device 기존정보 삭제처리
foreach ($this->getProductDeviceModel()->getEntitys(
['product_uid' => $entity->getPrimaryKey()]
) as $productDeviceEntity) {
$this->getProductDeviceModel()->delete($productDeviceEntity);
}
//상품관련 tw_product_device 기존정보 생성처리
$devices = [];
foreach (['server', 'cpu', 'memory', 'disk', 'nic', 'publicip', 'os'] as $field) {
$formDatas = ['product_uid' => $entity->getPrimaryKey()];
$formDatas['device_uid'] = $this->request->getPost($field);
$formDatas['device_cnt'] = $this->request->getPost($field . "_cnt") ?: 1;
if ($formDatas['product_uid'] && $formDatas['device_uid']) {
$devices[$field] = $this->getProductDeviceModel()->create($formDatas);
}
}
// dd($devices);
return $devices;
break;
default:
throw new \Exception($this->_viewDatas['fieldDatas']['category'] . "는 알수없는 상품 구분입니다. 다시 확인 부탁드립니다.");
break;
}
}
//Insert관련
protected function insert_process(): ProductEntity
{
$this->_viewDatas['fieldDatas']['price'] = $this->calculate_price();
$entity = parent::insert_process();
$entity->setDevices($this->device_process($entity));
return $entity;
}
//Update관련
protected function update_process($entity): ProductEntity
{
$this->_viewDatas['fieldDatas']['price'] = $this->calculate_price();
$entity = parent::update_process($entity);
$entity->setDevices($this->device_process($entity));
// dd($entity);
return $entity;
}
}