_model = new OrderModel(); } //Porduct모델 final public function getProductModel(): ProductModel { return is_null($this->_productModel) ? new ProductModel() : $this->_productModel; } //Field별 Form Option용 public 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->getClassName()}의 Field:{$field}의 FormOptionData가 array가 아닙니다.\n" . var_export($options, true)); } return $options; } //Insert관련 //장바구니에 담기 및 Product 재고 차감 public function insert(array $fieldDatas): OrderEntity { //상품정보가져오기 $product = $this->getProductModel()->getEntity([$this->getProductModel()->getPrimaryKey() => $fieldDatas['product_uid']]); if (!$fieldDatas['quantity']) { throw new \Exception("제품 구매수량을 확인해주세요"); } if (!$fieldDatas['price']) { throw new \Exception("제품 구매금액을 확인해주세요"); } if ($product->getStock() < $fieldDatas['quantity']) { throw new \Exception(sprintf( "%s의 재고수[%s]보다 많은 수량[%s]을 주문하셨습니다.\\n", $product->getTitle(), $product->getStock(), $fieldDatas['quantity'] )); } //구매금액 = (판매가-할인가)*구매수량과 맞는지 다시 확인. // $calculated_price = ($product->getPrice() - $product->getSale()) * $quantity; $calculated_price = ($product->getPrice() - $product->getSale()) * $fieldDatas['quantity']; if ($fieldDatas['price'] != $calculated_price) { throw new \Exception( sprintf( "%s상품의 구매금액[%s원]과 판매금액[%s원]이 맞지않습니다.", $product->getTitle(), number_format($fieldDatas['price']), number_format($calculated_price) ) ); } //상품모델에서 Order에 담은 갯수만큼 재고에서 뺀다. $this->getProductModel()->decreaseStock($product, $fieldDatas['quantity']); return parent::insert($fieldDatas); } }