|
@@ -71,6 +71,12 @@ class ProductCombination
|
|
|
*/
|
|
|
public $entity;
|
|
|
|
|
|
+ /**
|
|
|
+ * Combination price level
|
|
|
+ * @var ProductCombinationLevel[]
|
|
|
+ */
|
|
|
+ public $combination_price_levels;
|
|
|
+
|
|
|
/**
|
|
|
* Constructor
|
|
|
*
|
|
@@ -92,6 +98,8 @@ class ProductCombination
|
|
|
*/
|
|
|
public function fetch($rowid)
|
|
|
{
|
|
|
+ global $conf;
|
|
|
+
|
|
|
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE rowid = ".(int) $rowid." AND entity IN (".getEntity('product').")";
|
|
|
|
|
|
$query = $this->db->query($sql);
|
|
@@ -113,9 +121,118 @@ class ProductCombination
|
|
|
$this->variation_price_percentage = $obj->variation_price_percentage;
|
|
|
$this->variation_weight = $obj->variation_weight;
|
|
|
|
|
|
+ if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
|
|
|
+ $this->fetchCombinationPriceLevels();
|
|
|
+ }
|
|
|
+
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Retrieves combination price levels
|
|
|
+ *
|
|
|
+ * @param int $fk_price_level the price level to fetch, use 0 for all
|
|
|
+ * @param bool $useCache to use cache or not
|
|
|
+ * @return int <0 KO, >0 OK
|
|
|
+ */
|
|
|
+ public function fetchCombinationPriceLevels($fk_price_level = 0, $useCache = true)
|
|
|
+ {
|
|
|
+ global $conf;
|
|
|
+
|
|
|
+ // Check cache
|
|
|
+ if (!empty($this->combination_price_levels) && $useCache){
|
|
|
+ if ((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)){
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!is_array($this->combination_price_levels)
|
|
|
+ || empty($fk_price_level) // if fetch an unique level dont erase all already fetched
|
|
|
+ ){
|
|
|
+ $this->combination_price_levels = array();
|
|
|
+ }
|
|
|
+
|
|
|
+ $staticProductCombinationLevel = new ProductCombinationLevel($this->db);
|
|
|
+ $combination_price_levels = $staticProductCombinationLevel->fetchAll($this->id, $fk_price_level);
|
|
|
+
|
|
|
+ if (!is_array($combination_price_levels)) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($combination_price_levels)){
|
|
|
+
|
|
|
+ /**
|
|
|
+ * for auto retrocompatibility with last behavior
|
|
|
+ */
|
|
|
+ $productCombinationLevel = new ProductCombinationLevel($this->db);
|
|
|
+ $productCombinationLevel->fk_price_level = intval($fk_price_level);
|
|
|
+ $productCombinationLevel->fk_product_attribute_combination = $this->id;
|
|
|
+ $productCombinationLevel->variation_price = $this->variation_price;
|
|
|
+ $productCombinationLevel->variation_price_percentage = $this->variation_price_percentage;
|
|
|
+
|
|
|
+ if ($fk_price_level>0){
|
|
|
+ $combination_price_levels[$fk_price_level] = $productCombinationLevel;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++){
|
|
|
+ $combination_price_levels[$i] = $productCombinationLevel;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->combination_price_levels = $combination_price_levels;
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Retrieves combination price levels
|
|
|
+ *
|
|
|
+ * @param int $clean levels off PRODUIT_MULTIPRICES_LIMIT
|
|
|
+ * @return int <0 KO, >0 OK
|
|
|
+ */
|
|
|
+ public function saveCombinationPriceLevels($clean = 1)
|
|
|
+ {
|
|
|
+ global $conf;
|
|
|
+
|
|
|
+ $errors = 0;
|
|
|
+
|
|
|
+ $staticProductCombinationLevel = new ProductCombinationLevel($this->db);
|
|
|
+
|
|
|
+ // Delete all
|
|
|
+ if (empty($this->combination_price_levels)){
|
|
|
+ return $staticProductCombinationLevel->deleteAllForCombination($this->id);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Clean not needed price levels
|
|
|
+ if ($clean){
|
|
|
+ $res = $staticProductCombinationLevel->clean($this->id);
|
|
|
+
|
|
|
+ if ($res<0){
|
|
|
+ $this->errors[] = 'Fail to clean not needed price levels';
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($this->combination_price_levels as $fk_price_level => $combination_price_level){
|
|
|
+ $res = $combination_price_level->save();
|
|
|
+ if ($res<1){
|
|
|
+ $this->error = 'save combination price level '.$fk_price_level . ' '.$combination_price_level->error;
|
|
|
+ $this->errors[] = $this->error;
|
|
|
+ $errors ++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($errors > 0){
|
|
|
+ return $errors*-1;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* Retrieves a product combination by a child product row id
|
|
|
*
|
|
@@ -124,6 +241,8 @@ class ProductCombination
|
|
|
*/
|
|
|
public function fetchByFkProductChild($fk_child)
|
|
|
{
|
|
|
+ global $conf;
|
|
|
+
|
|
|
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_child = ".(int) $fk_child." AND entity IN (".getEntity('product').")";
|
|
|
|
|
|
$query = $this->db->query($sql);
|
|
@@ -145,6 +264,10 @@ class ProductCombination
|
|
|
$this->variation_price_percentage = $result->variation_price_percentage;
|
|
|
$this->variation_weight = $result->variation_weight;
|
|
|
|
|
|
+ if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
|
|
|
+ $this->fetchCombinationPriceLevels();
|
|
|
+ }
|
|
|
+
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
@@ -156,6 +279,8 @@ class ProductCombination
|
|
|
*/
|
|
|
public function fetchAllByFkProductParent($fk_product_parent)
|
|
|
{
|
|
|
+ global $conf;
|
|
|
+
|
|
|
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_parent = ".(int) $fk_product_parent." AND entity IN (".getEntity('product').")";
|
|
|
|
|
|
$query = $this->db->query($sql);
|
|
@@ -175,6 +300,10 @@ class ProductCombination
|
|
|
$tmp->variation_price_percentage = $result->variation_price_percentage;
|
|
|
$tmp->variation_weight = $result->variation_weight;
|
|
|
|
|
|
+ if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
|
|
|
+ $tmp->fetchCombinationPriceLevels();
|
|
|
+ }
|
|
|
+
|
|
|
$return[] = $tmp;
|
|
|
}
|
|
|
|
|
@@ -209,6 +338,8 @@ class ProductCombination
|
|
|
*/
|
|
|
public function create($user)
|
|
|
{
|
|
|
+ global $conf;
|
|
|
+
|
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_attribute_combination
|
|
|
(fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight, entity)
|
|
|
VALUES (".(int) $this->fk_product_parent.", ".(int) $this->fk_product_child.",
|
|
@@ -221,6 +352,13 @@ class ProductCombination
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
+ if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
|
|
|
+ $res = $this->saveCombinationPriceLevels();
|
|
|
+ if ($res<0){
|
|
|
+ return -2;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'product_attribute_combination');
|
|
|
|
|
|
return 1;
|
|
@@ -234,6 +372,8 @@ class ProductCombination
|
|
|
*/
|
|
|
public function update(User $user)
|
|
|
{
|
|
|
+ global $conf;
|
|
|
+
|
|
|
$sql = "UPDATE ".MAIN_DB_PREFIX."product_attribute_combination
|
|
|
SET fk_product_parent = ".(int) $this->fk_product_parent.", fk_product_child = ".(int) $this->fk_product_child.",
|
|
|
variation_price = ".(float) $this->variation_price.", variation_price_percentage = ".(int) $this->variation_price_percentage.",
|
|
@@ -245,6 +385,14 @@ class ProductCombination
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
|
|
|
+ $res = $this->saveCombinationPriceLevels();
|
|
|
+ if ($res<0){
|
|
|
+ return -2;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
$parent = new Product($this->db);
|
|
|
$parent->fetch($this->fk_product_parent);
|
|
|
|
|
@@ -266,6 +414,12 @@ class ProductCombination
|
|
|
$comb2val = new ProductCombination2ValuePair($this->db);
|
|
|
$comb2val->deleteByFkCombination($this->id);
|
|
|
|
|
|
+ // remove combination price levels
|
|
|
+ if (!$this->db->query("DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination_price_level WHERE fk_product_attribute_combination = ".(int) $this->id)) {
|
|
|
+ $this->db->rollback();
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE rowid = ".(int) $this->id;
|
|
|
|
|
|
if ($this->db->query($sql)) {
|
|
@@ -341,6 +495,7 @@ class ProductCombination
|
|
|
$child->label = $parent->label.$varlabel;;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
if ($child->update($child->id, $user) > 0) {
|
|
|
$new_vat = $parent->tva_tx;
|
|
|
$new_npr = $parent->tva_npr;
|
|
@@ -349,9 +504,12 @@ class ProductCombination
|
|
|
if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
|
|
|
for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++)
|
|
|
{
|
|
|
- if ($parent->multiprices[$i] != '') {
|
|
|
+ if ($parent->multiprices[$i] != '' || isset($this->combination_price_levels[$i]->variation_price)) {
|
|
|
$new_type = $parent->multiprices_base_type[$i];
|
|
|
$new_min_price = $parent->multiprices_min[$i];
|
|
|
+ $variation_price = doubleval(!isset($this->combination_price_levels[$i]->variation_price) ? $this->variation_price : $this->combination_price_levels[$i]->variation_price);
|
|
|
+ $variation_price_percentage = doubleval(!isset($this->combination_price_levels[$i]->variation_price_percentage) ? $this->variation_price_percentage : $this->combination_price_levels[$i]->variation_price_percentage);
|
|
|
+
|
|
|
if ($parent->prices_by_qty_list[$i]) {
|
|
|
$new_psq = 1;
|
|
|
} else {
|
|
@@ -364,12 +522,12 @@ class ProductCombination
|
|
|
$new_price = $parent->multiprices[$i];
|
|
|
}
|
|
|
|
|
|
- if ($this->variation_price_percentage) {
|
|
|
+ if ($variation_price_percentage) {
|
|
|
if ($new_price != 0) {
|
|
|
- $new_price *= 1 + ($this->variation_price / 100);
|
|
|
+ $new_price *= 1 + ($variation_price / 100);
|
|
|
}
|
|
|
} else {
|
|
|
- $new_price += $this->variation_price;
|
|
|
+ $new_price += $variation_price;
|
|
|
}
|
|
|
|
|
|
$child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, $i, $new_npr, $new_psq);
|
|
@@ -508,7 +666,7 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
|
|
* @param Product $product Parent product
|
|
|
* @param array $combinations Attribute and value combinations.
|
|
|
* @param array $variations Price and weight variations
|
|
|
- * @param bool $price_var_percent Is the price variation a relative variation?
|
|
|
+ * @param bool|array $price_var_percent Is the price variation a relative variation?
|
|
|
* @param bool|float $forced_pricevar If the price variation is forced
|
|
|
* @param bool|float $forced_weightvar If the weight variation is forced
|
|
|
* @param bool|string $forced_refvar If the reference is forced
|
|
@@ -523,6 +681,8 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
|
|
|
|
|
$db->begin();
|
|
|
|
|
|
+ $price_impact = array(1=>0); // init level price impact
|
|
|
+
|
|
|
$forced_refvar = trim($forced_refvar);
|
|
|
|
|
|
if (!empty($forced_refvar) && $forced_refvar != $product->ref) {
|
|
@@ -545,7 +705,12 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
|
|
$weight_impact = (float) $forced_weightvar; // If false, return 0
|
|
|
|
|
|
//Final price impact
|
|
|
- $price_impact = (float) $forced_pricevar; // If false, return 0
|
|
|
+ if (!is_array($forced_pricevar)){
|
|
|
+ $price_impact[1] = (float) $forced_pricevar; // If false, return 0
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $price_impact = $forced_pricevar;
|
|
|
+ }
|
|
|
|
|
|
$newcomb = new ProductCombination($db);
|
|
|
$existingCombination = $newcomb->fetchByProductCombination2ValuePairs($product->id, $combinations);
|
|
@@ -587,7 +752,15 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
|
|
$weight_impact += (float) price2num($variations[$currcombattr][$currcombval]['weight']);
|
|
|
}
|
|
|
if ($forced_pricevar === false) {
|
|
|
- $price_impact += (float) price2num($variations[$currcombattr][$currcombval]['price']);
|
|
|
+ $price_impact[1] += (float) price2num($variations[$currcombattr][$currcombval]['price']);
|
|
|
+
|
|
|
+ // Manage Price levels
|
|
|
+ if ($conf->global->PRODUIT_MULTIPRICES){
|
|
|
+ for ($i = 2; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++)
|
|
|
+ {
|
|
|
+ $price_impact[$i] += (float) price2num($variations[$currcombattr][$currcombval]['price']);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if ($forced_refvar === false) {
|
|
@@ -606,9 +779,27 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
|
|
}
|
|
|
|
|
|
$newcomb->variation_price_percentage = $price_var_percent;
|
|
|
- $newcomb->variation_price = $price_impact;
|
|
|
+ $newcomb->variation_price = $price_impact[1];
|
|
|
$newcomb->variation_weight = $weight_impact;
|
|
|
|
|
|
+ // Init price level
|
|
|
+ if ($conf->global->PRODUIT_MULTIPRICES){
|
|
|
+ for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++){
|
|
|
+ $productCombinationLevel = new ProductCombinationLevel($this->db);
|
|
|
+ $productCombinationLevel->fk_product_attribute_combination = 0;
|
|
|
+ $productCombinationLevel->fk_price_level = $i;
|
|
|
+ $productCombinationLevel->variation_price = $price_impact[$i];
|
|
|
+
|
|
|
+ if (is_array($price_var_percent)){
|
|
|
+ $productCombinationLevel->variation_price_percentage = !empty($price_var_percent[$i]) ? $price_var_percent[$i] : 0;
|
|
|
+ }else {
|
|
|
+ $productCombinationLevel->variation_price_percentage = $price_var_percent;
|
|
|
+ }
|
|
|
+
|
|
|
+ $newcomb->combination_price_levels[$i] = $productCombinationLevel;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
$newproduct->weight += $weight_impact;
|
|
|
|
|
|
// Now create the product
|
|
@@ -764,3 +955,260 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
|
|
return $label;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Class ProductCombinationLevel
|
|
|
+ * Used to represent a product combination Level
|
|
|
+ */
|
|
|
+class ProductCombinationLevel
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * Database handler
|
|
|
+ * @var DoliDB
|
|
|
+ */
|
|
|
+ private $db;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var string Name of table without prefix where object is stored
|
|
|
+ */
|
|
|
+ public $table_element = 'product_attribute_combination_price_level';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Rowid of combination
|
|
|
+ * @var int
|
|
|
+ */
|
|
|
+ public $id;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Rowid of parent product combination
|
|
|
+ * @var int
|
|
|
+ */
|
|
|
+ public $fk_product_attribute_combination;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Combination price level
|
|
|
+ * @var int
|
|
|
+ */
|
|
|
+ public $fk_price_level;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Price variation
|
|
|
+ * @var float
|
|
|
+ */
|
|
|
+ public $variation_price;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Is the price variation a relative variation?
|
|
|
+ * @var bool
|
|
|
+ */
|
|
|
+ public $variation_price_percentage = false;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Constructor
|
|
|
+ *
|
|
|
+ * @param DoliDB $db Database handler
|
|
|
+ */
|
|
|
+ public function __construct(DoliDB $db)
|
|
|
+ {
|
|
|
+ $this->db = $db;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Retrieves a combination level by its rowid
|
|
|
+ *
|
|
|
+ * @param int $rowid Row id
|
|
|
+ * @return int <0 KO, >0 OK
|
|
|
+ */
|
|
|
+ public function fetch($rowid)
|
|
|
+ {
|
|
|
+ $sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage FROM " . MAIN_DB_PREFIX . $this->table_element." WHERE rowid = " . (int) $rowid;
|
|
|
+
|
|
|
+ $obj = $this->db->getRow($sql);
|
|
|
+
|
|
|
+ if ($obj){
|
|
|
+ return $this->fetchFormObj($obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Retrieves combination price levels
|
|
|
+ *
|
|
|
+ * @param int $fk_product_attribute_combination
|
|
|
+ * @param int $fk_price_level the price level to fetch, use 0 for all
|
|
|
+ * @return self[] | -1 on KO
|
|
|
+ */
|
|
|
+ public function fetchAll($fk_product_attribute_combination, $fk_price_level = 0)
|
|
|
+ {
|
|
|
+
|
|
|
+ $sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage"
|
|
|
+ ." FROM ".MAIN_DB_PREFIX.$this->table_element
|
|
|
+ ." WHERE fk_product_attribute_combination = ".intval($fk_product_attribute_combination);
|
|
|
+
|
|
|
+ if (!empty($fk_price_level)){
|
|
|
+ $sql.= ' AND fk_price_level = '.intval($fk_price_level);
|
|
|
+ }
|
|
|
+
|
|
|
+ $combination_price_levels = $this->db->getRows($sql);
|
|
|
+
|
|
|
+ if (!is_array($combination_price_levels)) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = array();
|
|
|
+
|
|
|
+ if (!empty($combination_price_levels)) {
|
|
|
+ // For more simple usage set level as array key
|
|
|
+ foreach ($combination_price_levels as $k => $row){
|
|
|
+ $productCombinationLevel = new ProductCombinationLevel($this->db);
|
|
|
+ $productCombinationLevel->fetchFormObj($row);
|
|
|
+ $result[$row->fk_price_level] = $productCombinationLevel;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * assign vars form an stdclass like sql obj
|
|
|
+ *
|
|
|
+ * @param int $rowid Row id
|
|
|
+ * @return int <0 KO, >0 OK
|
|
|
+ */
|
|
|
+ public function fetchFormObj($obj)
|
|
|
+ {
|
|
|
+ if (!$obj) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->id = $obj->rowid;
|
|
|
+ $this->fk_product_attribute_combination = doubleval($obj->fk_product_attribute_combination);
|
|
|
+ $this->fk_price_level = intval($obj->fk_price_level);
|
|
|
+ $this->variation_price = doubleval($obj->variation_price);
|
|
|
+ $this->variation_price_percentage = (bool) $obj->variation_price_percentage;
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * save
|
|
|
+ *
|
|
|
+ * @return int <0 KO, >0 OK
|
|
|
+ */
|
|
|
+ public function save()
|
|
|
+ {
|
|
|
+ $errors = 0;
|
|
|
+
|
|
|
+
|
|
|
+ if (empty($this->fk_product_attribute_combination) || empty($this->fk_price_level)){
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // check if level exist in DB before add
|
|
|
+ if (empty($this->id)){
|
|
|
+ $sql = "SELECT rowid id"
|
|
|
+ ." FROM ".MAIN_DB_PREFIX . $this->table_element
|
|
|
+ ." WHERE fk_product_attribute_combination = ".(int) $this->fk_product_attribute_combination
|
|
|
+ .' AND fk_price_level = '.intval($this->fk_price_level);
|
|
|
+
|
|
|
+ $existObj = $this->db->getRow($sql);
|
|
|
+ if ($existObj){
|
|
|
+ $this->id = $existObj->id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Update
|
|
|
+ if (!empty($this->id)) {
|
|
|
+ $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET '
|
|
|
+ . ' variation_price = '.doubleval($this->variation_price)
|
|
|
+ . ' , variation_price_percentage = '.intval($this->variation_price_percentage)
|
|
|
+ . ' WHERE rowid = '.intval($this->id);
|
|
|
+
|
|
|
+ $res = $this->db->query($sql);
|
|
|
+ if ($res>0){
|
|
|
+ return $this->id;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $this->error = $this->db->error();
|
|
|
+ $this->errors[] = $this->error;
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // ADD
|
|
|
+ $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . " ("
|
|
|
+ . " fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage"
|
|
|
+ . " ) VALUES ( "
|
|
|
+ . intval($this->fk_product_attribute_combination)
|
|
|
+ . ' , '.intval($this->fk_price_level)
|
|
|
+ . ' , '.doubleval($this->variation_price)
|
|
|
+ . ' , '.intval($this->variation_price_percentage)
|
|
|
+ . " )";
|
|
|
+
|
|
|
+ $res = $this->db->query($sql);
|
|
|
+ if ($res){
|
|
|
+ $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $this->error = $this->db->error();
|
|
|
+ $this->errors[] = $this->error;
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->id;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * delete
|
|
|
+ *
|
|
|
+ * @return int <0 KO, >0 OK
|
|
|
+ */
|
|
|
+ public function delete()
|
|
|
+ {
|
|
|
+ $res = $this->db->query("DELETE FROM ".MAIN_DB_PREFIX.$this->table_element
|
|
|
+ ." WHERE rowid = ".(int) $this->id);
|
|
|
+
|
|
|
+ return $res ? 1 : -1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * delete all for a combination
|
|
|
+ *
|
|
|
+ * @param $fk_product_attribute_combination
|
|
|
+ * @return int <0 KO, >0 OK
|
|
|
+ */
|
|
|
+ public function deleteAllForCombination($fk_product_attribute_combination)
|
|
|
+ {
|
|
|
+ $res = $this->db->query("DELETE FROM ".MAIN_DB_PREFIX.$this->table_element
|
|
|
+ ." WHERE fk_product_attribute_combination = ".(int) $fk_product_attribute_combination);
|
|
|
+
|
|
|
+ return $res ? 1 : -1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Clean not needed price levels for a combination
|
|
|
+ *
|
|
|
+ * @param $fk_product_attribute_combination
|
|
|
+ * @return int <0 KO, >0 OK
|
|
|
+ */
|
|
|
+ public function clean($fk_product_attribute_combination)
|
|
|
+ {
|
|
|
+ global $conf;
|
|
|
+
|
|
|
+ $res = $this->db->query("DELETE FROM ".MAIN_DB_PREFIX.$this->table_element
|
|
|
+ . " WHERE fk_product_attribute_combination = ".(int) $fk_product_attribute_combination
|
|
|
+ . " AND fk_price_level > ".intval($conf->global->PRODUIT_MULTIPRICES_LIMIT) );
|
|
|
+
|
|
|
+
|
|
|
+ return $res ? 1 : -1;
|
|
|
+ }
|
|
|
+}
|