_model = new ProductModel(); } //Category모델 final protected function getCategoryModel(): CategoryModel { return is_null($this->_categoryModel) ? new CategoryModel() : $this->_categoryModel; } //Field별 Form Option용 public function getFieldFormOption(string $field): array { switch ($field) { case 'category_uid': $options = $this->_category_uids = $this->_category_uids ?: $this->getCategoryModel()->getFieldFormOptions(['status' => 'use']); break; default: return parent::getFieldFormOption($field); break; } if (!is_array($options)) { throw new \Exception(__FUNCTION__ . "에서 {$this->getClassName()}의 Field:{$field}의 FormOptionData가 array가 아닙니다.\n" . var_export($options, true)); } return $options; } public function addCart(ProductEntity $product, int $quantity, int $price) { if (!$quantity) { throw new \Exception("제품 구매수량을 확인해주세요"); } if (!$price) { throw new \Exception("제품 구매금액을 확인해주세요"); } if ($product->getStock() < $quantity) { throw new \Exception(sprintf( "%s의 재고수[%s]보다 많은 수량[%s]을 주문하셨습니다.\\n", $product->getTitle(), $product->getStock(), $quantity )); } //구매금액 = (판매가-할인가)*구매수량과 맞는지 다시 확인. // $calculated_price = ($product->getPrice() - $product->getSale()) * $quantity; $calculated_price = ($product->getPrice() - $product->getSale()) * $quantity; if ($price != $calculated_price) { throw new \Exception( sprintf( "%s상품의 구매금액[%s원]과 판매금액[%s원]이 맞지않습니다.", $product->getTitle(), number_format($price), number_format($calculated_price) ) ); } //상품모델에서 Order에 담은 갯수만큼 재고에서 뺀다. $this->_model->addCart($product, $quantity); } }