153 lines
5.5 KiB
PHP
153 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Entities\OrderEntity;
|
|
use App\Models\OrderModel;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use App\Models\ProductModel;
|
|
|
|
class OrderController extends AdminController
|
|
{
|
|
private $_productModel = null;
|
|
private $_product_uids = array();
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
$this->_className .= 'Order';
|
|
$this->_model = new OrderModel();
|
|
helper($this->_className);
|
|
$this->_viewPath .= strtolower($this->_className);
|
|
$this->_viewDatas['title'] = lang($this->_className . '.title');
|
|
$this->_viewDatas['className'] = $this->_className;
|
|
}
|
|
//Porduct모델
|
|
final protected function getProductModel(): ProductModel
|
|
{
|
|
return is_null($this->_productModel) ? new ProductModel() : $this->_productModel;
|
|
}
|
|
|
|
//Field별 Form Option용
|
|
protected function getFieldFormOption(string $field): array
|
|
{
|
|
switch ($field) {
|
|
case 'product_uid':
|
|
$options = $this->_product_uids = $this->_product_uids ?: $this->getProductModel()->getFieldFormOptions([]);
|
|
break;
|
|
default:
|
|
return parent::getFieldFormOption($field);
|
|
break;
|
|
}
|
|
if (!is_array($options)) {
|
|
throw new \Exception(__FUNCTION__ . "에서 {$this->_className}의 Field:{$field}의 FormOptionData가 array가 아닙니다.\n" . var_export($options, true));
|
|
}
|
|
return $options;
|
|
}
|
|
|
|
//구매상품검사
|
|
protected function insert_process(): OrderEntity
|
|
{
|
|
//상품정보가져오기
|
|
$product = $this->getProductModel()->getEntity([$this->getProductModel()->getPrimaryKey() => $this->_viewDatas['fieldDatas']['product_uid']]);
|
|
if (!$this->_viewDatas['fieldDatas']['quantity']) {
|
|
throw new \Exception("제품 구매수량을 확인해주세요");
|
|
}
|
|
if (!$this->_viewDatas['fieldDatas']['price']) {
|
|
throw new \Exception("제품 구매금액을 확인해주세요");
|
|
}
|
|
if ($product->getStock() < $this->_viewDatas['fieldDatas']['quantity']) {
|
|
throw new \Exception(sprintf(
|
|
"%s의 재고수[%s]보다 많은 수량[%s]을 주문하셨습니다.\\n",
|
|
$product->getTitle(),
|
|
$product->getStock(),
|
|
$this->_viewDatas['fieldDatas']['quantity']
|
|
));
|
|
}
|
|
//구매금액 = (판매가-할인가)*구매수량과 맞는지 다시 확인.
|
|
// $calculated_price = ($product->getPrice() - $product->getSale()) * $quantity;
|
|
$calculated_price = ($product->getPrice() - $product->getSale()) * $this->_viewDatas['fieldDatas']['quantity'];
|
|
if ($this->_viewDatas['fieldDatas']['price'] != $calculated_price) {
|
|
throw new \Exception(
|
|
sprintf(
|
|
"%s상품의 구매금액[%s원]과 판매금액[%s원]이 맞지않습니다.",
|
|
$product->getTitle(),
|
|
number_format($this->_viewDatas['fieldDatas']['price']),
|
|
number_format($calculated_price)
|
|
)
|
|
);
|
|
}
|
|
//상품모델에서 Order에 담은 갯수만큼 재고에서 뺀다.
|
|
$this->getProductModel()->decreaseStock($product, $this->_viewDatas['fieldDatas']['quantity']);
|
|
return parent::insert_process();
|
|
}
|
|
|
|
//장바구니에 담기
|
|
public function insert()
|
|
{
|
|
$msg = "";
|
|
try {
|
|
$this->_viewDatas['fields'] = $this->_model->getFields('insert');
|
|
$this->_viewDatas['fieldRules'] = $this->_model->getFieldRules($this->_viewDatas['fields'], 'insert');
|
|
$this->_viewDatas['fieldFilters'] = $this->_model->getFieldFilters();
|
|
$this->insert_validate();
|
|
//Transaction manully 시작
|
|
$this->_model->transBegin();
|
|
$this->insert_process();
|
|
//Transaction manully Commit
|
|
$this->_model->transCommit();
|
|
$msg = sprintf(
|
|
"%s에서 상품 %s개를 장바구니에 담았습니다.",
|
|
$this->_viewDatas['title'],
|
|
$this->_viewDatas['fieldDatas']['quantity']
|
|
);
|
|
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']));
|
|
} catch (\Exception $e) {
|
|
//Transaction manully Rollback
|
|
$this->_model->transRollback();
|
|
$msg = sprintf(
|
|
"%s에서 다음 오류로 인해 장바구니에 담기를 실패하였습니다.\n%s",
|
|
$this->_viewDatas['title'],
|
|
$e->getMessage()
|
|
);
|
|
log_message("error", $e->getMessage());
|
|
log_message("error", var_export($this->_viewDatas['fieldDatas'], true));
|
|
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']));
|
|
} finally {
|
|
$this->_session->setFlashdata("return_message", $msg);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//View관련
|
|
protected function view_process($entity)
|
|
{
|
|
$this->_viewDatas['user'] = $this->getUserModel()->getEntity([$this->getUserModel()->getPrimaryKey() => $entity->getUser_uid()]);
|
|
$this->_viewDatas['product'] = $this->getProductModel()->getEntity([$this->getProductModel()->getPrimaryKey() => $entity->getProduct_uid()]);
|
|
return $entity;
|
|
}
|
|
}
|