コード例 #1
0
 /**
  * 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]);
 }
コード例 #2
0
ファイル: Order.php プロジェクト: highestgoodlikewater/yes
 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;
 }
コード例 #3
0
 public static function find()
 {
     return parent::find()->where([Customer::tableName() . '.isdel' => 0]);
 }
コード例 #4
0
 public function actionSearch($format = false, $arraymap = false, $term = false)
 {
     $post = Yii::$app->request->post();
     $format = isset($post["format"]) ? $post["format"] : $format;
     $term = isset($post["term"]) ? $post["term"] : $term;
     $sql = "lower(name) LIKE :name";
     $search = [":name" => "%" . strtolower($term) . "%"];
     $arraymap = "name";
     $rs = false;
     if (isset($post["email"])) {
         if (!empty($post["email"])) {
             $sql .= " AND email = :email";
             $search[":email"] = $post["email"];
             $rs = true;
         }
     }
     if (isset($post["phones"])) {
         if (!empty($post["phones"])) {
             $sql .= " AND concat(',',phones,',') LIKE :phones";
             $search[":phones"] = "%," . $post["phones"] . ",%";
             $rs = true;
         }
     }
     if ($rs) {
         $arraymap = "name,email,phones,addresses";
     }
     $models = Customer::find()->where($sql, $search)->all();
     //print_r($search);
     //die($sql);
     if ($format == 'json') {
         $model = [];
         foreach ($models as $d) {
             $obj = $d->attributes;
             if ($arraymap) {
                 $map = explode(",", $arraymap);
                 if (count($map) == 1) {
                     $obj = isset($d[$arraymap]) ? $d[$arraymap] : null;
                 } else {
                     $obj = [];
                     foreach ($map as $a) {
                         $k = explode(":", $a);
                         $v = count($k) > 1 ? $k[1] : $k[0];
                         $obj[$k[0]] = $v == "Obj" ? json_encode($d->attributes) : (isset($d->{$v}) ? $d->{$v} : null);
                     }
                 }
             }
             if ($term) {
                 if (!in_array($obj, $model)) {
                     array_push($model, $obj);
                 }
             } else {
                 array_push($model, $obj);
             }
         }
         return \yii\helpers\Json::encode($model);
     }
 }
コード例 #5
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = $this->find();
     $query->joinWith(['customer']);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $dataProvider->sort->attributes['customerAdminName'] = ['asc' => [Customer::tableName() . '.name' => SORT_ASC], 'desc' => [Customer::tableName() . '.name' => SORT_DESC]];
     $dataProvider->sort->attributes['customerName'] = ['asc' => [Customer::tableName() . '.name' => SORT_ASC], 'desc' => [Customer::tableName() . '.name' => SORT_DESC]];
     /* uncomment to sort by relations table on respective column
     		$dataProvider->sort->attributes['confirmationsId'] = [			
     			'asc' => ['{{%confirmations}}.id' => SORT_ASC],
     			'desc' => ['{{%confirmations}}.id' => SORT_DESC],
     		];		
     		$dataProvider->sort->attributes['salesId'] = [			
     			'asc' => ['{{%sales}}.id' => SORT_ASC],
     			'desc' => ['{{%sales}}.id' => SORT_DESC],
     		];*/
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $params = self::queryNumber([['id', $this->tableName()], ['customer_id'], ['total'], ['status'], ['isdel']]);
     foreach ($params as $p) {
         $query->andFilterWhere($p);
     }
     $params = self::queryString([['reference'], ['data'], ['complete_reference'], ['log']]);
     foreach ($params as $p) {
         $query->andFilterWhere($p);
     }
     $params = self::queryTime([['time'], ['complete_time']]);
     foreach ($params as $p) {
         $query->andFilterWhere($p);
     }
     $query->andFilterWhere(['like', "lower(concat(" . Customer::tableName() . ".name," . Customer::tableName() . ".email," . Customer::tableName() . ".phones))", strtolower($this->customerAdminName)]);
     if ($this->customerName) {
         $query->andFilterWhere(['like', "lower(concat(','," . Customer::tableName() . ".name,','," . Customer::tableName() . ".email,','," . Customer::tableName() . ".phones,','))", strtolower("," . $this->customerName . ",")]);
     }
     return $dataProvider;
 }