Exemplo n.º 1
0
 /**
  * 获取商品对象
  */
 public function getGoods()
 {
     if ($this->getIsNewRecord()) {
         return [];
     }
     $data = Goods::findOne($this->getAttribute('gid'));
     return $data;
 }
Exemplo n.º 2
0
 /**
  * goodsIdList = [goodID => quantity]
  * @param int $orderID
  * @param array $goodsIdList
  */
 public static function saveOrderItems($orderID, $goodsIdList)
 {
     foreach ($goodsIdList as $id => $quantity) {
         $orderItemModel = new OrderItems();
         $orderItemModel->order_id = $orderID;
         $orderItemModel->goods_id = $id;
         $orderItemModel->multiplicity = $quantity;
         $orderItemModel->firm_id = Goods::findOne($id)->firm_id;
         $orderItemModel->save();
     }
 }
Exemplo n.º 3
0
 public static function del($id)
 {
     $connection = Yii::$app->db;
     //开启事务
     $transaction = $connection->beginTransaction();
     try {
         Goods::findOne($id)->delete();
         GoodsColor::deleteAll(['gid' => $id]);
         GoodsInventory::deleteAll(['gid' => $id]);
         //提交
         $transaction->commit();
         return true;
     } catch (Exception $e) {
         $transaction->rollBack();
         return false;
     }
 }
Exemplo n.º 4
0
 public function actionIndex()
 {
     $request = Yii::$app->getRequest();
     $id = current($request->get());
     //查询商品数据
     $goods = Goods::findOne($id);
     //查询收藏数量
     $collectNum = Collect::find()->where(['gid' => $id])->count('id');
     //颜色
     $goodsColor = GoodsColor::findAll(['gid' => $id]);
     $waresDir = 'static/uploaded/wares/i' . $id;
     //读取放大镜图片
     $files = scandir($waresDir);
     foreach ($files as $file) {
         if ($file == '.' || $file == '..' || $file == 'thumbnail') {
             continue;
         }
         $waresFilePath[] = $waresDir . '/' . $file;
     }
     $imgextraDir = 'static/uploaded/imgextra/i' . $id;
     //读取介绍图片
     $files = scandir($imgextraDir);
     foreach ($files as $file) {
         if ($file == '.' || $file == '..' || $file == 'thumbnail') {
             continue;
         }
         $imgextraFilePath[] = $imgextraDir . '/' . $file;
     }
     //查询商品评论
     $discuss = Discuss::findAll(['gid' => $id]);
     // 查询出商品库存总数和尺码
     $inventory = GoodsInventory::findAll(['gid' => $id]);
     $num = 0;
     foreach ($inventory as $key => $value) {
         $num += $value['inventory'];
         $size[$value['size']] = $value['size'];
     }
     $data = ['goods' => $goods, 'size' => $size, 'inventory' => $num, 'discuss' => $discuss, 'color' => $goodsColor, 'collectNum' => $collectNum, 'waresFile' => $waresFilePath, 'imgextraFile' => $imgextraFilePath];
     // print_r($goods);exit;
     return $this->render('index', $data);
 }
Exemplo n.º 5
0
 public function actionDelete($id)
 {
     $idstrList = explode(',', $id);
     $ids = [];
     foreach ($idstrList as $perId) {
         $ids[] = new MongoId($perId);
     }
     $where = ['_id' => ['$in' => $ids]];
     $goodsWhere = ['productId' => ['$in' => $ids]];
     $result = Product::findOne(array_merge($where, ['isBindCode' => true]));
     if (!empty($result)) {
         throw new BadRequestHttpException(Yii::t('product', 'can_not_delete_before_promocode'));
     }
     if (!empty(Campaign::getByProductIds($ids)) || !empty(Goods::findOne($goodsWhere))) {
         throw new BadRequestHttpException(Yii::t('product', 'can_not_delete'));
     }
     if (Product::deleteAll($where) == false) {
         throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');
     }
     //delete the product intro
     ProductInfo::deleteAll($where);
     //delete goods and storeGoods
     $this->attachBehavior('ProductBehavior', new ProductBehavior());
     $this->delete($ids);
     Yii::$app->getResponse()->setStatusCode(204);
 }
