public function attributeLabels() { return array_merge(parent::attributeLabels(), ['term' => Yii::t('app', 'Term')]); }
/** * Finds the Shipping model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Shipping the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Shipping::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]); }
public static function importCsv($f, $data = false, $dn = false) { $session = Yii::$app->session; if (!$data) { $header = false; $data = []; while ($row = fgetcsv($f)) { if (!$header) { foreach ($row as $r) { $header[] = $r; } } else { $d = []; foreach ($row as $i => $r) { $h = $header[$i]; $d[$h] = $r; } $data[] = $d; } } $session["yes-shipping-f"] = $data; } $dno = $dn ? $dn : $session["yes-shipping-dn"]; $transaction = Yii::$app->db->beginTransaction(); $res = true; $error = []; try { if ($dno >= 0) { //foreach ($data as $d) for ($dni = 0; $dni < 100; $dni++) { $dn = $dno + $dni; if (isset($data[$dn])) { $d = $data[$dn]; if (isset($d["cost"]) && isset($d["provider"]) && isset($d["remarks"]) && isset($d["city"]) && isset($d["area"]) && isset($d["code"])) { if (floatval($d["cost"]) > 0) { $model = Shipping::find()->where('code = :code OR (city = :city AND area = :area)', [':code' => $d["code"], ':city' => $d["city"], ':area' => $d["area"]])->one(); if (!$model) { $model = new Shipping(); } $old = empty($model->data) ? [] : json_decode($model->data, true); $exists = false; $n = 1; $new = []; foreach ($old as $o) { if ($o["provider"] == $d["provider"]) { $exists = true; $o["cost"] = $d["cost"]; $o["remarks"] = $d["remarks"]; } $new[] = $o; $n += 1; } if (!$exists) { $new[$n . ""] = ["provider" => $d["provider"], "cost" => $d["cost"], "remarks" => $d["remarks"]]; } $model->code = $d["code"]; $model->city = $d["city"]; $model->area = $d["area"]; $model->data = json_encode($new); $model->isdel = 0; $model->status = 1; if (!$model->save()) { $res = $res ? false : $res; $error[] = [$model->attributes, $model->getErrors()]; } } } } } } if ($dn >= count($data) || $dn < 0) { unset($session["yes-shipping-f"]); unset($session["yes-shipping-dn"]); $data = []; } if ($res) { $transaction->commit(); } else { $transaction->rollBack(); } } catch (Exception $e) { $transaction->rollBack(); } $session["yes-shipping-dn"] = $dn; if ($res) { return ["status" => $res, "count" => count($data), "n" => $dn]; } else { return ["status" => $res, "count" => count($data), "n" => $dn, "error" => $error]; } }