Пример #1
0
 private function addGood($item, $verificationCode)
 {
     $group = ShopGroups::findOne(['verification_code' => strval($item->{'Группы'}->{'Ид'})]);
     $good = ShopGoods::findOne(['verification_code' => $verificationCode]);
     if ($good) {
         $link = Links::findOne($good->links_id);
     }
     if (!isset($link)) {
         $link = new Links();
     }
     $translit = new Translit();
     $link->categories_id = Yii::$app->params['shop']['categoriesId'];
     $link->parent = $group->links_id;
     $link->anchor = strval(isset($item->{'НаименованиеНаСайте'}) && $item->{'НаименованиеНаСайте'} ? $item->{'НаименованиеНаСайте'} : $item->{'Наименование'});
     $link->name = isset($link->id) ? $translit->slugify($link->anchor, $link->tableName(), 'name', '-', $link->id) : $translit->slugify($link->anchor, $link->tableName(), 'name', '-', null);
     $link->level = $group->link->level + 1;
     $link->url = (new Links())->getPrefixUrl(Yii::$app->params['shop']['goodUrlPrefix'], $link->level, $group->links_id) . '/' . $link->name;
     $link->child_exist = 0;
     $link->seq = isset($link->id) ? $link->seq : Links::findLastSequence(Yii::$app->params['shop']['categoriesId'], $link->parent) + 1;
     $link->title = isset($link->id) ? $link->title : $link->anchor;
     $link->created_at = isset($link->id) ? $link->created_at : time();
     $link->updated_at = time();
     $link->state = $item->{'НеПубликуетсяНаСайте'} == 'истина' ? 0 : 1;
     $link->layouts_id = Yii::$app->params['shop']['good_layouts_id'];
     $link->views_id = Yii::$app->params['shop']['good_views_id'];
     $link->save();
     $content = Contents::findOne(['links_id' => $link->id]);
     if (!$content) {
         $content = new Contents();
         $content->links_id = $link->id;
         $content->seq = 1;
     }
     $content->text = strval($item->{'Описание'});
     $content->save();
     if (!$good) {
         $good = new ShopGoods();
         $good->shop_groups_id = $group->id;
         $good->verification_code = $verificationCode;
     }
     $good->links_id = $link->id;
     $shopUnit = ShopUnits::findOne(['name' => $item->{'БазоваяЕдиница'}]);
     if (!$shopUnit) {
         $shopUnit = new ShopUnits();
         $shopUnit->name = strval($item->{'БазоваяЕдиница'});
         $shopUnit->save();
     }
     $good->shop_units_id = $shopUnit->id;
     $good->name = strval($item->{'Наименование'});
     $good->code = preg_replace('/^\\D+0*/', '', $item->{'КодНоменклатуры'});
     $good->state = 1;
     $good->save();
     if ($item->{'Картинки'} && $item->{'Картинки'}->{'Картинка'}) {
         $this->addImage($item->{'Картинки'}->{'Картинка'}, Yii::$app->params['shop']['gallery']['good'], $link->id, $good->id);
     }
     return ['id' => $good->id, 'links_id' => $link->id];
 }
Пример #2
0
 private function parserOffers($offers_sxe)
 {
     /* Поиск и удаление цен для товара и его характеристик */
     foreach ($offers_sxe as $item_sxe) {
         if (preg_match('/(.+)#(.+)/', $item_sxe->{'Ид'}, $matches)) {
             $good_verification_code = strval($matches[1]);
             $item_verification_code = strval($matches[2]);
             $prices_item = ShopPriceItem::find()->innerJoinWith('shopItem')->where(['shop_items.verification_code' => $item_verification_code])->all();
             if ($prices_item) {
                 ShopPriceItem::deleteAll(['id' => ArrayHelper::getColumn($prices_item, 'id')]);
             }
         } else {
             $good_verification_code = strval($item_sxe->{'Ид'});
         }
         $price_good = ShopPriceGood::find()->innerJoinWith('shopGood')->where(['shop_goods.verification_code' => $good_verification_code])->all();
         if ($price_good) {
             ShopPriceGood::deleteAll(['id' => ArrayHelper::getColumn($price_good, 'id')]);
         }
     }
     $price_types = ArrayHelper::map(ShopPriceTypes::find()->all(), 'verification_code', 'id');
     $good_min_price = array();
     foreach ($offers_sxe as $item_sxe) {
         $item_verification_code = false;
         if (preg_match('/(.+)#(.+)/', $item_sxe->{'Ид'}, $matches)) {
             $good_verification_code = strval($matches[1]);
             $item_verification_code = strval($matches[2]);
         } else {
             $good_verification_code = strval($item_sxe->{'Ид'});
         }
         if (!isset($good_min_price[$good_verification_code])) {
             $good_min_price[$good_verification_code] = array();
         }
         foreach ($item_sxe->{'Цены'}->{'Цена'} as $item_price) {
             $verification_code = strval($item_price->{'ИдТипаЦены'});
             if (!isset($good_min_price[$good_verification_code][$price_types[$verification_code]])) {
                 $good_min_price[$good_verification_code][$price_types[$verification_code]] = floatval($item_price->{'ЦенаЗаЕдиницу'});
             } else {
                 if ($good_min_price[$good_verification_code][$price_types[$verification_code]] > floatval($item_price->{'ЦенаЗаЕдиницу'})) {
                     $good_min_price[$good_verification_code][$price_types[$verification_code]] = floatval($item_price->{'ЦенаЗаЕдиницу'});
                 }
             }
             if ($item_verification_code) {
                 $item = ShopItems::findOne(['verification_code' => $item_verification_code]);
                 if ($item) {
                     $price_item = new ShopPriceItem();
                     $price_item->shop_items_id = $item->id;
                     $price_item->shop_price_types_id = $price_types[$verification_code];
                     $price_item->price = floatval($item_price->{'ЦенаЗаЕдиницу'});
                     $price_item->save();
                 }
             }
         }
     }
     foreach ($good_min_price as $good_verification_code => $good_prices) {
         $good = ShopGoods::findOne(['verification_code' => $good_verification_code]);
         if ($good) {
             foreach ($good_prices as $price_types_id => $good_price) {
                 $price_good = new ShopPriceGood();
                 $price_good->shop_goods_id = $good->id;
                 $price_good->shop_price_types_id = $price_types_id;
                 $price_good->price = $good_price;
                 $price_good->save();
             }
         }
     }
 }