119 lines
4.6 KiB
PHP
119 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Front;
|
|
|
|
use App\Entities\OrderEntity;
|
|
use App\Models\OrderModel;
|
|
use App\Models\ProductModel;
|
|
use CodeIgniter\Cookie\Cookie;
|
|
use CodeIgniter\Cookie\CookieStore;
|
|
use CodeIgniter\Cookie\Exceptions\CookieException;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use DateTime;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class OrderController extends FrontController
|
|
{
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
$this->_model = new OrderModel($this->getFields());
|
|
parent::initController($request, $response, $logger);
|
|
$this->_viewDatas['title'] = lang($this->_model->getClassName() . '.title');
|
|
$this->_viewPath .= strtolower($this->_model->getClassName());
|
|
helper($this->_model->getClassName());
|
|
}
|
|
|
|
final public function getFields(string $action = ""): array
|
|
{
|
|
$fields = ['product_uid', "quantity", "price", "status"];
|
|
switch ($action) {
|
|
case "index":
|
|
case "excel":
|
|
case "view":
|
|
return ['product_uid', "quantity", "price", "status", "updated_at", "created_at"];
|
|
break;
|
|
default:
|
|
return $fields;
|
|
break;
|
|
}
|
|
}
|
|
final public function getFieldFilters(): array
|
|
{
|
|
return ['product_uid', "status"];
|
|
}
|
|
final public function getFieldBatchFilters(): array
|
|
{
|
|
return ["status"];
|
|
}
|
|
|
|
//세션에 장바구니에 담긴 Order UID를 추가해준다.
|
|
private function setCart(OrderEntity $entity)
|
|
{
|
|
$order_uids = $this->_session->get(SESSION_NAMES['CART']) ?: array();
|
|
$this->_session->set(SESSION_NAMES['CART'], [...$order_uids, $entity->getPrimaryKey()]);
|
|
}
|
|
|
|
//장바구니에 담기
|
|
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->getFields(__FUNCTION__);
|
|
$this->_viewDatas['fieldRules'] = $this->_model->getFieldRules($this->_viewDatas['fields'], __FUNCTION__);
|
|
$this->insert_process();
|
|
//Transaction 시작
|
|
$this->_model->transStart();
|
|
$entity = $this->_model->create($this->_viewDatas['fieldDatas']);
|
|
$this->setCart($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);
|
|
}
|
|
}
|
|
|
|
protected function index_setCondition()
|
|
{
|
|
parent::index_setCondition();
|
|
//세션에 Cart정보(order_uids)가 있으면
|
|
$uids = $this->_session->get(SESSION_NAMES['CART']) ?: array('NONE');
|
|
//또는 Login했으면 사용자정보(user_uid)에 맞는 Order정보 가져오기
|
|
if ($this->_session->get(SESSION_NAMES['ISLOGIN'])) {
|
|
$this->_model->where('user_uid', $this->_session->get(SESSION_NAMES['AUTH'])[AUTH_FIELDS['ID']]);
|
|
} elseif (count($uids)) {
|
|
$this->_model->whereIn('uid', $uids);
|
|
}
|
|
}
|
|
}
|