Exemplo n.º 6
0
 /**
  * check the goods info
  * @param $goods array
  */
 public static function checkGoodsInfo($goods)
 {
     //get the productId
     $productIdList = [];
     foreach ($goods as $key => $info) {
         if (empty($info['productId'])) {
             throw new ServerErrorHttpException(Yii::t('product', 'invalide_params'));
         }
         if (!isset($info['score'])) {
             throw new ServerErrorHttpException(Yii::t('product', 'score_not_empty'));
         }
         if (!isset($info['total'])) {
             throw new ServerErrorHttpException(Yii::t('product', 'total_not_empty'));
         }
         $productIdList[] = new MongoId($info['productId']);
     }
     //check  all product whether can be found in the product table
     $products = Product::findAll(['_id' => ['$in' => $productIdList]]);
     if (count($products) != count($productIdList) || count($products) <= 0) {
         throw new ServerErrorHttpException(Yii::t('product', 'invalide_params'));
     }
     //check the goods
     if (Goods::findOne(['productId' => ['$in' => $productIdList]])) {
         throw new ServerErrorHttpException(Yii::t('product', 'not_add_again'));
     }
     $data = [];
     foreach ($products as $product) {
         $key = $product['_id'] . '';
         $data[$key]['productName'] = $product['name'];
         $data[$key]['sku'] = $product['sku'];
     }
     unset($key, $productIdList);
     foreach ($goods as $key => $value) {
         $productId = $value['productId'] . '';
         $goods[$key]['productName'] = $data[$productId]['productName'];
         $goods[$key]['sku'] = $data[$productId]['sku'];
     }
     unset($productId, $data, $products);
     return $goods;
 }
Exemplo n.º 7
0
 /**
  * Finds the Goods model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param string $id
  * @return Goods the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Goods::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Exemplo n.º 8
0
 /**
  * goodsIdList = [goodID => quantity]
  * @param array $goodsIdList
  * @return int $orderID
  */
 public static function saveOrder($goodsIdList)
 {
     /* all the goods in $goodsIdList are ordered from the same firm */
     $goodID = key($goodsIdList);
     $firmID = Goods::findOne($goodID)->firm_id;
     $totalGoods = $priceWithVat = $priceWithoutVat = $incrementPrice = 0;
     foreach ($goodsIdList as $id => $quantity) {
         $totalGoods += $quantity;
         $good = Goods::findOne($id);
         $priceWithVat += $good->price_with_vat * $quantity;
         $priceWithoutVat += $good->price_without_vat * $quantity;
         $incrementPrice += $good->increment_price * $quantity;
     }
     $order = new Orders();
     $order->firm_id = $firmID;
     $order->price_with_vat = $priceWithVat;
     $order->price_without_vat = $priceWithoutVat;
     $order->increment_price = $incrementPrice;
     $order->total_types = count($goodsIdList);
     $order->total_goods = $totalGoods;
     $order->save();
     $orderID = $order->getPrimaryKey();
     return $orderID;
 }
Exemplo n.º 9
0
 /**
  * The default implementation returns the names of the columns whose values have been populated into Product.
  */
 public function fields()
 {
     return array_merge(parent::fields(), ['sku', 'name', 'pictures', 'category' => function () {
         $categorys = $this->category;
         if (!empty($categorys['id'])) {
             $categorys['id'] = $categorys['id'] . '';
         }
         return $categorys;
     }, 'intro' => function () {
         return ProductInfo::findByPk($this->_id)['intro'];
     }, 'isAssigned' => function () {
         if ($this->isBindCode) {
             return true;
         } else {
             return false;
         }
     }, 'codeNum' => function () {
         return PromotionCode::count(['productId' => $this->_id]);
     }, 'isSelected' => function () {
         $goods = Goods::findOne(['productId' => $this->_id]);
         if (empty($goods)) {
             return false;
         } else {
             return true;
         }
     }]);
 }