_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() { $this->_productModel ?: new ProductModel(); //상품정보 가져오기 $product = $this->_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']}이 서로 다릅니다."); } //상품재고 줄이기,Order(장바구니)에넣기,Order(장바구니)번호 세션넣기 $this->_productModel->decreaseStock($product, $this->_viewDatas['fieldDatas']['quantity']); //장바구니에 넣기 $entity = parent::insert_process(); //Cart세션정의 $this->setCart($entity); return $entity; } public function insert() { $msg = ""; try { //Transaction 시작 $this->_model->transStart(); $this->insert_process(); //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()->back()->withInput(); } 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); } } }