AdvancedPricing.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AdvancedPricingImportExport\Model\Export;
  7. use Magento\ImportExport\Model\Export;
  8. use Magento\Store\Model\Store;
  9. use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
  10. use Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing as ImportAdvancedPricing;
  11. use Magento\Catalog\Model\Product as CatalogProduct;
  12. /**
  13. * Export Advanced Pricing
  14. *
  15. * @author Magento Core Team <core@magentocommerce.com>
  16. * @SuppressWarnings(PHPMD.TooManyFields)
  17. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class AdvancedPricing extends \Magento\CatalogImportExport\Model\Export\Product
  21. {
  22. const ENTITY_ADVANCED_PRICING = 'advanced_pricing';
  23. /**
  24. * @var \Magento\CatalogImportExport\Model\Import\Product\StoreResolver
  25. */
  26. protected $_storeResolver;
  27. /**
  28. * @var \Magento\Customer\Api\GroupRepositoryInterface
  29. */
  30. protected $_groupRepository;
  31. /**
  32. * @var string
  33. */
  34. protected $_entityTypeCode;
  35. /**
  36. * @var \Magento\Framework\App\ResourceConnection
  37. */
  38. protected $_resource;
  39. /**
  40. * @var int
  41. */
  42. protected $_passTierPrice = 0;
  43. /**
  44. * List of items websites
  45. *
  46. * @var array
  47. */
  48. protected $_priceWebsite = [
  49. ImportAdvancedPricing::COL_TIER_PRICE_WEBSITE,
  50. ];
  51. /**
  52. * List of items customer groups
  53. *
  54. * @var array
  55. */
  56. protected $_priceCustomerGroup = [
  57. ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP,
  58. ];
  59. /**
  60. * Export template
  61. *
  62. * @var array
  63. */
  64. protected $templateExportData = [
  65. ImportAdvancedPricing::COL_SKU => '',
  66. ImportAdvancedPricing::COL_TIER_PRICE_WEBSITE => '',
  67. ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => '',
  68. ImportAdvancedPricing::COL_TIER_PRICE_QTY => '',
  69. ImportAdvancedPricing::COL_TIER_PRICE => '',
  70. ImportAdvancedPricing::COL_TIER_PRICE_TYPE => ''
  71. ];
  72. /**
  73. * @var string[]
  74. */
  75. private $websiteCodesMap = [];
  76. /**
  77. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  78. * @param \Magento\Eav\Model\Config $config
  79. * @param \Magento\Framework\App\ResourceConnection $resource
  80. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  81. * @param \Psr\Log\LoggerInterface $logger
  82. * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
  83. * @param \Magento\ImportExport\Model\Export\ConfigInterface $exportConfig
  84. * @param \Magento\Catalog\Model\ResourceModel\ProductFactory $productFactory
  85. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFactory
  86. * @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryColFactory
  87. * @param \Magento\CatalogInventory\Model\ResourceModel\Stock\ItemFactory $itemFactory
  88. * @param \Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory $optionColFactory
  89. * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeColFactory
  90. * @param \Magento\CatalogImportExport\Model\Export\Product\Type\Factory $_typeFactory
  91. * @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
  92. * @param \Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer
  93. * @param ImportProduct\StoreResolver $storeResolver
  94. * @param \Magento\Customer\Api\GroupRepositoryInterface $groupRepository
  95. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  96. */
  97. public function __construct(
  98. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  99. \Magento\Eav\Model\Config $config,
  100. \Magento\Framework\App\ResourceConnection $resource,
  101. \Magento\Store\Model\StoreManagerInterface $storeManager,
  102. \Psr\Log\LoggerInterface $logger,
  103. \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory,
  104. \Magento\ImportExport\Model\Export\ConfigInterface $exportConfig,
  105. \Magento\Catalog\Model\ResourceModel\ProductFactory $productFactory,
  106. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFactory,
  107. \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryColFactory,
  108. \Magento\CatalogInventory\Model\ResourceModel\Stock\ItemFactory $itemFactory,
  109. \Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory $optionColFactory,
  110. \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeColFactory,
  111. \Magento\CatalogImportExport\Model\Export\Product\Type\Factory $_typeFactory,
  112. \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider,
  113. \Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer,
  114. \Magento\CatalogImportExport\Model\Import\Product\StoreResolver $storeResolver,
  115. \Magento\Customer\Api\GroupRepositoryInterface $groupRepository
  116. ) {
  117. $this->_storeResolver = $storeResolver;
  118. $this->_groupRepository = $groupRepository;
  119. $this->_resource = $resource;
  120. parent::__construct(
  121. $localeDate,
  122. $config,
  123. $resource,
  124. $storeManager,
  125. $logger,
  126. $collectionFactory,
  127. $exportConfig,
  128. $productFactory,
  129. $attrSetColFactory,
  130. $categoryColFactory,
  131. $itemFactory,
  132. $optionColFactory,
  133. $attributeColFactory,
  134. $_typeFactory,
  135. $linkTypeProvider,
  136. $rowCustomizer
  137. );
  138. }
  139. /**
  140. * @return $this
  141. * @throws \Magento\Framework\Exception\LocalizedException
  142. */
  143. protected function initTypeModels()
  144. {
  145. $productTypes = $this->_exportConfig->getEntityTypes(CatalogProduct::ENTITY);
  146. foreach ($productTypes as $productTypeName => $productTypeConfig) {
  147. if (!($model = $this->_typeFactory->create($productTypeConfig['model']))) {
  148. throw new \Magento\Framework\Exception\LocalizedException(
  149. __('Entity type model \'%1\' is not found', $productTypeConfig['model'])
  150. );
  151. }
  152. if (!$model instanceof \Magento\CatalogImportExport\Model\Export\Product\Type\AbstractType) {
  153. throw new \Magento\Framework\Exception\LocalizedException(
  154. __(
  155. 'Entity type model must be an instance of'
  156. . ' \Magento\CatalogImportExport\Model\Export\Product\Type\AbstractType'
  157. )
  158. );
  159. }
  160. if ($model->isSuitable()) {
  161. $this->_productTypeModels[$productTypeName] = $model;
  162. $this->_disabledAttrs = array_merge($this->_disabledAttrs, $model->getDisabledAttrs());
  163. $this->_indexValueAttributes = array_merge(
  164. $this->_indexValueAttributes,
  165. $model->getIndexValueAttributes()
  166. );
  167. }
  168. }
  169. if (!$this->_productTypeModels) {
  170. throw new \Magento\Framework\Exception\LocalizedException(
  171. __('There are no product types available for export')
  172. );
  173. }
  174. $this->_disabledAttrs = array_unique($this->_disabledAttrs);
  175. return $this;
  176. }
  177. /**
  178. * Export process
  179. *
  180. * @return string
  181. * @throws \Magento\Framework\Exception\LocalizedException
  182. */
  183. public function export()
  184. {
  185. //Execution time may be very long
  186. set_time_limit(0);
  187. $writer = $this->getWriter();
  188. $page = 0;
  189. while (true) {
  190. ++$page;
  191. $entityCollection = $this->_getEntityCollection(true);
  192. $entityCollection->setOrder('has_options', 'asc');
  193. $entityCollection->setStoreId(Store::DEFAULT_STORE_ID);
  194. $this->_prepareEntityCollection($entityCollection);
  195. $this->paginateCollection($page, $this->getItemsPerPage());
  196. if ($entityCollection->count() == 0) {
  197. break;
  198. }
  199. $exportData = $this->getExportData();
  200. foreach ($exportData as $dataRow) {
  201. $writer->writeRow($dataRow);
  202. }
  203. if ($entityCollection->getCurPage() >= $entityCollection->getLastPageNumber()) {
  204. break;
  205. }
  206. }
  207. return $writer->getContents();
  208. }
  209. /**
  210. * Clean up attribute collection.
  211. *
  212. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $collection
  213. * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
  214. */
  215. public function filterAttributeCollection(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $collection)
  216. {
  217. $collection->load();
  218. foreach ($collection as $attribute) {
  219. if (in_array($attribute->getAttributeCode(), $this->_disabledAttrs)) {
  220. if (isset($this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_SKIP])) {
  221. if ($attribute->getAttributeCode() == ImportAdvancedPricing::COL_TIER_PRICE
  222. && in_array(
  223. $attribute->getId(),
  224. $this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_SKIP]
  225. )
  226. ) {
  227. $this->_passTierPrice = 1;
  228. }
  229. }
  230. $collection->removeItemByKey($attribute->getId());
  231. }
  232. }
  233. return $collection;
  234. }
  235. /**
  236. * Get export data for collection
  237. *
  238. * @return array|mixed
  239. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  240. * @SuppressWarnings(PHPMD.NPathComplexity)
  241. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  242. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  243. */
  244. protected function getExportData()
  245. {
  246. if ($this->_passTierPrice) {
  247. return [];
  248. }
  249. $exportData = [];
  250. try {
  251. $productsByStores = $this->loadCollection();
  252. if (!empty($productsByStores)) {
  253. $linkField = $this->getProductEntityLinkField();
  254. $productLinkIds = [];
  255. foreach ($productsByStores as $product) {
  256. $productLinkIds[array_pop($product)[$linkField]] = true;
  257. }
  258. $productLinkIds = array_keys($productLinkIds);
  259. $tierPricesData = $this->fetchTierPrices($productLinkIds);
  260. $exportData = $this->prepareExportData(
  261. $productsByStores,
  262. $tierPricesData
  263. );
  264. if (!empty($exportData)) {
  265. asort($exportData);
  266. }
  267. }
  268. } catch (\Throwable $e) {
  269. $this->_logger->critical($e);
  270. }
  271. return $exportData;
  272. }
  273. /**
  274. * Creating export-formatted row from tier price.
  275. *
  276. * @param array $tierPriceData Tier price information.
  277. *
  278. * @return array Formatted for export tier price information.
  279. */
  280. private function createExportRow(array $tierPriceData): array
  281. {
  282. //List of columns to display in export row.
  283. $exportRow = $this->templateExportData;
  284. foreach (array_keys($exportRow) as $keyTemplate) {
  285. if (array_key_exists($keyTemplate, $tierPriceData)) {
  286. if (in_array($keyTemplate, $this->_priceWebsite)) {
  287. //If it's website column then getting website code.
  288. $exportRow[$keyTemplate] = $this->_getWebsiteCode(
  289. $tierPriceData[$keyTemplate]
  290. );
  291. } elseif (in_array($keyTemplate, $this->_priceCustomerGroup)) {
  292. //If it's customer group column then getting customer
  293. //group name by ID.
  294. $exportRow[$keyTemplate] = $this->_getCustomerGroupById(
  295. $tierPriceData[$keyTemplate],
  296. $tierPriceData[ImportAdvancedPricing::VALUE_ALL_GROUPS]
  297. );
  298. unset($exportRow[ImportAdvancedPricing::VALUE_ALL_GROUPS]);
  299. } elseif ($keyTemplate
  300. === ImportAdvancedPricing::COL_TIER_PRICE
  301. ) {
  302. //If it's price column then getting value and type
  303. //of tier price.
  304. $exportRow[$keyTemplate]
  305. = $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
  306. ? $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
  307. : $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE];
  308. $exportRow[ImportAdvancedPricing::COL_TIER_PRICE_TYPE]
  309. = $this->tierPriceTypeValue($tierPriceData);
  310. } else {
  311. //Any other column just goes as is.
  312. $exportRow[$keyTemplate] = $tierPriceData[$keyTemplate];
  313. }
  314. }
  315. }
  316. return $exportRow;
  317. }
  318. /**
  319. * Prepare data for export.
  320. *
  321. * @param array $productsData Products to export.
  322. * @param array $tierPricesData Their tier prices.
  323. *
  324. * @return array Export rows to display.
  325. */
  326. private function prepareExportData(
  327. array $productsData,
  328. array $tierPricesData
  329. ): array {
  330. //Assigning SKUs to tier prices data.
  331. $productLinkIdToSkuMap = [];
  332. foreach ($productsData as $productData) {
  333. $productLinkIdToSkuMap[$productData[Store::DEFAULT_STORE_ID][$this->getProductEntityLinkField()]]
  334. = $productData[Store::DEFAULT_STORE_ID]['sku'];
  335. }
  336. //Adding products' SKUs to tier price data.
  337. $linkedTierPricesData = [];
  338. foreach ($tierPricesData as $tierPriceData) {
  339. $sku = $productLinkIdToSkuMap[$tierPriceData['product_link_id']];
  340. $linkedTierPricesData[] = array_merge(
  341. $tierPriceData,
  342. [ImportAdvancedPricing::COL_SKU => $sku]
  343. );
  344. }
  345. //Formatting data for export.
  346. $customExportData = [];
  347. foreach ($linkedTierPricesData as $row) {
  348. $customExportData[] = $this->createExportRow($row);
  349. }
  350. return $customExportData;
  351. }
  352. /**
  353. * Correct export data.
  354. *
  355. * @param array $exportData
  356. * @return array
  357. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  358. * @deprecated 100.3.0
  359. * @see prepareExportData
  360. */
  361. protected function correctExportData($exportData)
  362. {
  363. $customExportData = [];
  364. foreach ($exportData as $key => $row) {
  365. $exportRow = $this->templateExportData;
  366. foreach ($exportRow as $keyTemplate => $valueTemplate) {
  367. if (isset($row[$keyTemplate])) {
  368. if (in_array($keyTemplate, $this->_priceWebsite)) {
  369. $exportRow[$keyTemplate] = $this->_getWebsiteCode(
  370. $row[$keyTemplate]
  371. );
  372. } elseif (in_array($keyTemplate, $this->_priceCustomerGroup)) {
  373. $exportRow[$keyTemplate] = $this->_getCustomerGroupById(
  374. $row[$keyTemplate],
  375. isset($row[ImportAdvancedPricing::VALUE_ALL_GROUPS])
  376. ? $row[ImportAdvancedPricing::VALUE_ALL_GROUPS]
  377. : null
  378. );
  379. unset($exportRow[ImportAdvancedPricing::VALUE_ALL_GROUPS]);
  380. } elseif ($keyTemplate === ImportAdvancedPricing::COL_TIER_PRICE) {
  381. $exportRow[$keyTemplate] = $row[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
  382. ? $row[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
  383. : $row[ImportAdvancedPricing::COL_TIER_PRICE];
  384. $exportRow[ImportAdvancedPricing::COL_TIER_PRICE_TYPE]
  385. = $this->tierPriceTypeValue($row[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]);
  386. } else {
  387. $exportRow[$keyTemplate] = $row[$keyTemplate];
  388. }
  389. }
  390. }
  391. $customExportData[$key] = $exportRow;
  392. unset($exportRow);
  393. }
  394. return $customExportData;
  395. }
  396. /**
  397. * Check type for tier price.
  398. *
  399. * @param array $tierPriceData
  400. * @return string
  401. */
  402. private function tierPriceTypeValue(array $tierPriceData): string
  403. {
  404. return $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
  405. ? ImportAdvancedPricing::TIER_PRICE_TYPE_PERCENT
  406. : ImportAdvancedPricing::TIER_PRICE_TYPE_FIXED;
  407. }
  408. /**
  409. * Load tier prices for given products.
  410. *
  411. * @param string[] $productIds Link IDs of products to find tier prices for.
  412. *
  413. * @return array Tier prices data.
  414. *
  415. * @SuppressWarnings(PHPMD.NPathComplexity)
  416. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  417. */
  418. private function fetchTierPrices(array $productIds): array
  419. {
  420. if (empty($productIds)) {
  421. throw new \InvalidArgumentException(
  422. 'Can only load tier prices for specific products'
  423. );
  424. }
  425. $pricesTable = ImportAdvancedPricing::TABLE_TIER_PRICE;
  426. $exportFilter = null;
  427. $priceFromFilter = null;
  428. $priceToFilter = null;
  429. if (isset($this->_parameters[Export::FILTER_ELEMENT_GROUP])) {
  430. $exportFilter = $this->_parameters[Export::FILTER_ELEMENT_GROUP];
  431. }
  432. $productEntityLinkField = $this->getProductEntityLinkField();
  433. $selectFields = [
  434. ImportAdvancedPricing::COL_TIER_PRICE_WEBSITE => 'ap.website_id',
  435. ImportAdvancedPricing::VALUE_ALL_GROUPS => 'ap.all_groups',
  436. ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'ap.customer_group_id',
  437. ImportAdvancedPricing::COL_TIER_PRICE_QTY => 'ap.qty',
  438. ImportAdvancedPricing::COL_TIER_PRICE => 'ap.value',
  439. ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE => 'ap.percentage_value',
  440. 'product_link_id' => 'ap.' .$productEntityLinkField,
  441. ];
  442. if ($exportFilter && array_key_exists('tier_price', $exportFilter)) {
  443. if (!empty($exportFilter['tier_price'][0])) {
  444. $priceFromFilter = $exportFilter['tier_price'][0];
  445. }
  446. if (!empty($exportFilter['tier_price'][1])) {
  447. $priceToFilter = $exportFilter['tier_price'][1];
  448. }
  449. }
  450. $select = $this->_connection->select()
  451. ->from(
  452. ['ap' => $this->_resource->getTableName($pricesTable)],
  453. $selectFields
  454. )
  455. ->where(
  456. 'ap.'.$productEntityLinkField.' IN (?)',
  457. $productIds
  458. );
  459. if ($priceFromFilter !== null) {
  460. $select->where('ap.value >= ?', $priceFromFilter);
  461. }
  462. if ($priceToFilter !== null) {
  463. $select->where('ap.value <= ?', $priceToFilter);
  464. }
  465. if ($priceFromFilter || $priceToFilter) {
  466. $select->orWhere('ap.percentage_value IS NOT NULL');
  467. }
  468. return $this->_connection->fetchAll($select);
  469. }
  470. /**
  471. * Get tier prices.
  472. *
  473. * @param array $listSku
  474. * @param string $table
  475. * @return array|bool
  476. * @SuppressWarnings(PHPMD.NPathComplexity)
  477. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  478. * @deprecated 100.3.0
  479. * @see fetchTierPrices
  480. */
  481. protected function getTierPrices(array $listSku, $table)
  482. {
  483. if (isset($this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP])) {
  484. $exportFilter = $this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP];
  485. }
  486. if ($table == ImportAdvancedPricing::TABLE_TIER_PRICE) {
  487. $selectFields = [
  488. ImportAdvancedPricing::COL_SKU => 'cpe.sku',
  489. ImportAdvancedPricing::COL_TIER_PRICE_WEBSITE => 'ap.website_id',
  490. ImportAdvancedPricing::VALUE_ALL_GROUPS => 'ap.all_groups',
  491. ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'ap.customer_group_id',
  492. ImportAdvancedPricing::COL_TIER_PRICE_QTY => 'ap.qty',
  493. ImportAdvancedPricing::COL_TIER_PRICE => 'ap.value',
  494. ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE => 'ap.percentage_value',
  495. ];
  496. if (isset($exportFilter) && !empty($exportFilter)) {
  497. $price = $exportFilter['tier_price'];
  498. }
  499. }
  500. if ($listSku) {
  501. if (isset($exportFilter) && !empty($exportFilter)) {
  502. $date = $exportFilter[\Magento\Catalog\Model\Category::KEY_UPDATED_AT];
  503. if (isset($date[0]) && !empty($date[0])) {
  504. $updatedAtFrom = $this->_localeDate->date($date[0], null, false)->format('Y-m-d H:i:s');
  505. }
  506. if (isset($date[1]) && !empty($date[1])) {
  507. $updatedAtTo = $this->_localeDate->date($date[1], null, false)->format('Y-m-d H:i:s');
  508. }
  509. }
  510. try {
  511. $productEntityLinkField = $this->getProductEntityLinkField();
  512. $select = $this->_connection->select()
  513. ->from(
  514. ['cpe' => $this->_resource->getTableName('catalog_product_entity')],
  515. $selectFields
  516. )
  517. ->joinInner(
  518. ['ap' => $this->_resource->getTableName($table)],
  519. 'ap.' . $productEntityLinkField . ' = cpe.' . $productEntityLinkField,
  520. []
  521. )
  522. ->where('cpe.entity_id IN (?)', $listSku);
  523. if (isset($price[0]) && !empty($price[0])) {
  524. $select->where('ap.value >= ?', $price[0]);
  525. }
  526. if (isset($price[1]) && !empty($price[1])) {
  527. $select->where('ap.value <= ?', $price[1]);
  528. }
  529. if (isset($price[0]) && !empty($price[0]) || isset($price[1]) && !empty($price[1])) {
  530. $select->orWhere('ap.percentage_value IS NOT NULL');
  531. }
  532. if (isset($updatedAtFrom) && !empty($updatedAtFrom)) {
  533. $select->where('cpe.updated_at >= ?', $updatedAtFrom);
  534. }
  535. if (isset($updatedAtTo) && !empty($updatedAtTo)) {
  536. $select->where('cpe.updated_at <= ?', $updatedAtTo);
  537. }
  538. $exportData = $this->_connection->fetchAll($select);
  539. } catch (\Exception $e) {
  540. return false;
  541. }
  542. }
  543. return $exportData;
  544. }
  545. /**
  546. * Get Website code.
  547. *
  548. * @param int $websiteId
  549. * @return string
  550. * @throws \Magento\Framework\Exception\LocalizedException
  551. */
  552. protected function _getWebsiteCode(int $websiteId): string
  553. {
  554. if (!array_key_exists($websiteId, $this->websiteCodesMap)) {
  555. $storeName = ($websiteId == 0)
  556. ? ImportAdvancedPricing::VALUE_ALL_WEBSITES
  557. : $this->_storeManager->getWebsite($websiteId)->getCode();
  558. $currencyCode = '';
  559. if ($websiteId == 0) {
  560. $currencyCode = $this->_storeManager->getWebsite($websiteId)
  561. ->getBaseCurrencyCode();
  562. }
  563. if ($storeName && $currencyCode) {
  564. $code = $storeName.' ['.$currencyCode.']';
  565. } else {
  566. $code = $storeName;
  567. }
  568. $this->websiteCodesMap[$websiteId] = $code;
  569. }
  570. return $this->websiteCodesMap[$websiteId];
  571. }
  572. /**
  573. * Get Customer Group By Id.
  574. *
  575. * @param int $groupId
  576. * @param int $allGroups
  577. * @return string
  578. * @throws \Magento\Framework\Exception\LocalizedException
  579. * @throws \Magento\Framework\Exception\NoSuchEntityException
  580. */
  581. protected function _getCustomerGroupById(
  582. int $groupId,
  583. int $allGroups = 0
  584. ): string {
  585. if ($allGroups !== 0) {
  586. return ImportAdvancedPricing::VALUE_ALL_GROUPS;
  587. }
  588. return $this->_groupRepository->getById($groupId)->getCode();
  589. }
  590. /**
  591. * Get Entity type code
  592. *
  593. * @return string
  594. */
  595. public function getEntityTypeCode()
  596. {
  597. if (!$this->_entityTypeCode) {
  598. $this->_entityTypeCode = CatalogProduct::ENTITY;
  599. } else {
  600. $this->_entityTypeCode = self::ENTITY_ADVANCED_PRICING;
  601. }
  602. return $this->_entityTypeCode;
  603. }
  604. }