89 lines
3.5 KiB
PHP
89 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Entities\OrderEntity;
|
|
use App\Models\OrderModel;
|
|
use App\Models\ProductModel;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class OrderController extends AdminController
|
|
{
|
|
private $_cart_name = "order_uids";
|
|
private $_cart_delimeter = "||";
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
$this->_model = new OrderModel();
|
|
parent::initController($request, $response, $logger);
|
|
$this->_viewPath .= strtolower($this->_model->getClassName());
|
|
helper("cookie");
|
|
}
|
|
|
|
//쿠키에 장바구니에 담긴 Order UID를 추가해준다.
|
|
private function setOrderCookie(OrderEntity $entity)
|
|
{
|
|
$order_uids = array();
|
|
if (has_cookie($this->_cart_name)) {
|
|
$order_uids = explode($this->_cart_delimeter, get_cookie($this->_cart_name));
|
|
}
|
|
delete_cookie($this->_cart_name);
|
|
//24시간 저장
|
|
set_cookie([
|
|
'name' => $this->_cart_name,
|
|
'value' => implode($this->_cart_delimeter, [...$order_uids, $entity->getPrimaryKey()]),
|
|
'expire' => '36400',
|
|
]);
|
|
}
|
|
|
|
//장바구니에 담기
|
|
protected function insert_process()
|
|
{
|
|
//fieldData Rule 검사및 fieldData 적용
|
|
parent::insert_process();
|
|
//상품재고 줄이기
|
|
$productModel = new ProductModel();
|
|
$product = $productModel->getEntity([$this->_model->getPrimaryKey() => $this->_viewDatas['fieldDatas']['product_uid']]);
|
|
//구매 금액 비교
|
|
$price = ($product->getPrice() - $product->getSale()) * $this->_viewDatas['fieldDatas']['quantity'];
|
|
if ($price != $this->_viewDatas['fieldDatas']['price']) {
|
|
throw new \Exception("실 상품금액{$price} 와 구매금액{$this->_viewDatas['fieldDatas']['price']}이 서로 다릅니다.");
|
|
}
|
|
$productModel->decreaseStock($product, $this->_viewDatas['fieldDatas']['quantity']);
|
|
}
|
|
public function insert()
|
|
{
|
|
$msg = "";
|
|
try {
|
|
$this->_viewDatas['fields'] = $this->_model->getFields(__FUNCTION__);
|
|
$this->_viewDatas['fieldRules'] = $this->_model->getFieldRules($this->_viewDatas['fields'], __FUNCTION__);
|
|
//Transaction 시작
|
|
$this->insert_process();
|
|
$this->_model->transStart();
|
|
$entity = $this->_model->create($this->_viewDatas['fieldDatas']);
|
|
$this->setOrderCookie($entity);
|
|
//Transaction Commit
|
|
$this->_model->transComplete();
|
|
$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 Rollback
|
|
$this->_model->transRollback();
|
|
log_message("error", $e->getMessage());
|
|
$msg = sprintf(
|
|
"%s에서 다음 오류로 인해 장바구니에 담기를 실패하였습니다.\n%s",
|
|
$this->_viewDatas['title'],
|
|
$e->getMessage()
|
|
);
|
|
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']));
|
|
} finally {
|
|
$this->_session->setFlashdata("return_message", $msg);
|
|
}
|
|
}
|
|
}
|