/**
  * @return \yii\db\ActiveQuery
  */
 public function getDeliveryDetails()
 {
     return $this->hasMany(DeliveryDetail::className(), ['product_id' => 'id']);
 }
 /**
  * Updates an existing Delivery model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $oldMoney = $model->money;
     $modelDetails = $model->deliveryDetails;
     if ($model->load(Yii::$app->request->post())) {
         $oldIDs = ArrayHelper::map($modelDetails, 'id', 'id');
         $modelDetails = Delivery::createMultiple(DeliveryDetail::className(), $modelDetails);
         Delivery::loadMultiple($modelDetails, Yii::$app->request->post());
         $deleteIDS = array_diff($oldIDs, array_filter(ArrayHelper::map($modelDetails, 'id', 'id')));
         //ajax validation
         if (Yii::$app->request->isAjax) {
             Yii::$app->response->format = Response::FORMAT_JSON;
             return ArrayHelper::merge(ActiveForm::validateMultiple($modelDetails), ActiveForm::validate($model));
         }
         // set the time
         if ($model->time) {
             date_default_timezone_set("Asia/ShangHai");
             $model->time .= "  " . date("H:i:s");
         }
         //validate all models
         $valid = $model->validate();
         $valid = Delivery::validateMultiple($modelDetails) && $valid;
         $profit = 0;
         if ($valid) {
             $transcation = Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     if (!empty($deleteIDS)) {
                         DeliveryDetail::deleteAll(['id' => $deleteIDS]);
                     }
                     foreach ($modelDetails as $modelDetail) {
                         $modelDetail->delivery_id = $model->id;
                         //count the profit;
                         $profit += $modelDetail->count * ($modelDetail->price - ($modelDetail->product->unit == 'B' ? $modelDetail->product->cost : $modelDetail->product->cost * $modelDetail->product->specification));
                         if (!($flag = $modelDetail->save(false))) {
                             $transcation->rollBack();
                         }
                     }
                 }
                 if ($flag) {
                     //update the money to customer
                     $customer = $model->customer;
                     $customer->unpay -= $oldMoney;
                     $customer->sum -= $oldMoney;
                     $customer->unpay += $model->money;
                     $customer->sum += $model->money;
                     $customer->save();
                     // save the profit
                     $model->profit = $profit;
                     $model->save();
                     $transcation->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 }
             } catch (Exception $e) {
                 $transcation->rollBack();
             }
         }
     }
     return $this->render('update', ['model' => $model, 'modelDetails' => empty($modelDetails) ? [new DeliveryDetail()] : $modelDetails]);
 }