/** * Import/Scrap a new Package model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionImport() { $model = new Package(); if (Yii::$app->request->isPost) { $product_url = $_POST['Package']['product_url']; $scraped = $this->_scrapAliExpressProduct($product_url); if (!empty($scraped['store_id'])) { $check_flag = Store::find()->where(['store_number' => $scraped['store_id']])->exists(); if ($check_flag == false) { $store = new Store(); $store->store_number = $scraped['store_id']; $store->name = urldecode($scraped['store_name']); $store->location = $scraped['store_location']; $store->since = date('Y-m-d', strtotime($scraped['store_since'])); $store->notes = "Import request Initiated from IP '" . Yii::$app->request->userIP . "'\n"; $store->save(); $data = ArrayHelper::toArray($store->id); $store_internal_id = $data[0]; } else { // store found so do nothing $primary = Store::find()->where(['store_number' => $scraped['store_id']])->one(); $data = ArrayHelper::toArray($primary); if ($primary != false) { $store_internal_id = $data['id']; } else { $store_internal_id = 0; } } } else { // if seller (store) information in unavailable $store_internal_id = 133; } // start updating the fields with latest data $model->store_id = $store_internal_id; $model->order_id = $_POST['Package']['order_id']; $model->order_date = date('Y-m-d'); // lets go with today's date $model->paid_with = "4"; // lets go with SCB-DebitCard $model->product_url = $product_url; $model->price = substr($scraped['price'], 0, 5); $model->description = substr($scraped['name'], 0, 75); $model->status = "Awaiting Shipment"; $model->notes = "Import request Initiated from IP '" . Yii::$app->request->userIP . "'\n" . "Data scrapped from " . $product_url; $model->save(); } if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['update', 'id' => $model->id]); } else { return $this->render('import', ['model' => $model]); } }