Ejemplo n.º 1
0
 /**
  * Updates an existing product model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $thirdfundModel = [];
     $newthidModel = [];
     $modelmid = FundProductThirdproduct::find()->where(['product_id' => $id])->asArray()->all();
     if ($modelmid) {
         foreach ($modelmid as $v) {
             array_push($thirdfundModel, Thirdproduct::find()->where(['id' => $v['thirdproduct_id']])->asArray()->one());
         }
         $thmodels = Thirdproduct::find()->andWhere(['intent' => Thirdproduct::INTENT_CHECK])->andWhere(['status' => Thirdproduct::STATUS_ACTIVE])->andWhere(['process_status' => Thirdproduct::PROCESS_STATUS_INACTIVE])->asArray()->all();
         $newthidModel = array_merge($thirdfundModel, $thmodels);
     }
     $arraynew = [];
     foreach ($newthidModel as $n) {
         $arraynew[$n['id']]['id'] = $n['id'];
         $arraynew[$n['id']]['title'] = $n['title'];
     }
     $model = new product();
     if ($model->load(Yii::$app->request->post())) {
         if (\App::$app->request->post()['Product']['type'] == Product::TYPE_THIRD) {
             foreach ($modelmid as $V) {
                 $cthirdModel = Thirdproduct::find()->where(['id' => $V['thirdproduct_id']])->one();
                 $cthirdModel->process_status = 0;
                 $cthirdModel->save(false);
             }
             FundProductThirdproduct::deleteAll(['product_id' => $id]);
         }
         $products_arr = \App::$app->request->post()['Product']['temp_products'];
         $model->start_at = strtotime(\App::$app->request->post()["Product"]['start_at']);
         $end = \App::$app->request->post()["Product"]['end_at'] ? strtotime(Yii::$app->request->post()["Product"]['end_at']) : 0;
         $model->end_at = $end;
         $model->create_at = strtotime("now");
         $model->type = \App::$app->request->post()['Product']['type'];
         $model->each_min = \App::$app->request->post()['Product']['each_min'];
         $model->each_max = \App::$app->request->post()['Product']['each_max'];
         $model->rate = \App::$app->request->post()['Product']['rate'] * 0.01;
         $model->intro = \App::$app->request->post()['Product']['intro'];
         $model->title = \App::$app->request->post()['Product']['title'];
         $model->create_user_id = \App::$app->user->id;
         if (\App::$app->request->post()['Product']['type'] == Product::TYPE_THIRD) {
             $total = 0;
             foreach ($products_arr as $K => $V) {
                 $thirModel = Thirdproduct::findOne(['id' => $V]);
                 $total += $thirModel->amount - $thirModel->invest_sum;
             }
             $model->amount = $total;
         } else {
             // $model->title = \App::$app->request->post()['Product']['amount'];
             $model->amount = \App::$app->request->post()['Product']['amount'];
         }
         $model->contract = UploadedFile::getInstance($model, 'contract');
         if ($model->contract) {
             $contractName = mt_rand(1100, 9900) . time() . '.' . $model->contract->extension;
             $model->contract->saveAs('../web/upload/' . $contractName);
             $model->contract = $contractName;
         }
         if (!$model->contract) {
             $new = $this->findModel($id);
             $model->contract = $new->contract;
         }
         if ($model->save()) {
             //把第三方项目存入中间表
             if (\App::$app->request->post()['Product']['type'] == Product::TYPE_THIRD) {
                 foreach ($products_arr as $K => $V) {
                     $newModel = new FundProductThirdproduct();
                     $newModel->thirdproduct_id = $V;
                     $newModel->product_id = $model->id;
                     $newModel->save();
                     $thirModel = Thirdproduct::findOne(['id' => $V]);
                     $thirModel->process_status = 1;
                     $thirModel->save(false);
                 }
             } else {
                 $thirdproduct = new Thirdproduct();
                 $thirdproduct->title = \App::$app->request->post()['Product']['title'];
                 $thirdproduct->intro = \App::$app->request->post()['Product']['intro'];
                 $thirdproduct->source = '本站';
                 $thirdproduct->creditor = \App::$app->request->post()['Product']['ocreditor'];
                 $thirdproduct->contract = $model->contract;
                 $thirdproduct->realname = $model->contract;
                 $thirdproduct->remarks = \App::$app->request->post()['Product']['intro'];
                 $thirdproduct->amount = $model->amount;
                 $thirdproduct->ocmoney = $model->amount;
                 $thirdproduct->start_at = $model->start_at;
                 $thirdproduct->end_at = $model->end_at;
                 $thirdproduct->status = Thirdproduct::STATUS_ACTIVE;
                 $thirdproduct->create_user_id = \App::$app->user->id;
                 $thirdproduct->check_user_id = \App::$app->user->id;
                 $thirdproduct->rate = \App::$app->request->post()['Product']['rate'] * 0.01;
                 $thirdproduct->process_status = 1;
                 $thirdproduct->intent = Thirdproduct::INTENT_CHECK;
                 $thirdproduct->maxcreditor = \App::$app->request->post()['Product']['maxcreditor'];
                 $thirdproduct->save();
                 $thirdmid = new FundProductThirdproduct();
                 $thirdmid->thirdproduct_id = $thirdproduct->id;
                 $thirdmid->product_id = $model->id;
                 $thirdmid->save();
             }
             Product::deleteAll(['id' => $id]);
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         //var_dump($newthidModel);
         $model = $this->findModel($id);
         $model['start_at'] = date("Y-m-d H:i", $model['start_at']);
         $model['end_at'] = date("Y-m-d H:i", $model['end_at']);
         $model['rate'] = $model['rate'] * 100;
         return $this->render('update', ['model' => $model, 'products' => $arraynew]);
     }
 }