Exemple #1
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCoupon()
 {
     return $this->hasOne(Coupon::className(), ['id' => 'coupon_id']);
 }
 /**
  * Finds the Coupon model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Coupon the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Coupon::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * Updates an existing Order 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);
     if (!Yii::$app->user->isGuest) {
         $model->captchaRequired = false;
     }
     if (Yii::$app->request->post()) {
         $transaction = Yii::$app->db->beginTransaction();
         try {
             $post = Yii::$app->request->post();
             $data = [];
             if (isset($post['Order']['data'])) {
                 $cart = Yii::$app->session->get('YES_SHOPCART');
                 $data = $post['Order']['data'];
                 $data["cart"] = json_encode($cart);
                 $vtotal = 0;
                 $wtotal = 0;
                 $ptotal = 0;
                 foreach ($cart as $c) {
                     $qty = $c['quantity'];
                     $wtotal += $c['data_weight'] * $qty;
                     $vtotal += $c['data_vat'] * $c['price'] * $qty;
                     $ptotal += ($c['data_vat'] * $c['price'] + $c['price']) * $qty;
                 }
                 $data["vat"] = $vtotal;
                 $shipping = false;
                 if (isset($data["shipping"])) {
                     $shipping = json_decode($data["shipping"]);
                     if (empty($shipping->code)) {
                         $shipping = false;
                     }
                 }
                 $valid = true;
                 if (!$shipping) {
                     if ($wtotal > 0) {
                         $valid = false;
                     }
                 } else {
                     $ship = Shipping::findOne(['code' => $shipping->code]);
                     $shipdata = json_decode($ship->data);
                     foreach ($shipdata as $s) {
                         if ($s->provider == $shipping->provider) {
                             $shipping->cost = $s->cost;
                             $shippingcost = $shipping->cost * ceil($wtotal);
                             $ptotal += $shippingcost;
                         }
                     }
                     $data["shipping"] = json_encode($shipping);
                     $data["shippingcost"] = $shippingcost;
                 }
                 $redeem = 0;
                 $couponcode = isset($post['Order']['complete_reference']) ? isset($post['Order']['complete_reference']['coupon']) ? $post['Order']['complete_reference']['coupon'] : null : null;
                 if ($couponcode != null) {
                     $now = date('Y-m-d H:i:s');
                     $coupon = Coupon::find()->where("isdel = 0 and status = 1 and time_from <= '" . $now . "' and time_to >= '" . $now . "' and code = :code", [':code' => $couponcode])->one();
                     if ($coupon) {
                         if ($coupon->price > 0) {
                             $redeem = $coupon->price * -1;
                         } elseif ($coupon->price <= 0 && $coupon->discount > 0) {
                             $redeem = $coupon->discount / 100 * $ptotal * -1;
                         }
                         $data["coupon"] = $redeem;
                         $data["couponcode"] = $couponcode;
                     }
                 }
                 $ptotal = max(0, $ptotal + $redeem);
                 if (isset($post['Order']['customer_id'])) {
                     $email = isset($post['Order']['complete_reference']) ? isset($post['Order']['complete_reference']['email']) ? $post['Order']['complete_reference']['email'] : null : null;
                     unset($post['Order']['complete_reference']);
                     $data["customer"] = $post['Order']['customer_id'];
                     //$customer = Customer::find()->where(["name"=>$data["customer"]["name"],"email"=>$data["customer"]["email"]])->one();
                     if ($email != null) {
                         $customer = Customer::find()->where("email = :email OR concat(',',phones,',') like :phone", [":email" => $email, ":phone" => "%," . $data["customer"]["phones"] . ",%"])->one();
                     } else {
                         $customer = Customer::find()->where("concat(',',phones,',') like :phone", [":phone" => "%," . $data["customer"]["phones"] . ",%"])->one();
                     }
                     if (!$customer) {
                         $customer = new Customer();
                         $customer->isdel = 0;
                     }
                     $shipping = json_decode($data["shipping"]);
                     $phones = empty($customer->phones) ? [] : explode(",", $customer->phones);
                     $phones = array_unique(array_merge($phones, explode(",", $post['Order']['customer_id']['phones'])));
                     $addresses = array_unique(array_merge(json_decode($customer->addresses == null ? "[]" : $customer->addresses), array($post['Order']['customer_id']['address'] . ", code:" . $shipping->code)));
                     $customer->phones = implode(",", $phones);
                     $customer->addresses = json_encode($addresses);
                     $customer->name = $data["customer"]["name"];
                     $customer->email = $data["customer"]["email"];
                     if ($customer->save()) {
                         $post['Order']['customer_id'] = $customer->id;
                     } else {
                         $post['Order']['customer_id'] = null;
                     }
                 }
                 if (!$valid) {
                     $post['Order']['data'] = null;
                 } else {
                     $post['Order']['data'] = json_encode($data);
                 }
                 $post['Order']['total'] = $ptotal;
             }
             $model->load($post);
             $model->log = json_encode($_SERVER);
             if ($model->save()) {
                 Yii::$app->session->set('YES_SHOPCART', null);
                 $transaction->commit();
                 return $this->redirect(['view', 'reference' => $model->reference]);
             } else {
                 $transaction->rollBack();
             }
         } catch (Exception $e) {
             $transaction->rollBack();
         }
     } else {
         $data = json_decode($model->data);
         $cart = json_decode($data->cart);
         Yii::$app->session->set('YES_SHOPCART', ArrayHelper::toArray($cart));
     }
     return $this->render('update', ['model' => $model]);
 }