public function actionAuditView($id)
 {
     $model = Commodity::findOne($id);
     $commodityOrderTemplet = new CommodityOrderTemplet();
     if ($commodityOrderTemplet->load(Yii::$app->request->post())) {
         $commodityOrderTemplet->cid = $model->id;
         $entrance = Entrance::findOne($commodityOrderTemplet->eid);
         if (!empty($entrance)) {
             $commodityOrderTemplet->entrance = $entrance->name;
         }
         $commodityOrderTemplet->save();
         //$commodityOrderTemplet->dirtyAttributes;
     }
     $dataProvider = new ActiveDataProvider(['query' => CommodityOrderTemplet::find()->where('cid = :cid', [':cid' => $id])->orderBy('keyword'), 'pagination' => false]);
     $entrances = Entrance::find()->all();
     $entrances_json = array();
     foreach ($entrances as $entrance) {
         $enarray = ['value' => $entrance->id, 'text' => $entrance->name];
         array_push($entrances_json, $enarray);
     }
     $entrances_json = json_encode($entrances_json);
     return $this->render('audit-view', array('model' => $model, 'dataProvider' => $dataProvider, 'commodityOrderTemplet' => $commodityOrderTemplet, 'entrances' => $entrances, 'entrances_json' => $entrances_json));
 }
 public function actionCreate($cid)
 {
     $commodity = Commodity::findOne($cid);
     if (empty($commodity)) {
         throw new HttpException(404, '商品不存在!');
     }
     if ($commodity->statu != Commodity::$_AUDIT_ACCESS) {
         throw new HttpException(404, '商品未审核通过!');
     }
     $model = new CommodityOrder();
     if ($model->load(Yii::$app->request->post())) {
         $model->create_time = date('Y-m-d H:i:s');
         $model->uid = $this->user->id;
         $model->statu = CommodityOrder::$_AUDIT_PEND;
         $model->op_statu = CommodityOrder::$_OP_NOT;
         $model->platform_id = $commodity->platform_id;
         if ($model->save()) {
             $commodityOrderDetails = Yii::$app->request->post('CommodityOrderDetail');
             $size = count($commodityOrderDetails['keyword']);
             $corpus = 0;
             $total_fee = 0;
             $budget_num = 0;
             for ($i = 0; $i < $size; $i++) {
                 $commodityOrderDetail = new CommodityOrderDetail();
                 $commodityOrderDetail->coid = $model->id;
                 $commodityOrderDetail->keyword = $commodityOrderDetails['keyword'][$i];
                 $commodityOrderDetail->eid = $commodityOrderDetails['eid'][$i];
                 $entrance = Entrance::findOne($commodityOrderDetail->eid);
                 if (!is_null($entrance)) {
                     $commodityOrderDetail->entrance = $entrance->name;
                 }
                 $commodityOrderDetail->condition = $commodityOrderDetails['condition'][$i];
                 $commodityOrderDetail->num = $commodityOrderDetails['num'][$i];
                 $commodityOrderDetail->price = $commodityOrderDetails['price'][$i];
                 $commodityOrderDetail->fee = $commodityOrderDetails['fee'][$i];
                 $commodityOrderDetail->save();
                 $num = intval($commodityOrderDetail->num);
                 $price = floatval($commodityOrderDetail->price);
                 $fee = floatval($commodityOrderDetail->fee);
                 $total_price = $num * $price;
                 $sum_fee = $num * $fee;
                 $corpus += $total_price;
                 $total_fee += $sum_fee;
                 $budget_num += $num;
             }
             $statistics = new Statistics();
             $statistics->coid = $model->id;
             $statistics->shop = $model->shop;
             $statistics->platform = $model->platform;
             $statistics->commodity = $model->commodity_id;
             $statistics->corpus = $corpus;
             $statistics->total_fee = $total_fee;
             $statistics->real_income = 0;
             $statistics->fact_fee = 0;
             $statistics->total_num = 0;
             $statistics->total_income = 0;
             $statistics->budget_num = $budget_num;
             $statistics->handle_time = $model->handle_time;
             $statistics->uid = $model->uid;
             $statistics->save();
             echo "\t<meta charset='utf-8'>";
             echo "<script>alert('提交成功!')</script>";
             echo "<script>history.go(-1);</script>";
             return;
         }
     }
     $model->cid = $cid;
     $model->shop = $commodity->shop;
     $model->commodity_id = $commodity->commodity_id;
     $model->sku = $commodity->sku;
     $model->img = $commodity->img;
     $model->rule = $commodity->rule;
     $model->buyer_rule = $commodity->buyer_rule;
     $model->remark = $commodity->remark;
     $model->platform = $commodity->platform;
     $model->credit = $commodity->credit;
     $model->trade_num = $commodity->trade_num;
     $dataProvider = new ActiveDataProvider(['query' => CommodityOrderTemplet::find()->where('cid = :cid', [':cid' => $cid])->orderBy('keyword'), 'pagination' => false]);
     return $this->render('create', array('model' => $model, 'commodity' => $commodity, 'dataProvider' => $dataProvider));
 }