PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/storecommander/ead6f6fce09/SC/lib/cat/specificprice/cat_specificprice_update.php

https://gitlab.com/ptisky/API_prestashop
PHP | 222 lines | 169 code | 24 blank | 29 comment | 28 complexity | bbde111fb28e11cdc27f0f0a9567ec71 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. @error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
  24. @ini_set("display_errors", "ON");
  25. $id_lang = Tools::getValue('id_lang','0');
  26. $action = Tools::getValue('action','');
  27. $return = "ERROR: Try again later";
  28. // FUNCTIONS
  29. $updated_products = array();
  30. // Récupération de toutes les modifications à effectuer
  31. if(!empty($_POST["rows"]) || $action=="insert")
  32. {
  33. if($action!="insert")
  34. {
  35. if(_PS_MAGIC_QUOTES_GPC_)
  36. $_POST["rows"] = stripslashes($_POST["rows"]);
  37. $rows = json_decode($_POST["rows"]);
  38. }
  39. else
  40. {
  41. $rows = array();
  42. $rows[0] = new stdClass();
  43. $rows[0]->name = Tools::getValue('act','');
  44. $rows[0]->action = Tools::getValue('action','');
  45. $rows[0]->row = Tools::getValue('gr_id','');
  46. $rows[0]->callback = Tools::getValue('callback','');
  47. $rows[0]->params = $_POST;
  48. }
  49. if(is_array($rows) && count($rows)>0)
  50. {
  51. $callbacks = '';
  52. // Première boucle pour remplir la table sc_queue_log
  53. // avec toutes ces modifications
  54. $log_ids = array();
  55. $date = date("Y-m-d H:i:s");
  56. foreach($rows as $num => $row)
  57. {
  58. $id = QueueLog::add($row->name, $row->row, $row->action, (!empty($row->params)?$row->params:array()), (!empty($row->callback)?$row->callback:null), $date);
  59. $log_ids[$num] = $id;
  60. }
  61. // Deuxième boucle pour effectuer les
  62. // actions les une après les autres
  63. foreach($rows as $num => $row)
  64. {
  65. if(!empty($log_ids[$num]))
  66. {
  67. $gr_id = intval($row->row);
  68. $id_specific_price=$row->row;
  69. $action = $row->action;
  70. if(!empty($row->callback))
  71. $callbacks .= $row->callback.";";
  72. if($action!="insert")
  73. {
  74. $_POST=array();
  75. $_POST = (array) json_decode($row->params);
  76. }
  77. if(!empty($action) && $action=="insert")
  78. {
  79. $id_shop = Tools::getValue('id_shop',0);
  80. $id_shop_group = Tools::getValue('id_shop_group',0);
  81. $id_currency = Tools::getValue('id_currency',0);
  82. $id_country = Tools::getValue('id_country',0);
  83. $id_group = Tools::getValue('id_group',0);
  84. $id_customer = Tools::getValue('id_customer',0); // TODO: grid management
  85. $price = str_replace(',','.',trim(Tools::getValue('price')));
  86. $from_quantity = Tools::getValue('from_quantity');
  87. $reduction_tax = Tools::getValue('reduction_tax');
  88. $reduction = str_replace(',','.',Tools::getValue('reduction'));
  89. $reduction_type=(strpos(trim($reduction),'%')!==false?'percentage' : 'amount');
  90. $reduction = str_replace('%','',$reduction);
  91. $reduction=str_replace(',','.',$reduction);
  92. $from = Tools::getValue('from');
  93. $to = Tools::getValue('to');
  94. $id_product = (Tools::getValue('id_product',0));
  95. $id_products = explode(",", $id_product);
  96. if(!empty($id_products) && count($id_products)==1)
  97. $updated_products[$id_products]=$id_products;
  98. elseif(!empty($id_products) && count($id_products)>1)
  99. $updated_products = array_merge($updated_products,$id_products);
  100. $spe_id = "";
  101. $id_products = explode(",", $id_product);
  102. foreach($id_products as $id_product)
  103. {
  104. if ((int)$id_product > 0)
  105. {
  106. $specificPrice = new SpecificPrice();
  107. $specificPrice->id_product = $id_product;
  108. $specificPrice->id_product_attribute = 0;
  109. $specificPrice->id_shop = 0;//(count(SCI::getSelectedShopActionList()) > 1 || SCI::getSelectedShop() == 0 ? 0 : (int)SCI::getSelectedShop() );
  110. $specificPrice->id_shop_group = 0;
  111. $specificPrice->id_currency = (int)($id_currency);
  112. $specificPrice->id_country = (int)($id_country);
  113. $specificPrice->id_group = (int)($id_group);
  114. if (version_compare(_PS_VERSION_, '1.5.0.0', '>='))
  115. $specificPrice->id_customer = (int)($id_customer);
  116. if (version_compare(_PS_VERSION_, '1.6.0.11', '>='))
  117. $specificPrice->reduction_tax = (int)$reduction_tax;
  118. $specificPrice->price = (float)($price);
  119. $specificPrice->from_quantity = 1;
  120. $specificPrice->reduction = (float)($reduction_type == 'percentage' ? (floatval($reduction) / 100) : $reduction);
  121. $specificPrice->reduction_type = $reduction_type;
  122. $specificPrice->from = !$from ? '0000-00-00 00:00:00' : $from;
  123. $specificPrice->to = !$to ? '0000-00-00 00:00:00' : $to;
  124. $specificPrice->save();
  125. if(!empty($spe_id))
  126. $spe_id .= ",";
  127. $spe_id .= $specificPrice->id;
  128. }
  129. }
  130. $newId = $spe_id;
  131. if(!empty($newId))
  132. {
  133. $callbacks = str_replace("{newid}", $newId, $callbacks) ;
  134. }
  135. }
  136. elseif(!empty($action) && $action=="delete" && !empty($gr_id))
  137. {
  138. $id_specific_prices = explode(",", $id_specific_price);
  139. foreach($id_specific_prices as $id_specific_price)
  140. {
  141. $specificPrice = new SpecificPrice((int)($id_specific_price));
  142. $updated_products[$specificPrice->id_product]=$specificPrice->id_product;
  143. $specificPrice->delete();
  144. }
  145. }
  146. elseif(!empty($action) && $action=="update" && !empty($gr_id))
  147. {
  148. $fields=array('price','from_quantity','id_shop','id_shop_group','id_group','id_country','id_currency','reduction','reduction_type','from','to','reduction_tax');
  149. $reduction = str_replace(',','.',Tools::getValue('reduction'));
  150. $reduction_type=(strpos(trim($reduction),'%')!==false?'percentage' : 'amount');
  151. $reduction = str_replace('%','',$reduction);
  152. $reduction=str_replace(',','.',$reduction);
  153. $from = Tools::getValue('from');
  154. $to = Tools::getValue('to');
  155. $id_specific_prices = explode(",", $id_specific_price);
  156. foreach($id_specific_prices as $id_specific_price)
  157. {
  158. $specificPrice = new SpecificPrice((int)($id_specific_price));
  159. $updated_products[$specificPrice->id_product]=$specificPrice->id_product;
  160. foreach($fields as $field)
  161. {
  162. if (isset($_POST[$field]))
  163. {
  164. if($field=="reduction")
  165. {
  166. $specificPrice->reduction = (float)($reduction_type == 'percentage' ? ($reduction / 100) : $reduction);
  167. $specificPrice->reduction_type = $reduction_type;
  168. }
  169. elseif($field=="reduction_type")
  170. $specificPrice->reduction_type = $reduction_type;
  171. elseif($field=="from")
  172. $specificPrice->from = !$from ? '0000-00-00 00:00:00' : $from;
  173. elseif($field=="to")
  174. $specificPrice->to = !$to ? '0000-00-00 00:00:00' : $to;
  175. elseif($field=="price")
  176. $specificPrice->price = (float)$_POST[$field];
  177. elseif($field=="from_quantity")
  178. {
  179. $specificPrice->from_quantity = (_s("APP_COMPAT_MODULE_PPE")?floatval($_POST[$field]):(int)$_POST[$field]);
  180. }
  181. else
  182. $specificPrice->$field = (int)$_POST[$field];
  183. }
  184. }
  185. $specificPrice->update();
  186. }
  187. }
  188. QueueLog::delete(($log_ids[$num]));
  189. }
  190. }
  191. // PM Cache
  192. if(!empty($updated_products))
  193. ExtensionPMCM::clearFromIdsProduct($updated_products);
  194. // RETURN
  195. $return = json_encode(array("callback"=>$callbacks));
  196. }
  197. }
  198. echo $return;