This commit is contained in:
Laurent Destailleur 2020-05-23 13:53:47 +02:00
parent 83ecbef9bc
commit 3a15688f17

View File

@ -71,12 +71,12 @@ class ProductCombination
*/ */
public $entity; public $entity;
/** /**
* Constructor * Constructor
* *
* @param DoliDB $db Database handler * @param DoliDB $db Database handler
*/ */
public function __construct(DoliDB $db) public function __construct(DoliDB $db)
{ {
global $conf; global $conf;
@ -189,16 +189,16 @@ class ProductCombination
*/ */
public function countNbOfCombinationForFkProductParent($fk_product_parent) public function countNbOfCombinationForFkProductParent($fk_product_parent)
{ {
$nb = 0; $nb = 0;
$sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_parent = ".(int) $fk_product_parent." AND entity IN (".getEntity('product').")"; $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_parent = ".(int) $fk_product_parent." AND entity IN (".getEntity('product').")";
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
if ($obj) $nb = $obj->nb; if ($obj) $nb = $obj->nb;
} }
return $nb; return $nb;
} }
/** /**
@ -326,14 +326,20 @@ class ProductCombination
$child = new Product($this->db); $child = new Product($this->db);
$child->fetch($this->fk_product_child); $child->fetch($this->fk_product_child);
$child->price_autogen = $parent->price_autogen; $child->price_autogen = $parent->price_autogen;
$child->weight = $parent->weight; $child->weight = $parent->weight;
if ($this->variation_weight) { // If we must add a delta on weight if ($this->variation_weight) { // If we must add a delta on weight
$child->weight = ($child->weight ? $child->weight : 0) + $this->variation_weight; $child->weight = ($child->weight ? $child->weight : 0) + $this->variation_weight;
} }
$child->weight_units = $parent->weight_units; $child->weight_units = $parent->weight_units;
$varlabel = $this->getCombinationLabel($this->fk_product_child);
$child->label = $parent->label.$varlabel; // Don't update the child label if the user has already modified it.
if ($child->label == $parent->label) {
// This will trigger only at variant creation time
$varlabel = $this->getCombinationLabel($this->fk_product_child);
$child->label = $parent->label.$varlabel;;
}
if ($child->update($child->id, $user) > 0) { if ($child->update($child->id, $user) > 0) {
$new_vat = $parent->tva_tx; $new_vat = $parent->tva_tx;
@ -530,9 +536,9 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
$newproduct->ref = $forced_refvar; $newproduct->ref = $forced_refvar;
} }
} else { } else {
$forced_refvar = false; $forced_refvar = false;
$existingProduct = false; $existingProduct = false;
$newproduct = clone $product; $newproduct = clone $product;
} }
//Final weight impact //Final weight impact
@ -593,11 +599,11 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
} }
if ($forced_refvar === false) { if ($forced_refvar === false) {
if (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) { if (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) {
$newproduct->ref .= $conf->global->PRODUIT_ATTRIBUTES_SEPARATOR.$prodattrval->ref; $newproduct->ref .= $conf->global->PRODUIT_ATTRIBUTES_SEPARATOR.$prodattrval->ref;
} else { } else {
$newproduct->ref .= '_'.$prodattrval->ref; $newproduct->ref .= '_'.$prodattrval->ref;
} }
} }
//The first one should not contain a linebreak //The first one should not contain a linebreak
@ -616,62 +622,62 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
// Now create the product // Now create the product
//print 'Create prod '.$newproduct->ref.'<br>'."\n"; //print 'Create prod '.$newproduct->ref.'<br>'."\n";
if ($existingProduct === false) { if ($existingProduct === false) {
//To avoid wrong information in price history log //To avoid wrong information in price history log
$newproduct->price = 0; $newproduct->price = 0;
$newproduct->price_ttc = 0; $newproduct->price_ttc = 0;
$newproduct->price_min = 0; $newproduct->price_min = 0;
$newproduct->price_min_ttc = 0; $newproduct->price_min_ttc = 0;
// A new variant must use a new barcode (not same product) // A new variant must use a new barcode (not same product)
$newproduct->barcode = -1; $newproduct->barcode = -1;
$result = $newproduct->create($user); $result = $newproduct->create($user);
if ($result < 0) if ($result < 0)
{ {
//In case the error is not related with an already existing product //In case the error is not related with an already existing product
if ($newproduct->error != 'ErrorProductAlreadyExists') { if ($newproduct->error != 'ErrorProductAlreadyExists') {
$this->error[] = $newproduct->error; $this->error[] = $newproduct->error;
$this->errors = $newproduct->errors; $this->errors = $newproduct->errors;
$db->rollback(); $db->rollback();
return -1; return -1;
} }
/** /**
* If there is an existing combination, then we update the prices and weight * If there is an existing combination, then we update the prices and weight
* Otherwise, we try adding a random number to the ref * Otherwise, we try adding a random number to the ref
*/ */
if ($newcomb->fk_product_child) { if ($newcomb->fk_product_child) {
$res = $newproduct->fetch($existingCombination->fk_product_child); $res = $newproduct->fetch($existingCombination->fk_product_child);
} else { } else {
$orig_prod_ref = $newproduct->ref; $orig_prod_ref = $newproduct->ref;
$i = 1; $i = 1;
do { do {
$newproduct->ref = $orig_prod_ref.$i; $newproduct->ref = $orig_prod_ref.$i;
$res = $newproduct->create($user); $res = $newproduct->create($user);
if ($newproduct->error != 'ErrorProductAlreadyExists') { if ($newproduct->error != 'ErrorProductAlreadyExists') {
$this->errors[] = $newproduct->error; $this->errors[] = $newproduct->error;
break; break;
} }
$i++; $i++;
} while ($res < 0); } while ($res < 0);
} }
if ($res < 0) { if ($res < 0) {
$db->rollback(); $db->rollback();
return -1; return -1;
} }
} }
} else { } else {
$result = $newproduct->update($newproduct->id, $user); $result = $newproduct->update($newproduct->id, $user);
if ($result < 0) if ($result < 0)
{ {
$db->rollback(); $db->rollback();
return -1; return -1;
} }
} }
$newcomb->fk_product_child = $newproduct->id; $newcomb->fk_product_child = $newproduct->id;
@ -686,14 +692,14 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
return $newproduct->id; return $newproduct->id;
} }
/** /**
* Copies all product combinations from the origin product to the destination product * Copies all product combinations from the origin product to the destination product
* *
* @param User $user Object user * @param User $user Object user
* @param int $origProductId Origin product id * @param int $origProductId Origin product id
* @param Product $destProduct Destination product * @param Product $destProduct Destination product
* @return int >0 OK <0 KO * @return int >0 OK <0 KO
*/ */
public function copyAll(User $user, $origProductId, Product $destProduct) public function copyAll(User $user, $origProductId, Product $destProduct)
{ {
require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php'; require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php';
@ -715,7 +721,7 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
$variations[$tmp_pc2v->fk_prod_attr] = $tmp_pc2v->fk_prod_attr_val; $variations[$tmp_pc2v->fk_prod_attr] = $tmp_pc2v->fk_prod_attr_val;
} }
if ($this->createProductCombination( if ($this->createProductCombination(
$user, $user,
$destProduct, $destProduct,
$variations, $variations,
@ -723,7 +729,7 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
$combination->variation_price_percentage, $combination->variation_price_percentage,
$combination->variation_price, $combination->variation_price,
$combination->variation_weight $combination->variation_weight
) < 0) ) < 0)
{ {
return -1; return -1;
} }