public function run() { // Ключ кеша $key = $this->providerId . '_' . $this->filename . '_' . filectime(Yii::$app->params['uploadFolder'] . $this->filename); if (Yii::$app->cache->exists($key)) { $arRes = Yii::$app->cache->get($key); } else { // Удалить старые предложения поставщика \app\models\Offers::deleteAll('providers_id = :providers_id', [':providers_id' => $this->providerId]); // Сохранить дату последней загрузки прайса $provider = \app\models\Providers::findOne($this->providerId); $provider->date_last_down = time(); $provider->save(); // Получить файл $arrCSV = file(Yii::$app->params['uploadFolder'] . $this->filename); // Обработка каждой строки файла $arrToAccord = []; foreach ($arrCSV as $k => $strCSV) { // Строку в массив $arStrCSV = str_getcsv($strCSV, ';'); // Пропустить неполные строки if (strlen(trim($arStrCSV[$this->arrAccords['name']])) == 0 || strlen(trim($arStrCSV[$this->arrAccords['price']])) == 0 || strlen(trim($arStrCSV[$this->arrAccords['quantity']])) == 0) { continue; } // Нормализовать массив строки от текущего поставщика $arResRow = $this->normalizeStrOfPrice($arStrCSV); // Получить тип товара $arResRow = $this->getGoodType($arResRow); if (isset($this->arrAccords['identifier'])) { // Задан артикул $res = $this->getAccordsByIdentifier($arResRow); } else { // Не задан артикул $res = $this->getAccordsByName($arResRow); } if ($res instanceof Accords) { // Есть соответствие // Пишем в таблицу предложений $offer = new \app\models\Offers(); $offer->quantity = $arResRow['quantity']; $offer->price = $arResRow['price']; $offer->goods_id = $res->goods_id; $offer->providers_id = $res->providers_id; $offer->save(); } else { // Нет соответствия // Вывести на согласование $arResRow['k'] = $k; $arrToAccord[$k] = $arResRow; } // if ($k > 10) // break; // break; } // Сохранить полученные данные в кеш $arRes = ['arrToAccord' => $arrToAccord, 'arGoodTypes' => $this->arGoodTypes, 'providerId' => $this->providerId, 'key' => $key]; Yii::$app->cache->set($key, $arRes); } return $arRes; }
/** * Lists all Discounts models. * @return mixed */ public function actionIndex($providerId = null, $typePrice = null) { $session = Yii::$app->session; $session->open(); if ($providerId) { $session['providerIdFullName'] = $providerId; $provider = \app\models\Providers::findOne($providerId); $session['providerIdFullNameName'] = $provider->name; $this->redirect('/' . $this->id); } $arrPriceType = \yii\helpers\ArrayHelper::map(\app\models\PriceType::find()->all(), 'id', 'name'); if ($typePrice) { $session['typePrice'] = $typePrice; $session['typePriceName'] = $arrPriceType[$typePrice]; $this->redirect('/' . $this->id); } $searchModel = new DiscountsSearch(); $arParams = Yii::$app->request->queryParams; $arParams['DiscountsSearch']['providers_id'] = $session['providerIdFullName']; // if ($session['typePrice']) $arParams['DiscountsSearch']['price_type_id'] = $session['typePrice']; $dataProvider = $searchModel->search($arParams); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'providerName' => $session['providerIdFullNameName'], 'arrPriceType' => $arrPriceType, 'typePrice' => $session['typePrice'], 'typePriceName' => $session['typePriceName']]); }