/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Client::find()->select("CONCAT_WS(' ',`surname`,clients.`name`) as name,phone,address, sum(cost*count) as cost,clients.id as id")->leftJoin(Order::tableName(), Order::tableName() . '.id_client=' . Client::tableName() . '.id')->groupBy('id_client'); $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(['name' => $this->name, 'surname' => $this->surname, 'phone' => $this->phone, 'address' => $this->address]); return $dataProvider; }
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, '该商品订单已满'); } } }
public function actionAudit() { $model = new OrderSearchForm(); $model->load(Yii::$app->request->post()); $query = (new Query())->select('*')->from(Order::tableName())->where('1=1'); if (!empty($model->buyer)) { $query->andWhere('buyer=:buyer', [':buyer' => $model->buyer]); } if (!empty($model->shop)) { $query->andWhere('shop=:shop', [':shop' => $model->shop]); } if (!empty($model->commodity)) { $query->andWhere('commodity_id like :commodity_id', [':commodity_id' => '%' . $model->commodity . '%']); } if (!empty($model->order_no)) { $query->andWhere('order_no=:order_no', [':order_no' => $model->order_no]); } if (!empty($model->btime)) { $query->andWhere('create_time >= :create_time', [':create_time' => $model->btime]); } if (!empty($model->etime)) { $query->andWhere('create_time <= :create_time', [':create_time' => $model->etime]); } $query->andWhere('statu = 0'); $query->orderBy('create_time desc'); $pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => '20']); $query->limit($pages->limit)->offset($pages->offset); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => false]); $audit_json = array(); foreach (Order::$Audits as $key => $value) { $enarray = ['value' => $key, 'text' => $value]; array_push($audit_json, $enarray); } $audit_json = json_encode($audit_json); return $this->render('audit', ['model' => $model, 'dataProvider' => $dataProvider, 'pages' => $pages, 'audit_json' => $audit_json]); }