Advertisement
Guest User

Untitled

a guest
Jun 26th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.76 KB | None | 0 0
  1. <?php
  2. /**
  3. *
  4. * @ This file is created by http://DeZender.Net
  5. * @ deZender (PHP8 Decoder for ionCube Encoder)
  6. *
  7. * @ Version : 8.1.0.9
  8. * @ Author : DeZender
  9. * @ Release on : 27.10.2023
  10. * @ Official site : http://DeZender.Net
  11. *
  12. */
  13.  
  14. class XImportCore
  15. {
  16. protected $_idLang = null;
  17. protected $_suppliers = null;
  18.  
  19. public function __construct()
  20. {
  21. $collection = new Collection('Supplier');
  22. $this->_suppliers = $collection->getResults();
  23. $this->_idLang = Context::getContext()->language->id;
  24. }
  25.  
  26. public static function truncateItemTables()
  27. {
  28. $itemTables = ['ximport_item', 'ximport_item_attachment', 'ximport_item_attribute', 'ximport_item_feature', 'ximport_item_image'];
  29.  
  30. foreach ($itemTables as $itemTable) {
  31. Db::getInstance()->execute('TRUNCATE TABLE `' . _DB_PREFIX_ . $itemTable . '`');
  32. }
  33. }
  34.  
  35. public static function checkAfterCronBlock($code, $qty)
  36. {
  37. $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 . '"');
  38.  
  39. if (!$items) {
  40. return false;
  41. }
  42.  
  43. $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');
  44. return $results < $qty;
  45. }
  46.  
  47. public static function afterCron()
  48. {
  49. x13import\Component\Logger::instance()->info('Delete non existent product relations', x13import\Component\Logger::STEP_START);
  50. $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';
  51. Db::getInstance()->execute($query);
  52. x13import\Component\Logger::instance()->info('Non existent product relations have been removed', x13import\Component\Logger::STEP_STOP);
  53. x13import\Component\Logger::instance()->info('Delete non existent product combination relations', x13import\Component\Logger::STEP_START);
  54. $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';
  55. Db::getInstance()->execute($query);
  56. x13import\Component\Logger::instance()->info('Delete non existent product combination relations', x13import\Component\Logger::STEP_STOP);
  57. x13import\Component\Logger::instance()->info('Clean already added images', x13import\Component\Logger::STEP_START);
  58. $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';
  59. Db::getInstance()->execute($query);
  60. x13import\Component\Logger::instance()->info('Clean already added images', x13import\Component\Logger::STEP_STOP);
  61. x13import\Component\Logger::instance()->info('Clean already added attachments', x13import\Component\Logger::STEP_START);
  62. 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');
  63. x13import\Component\Logger::instance()->info('Clean already added attachments', x13import\Component\Logger::STEP_STOP);
  64. x13import\Component\Logger::instance()->info('Update exclusions in attributes', x13import\Component\Logger::STEP_START);
  65. 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');
  66. x13import\Component\Logger::instance()->info('Update exclusions in attributes', x13import\Component\Logger::STEP_STOP);
  67. $to_update_product = [];
  68. $item = [];
  69. XImportItem::setAvailability($item, ['MOD_ADVANCED_STOCK_UPD']);
  70. if (!(bool) XImportConfiguration::get('MOD_ADVANCED_STOCK_UPD') || (bool) XImportConfiguration::get('MOD_ADVANCED_STOCK_UPD') && (bool) XImportConfiguration::get('MOD_ADVANCED_STOCK_UPD_QTY_ZERO')) {
  71. $to_update_product[] = 'sav.`quantity` = 0';
  72. }
  73.  
  74. if ($item['out_of_stock'] < 4) {
  75. $to_update_product[] = 'sav.`out_of_stock` = ' . (int) $item['out_of_stock'];
  76. }
  77.  
  78. if (!empty($to_update_product)) {
  79. 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');
  80. x13import\Component\Logger::instance()->info('Quantity/update out_of_stock for nonexistent products have been resetted');
  81. }
  82.  
  83. 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');
  84. x13import\Component\Logger::instance()->info('Quantities for nonexistent combinations have been resetted');
  85.  
  86. if (XImportConfiguration::get('UPDATE_CATEGORY')) {
  87. x13import\Component\Logger::instance()->info('Update default category', x13import\Component\Logger::STEP_START);
  88. 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');
  89. x13import\Component\Logger::instance()->info('Update default category', x13import\Component\Logger::STEP_STOP);
  90.  
  91. if (XImportConfiguration::get('CATEGORY_PARENTS')) {
  92. x13import\Component\Logger::instance()->info('Update categories with parents', x13import\Component\Logger::STEP_START);
  93. $root_category = NULL;
  94. $parents = [];
  95. $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');
  96.  
  97. foreach ($results as $r) {
  98. if ($r['is_root_category']) {
  99. $root_category = $r['id_category'];
  100. }
  101. else if ($root_category != $r['id_parent']) {
  102. if (isset($parents[$r['id_parent']])) {
  103. $parents[$r['id_category']] = array_merge([$r['id_parent']], $parents[$r['id_parent']]);
  104. }
  105. else {
  106. $parents[$r['id_category']][] = $r['id_parent'];
  107. }
  108. }
  109. }
  110.  
  111. $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`');
  112.  
  113. foreach ($results as $r) {
  114. $to_delete[] = $r['id_product'];
  115.  
  116. foreach (explode(',', $r['additional_categories']) as $e) {
  117. if (isset($parents[$e])) {
  118. foreach ($parents[$e] as $p) {
  119. $to_insert[(int) $p . '-' . (int) $r['id_product']] = '(' . (int) $p . ', ' . (int) $r['id_product'] . ', 0)';
  120. }
  121. }
  122. }
  123. }
  124.  
  125. x13import\Component\Logger::instance()->info('Update categories with parents', x13import\Component\Logger::STEP_STOP);
  126. }
  127.  
  128. x13import\Component\Logger::instance()->info('Update additional categories', x13import\Component\Logger::STEP_START);
  129. $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`');
  130.  
  131. foreach ($results as $r) {
  132. foreach (explode(',', $r['additional_categories']) as $cp) {
  133. $to_delete[] = $r['id_product'];
  134. $to_insert[(int) $cp . '-' . (int) $r['id_product']] = '(' . (int) $cp . ', ' . (int) $r['id_product'] . ', 0)';
  135. }
  136. }
  137.  
  138. x13import\Component\Logger::instance()->info('Start transaction for categories update');
  139. Db::getInstance()->execute('START TRANSACTION');
  140.  
  141. if (isset($to_delete)) {
  142. x13import\Component\Logger::instance()->info('Delete old categories associations', x13import\Component\Logger::STEP_START);
  143.  
  144. foreach (array_chunk(array_unique($to_delete), 2500) as $ch) {
  145. Db::getInstance()->execute("\n" . ' DELETE FROM `' . _DB_PREFIX_ . 'category_product`' . "\n" . ' WHERE `id_product` IN(' . implode(',', $ch) . ')');
  146. }
  147.  
  148. x13import\Component\Logger::instance()->info('Delete old categories associations', x13import\Component\Logger::STEP_STOP);
  149. unset($to_delete);
  150. }
  151.  
  152. if (isset($to_insert)) {
  153. x13import\Component\Logger::instance()->info('Assign new categories associations', x13import\Component\Logger::STEP_START);
  154.  
  155. foreach (array_chunk($to_insert, 2500) as $ch) {
  156. Db::getInstance()->execute("\n" . ' INSERT IGNORE INTO `' . _DB_PREFIX_ . 'category_product`' . "\n" . ' (`id_category`, `id_product`, `position`)' . "\n" . ' VALUES ' . implode(',', $ch));
  157. }
  158.  
  159. x13import\Component\Logger::instance()->info('Assign new categories associations', x13import\Component\Logger::STEP_STOP);
  160. unset($to_insert);
  161. }
  162.  
  163. Db::getInstance()->execute('COMMIT');
  164. }
  165.  
  166. if (XImportConfiguration::get('UPDATE_SUPPLIER')) {
  167. x13import\Component\Logger::instance()->info('Udpate suppliers');
  168. $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`');
  169. $supplierCollation = x13import\Adapter\DbAdapter::getColumnCollation('supplier', 'name');
  170.  
  171. foreach ($results as $r) {
  172. $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'])) . '"');
  173.  
  174. if (!$id_supplier) {
  175. $supplier = new Supplier();
  176. $supplier->name = trim($r['supplier']);
  177. $supplier->active = 1;
  178. $supplier->save();
  179. x13import\Component\Logger::instance()->info('id: ' . $supplier->id . '; name: ' . $supplier->name, 'new-supplier');
  180. }
  181. }
  182.  
  183. Db::getInstance()->execute('START TRANSACTION');
  184. x13import\Component\Logger::instance()->info('Update default suppliers for products', x13import\Component\Logger::STEP_START);
  185. 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');
  186. x13import\Component\Logger::instance()->info('Update default suppliers for products', x13import\Component\Logger::STEP_STOP);
  187. x13import\Component\Logger::instance()->info('Delete old suppliers associations', x13import\Component\Logger::STEP_START);
  188. 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" . ' ');
  189. x13import\Component\Logger::instance()->info('Delete old suppliers associations', x13import\Component\Logger::STEP_STOP);
  190. x13import\Component\Logger::instance()->info('Update suppliers associations', x13import\Component\Logger::STEP_START);
  191. 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');
  192. Db::getInstance()->execute('COMMIT');
  193. x13import\Component\Logger::instance()->info('Update suppliers', x13import\Component\Logger::STEP_STOP);
  194. }
  195.  
  196. if (XImportConfiguration::get('UPDATE_MANUFACTURER')) {
  197. x13import\Component\Logger::instance()->info('Update manufacturers');
  198.  
  199. while ($unassignedManufacturers = x13import\Importer\ItemRepository::countUnassignedItemManufacturers()) {
  200. x13import\Component\Logger::instance()->info('Import unassigned manufacturers (' . $unassignedManufacturers . ' left)');
  201. $manufacturers = x13import\Importer\ItemRepository::getUnassignedItemManufacturers(10000);
  202.  
  203. if (!$manufacturers) {
  204. break;
  205. }
  206.  
  207. foreach ($manufacturers as $manufacturer) {
  208. if (!$manufacturer['id_manufacturer']) {
  209. ..............................................................................
  210. ................................................
  211. ..................
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement