PageRenderTime 26ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/storecommander/ead6f6fce09/SC/lib/cat/combination/cat_combination_update.php

https://gitlab.com/ptisky/API_prestashop
PHP | 255 lines | 202 code | 29 blank | 24 comment | 41 complexity | 68d16ea9b410cfe8f09e0fb26537c815 MD5 | raw file
  1. <?php
  2. /**
  3. * Store Commander
  4. *
  5. * @category administration
  6. * @author Store Commander - support@storecommander.com
  7. * @version 2015-09-15
  8. * @uses Prestashop modules
  9. * @since 2009
  10. * @copyright Copyright &copy; 2009-2015, Store Commander
  11. * @license commercial
  12. * All rights reserved! Copying, duplication strictly prohibited
  13. *
  14. * *****************************************
  15. * * STORE COMMANDER *
  16. * * http://www.StoreCommander.com *
  17. * * V 2015-09-15 *
  18. * *****************************************
  19. *
  20. * Compatibility: PS version: 1.1 to 1.6.1
  21. *
  22. **/
  23. $debug=false;
  24. $gr_id = Tools::getValue('gr_id');
  25. $id_lang = Tools::getValue('id_lang','0');
  26. $action = Tools::getValue('action','');
  27. $callback = Tools::getValue('callback','');
  28. $error='';
  29. $return = "";
  30. if (substr($gr_id,0,3)=='NEW' && $action=="insert")
  31. {
  32. $newId = 0;
  33. $id_product=intval(Tools::getValue('id_product'));
  34. if (version_compare(_PS_VERSION_, '1.5.0.0', '>='))
  35. $product = new Product($id_product, false, (int)$id_lang, (int)SCI::getSelectedShop());
  36. else
  37. $product = new Product($id_product);
  38. if (Validate::isLoadedObject($product))
  39. {
  40. if (version_compare(_PS_VERSION_, '1.5.0.0', '>='))
  41. {
  42. $has_small_num = false;
  43. $price = max(floatval(Tools::getValue('price',0)) - floatval(Tools::getValue('pprice',0)),0);
  44. $small_price = $price;
  45. if($price>0 && $price<0.001)
  46. {
  47. $price = 0;
  48. $has_small_num = true;
  49. }
  50. $weight = max(floatval(Tools::getValue('weight',0)) - floatval(Tools::getValue('pweight',0)),0);
  51. $small_weight = $weight;
  52. if($weight>0 && $weight<0.001)
  53. {
  54. $weight = 0;
  55. $has_small_num = true;
  56. }
  57. $id_product_attribute = $product->addAttribute(
  58. number_format( $price , 6, ".", ""),
  59. number_format( $weight ,6, ".", ""),
  60. 0,
  61. 0,
  62. 0,
  63. Tools::getValue('reference'),
  64. Tools::getValue('ean13'),
  65. 0,
  66. Tools::getValue('location'),
  67. Tools::getValue('upc'),
  68. 1,
  69. SCI::getShopsByProduct($product->id)
  70. );
  71. if($has_small_num && (!empty($small_price) || !empty($small_weight)))
  72. {
  73. $set = "";
  74. if(!empty($small_price))
  75. $set .= ", price='".pSQL(number_format( $small_price , 6, ".", ""))."'";
  76. if(!empty($small_weight))
  77. $set .= ", weight='".pSQL(number_format( $small_weight , 6, ".", ""))."'";
  78. if(!empty($set))
  79. {
  80. $sql = "UPDATE "._DB_PREFIX_."product_attribute SET `date_upd`=NOW() ".$set." WHERE id_product_attribute=".intval($id_product_attribute);
  81. Db::getInstance()->Execute($sql);
  82. if(SCMS)
  83. {
  84. $shops = SCI::getShopsByProduct($product->id);
  85. foreach ($shops as $shop_id)
  86. {
  87. $sql = "UPDATE "._DB_PREFIX_."product_attribute_shop SET `date_upd`=NOW() ".$set." WHERE id_shop = ".(int)$shop_id." AND id_product_attribute=".intval($id_product_attribute);
  88. Db::getInstance()->Execute($sql);
  89. }
  90. }
  91. }
  92. }
  93. $qty = Tools::getValue('quantity',0);
  94. if($qty==0)
  95. $qty = _s("CAT_PROD_COMBI_CREA_QTY");
  96. SCI::setQuantity($product->id, $id_product_attribute, $qty, SCI::getShopsByProduct($product->id));
  97. $combination = new Combination((int)$id_product_attribute);
  98. $combination->id_product=$product->id;
  99. $combination->minimal_quantity=max(1,(int)$combination->minimal_quantity);
  100. $combination->id_shop_list=SCI::getShopsByProduct($product->id);
  101. //$combination->setAttributes($attribute_combinaison_list);
  102. $combination->save();
  103. if(_s("CAT_APPLY_ALL_CART_RULES"))
  104. SpecificPriceRule::applyAllRules(array((int)$product->id));
  105. if(SCAS && $product->advanced_stock_management=="1")
  106. {
  107. $row = Db::getInstance()->getRow('
  108. SELECT pa.id_product_attribute
  109. FROM `'._DB_PREFIX_.'product_attribute` pa
  110. '.Shop::addSqlAssociation('product_attribute', 'pa').'
  111. WHERE product_attribute_shop.`default_on` = 1
  112. AND pa.`id_product` = '.(int)$product->id
  113. );
  114. if (!empty($row["id_product_attribute"]))
  115. {
  116. $sql = 'SELECT DISTINCT(id_warehouse) as id_warehouse
  117. FROM `'._DB_PREFIX_.'warehouse_product_location`
  118. WHERE id_product_attribute = "'.(int)$row["id_product_attribute"].'"';
  119. }
  120. else
  121. {
  122. $sql = 'SELECT DISTINCT(id_warehouse) as id_warehouse
  123. FROM `'._DB_PREFIX_.'warehouse_product_location`
  124. WHERE id_product = "'.(int)$product->id.'"
  125. AND id_product_attribute="0"';
  126. }
  127. $warehouses = Db::getInstance()->executeS($sql);
  128. if(!empty($warehouses) && count($warehouses)>0)
  129. {
  130. foreach($warehouses as $warehouse)
  131. {
  132. if(!empty($warehouse["id_warehouse"]))
  133. {
  134. Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'warehouse_product_location (id_product, id_product_attribute, id_warehouse)
  135. VALUES ("'.(int)$product->id.'","'.(int)$id_product_attribute.'","'.(int)$warehouse["id_warehouse"].'")');
  136. }
  137. }
  138. }
  139. Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'warehouse_product_location WHERE id_product = "'.(int)$product->id.'" AND id_product_attribute="0"');
  140. }
  141. }else{
  142. if (version_compare(_PS_VERSION_, '1.4.0.0', '>='))
  143. {
  144. $has_small_num = false;
  145. $price = max(floatval(Tools::getValue('price',0)) - floatval(Tools::getValue('pprice',0)),0);
  146. $small_price = $price;
  147. if($price>0 && $price<0.001)
  148. {
  149. $price = 0;
  150. $has_small_num = true;
  151. }
  152. $weight = max(floatval(Tools::getValue('weight',0)) - floatval(Tools::getValue('pweight',0)),0);
  153. $small_weight = $weight;
  154. if($weight>0 && $weight<0.001)
  155. {
  156. $weight = 0;
  157. $has_small_num = true;
  158. }
  159. $qty = Tools::getValue('quantity',0);
  160. if($qty==0)
  161. $qty = _s("CAT_PROD_COMBI_CREA_QTY");
  162. $id_product_attribute = $product->addProductAttribute(
  163. number_format( $price , 6, ".", ""),
  164. number_format( $weight ,6, ".", ""),
  165. 0,
  166. 0,
  167. $qty,
  168. '',
  169. Tools::getValue('reference'),
  170. Tools::getValue('supplier_reference'),
  171. Tools::getValue('ean13'),
  172. 0,
  173. Tools::getValue('location'));
  174. if($has_small_num && (!empty($small_price) || !empty($small_weight)))
  175. {
  176. $set = "";
  177. if(!empty($small_price))
  178. $set .= ", price='".pSQL(number_format( $small_price , 6, ".", ""))."'";
  179. if(!empty($small_weight))
  180. $set .= ", weight='".pSQL(number_format( $small_weight , 6, ".", ""))."'";
  181. if(!empty($set))
  182. {
  183. $sql = "UPDATE "._DB_PREFIX_."product_attribute SET `date_upd`=NOW() ".$set." WHERE id_product_attribute=".intval($id_product_attribute);
  184. Db::getInstance()->Execute($sql);
  185. }
  186. }
  187. }else{
  188. $qty = Tools::getValue('quantity',0);
  189. if($qty==0)
  190. $qty = _s("CAT_PROD_COMBI_CREA_QTY");
  191. $id_product_attribute = $product->addProductAttribute(
  192. round(max(floatval(Tools::getValue('price')) - floatval(Tools::getValue('pprice')),0),6),
  193. round(max(floatval(Tools::getValue('weight')) - floatval(Tools::getValue('pweight')),0),6),
  194. 0,
  195. $qty,
  196. '',
  197. Tools::getValue('reference'),
  198. Tools::getValue('supplier_reference'),
  199. Tools::getValue('ean13'),
  200. 0,
  201. Tools::getValue('location'));
  202. }
  203. //$product->addAttributeCombinaison($id_product_attribute, $attribute_combinaison_list);
  204. }
  205. $product->checkDefaultAttributes();
  206. $sql = "UPDATE "._DB_PREFIX_."product_attribute SET `date_upd`=NOW() WHERE id_product_attribute=".intval($id_product_attribute);
  207. Db::getInstance()->Execute($sql);
  208. $newId = $id_product_attribute;
  209. if(!empty($id_product))
  210. ExtensionPMCM::clearFromIdsProduct($id_product);
  211. // RETURN
  212. if(!empty($newId))
  213. {
  214. $callback = str_replace("{newid}", $newId, $callback) ;
  215. $return = json_encode(array("callback"=>$callback));
  216. }
  217. }
  218. else
  219. $error='Product not found';
  220. }
  221. sc_ext::readCustomCombinationsGridConfigXML('extraVars');
  222. if(empty($return))
  223. $error = "ERROR: Try again";
  224. if(!empty($error))
  225. $return = $error;
  226. echo $return;