Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- * @ This file is created by http://DeZender.Net
- * @ deZender (PHP8 Decoder for ionCube Encoder)
- *
- * @ Version : 8.1.0.9
- * @ Author : DeZender
- * @ Release on : 27.10.2023
- * @ Official site : http://DeZender.Net
- *
- */
- class XImportCore
- {
- protected $_idLang = null;
- protected $_suppliers = null;
- public function __construct()
- {
- $collection = new Collection('Supplier');
- $this->_suppliers = $collection->getResults();
- $this->_idLang = Context::getContext()->language->id;
- }
- public static function truncateItemTables()
- {
- $itemTables = ['ximport_item', 'ximport_item_attachment', 'ximport_item_attribute', 'ximport_item_feature', 'ximport_item_image'];
- foreach ($itemTables as $itemTable) {
- Db::getInstance()->execute('TRUNCATE TABLE `' . _DB_PREFIX_ . $itemTable . '`');
- }
- }
- public static function checkAfterCronBlock($code, $qty)
- {
- $items = (int) Db::getInstance()->getValue("\n" . ' SELECT COUNT(ip.`id_ximport_product`)' . "\n" . ' FROM `' . _DB_PREFIX_ . 'ximport_item` ii' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'ximport_product` ip' . "\n" . ' ON (ip.`fingerprint` = ii.`fingerprint`)' . "\n" . ' WHERE ii.`wholesaler_code` = "' . $code . '"');
- if (!$items) {
- return false;
- }
- $results = (int) Db::getInstance()->getValue("\n" . ' SELECT COUNT(ii.`quantity`)' . "\n" . ' FROM `' . _DB_PREFIX_ . 'ximport_item` ii' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'ximport_product` ip' . "\n" . ' ON (ip.`fingerprint` = ii.`fingerprint`)' . "\n" . ' WHERE ii.`wholesaler_code` = "' . $code . '"' . "\n" . ' AND ii.`quantity` > 0' . "\n" . ' AND ip.`exclude` = 0');
- return $results < $qty;
- }
- public static function afterCron()
- {
- x13import\Component\Logger::instance()->info('Delete non existent product relations', x13import\Component\Logger::STEP_START);
- $query = 'DELETE ip FROM ' . _DB_PREFIX_ . 'ximport_product AS ip ' . 'LEFT JOIN ' . _DB_PREFIX_ . 'product AS p ' . 'ON ip.id_product = p.id_product ' . 'WHERE p.id_product IS NULL';
- Db::getInstance()->execute($query);
- x13import\Component\Logger::instance()->info('Non existent product relations have been removed', x13import\Component\Logger::STEP_STOP);
- x13import\Component\Logger::instance()->info('Delete non existent product combination relations', x13import\Component\Logger::STEP_START);
- $query = 'DELETE ip FROM ' . _DB_PREFIX_ . 'ximport_product AS ip ' . 'LEFT JOIN ' . _DB_PREFIX_ . 'product_attribute AS pa ' . 'ON (ip.id_product = pa.id_product AND ip.id_product_attribute = pa.id_product_attribute) ' . 'WHERE pa.id_product IS NULL AND ip.id_product_attribute !=0';
- Db::getInstance()->execute($query);
- x13import\Component\Logger::instance()->info('Delete non existent product combination relations', x13import\Component\Logger::STEP_STOP);
- x13import\Component\Logger::instance()->info('Clean already added images', x13import\Component\Logger::STEP_START);
- $query = 'DELETE iii FROM ' . _DB_PREFIX_ . 'ximport_item_image AS iii ' . 'LEFT JOIN ' . _DB_PREFIX_ . 'ximport_item AS ii ' . 'ON (iii.fingerprint = ii.fingerprint) ' . 'LEFT JOIN ' . _DB_PREFIX_ . 'ximport_image AS iim ' . 'ON (iii.value_md5 = iim.fingerprint AND ii.code = iim.code) ' . 'WHERE iim.id_image IS NOT NULL';
- Db::getInstance()->execute($query);
- x13import\Component\Logger::instance()->info('Clean already added images', x13import\Component\Logger::STEP_STOP);
- x13import\Component\Logger::instance()->info('Clean already added attachments', x13import\Component\Logger::STEP_START);
- Db::getInstance()->execute("\n" . ' DELETE ia' . "\n" . ' FROM `' . _DB_PREFIX_ . 'ximport_item_attachment` ia' . "\n" . ' LEFT JOIN `' . _DB_PREFIX_ . 'ximport_item` ii' . "\n" . ' ON (ia.`code` = ii.`code`)' . "\n" . ' LEFT JOIN `' . _DB_PREFIX_ . 'ximport_attachment` a' . "\n" . ' ON (ia.`code` = a.`code`' . "\n" . ' AND ia.`value_md5` = a.`fingerprint`)' . "\n" . ' WHERE a.`id_attachment` IS NOT NULL');
- x13import\Component\Logger::instance()->info('Clean already added attachments', x13import\Component\Logger::STEP_STOP);
- x13import\Component\Logger::instance()->info('Update exclusions in attributes', x13import\Component\Logger::STEP_START);
- Db::getInstance()->execute("\n" . ' UPDATE `' . _DB_PREFIX_ . 'ximport_product` xp1' . "\n" . ' INNER JOIN `' . _DB_PREFIX_ . 'ximport_product` xp2' . "\n" . ' ON (xp1.`id_product` = xp2.`id_product`)' . "\n" . ' SET xp1.`exclude` = xp2.`exclude`,' . "\n" . ' xp1.`exclude_price` = xp2.`exclude_price`,' . "\n" . ' xp1.`exclude_quantity` = xp2.`exclude_quantity`' . "\n" . ' WHERE xp1.`id_product_attribute` != 0');
- x13import\Component\Logger::instance()->info('Update exclusions in attributes', x13import\Component\Logger::STEP_STOP);
- $to_update_product = [];
- $item = [];
- XImportItem::setAvailability($item, ['MOD_ADVANCED_STOCK_UPD']);
- if (!(bool) XImportConfiguration::get('MOD_ADVANCED_STOCK_UPD') || (bool) XImportConfiguration::get('MOD_ADVANCED_STOCK_UPD') && (bool) XImportConfiguration::get('MOD_ADVANCED_STOCK_UPD_QTY_ZERO')) {
- $to_update_product[] = 'sav.`quantity` = 0';
- }
- if ($item['out_of_stock'] < 4) {
- $to_update_product[] = 'sav.`out_of_stock` = ' . (int) $item['out_of_stock'];
- }
- if (!empty($to_update_product)) {
- Db::getInstance()->execute("\n" . ' UPDATE `' . _DB_PREFIX_ . 'stock_available` sav' . "\n" . ' LEFT JOIN `' . _DB_PREFIX_ . 'ximport_product` ip' . "\n" . ' ON (sav.`id_product` = ip.`id_product`' . "\n" . ' AND sav.`id_product_attribute` = 0)' . "\n" . ' LEFT JOIN `' . _DB_PREFIX_ . 'ximport_item` ii' . "\n" . ' ON (ip.`code` = ii.`code`)' . "\n" . ' SET ' . implode(', ', $to_update_product) . "\n" . ' WHERE ip.id_ximport_product IS NOT NULL' . "\n" . ' AND ip.`exclude` = 0' . "\n" . ' AND ip.`exclude_quantity` = 0' . "\n" . ' AND ii.id_ximport_item IS NULL');
- x13import\Component\Logger::instance()->info('Quantity/update out_of_stock for nonexistent products have been resetted');
- }
- Db::getInstance()->execute("\n" . ' UPDATE `' . _DB_PREFIX_ . 'stock_available` sav' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'product_attribute` pa' . "\n" . ' ON (pa.`id_product` = sav.`id_product`' . "\n" . ' AND pa.`id_product_attribute` = sav.`id_product_attribute`)' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'product_attribute_shop` pas' . "\n" . ' ON (pa.`id_product_attribute` = pas.`id_product_attribute`)' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'ximport_product` ip' . "\n" . ' ON (pa.`id_product` = ip.`id_product`' . "\n" . ' AND pa.`id_product_attribute` = ip.`id_product_attribute`)' . "\n" . ' LEFT JOIN `' . _DB_PREFIX_ . 'ximport_item` ii' . "\n" . ' ON (ip.`fingerprint` = ii.`fingerprint`)' . "\n" . ' SET sav.`quantity` = 0' . "\n" . ' WHERE ip.id_ximport_product IS NOT NULL' . "\n" . ' AND ip.`exclude` = 0' . "\n" . ' AND ip.`exclude_quantity` = 0' . "\n" . ' AND ii.id_ximport_item IS NULL');
- x13import\Component\Logger::instance()->info('Quantities for nonexistent combinations have been resetted');
- if (XImportConfiguration::get('UPDATE_CATEGORY')) {
- x13import\Component\Logger::instance()->info('Update default category', x13import\Component\Logger::STEP_START);
- Db::getInstance()->execute("\n" . ' UPDATE `' . _DB_PREFIX_ . 'product` p' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'product_shop` ps' . "\n" . ' ON (p.`id_product` = ps.`id_product`)' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'ximport_product` ip' . "\n" . ' ON (p.`id_product` = ip.`id_product` ' . "\n" . ' AND ip.`id_product_attribute` = 0)' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'ximport_item` ii' . "\n" . ' ON (ip.`code` = ii.`code`)' . "\n" . ' SET p.`id_category_default` = ii.`id_category`,' . "\n" . ' ps.`id_category_default` = ii.`id_category`' . "\n" . ' WHERE ip.`exclude` = 0');
- x13import\Component\Logger::instance()->info('Update default category', x13import\Component\Logger::STEP_STOP);
- if (XImportConfiguration::get('CATEGORY_PARENTS')) {
- x13import\Component\Logger::instance()->info('Update categories with parents', x13import\Component\Logger::STEP_START);
- $root_category = NULL;
- $parents = [];
- $results = (array) Db::getInstance()->executeS("\n" . ' SELECT `id_category`, `id_parent`, `is_root_category`' . "\n" . ' FROM `' . _DB_PREFIX_ . 'category`' . "\n" . ' ORDER BY `id_parent` ASC');
- foreach ($results as $r) {
- if ($r['is_root_category']) {
- $root_category = $r['id_category'];
- }
- else if ($root_category != $r['id_parent']) {
- if (isset($parents[$r['id_parent']])) {
- $parents[$r['id_category']] = array_merge([$r['id_parent']], $parents[$r['id_parent']]);
- }
- else {
- $parents[$r['id_category']][] = $r['id_parent'];
- }
- }
- }
- $results = (array) Db::getInstance()->executeS("\n" . ' SELECT ip.`id_product`, ii.`additional_categories`' . "\n" . ' FROM `' . _DB_PREFIX_ . 'ximport_item` ii' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'ximport_product` ip' . "\n" . ' ON (ip.`code` = ii.`code`' . "\n" . ' AND ip.`id_product_attribute` = 0)' . "\n" . ' WHERE ip.`exclude` = 0' . "\n" . ' GROUP BY ip.`id_product`');
- foreach ($results as $r) {
- $to_delete[] = $r['id_product'];
- foreach (explode(',', $r['additional_categories']) as $e) {
- if (isset($parents[$e])) {
- foreach ($parents[$e] as $p) {
- $to_insert[(int) $p . '-' . (int) $r['id_product']] = '(' . (int) $p . ', ' . (int) $r['id_product'] . ', 0)';
- }
- }
- }
- }
- x13import\Component\Logger::instance()->info('Update categories with parents', x13import\Component\Logger::STEP_STOP);
- }
- x13import\Component\Logger::instance()->info('Update additional categories', x13import\Component\Logger::STEP_START);
- $results = (array) Db::getInstance()->executeS("\n" . ' SELECT ip.`id_product`, ii.`additional_categories`' . "\n" . ' FROM `' . _DB_PREFIX_ . 'ximport_item` ii' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'ximport_product` ip' . "\n" . ' ON (ip.`code` = ii.`code`' . "\n" . ' AND ip.`id_product_attribute` = 0)' . "\n" . ' WHERE ip.`exclude` = 0' . "\n" . ' GROUP BY ip.`id_product`');
- foreach ($results as $r) {
- foreach (explode(',', $r['additional_categories']) as $cp) {
- $to_delete[] = $r['id_product'];
- $to_insert[(int) $cp . '-' . (int) $r['id_product']] = '(' . (int) $cp . ', ' . (int) $r['id_product'] . ', 0)';
- }
- }
- x13import\Component\Logger::instance()->info('Start transaction for categories update');
- Db::getInstance()->execute('START TRANSACTION');
- if (isset($to_delete)) {
- x13import\Component\Logger::instance()->info('Delete old categories associations', x13import\Component\Logger::STEP_START);
- foreach (array_chunk(array_unique($to_delete), 2500) as $ch) {
- Db::getInstance()->execute("\n" . ' DELETE FROM `' . _DB_PREFIX_ . 'category_product`' . "\n" . ' WHERE `id_product` IN(' . implode(',', $ch) . ')');
- }
- x13import\Component\Logger::instance()->info('Delete old categories associations', x13import\Component\Logger::STEP_STOP);
- unset($to_delete);
- }
- if (isset($to_insert)) {
- x13import\Component\Logger::instance()->info('Assign new categories associations', x13import\Component\Logger::STEP_START);
- foreach (array_chunk($to_insert, 2500) as $ch) {
- Db::getInstance()->execute("\n" . ' INSERT IGNORE INTO `' . _DB_PREFIX_ . 'category_product`' . "\n" . ' (`id_category`, `id_product`, `position`)' . "\n" . ' VALUES ' . implode(',', $ch));
- }
- x13import\Component\Logger::instance()->info('Assign new categories associations', x13import\Component\Logger::STEP_STOP);
- unset($to_insert);
- }
- Db::getInstance()->execute('COMMIT');
- }
- if (XImportConfiguration::get('UPDATE_SUPPLIER')) {
- x13import\Component\Logger::instance()->info('Udpate suppliers');
- $results = (array) Db::getInstance()->executeS("\n" . ' SELECT ii.`supplier`' . "\n" . ' FROM `' . _DB_PREFIX_ . 'ximport_item` ii' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'ximport_product` ip' . "\n" . ' ON (ip.`code` = ii.`code` ' . "\n" . ' AND ip.`id_product_attribute` = 0)' . "\n" . ' WHERE ii.`supplier` <> "" ' . "\n" . ' AND ii.`supplier` IS NOT NULL ' . "\n" . ' AND ip.`exclude` = 0' . "\n" . ' GROUP BY ii.`supplier`');
- $supplierCollation = x13import\Adapter\DbAdapter::getColumnCollation('supplier', 'name');
- foreach ($results as $r) {
- $id_supplier = (int) Db::getInstance()->getValue("\n" . ' SELECT `id_supplier`' . "\n" . ' FROM `' . _DB_PREFIX_ . 'supplier`' . "\n" . ' WHERE `name` COLLATE ' . pSQL($supplierCollation) . ' LIKE "' . pSQL(trim($r['supplier'])) . '"');
- if (!$id_supplier) {
- $supplier = new Supplier();
- $supplier->name = trim($r['supplier']);
- $supplier->active = 1;
- $supplier->save();
- x13import\Component\Logger::instance()->info('id: ' . $supplier->id . '; name: ' . $supplier->name, 'new-supplier');
- }
- }
- Db::getInstance()->execute('START TRANSACTION');
- x13import\Component\Logger::instance()->info('Update default suppliers for products', x13import\Component\Logger::STEP_START);
- Db::getInstance()->execute("\n" . ' UPDATE `' . _DB_PREFIX_ . 'product` p' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'ximport_product` ip' . "\n" . ' ON (p.`id_product` = ip.`id_product` ' . "\n" . ' AND ip.`id_product_attribute` = 0)' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'ximport_item` ii' . "\n" . ' ON (ip.`code` = ii.`code`)' . "\n" . ' LEFT JOIN `' . _DB_PREFIX_ . 'supplier` s' . "\n" . ' ON (s.`name` COLLATE ' . pSQL($supplierCollation) . ' = ii.`supplier`)' . "\n" . ' SET p.`id_supplier` = s.`id_supplier`' . "\n" . ' WHERE ip.`exclude` = 0');
- x13import\Component\Logger::instance()->info('Update default suppliers for products', x13import\Component\Logger::STEP_STOP);
- x13import\Component\Logger::instance()->info('Delete old suppliers associations', x13import\Component\Logger::STEP_START);
- Db::getInstance()->execute("\n" . ' DELETE ps' . "\n" . ' FROM `' . _DB_PREFIX_ . 'product_supplier` ps' . "\n" . ' WHERE ps.`id_product` IN (' . "\n" . ' SELECT DISTINCT ip.id_product FROM `' . _DB_PREFIX_ . 'ximport_product` ip ' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'ximport_item` ii ON (ip.fingerprint = ii.fingerprint)' . "\n" . ' WHERE ip.exclude = 0' . "\n" . ' )' . "\n" . ' ');
- x13import\Component\Logger::instance()->info('Delete old suppliers associations', x13import\Component\Logger::STEP_STOP);
- x13import\Component\Logger::instance()->info('Update suppliers associations', x13import\Component\Logger::STEP_START);
- Db::getInstance()->execute("\n" . ' INSERT INTO `' . _DB_PREFIX_ . 'product_supplier` (' . "\n" . ' `id_product`, `id_product_attribute`, `id_supplier`, `product_supplier_reference`, `product_supplier_price_te`' . "\n" . ' )' . "\n" . ' SELECT ip.`id_product`, ip.`id_product_attribute`, s.`id_supplier`, ii.`reference`, ii.`price`' . "\n" . ' FROM `' . _DB_PREFIX_ . 'ximport_product` ip' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'ximport_item` ii' . "\n" . ' ON (ip.`fingerprint` = ii.`fingerprint`)' . "\n" . ' JOIN `' . _DB_PREFIX_ . 'supplier` s' . "\n" . ' ON (s.`name` = ii.`supplier`)' . "\n" . ' WHERE ip.`exclude` = 0');
- Db::getInstance()->execute('COMMIT');
- x13import\Component\Logger::instance()->info('Update suppliers', x13import\Component\Logger::STEP_STOP);
- }
- if (XImportConfiguration::get('UPDATE_MANUFACTURER')) {
- x13import\Component\Logger::instance()->info('Update manufacturers');
- while ($unassignedManufacturers = x13import\Importer\ItemRepository::countUnassignedItemManufacturers()) {
- x13import\Component\Logger::instance()->info('Import unassigned manufacturers (' . $unassignedManufacturers . ' left)');
- $manufacturers = x13import\Importer\ItemRepository::getUnassignedItemManufacturers(10000);
- if (!$manufacturers) {
- break;
- }
- foreach ($manufacturers as $manufacturer) {
- if (!$manufacturer['id_manufacturer']) {
- ..............................................................................
- ................................................
- ..................
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement