public function api_add($item_id, $count = 1, $options = '', $increaseOnDuplicate = true) { $item = Item::findOne($item_id); if (!$item) { return ['result' => 'error', 'code' => self::ERROR_ITEM_NOT_FOUND, 'error' => 'Item no found']; } if (!$this->order->id) { if (!$this->order->model->save()) { return ['result' => 'error', 'code' => self::ERROR_CREATE_ORDER, 'error' => 'Cannot create order. ' . $this->order->formatErrors()]; } Yii::$app->session->set(Order::SESSION_KEY, $this->order->model->access_token); } $good = Good::findOne(['order_id' => $this->order->id, 'item_id' => $item->primaryKey, 'options' => $options]); if ($good && !$increaseOnDuplicate) { return ['result' => 'error', 'code' => self::ERROR_GOOD_DUPLICATE, 'error' => 'Dublicate good in order.']; } if ($good) { $good->count += $count; } else { $good = new Good(['order_id' => $this->order->id, 'item_id' => $item->primaryKey, 'count' => (int) $count, 'options' => $options, 'discount' => $item->discount, 'price' => $item->price]); } if ($good->save()) { $response = ['result' => 'success', 'order_id' => $this->order->id, 'good_id' => $good->primaryKey, 'item_id' => $item->primaryKey, 'options' => $good->options, 'discount' => $good->discount]; if ($response['discount']) { $response['price'] = round($good->price * (1 - $good->discount / 100)); $response['old_price'] = $good->price; } else { $response['price'] = $good->price; } return $response; } else { return ['result' => 'error', 'code' => self::ERROR_CREATE_GOOD, 'error' => $good->formatErrors()]; } }
public function actionSort($id, $start_pos, $end_pos) { $item = Item::findOne($id); return $this->sort($id, $start_pos, $end_pos, ['category_id' => $item->category_id]); }
public function actionDelete($id) { if ($model = Item::findOne($id)) { $model->delete(); } else { $this->error = Yii::t('easyii', 'Not found'); } return $this->formatResponse(Yii::t('easyii/catalog', 'Item deleted')); }
public function checkImport($file = null) { if (!file_exists($file)) { throw new \Exception('Файл не загружен'); } $reader = \PHPExcel_IOFactory::createReader(\PHPExcel_IOFactory::identify($file)); $this->_phpExcel = $reader->load($file); $this->_worksheet = $this->_phpExcel->setActiveSheetIndex(0); $export = $this->getExport(); $highestRow = $this->_worksheet->getHighestRow(); $highestCol = $this->_worksheet->getHighestColumn(); $result = new ImportResult(); $sheetData = []; $templateItem = new Item(); $templateSeo = new SeoText(); $rowIterator = $this->_worksheet->getRowIterator(); $rowIterator->next(); // Пропускаем заголовки // Собираем строки for ($i = 2; $i <= $highestRow; ++$i) { $sheetData[] = $this->_worksheet->rangeToArray("A{$i}:{$highestCol}{$i}", '', false, false, false)[0]; } // Собираем id для проверки на удалённые строки $itemsId = []; foreach ($sheetData as $sheetDataRow) { $itemsId[] = $sheetDataRow[0]; } $result->checkDeletedItems($itemsId, $this->categoryId); foreach ($sheetData as $sheetDataRow) { $itemId = $sheetDataRow[0]; $item = Item::findOne($itemId); if (!$item) { $itemId = $result->addNewItem($itemId); $item = $templateItem; $seo = $templateSeo; /* $result->addInvalidItem($itemId); continue; */ } else { $seo = $item->seo; } foreach ($export as $exportIndex => $exportValue) { if ($exportIndex == 0) { continue; } $value = $sheetDataRow[$exportIndex]; switch ($exportValue[0]) { case static::TYPE_ATTRIBUTE: $result->addValue($itemId, $exportValue[0] . '.' . $exportValue[1], $item->getAttributeLabel($exportValue[1]), $item->{$exportValue[1]}, $value); break; case static::TYPE_FIELD: $field = $this->_category->getFieldByName($exportValue[1]); $result->addValue($itemId, $exportValue[0] . '.' . $exportValue[1], $field->admin_name ?: $field->title, ArrayHelper::getValue($item->data, $exportValue[1]), $value); break; case static::TYPE_SEO: $result->addValue($itemId, $exportValue[0] . '.' . $exportValue[1], $seo->getAttributeLabel($exportValue[1]), $item->seo->{$exportValue[1]}, $value); break; case static::TYPE_CUSTOM: $result->addValue($itemId, $exportValue[0] . '.' . $exportValue[1], $exportValue[2]['label'], $exportValue[2]['get']($item), $exportValue[2]['preSet']($item, $value)); break; } } } return $result; }