PageRenderTime 28ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/updateproducts/update.php

https://gitlab.com/ptisky/API_prestashop
PHP | 736 lines | 672 code | 64 blank | 0 comment | 149 complexity | 124e726fba2bac6eaf9993758b0816b5 MD5 | raw file
  1. <?php
  2. class updateProductCatalog
  3. {
  4. private $_context;
  5. private $_idShop;
  6. private $_idLang;
  7. private $_format;
  8. private $_model;
  9. private $_PHPExcelFactory;
  10. private $_alphabet;
  11. private $_updateFields;
  12. private $_head;
  13. private $_productsForUpdate = 0;
  14. private $_updatedProducts = 0;
  15. private $_isFeature = false;
  16. public function __construct( $idShop, $idLang, $format, $fields ){
  17. include_once(dirname(__FILE__).'/../../config/config.inc.php');
  18. include_once(dirname(__FILE__).'/../../init.php');
  19. include_once(_PS_MODULE_DIR_ . 'updateproducts/libraries/PHPExcel_1.7.9/Classes/PHPExcel.php');
  20. include_once(_PS_MODULE_DIR_ . 'updateproducts/libraries/PHPExcel_1.7.9/Classes/PHPExcel/IOFactory.php');
  21. include_once(_PS_MODULE_DIR_ . 'updateproducts/updateproducts.php');
  22. include_once('datamodel.php');
  23. $this->_context = Context::getContext();
  24. $this->_idShop = $idShop;
  25. $this->_idLang = $idLang;
  26. $this->_format = $format;
  27. $this->_model = new productsUpdateModel();
  28. $obj = new updateProducts();
  29. $fields['id_product'] = $obj->fieldIdProduct;
  30. $fields['id_product_attribute'] = $obj->fieldIdProductCombination;
  31. $fields['id_specific_price'] = $obj->fieldIdSpecificPrice;
  32. $this->_updateFields = $fields;
  33. $this->_alphabet = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  34. 'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ',
  35. 'BA','BB','BC','BD','BE','BF','BG','BH', 'BI','BJ','BK','BL','BM','BN','BO','BP','BQ', 'BR','BS','BT','BU','BV','BW','BX','BY','BZ',
  36. 'CA','CC','CC','CD','CE','CF','CG','CH', 'CI','CJ','CK','CL','CM','CN','CO','CP','CQ', 'CR','CS','CT','CU','CV','CW','CX','CY','CZ',
  37. 'DA','DD','DD','DD','DE','DF','DG','DH', 'DI','DJ','DK','DL','DM','DN','DO','DP','DQ', 'DR','DS','DT','DU','DV','DW','DX','DY','DZ'
  38. );
  39. }
  40. public function update()
  41. {
  42. $this->_clearErrorFile();
  43. $this->_copyFile();
  44. $this->_updateData();
  45. if( $this->_updatedProducts != $this->_productsForUpdate ){
  46. $res = array(
  47. 'message' => sprintf(Module::getInstanceByName('updateproducts')->l('Successfully updated %1s products from: %2s'), $this->_updatedProducts, $this->_productsForUpdate),
  48. 'error_logs' => _PS_BASE_URL_.__PS_BASE_URI__.'modules/updateproducts/error/error_logs.csv',
  49. );
  50. return $res;
  51. }
  52. $res = array(
  53. 'message' => sprintf(Module::getInstanceByName('updateproducts')->l('Successfully updated %s products!'), $this->_updatedProducts),
  54. 'error_logs' => false
  55. );
  56. return $res;
  57. }
  58. private function _clearErrorFile()
  59. {
  60. $write_fd = fopen('error/error_logs.csv', 'w');
  61. fwrite($write_fd, 'id_product,error'."\r\n");
  62. fclose($write_fd);
  63. }
  64. private function _checkHead( $fileFields )
  65. {
  66. $mappingFields = array();
  67. foreach( $this->_updateFields as $key => $field ){
  68. if( $key == "features" ){
  69. foreach( $fileFields as $fileField ){
  70. if( stripos( $fileField, "feature" ) !== false ){
  71. $this->_isFeature = true;
  72. }
  73. }
  74. if( !$this->_isFeature ){
  75. throw new Exception( sprintf(Module::getInstanceByName('updateproducts')->l('No %s was found in file!'), $field) );
  76. }
  77. continue;
  78. }
  79. if( !in_array( $field, $fileFields ) ){
  80. throw new Exception( sprintf(Module::getInstanceByName('updateproducts')->l('Field: %s need to be in file for update!'), $field) );
  81. }
  82. else{
  83. $mappingFields[$field] = $key;
  84. }
  85. }
  86. return $mappingFields;
  87. }
  88. private function _updateData()
  89. {
  90. foreach ($this->_PHPExcelFactory->getWorksheetIterator() as $worksheet) {
  91. $highestRow = $worksheet->getHighestRow(); // e.g. 10
  92. $highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
  93. $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
  94. $fileFields = array();
  95. for ($row = 1; $row <= $highestRow; ++ $row) {
  96. $product = array();
  97. for ($col = 0; $col < $highestColumnIndex; ++ $col) {
  98. $cell = $worksheet->getCellByColumnAndRow($col, $row);
  99. $val = $cell->getValue();
  100. if($row == 1){
  101. if( !$val ){
  102. continue;
  103. }
  104. $fileFields[$col] = $val;
  105. }
  106. else{
  107. if(!isset($fileFields[$col])){
  108. continue;
  109. }
  110. if( $row == 2 && $col == 0 ){
  111. $mappingFields = $this->_checkHead($fileFields);
  112. }
  113. if( isset($mappingFields[$fileFields[$col]]) ){
  114. $product[$mappingFields[$fileFields[$col]]] = $val;
  115. }
  116. if( $this->_isFeature ){
  117. if( stripos( $fileFields[$col], "feature" ) !== false ){
  118. $product['features'][$fileFields[$col]] = $val;
  119. }
  120. }
  121. }
  122. }
  123. if( $product ){
  124. $this->_productsForUpdate++;
  125. $this->_updateProduct($product);
  126. }
  127. }
  128. }
  129. }
  130. private function _updateProduct( $product )
  131. {
  132. $productObject = new ProductCore( $product['id_product'], true );
  133. foreach( $product as $field => $value ){
  134. if( $field == "categories_ids" ){
  135. $value = str_replace(" ", "", $value);
  136. $value = explode(";", $value);
  137. if( $value ){
  138. foreach( $value as $key => $categoryId ){
  139. $value[$key] = array('id' => $categoryId);
  140. }
  141. }
  142. $productObject->setWsCategories($value);
  143. }
  144. elseif( $field == "combinations_price" ){
  145. $attributes = $product['id_product_attribute'];
  146. $attributes = str_replace(" ", "", $attributes);
  147. $attributes = explode(";", $attributes);
  148. $values = $product['combinations_price'];
  149. $values = str_replace(" ", "", $values);
  150. if( $values == "" ){
  151. continue;
  152. }
  153. $values = explode(";", $values);
  154. foreach( $attributes as $key => $attribute ){
  155. $combination = new Combination($attribute);
  156. if( !isset($values[$key]) ){
  157. continue;
  158. }
  159. $combination->price = number_format($values[$key], 4, '.', '');
  160. if( ( $error = $combination->validateFields(false, true) ) !== true ){
  161. $this->_createErrorsFile($error, $product['id_product']);
  162. return false;
  163. }
  164. $combination->update();
  165. }
  166. }
  167. elseif( $field == "combinations_price_with_tax" ){
  168. $attributes = $product['id_product_attribute'];
  169. $attributes = str_replace(" ", "", $attributes);
  170. $attributes = explode(";", $attributes);
  171. $values = $product['combinations_price_with_tax'];
  172. $values = str_replace(" ", "", $values);
  173. if( $values == "" ){
  174. continue;
  175. }
  176. $values = explode(";", $values);
  177. foreach( $attributes as $key => $attribute ){
  178. $combination = new Combination($attribute);
  179. if( !isset($values[$key]) ){
  180. continue;
  181. }
  182. $taxPrice = (float)$values[$key];
  183. if( $productObject->tax_rate ){
  184. $taxPrice = $taxPrice / (($productObject->tax_rate/100)+1);
  185. }
  186. $combination->price = number_format($taxPrice, 4, '.', '');
  187. if( ( $error = $combination->validateFields(false, true) ) !== true ){
  188. $this->_createErrorsFile($error, $product['id_product']);
  189. return false;
  190. }
  191. $combination->update();
  192. }
  193. }
  194. elseif( $field == "combinations_quantity" ){
  195. $attributes = $product['id_product_attribute'];
  196. $attributes = str_replace(" ", "", $attributes);
  197. $attributes = explode(";", $attributes);
  198. $values = $product['combinations_quantity'];
  199. $values = str_replace(" ", "", $values);
  200. if( $values == "" ){
  201. continue;
  202. }
  203. $values = explode(";", $values);
  204. foreach( $attributes as $key => $attribute ){
  205. if( !isset($values[$key]) ){
  206. continue;
  207. }
  208. StockAvailable::setQuantity($product['id_product'], (int)$attribute, (int)$values[$key]);
  209. }
  210. }
  211. elseif( $field == "combinations_wholesale_price" ){
  212. $attributes = $product['id_product_attribute'];
  213. $attributes = str_replace(" ", "", $attributes);
  214. $attributes = explode(";", $attributes);
  215. $values = $product['combinations_wholesale_price'];
  216. $values = str_replace(" ", "", $values);
  217. if( $values == "" ){
  218. continue;
  219. }
  220. $values = explode(";", $values);
  221. foreach( $attributes as $key => $attribute ){
  222. $combination = new Combination($attribute);
  223. if( !isset($values[$key]) ){
  224. continue;
  225. }
  226. $combination->wholesale_price = number_format($values[$key], 4, '.', '');
  227. if( ( $error = $combination->validateFields(false, true) ) !== true ){
  228. $this->_createErrorsFile($error, $product['id_product']);
  229. return false;
  230. }
  231. $combination->update();
  232. }
  233. }
  234. elseif( $field == "combinations_unit_price_impact" ){
  235. $attributes = $product['id_product_attribute'];
  236. $attributes = str_replace(" ", "", $attributes);
  237. $attributes = explode(";", $attributes);
  238. $values = $product['combinations_unit_price_impact'];
  239. $values = str_replace(" ", "", $values);
  240. if( $values == "" ){
  241. continue;
  242. }
  243. $values = explode(";", $values);
  244. foreach( $attributes as $key => $attribute ){
  245. $combination = new Combination($attribute);
  246. if( !isset($values[$key]) ){
  247. continue;
  248. }
  249. $combination->unit_price_impact = number_format($values[$key], 4, '.', '');
  250. if( ( $error = $combination->validateFields(false, true) ) !== true ){
  251. $this->_createErrorsFile($error, $product['id_product']);
  252. return false;
  253. }
  254. $combination->update();
  255. }
  256. }
  257. elseif( $field == "combinations_minimal_quantity" ){
  258. $attributes = $product['id_product_attribute'];
  259. $attributes = str_replace(" ", "", $attributes);
  260. $attributes = explode(";", $attributes);
  261. $values = $product['combinations_minimal_quantity'];
  262. $values = str_replace(" ", "", $values);
  263. if( $values == "" ){
  264. continue;
  265. }
  266. $values = explode(";", $values);
  267. foreach( $attributes as $key => $attribute ){
  268. $combination = new Combination($attribute);
  269. if( !isset($values[$key]) ){
  270. continue;
  271. }
  272. $combination->minimal_quantity = $values[$key];
  273. if( ( $error = $combination->validateFields(false, true) ) !== true ){
  274. $this->_createErrorsFile($error, $product['id_product']);
  275. return false;
  276. }
  277. $combination->update();
  278. }
  279. }
  280. elseif( $field == "combinations_reference" ){
  281. $attributes = $product['id_product_attribute'];
  282. $attributes = str_replace(" ", "", $attributes);
  283. $attributes = explode(";", $attributes);
  284. $values = $product['combinations_reference'];
  285. $values = str_replace(" ", "", $values);
  286. if( $values == "" ){
  287. continue;
  288. }
  289. $values = explode(";", $values);
  290. foreach( $attributes as $key => $attribute ){
  291. $combination = new Combination($attribute);
  292. if( !isset($values[$key]) ){
  293. continue;
  294. }
  295. $combination->reference = $values[$key];
  296. if( ( $error = $combination->validateFields(false, true) ) !== true ){
  297. $this->_createErrorsFile($error, $product['id_product']);
  298. return false;
  299. }
  300. $combination->update();
  301. }
  302. }
  303. elseif( $field == "combinations_location" ){
  304. $attributes = $product['id_product_attribute'];
  305. $attributes = str_replace(" ", "", $attributes);
  306. $attributes = explode(";", $attributes);
  307. $values = $product['combinations_location'];
  308. $values = str_replace(" ", "", $values);
  309. if( $values == "" ){
  310. continue;
  311. }
  312. $values = explode(";", $values);
  313. foreach( $attributes as $key => $attribute ){
  314. $combination = new Combination($attribute);
  315. if( !isset($values[$key]) ){
  316. continue;
  317. }
  318. $combination->location = $values[$key];
  319. if( ( $error = $combination->validateFields(false, true) ) !== true ){
  320. $this->_createErrorsFile($error, $product['id_product']);
  321. return false;
  322. }
  323. $combination->update();
  324. }
  325. }
  326. elseif( $field == "combinations_weight" ){
  327. $attributes = $product['id_product_attribute'];
  328. $attributes = str_replace(" ", "", $attributes);
  329. $attributes = explode(";", $attributes);
  330. $values = $product['combinations_weight'];
  331. $values = str_replace(" ", "", $values);
  332. if( $values == "" ){
  333. continue;
  334. }
  335. $values = explode(";", $values);
  336. foreach( $attributes as $key => $attribute ){
  337. $combination = new Combination($attribute);
  338. if( !isset($values[$key]) ){
  339. continue;
  340. }
  341. $combination->weight = $values[$key];
  342. if( ( $error = $combination->validateFields(false, true) ) !== true ){
  343. $this->_createErrorsFile($error, $product['id_product']);
  344. return false;
  345. }
  346. $combination->update();
  347. }
  348. }
  349. elseif( $field == "combinations_ecotax" ){
  350. $attributes = $product['id_product_attribute'];
  351. $attributes = str_replace(" ", "", $attributes);
  352. $attributes = explode(";", $attributes);
  353. $values = $product['combinations_ecotax'];
  354. $values = str_replace(" ", "", $values);
  355. if( $values == "" ){
  356. continue;
  357. }
  358. $values = explode(";", $values);
  359. foreach( $attributes as $key => $attribute ){
  360. $combination = new Combination($attribute);
  361. if( !isset($values[$key]) ){
  362. continue;
  363. }
  364. $combination->ecotax = $values[$key];
  365. if( ( $error = $combination->validateFields(false, true) ) !== true ){
  366. $this->_createErrorsFile($error, $product['id_product']);
  367. return false;
  368. }
  369. $combination->update();
  370. }
  371. }
  372. elseif( $field == "combinations_ean13" ){
  373. $attributes = $product['id_product_attribute'];
  374. $attributes = str_replace(" ", "", $attributes);
  375. $attributes = explode(";", $attributes);
  376. $values = $product['combinations_ean13'];
  377. $values = str_replace(" ", "", $values);
  378. if( $values == "" ){
  379. continue;
  380. }
  381. $values = explode(";", $values);
  382. foreach( $attributes as $key => $attribute ){
  383. $combination = new Combination($attribute);
  384. if( !isset($values[$key]) ){
  385. continue;
  386. }
  387. $combination->ean13 = $values[$key];
  388. if( ( $error = $combination->validateFields(false, true) ) !== true ){
  389. $this->_createErrorsFile($error, $product['id_product']);
  390. return false;
  391. }
  392. $combination->update();
  393. }
  394. }
  395. elseif( $field == "combinations_upc" ){
  396. $attributes = $product['id_product_attribute'];
  397. $attributes = str_replace(" ", "", $attributes);
  398. $attributes = explode(";", $attributes);
  399. $values = $product['combinations_upc'];
  400. $values = str_replace(" ", "", $values);
  401. if( $values == "" ){
  402. continue;
  403. }
  404. $values = explode(";", $values);
  405. foreach( $attributes as $key => $attribute ){
  406. $combination = new Combination($attribute);
  407. if( !isset($values[$key]) ){
  408. continue;
  409. }
  410. $combination->upc = $values[$key];
  411. if( ( $error = $combination->validateFields(false, true) ) !== true ){
  412. $this->_createErrorsFile($error, $product['id_product']);
  413. return false;
  414. }
  415. $combination->update();
  416. }
  417. }
  418. elseif( $field == "tags" ){
  419. Tag::deleteTagsForProduct($product['id_product']);
  420. $tags = str_replace(" ", "", $value);
  421. if( $tags ){
  422. Tag::addTags( $this->_idLang, $product['id_product'], $tags );
  423. }
  424. }
  425. elseif( $field == "specific_price_from_quantity" ){
  426. $specific_price = $product['id_specific_price'];
  427. $specific_price = str_replace(" ", "", $specific_price);
  428. $specific_price = explode(";", $specific_price);
  429. $values = $product['specific_price_from_quantity'];
  430. $values = str_replace(" ", "", $values);
  431. if( $values == "" ){
  432. continue;
  433. }
  434. $values = explode(";", $values);
  435. foreach( $specific_price as $key => $specific_id ){
  436. $specificObject = new SpecificPrice($specific_id);
  437. if( !isset($values[$key]) ){
  438. continue;
  439. }
  440. $specificObject->from_quantity = $values[$key];
  441. if( ( $error = $specificObject->validateFields(false, true) ) !== true ){
  442. $this->_createErrorsFile($error, $product['id_product']);
  443. return false;
  444. }
  445. $specificObject->update();
  446. }
  447. }
  448. elseif( $field == "specific_price_reduction" ){
  449. $specific_price = $product['id_specific_price'];
  450. $specific_price = str_replace(" ", "", $specific_price);
  451. $specific_price = explode(";", $specific_price);
  452. $values = $product['specific_price_reduction'];
  453. $values = str_replace(" ", "", $values);
  454. if( $values == "" ){
  455. continue;
  456. }
  457. $values = explode(";", $values);
  458. foreach( $specific_price as $key => $specific_id ){
  459. $specificObject = new SpecificPrice($specific_id);
  460. if( !isset($values[$key]) ){
  461. continue;
  462. }
  463. $specificObject->reduction = $values[$key];
  464. if( ( $error = $specificObject->validateFields(false, true) ) !== true ){
  465. $this->_createErrorsFile($error, $product['id_product']);
  466. return false;
  467. }
  468. $specificObject->update();
  469. }
  470. }
  471. elseif( $field == "specific_price_reduction_type" ){
  472. $specific_price = $product['id_specific_price'];
  473. $specific_price = str_replace(" ", "", $specific_price);
  474. $specific_price = explode(";", $specific_price);
  475. $values = $product['specific_price_reduction_type'];
  476. $values = str_replace(" ", "", $values);
  477. if( $values == "" ){
  478. continue;
  479. }
  480. $values = explode(";", $values);
  481. foreach( $specific_price as $key => $specific_id ){
  482. $specificObject = new SpecificPrice($specific_id);
  483. if( !isset($values[$key]) ){
  484. continue;
  485. }
  486. $specificObject->reduction_type = $values[$key];
  487. if( ( $error = $specificObject->validateFields(false, true) ) !== true ){
  488. $this->_createErrorsFile($error, $product['id_product']);
  489. return false;
  490. }
  491. $specificObject->update();
  492. }
  493. }
  494. elseif( $field == "specific_price_from" ){
  495. $specific_price = $product['id_specific_price'];
  496. $specific_price = str_replace(" ", "", $specific_price);
  497. $specific_price = explode(";", $specific_price);
  498. $values = $product['specific_price_from'];
  499. $values = str_replace("; ", ";", $values);
  500. $values = str_replace(" ;", ";", $values);
  501. if( $values == "" ){
  502. continue;
  503. }
  504. $values = explode(";", $values);
  505. foreach( $specific_price as $key => $specific_id ){
  506. $specificObject = new SpecificPrice($specific_id);
  507. if( !isset($values[$key]) ){
  508. continue;
  509. }
  510. $specificObject->from = $values[$key];
  511. if( ( $error = $specificObject->validateFields(false, true) ) !== true ){
  512. $this->_createErrorsFile($error, $product['id_product']);
  513. return false;
  514. }
  515. $specificObject->update();
  516. }
  517. }
  518. elseif( $field == "specific_price_to" ){
  519. $specific_price = $product['id_specific_price'];
  520. $specific_price = str_replace(" ", "", $specific_price);
  521. $specific_price = explode(";", $specific_price);
  522. $values = $product['specific_price_to'];
  523. $values = str_replace("; ", ";", $values);
  524. $values = str_replace(" ;", ";", $values);
  525. if( $values == "" ){
  526. continue;
  527. }
  528. $values = explode(";", $values);
  529. foreach( $specific_price as $key => $specific_id ){
  530. $specificObject = new SpecificPrice($specific_id);
  531. if( !isset($values[$key]) ){
  532. continue;
  533. }
  534. $specificObject->to = $values[$key];
  535. if( ( $error = $specificObject->validateFields(false, true) ) !== true ){
  536. $this->_createErrorsFile($error, $product['id_product']);
  537. return false;
  538. }
  539. $specificObject->update();
  540. }
  541. }
  542. elseif( $field == "features" ){
  543. $productObject->deleteProductFeatures();
  544. foreach( $value as $feature => $featureValue ){
  545. if( !$featureValue ){
  546. continue;
  547. }
  548. $featureId = explode('_FEATURE_',$feature);
  549. $featureId = $featureId[0];
  550. $featureValueId = FeatureValue::addFeatureValueImport( $featureId, $featureValue, false, $this->_idLang );
  551. $productObject->addFeatureProductImport( $product['id_product'], $featureId, $featureValueId );
  552. }
  553. }
  554. elseif( $field == "quantity" && !($product['id_product_attribute'])){
  555. StockAvailable::setQuantity($product['id_product'], 0, $value);
  556. }
  557. elseif( $field == "redirect_type" ){
  558. $productObject->redirect_type = (string)$value;
  559. }
  560. elseif( $field == "meta_description" ){
  561. $currentValue = $productObject->meta_description;
  562. $currentValue[$this->_idLang] = (string)$value;
  563. $productObject->meta_description = $currentValue;
  564. }
  565. elseif( $field == "meta_keywords" ){
  566. $currentValue = $productObject->meta_keywords;
  567. $currentValue[$this->_idLang] = (string)$value;
  568. $productObject->meta_keywords = $currentValue;
  569. }
  570. elseif( $field == "meta_title" ){
  571. $currentValue = $productObject->meta_title;
  572. $currentValue[$this->_idLang] = (string)$value;
  573. $productObject->meta_title = $currentValue;
  574. }
  575. elseif( $field == "link_rewrite" ){
  576. $currentValue = $productObject->link_rewrite;
  577. $currentValue[$this->_idLang] = (string)$value;
  578. $productObject->link_rewrite = $currentValue;
  579. }
  580. elseif( $field == "name" ){
  581. $currentValue = $productObject->name;
  582. $currentValue[$this->_idLang] = (string)$value;
  583. $productObject->name = $currentValue;
  584. }
  585. elseif( $field == "description" ){
  586. $currentValue = $productObject->description;
  587. $currentValue[$this->_idLang] = (string)$value;
  588. $productObject->description = $currentValue;
  589. }
  590. elseif( $field == "description_short" ){
  591. $currentValue = $productObject->description_short;
  592. $currentValue[$this->_idLang] = (string)$value;
  593. $productObject->description_short = $currentValue;
  594. }
  595. elseif( $field == "available_now" ){
  596. $currentValue = $productObject->available_now;
  597. $currentValue[$this->_idLang] = (string)$value;
  598. $productObject->available_now = $currentValue;
  599. }
  600. elseif( $field == "available_later" ){
  601. $currentValue = $productObject->available_later;
  602. $currentValue[$this->_idLang] = (string)$value;
  603. $productObject->available_later = $currentValue;
  604. }
  605. elseif( $field == "suppliers_ids" ){
  606. $productObject->deleteFromSupplier();
  607. $values = explode(";", $value);
  608. foreach ($values as $id) {
  609. $supplierExists = SupplierCore::supplierExists($id);
  610. if($supplierExists){
  611. $product_supplier = new ProductSupplier();
  612. $product_supplier->id_product = $product['id_product'];
  613. $product_supplier->id_product_attribute = 0;
  614. $product_supplier->id_supplier = $id;
  615. $product_supplier->save();
  616. }
  617. }
  618. }
  619. elseif( $field == "base_price" ){
  620. $productObject->price = number_format($value, 4, '.', '');
  621. }
  622. elseif( $field == "base_price_with_tax" ){
  623. $taxPrice = (float)$value;
  624. if( $productObject->tax_rate ){
  625. $taxPrice = $taxPrice / (($productObject->tax_rate/100)+1);
  626. }
  627. $productObject->price = number_format($taxPrice, 4, '.', '');
  628. }
  629. elseif( $field == "wholesale_price" ){
  630. $productObject->wholesale_price = number_format($value, 4, '.', '');
  631. }
  632. else{
  633. if( $field != "id_product" && $field != "id_product_attribute" && $field != "id_specific_price" ){
  634. $productObject->$field = $value;
  635. }
  636. }
  637. }
  638. if( ( $error = $productObject->validateFields(false, true) ) !== true ){
  639. $this->_createErrorsFile($error, $product['id_product']);
  640. return false;
  641. }
  642. $productObject->update();
  643. $this->_updatedProducts++;
  644. return true;
  645. }
  646. private function _copyFile()
  647. {
  648. if ( !isset($_FILES['file']) )
  649. {
  650. throw new Exception( Module::getInstanceByName('updateproducts')->l('Please select file for update!') );
  651. }
  652. $file_type = Tools::substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.')+1);
  653. if( $file_type != $this->_format && $this->_format == 'xlsx' ){
  654. throw new Exception( Module::getInstanceByName('updateproducts')->l('Error: Your select in setting XLSX extension file. Uploaded file must have XLSX extension!') );
  655. }
  656. if( $file_type != $this->_format && $this->_format == 'csv' ){
  657. throw new Exception( Module::getInstanceByName('updateproducts')->l('Error: Your select in setting CSV extension file. Uploaded file must have CSV extension!') );
  658. }
  659. if (!Tools::copy($_FILES['file']['tmp_name'], dirname(__FILE__).'/files/import_products.'. $this->_format)){
  660. throw new Exception(Module::getInstanceByName('updateproducts')->l('An error occurred while uploading: ', 'import').$_FILES['file']['tmp_name']);
  661. }
  662. $this->_PHPExcelFactory = PHPExcel_IOFactory::load("files/import_products." . $this->_format );
  663. }
  664. private function _createErrorsFile($error, $nameProduct)
  665. {
  666. $write_fd = fopen('error/error_logs.csv', 'a+');
  667. if (@$write_fd !== false){
  668. fwrite($write_fd, $nameProduct . ',' . $error . "\r\n");
  669. }
  670. fclose($write_fd);
  671. }
  672. }