示例#1
0
 public function createSales()
 {
     $data = json_decode($this->data);
     $cart = isset($data->cart) ? json_decode($data->cart) : null;
     $res = true;
     foreach ($cart as $p) {
         $remarks = "";
         foreach ($p as $k => $v) {
             if (substr($k, 0, 5) == "data_") {
                 $remarks .= ($remarks == "" ? "" : ", ") . substr($k, 5) . ": " . $v;
             }
         }
         $sale = Sale::findOne(['product_id' => $p->id, 'order_id' => $this->id, 'data' => $remarks]);
         if (!$sale) {
             $sale = new Sale();
         }
         $sale->time = date("Y-m-d H:i:s");
         $sale->isdel = 0;
         $sale->order_id = $this->id;
         $sale->product_id = $p->id;
         $sale->quantity = $p->quantity;
         $sale->amount = $p->quantity * $p->price;
         $sale->data = $remarks;
         if (!$sale->save()) {
             $res = false;
         } else {
             $customer = Customer::findOne($this->customer_id);
             if ($customer) {
                 $customer->last_action = 2;
                 $customer->last_time = date("Y-m-d H:i:s");
                 if (!$customer->save()) {
                     $res = false;
                 }
             }
         }
     }
     return $res;
 }
 /**
  * Creates a new Sale model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     return $this->redirect(['index']);
     $model = new Sale();
     $model->isdel = 0;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }