/**
  * Retrieve the details for a specific commodity
  * @param integer $id
  * @return array
  */
 public function actionView($id = NULL)
 {
     if ($id === NULL) {
         throw new HttpException(400, 'Missing ID parameter');
     }
     $query = Commodity::find()->where(['id' => $id]);
     return ResponseBuilder::build($query, 'commodities+stations', Yii::$app->request->get('sort', 'name'), Yii::$app->request->get('order', 'asc'));
 }
Exemplo n.º 2
0
 private function getAllSection()
 {
     $commodity = Commodity::find()->where(['uid' => Yii::$app->user->id])->all();
     $commodityArray = array();
     foreach ($commodity as $item) {
         $name = $item->getAttribute('name');
         $commodityArray[$name] = $name;
     }
     return $commodityArray;
 }
Exemplo n.º 3
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Commodity::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'uid' => Yii::$app->user->id]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
Exemplo n.º 4
0
 public function testAddItem()
 {
     $item = \App\Models\Commodity::find(1);
     $item2 = \App\Models\Commodity::find(2);
     Cart::addItem($item);
     $this->assertEquals(Cart::getItems()->count(), 1);
     $this->assertEquals(Cart::getTotalCost(), $item->getPrice());
     Cart::removeItemsById($item->getIdentifer());
     $this->assertEquals(Cart::getItems()->count(), 0);
     $this->assertEquals(Cart::getTotalCost(), 0);
     Cart::addItem($item);
     Cart::addItem($item2);
     Cart::addItem($item);
     $this->assertEquals(Cart::getItems()->count(), 2);
     $this->assertEquals(Cart::getTotalCost(), $item->getPrice() * 2 + $item2->getPrice());
     Cart::removeItems(['name' => $item->getName()]);
     $this->assertEquals(Cart::getItems()->count(), 1);
     $this->assertEquals(Cart::getTotalCost(), $item2->getPrice());
 }
Exemplo n.º 5
0
 public function validateCommodity($attribute, $params)
 {
     echo 'validateCommodity';
     $commodity = Commodity::find()->where('commodity_id = :commodity_id', [':commodity_id' => $this->{$attribute}])->one();
     if (is_null($commodity)) {
         return $this->addError($attribute, '商品不存在');
     }
     if ($this->scenario == 'create') {
         $orderCount = (new Query())->from(Order::tableName())->where('commodity = :commodity', [':commodity' => $this->{$attribute}])->count();
         $coCount = (new Query())->select(CommodityOrderDetail::tableName() . '.num as num')->from(CommodityOrder::tableName())->leftJoin(CommodityOrderDetail::tableName(), CommodityOrderDetail::tableName() . '.coid = ' . CommodityOrder::tableName() . '.id')->where('commodity_id = :commodity_id', [':commodity_id' => $this->{$attribute}])->sum('num');
         if ($orderCount + 1 == $coCount) {
             $sql = "update " . CommodityOrder::tableName() . " set op_statu = " . CommodityOrder::$_OP_FINISH . " where commodity_id = :commodity_id";
             $db = Yii::$app->db;
             $command = $db->createCommand($sql);
             $command->bindParam(":commodity_id", $this->{$attribute}, PDO::PARAM_STR);
             $command->execute();
         }
         if ($orderCount >= $coCount) {
             return $this->addError($attribute, '该商品订单已满');
         }
     }
 }
 /**
  * Import station commodities information
  * @param array $station
  * @param string $class
  * @param array $data
  * @return boolean
  */
 private function importStationCommodity($station, $class, $model, $data)
 {
     Yii::$app->db->createCommand('DELETE FROM station_commodities WHERE station_id = :station_id AND type=:type')->bindValue(':station_id', $station['id'])->bindValue(':type', $class)->execute();
     $i = 0;
     foreach ($data as $d) {
         if ($class === 'listings') {
             $commodity = Commodity::find()->where(['id' => $d['commodity_id']])->one();
         } else {
             $commodity = Commodity::find()->where(['name' => $d])->one();
         }
         if ($commodity !== NULL) {
             $model->attributes = ['station_id' => $station['id'], 'commodity_id' => $commodity->id, 'type' => (string) $class, 'supply' => isset($d['supply']) ? $d['supply'] : null, 'buy_price' => isset($d['buy_price']) ? $d['buy_price'] : null, 'sell_price' => isset($d['sell_price']) ? $d['sell_price'] : null, 'demand' => isset($d['demand']) ? $d['demand'] : null];
             if ($model->save()) {
                 $i++;
             }
         } else {
             Yii::warning("{$station['id']}::{$station['name']} - Couldn't find commodity {$commodity}", __METHOD__);
         }
     }
     $inflected = \yii\helpers\Inflector::humanize($class);
     $this->stdOut("    - {$inflected} :: {$i}\n");
 }
Exemplo n.º 7
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function delete($id)
 {
     //Soft delete the item
     $commodity = Commodity::find($id);
     $commodity->delete();
     // redirect
     return redirect()->to('commodity.index')->with('message', trans('messages.commodity-succesfully-deleted'));
 }