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;
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct(DoliDB $db)
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct(DoliDB $db)
{
global $conf;
@ -189,16 +189,16 @@ class ProductCombination
*/
public function countNbOfCombinationForFkProductParent($fk_product_parent)
{
$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').")";
$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').")";
$resql = $this->db->query($sql);
if ($resql) {
$obj = $this->db->fetch_object($resql);
if ($obj) $nb = $obj->nb;
}
$resql = $this->db->query($sql);
if ($resql) {
$obj = $this->db->fetch_object($resql);
if ($obj) $nb = $obj->nb;
}
return $nb;
return $nb;
}
/**
@ -326,14 +326,20 @@ class ProductCombination
$child = new Product($this->db);
$child->fetch($this->fk_product_child);
$child->price_autogen = $parent->price_autogen;
$child->weight = $parent->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_units = $parent->weight_units;
$varlabel = $this->getCombinationLabel($this->fk_product_child);
$child->label = $parent->label.$varlabel;
$child->weight_units = $parent->weight_units;
// 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) {
$new_vat = $parent->tva_tx;
@ -530,9 +536,9 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
$newproduct->ref = $forced_refvar;
}
} else {
$forced_refvar = false;
$existingProduct = false;
$newproduct = clone $product;
$forced_refvar = false;
$existingProduct = false;
$newproduct = clone $product;
}
//Final weight impact
@ -593,11 +599,11 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
}
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;
} else {
} else {
$newproduct->ref .= '_'.$prodattrval->ref;
}
}
}
//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
//print 'Create prod '.$newproduct->ref.'<br>'."\n";
if ($existingProduct === false) {
//To avoid wrong information in price history log
$newproduct->price = 0;
$newproduct->price_ttc = 0;
$newproduct->price_min = 0;
$newproduct->price_min_ttc = 0;
//To avoid wrong information in price history log
$newproduct->price = 0;
$newproduct->price_ttc = 0;
$newproduct->price_min = 0;
$newproduct->price_min_ttc = 0;
// A new variant must use a new barcode (not same product)
$newproduct->barcode = -1;
$result = $newproduct->create($user);
// A new variant must use a new barcode (not same product)
$newproduct->barcode = -1;
$result = $newproduct->create($user);
if ($result < 0)
{
//In case the error is not related with an already existing product
if ($newproduct->error != 'ErrorProductAlreadyExists') {
$this->error[] = $newproduct->error;
$this->errors = $newproduct->errors;
$db->rollback();
return -1;
}
if ($result < 0)
{
//In case the error is not related with an already existing product
if ($newproduct->error != 'ErrorProductAlreadyExists') {
$this->error[] = $newproduct->error;
$this->errors = $newproduct->errors;
$db->rollback();
return -1;
}
/**
* If there is an existing combination, then we update the prices and weight
* Otherwise, we try adding a random number to the ref
*/
/**
* If there is an existing combination, then we update the prices and weight
* Otherwise, we try adding a random number to the ref
*/
if ($newcomb->fk_product_child) {
$res = $newproduct->fetch($existingCombination->fk_product_child);
} else {
$orig_prod_ref = $newproduct->ref;
$i = 1;
if ($newcomb->fk_product_child) {
$res = $newproduct->fetch($existingCombination->fk_product_child);
} else {
$orig_prod_ref = $newproduct->ref;
$i = 1;
do {
$newproduct->ref = $orig_prod_ref.$i;
$res = $newproduct->create($user);
do {
$newproduct->ref = $orig_prod_ref.$i;
$res = $newproduct->create($user);
if ($newproduct->error != 'ErrorProductAlreadyExists') {
$this->errors[] = $newproduct->error;
break;
}
if ($newproduct->error != 'ErrorProductAlreadyExists') {
$this->errors[] = $newproduct->error;
break;
}
$i++;
} while ($res < 0);
}
$i++;
} while ($res < 0);
}
if ($res < 0) {
$db->rollback();
return -1;
}
}
if ($res < 0) {
$db->rollback();
return -1;
}
}
} else {
$result = $newproduct->update($newproduct->id, $user);
if ($result < 0)
{
$db->rollback();
return -1;
}
$result = $newproduct->update($newproduct->id, $user);
if ($result < 0)
{
$db->rollback();
return -1;
}
}
$newcomb->fk_product_child = $newproduct->id;
@ -686,14 +692,14 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
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 int $origProductId Origin product id
* @param Product $destProduct Destination product
* @return int >0 OK <0 KO
*/
* @param int $origProductId Origin product id
* @param Product $destProduct Destination product
* @return int >0 OK <0 KO
*/
public function copyAll(User $user, $origProductId, Product $destProduct)
{
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;
}
if ($this->createProductCombination(
if ($this->createProductCombination(
$user,
$destProduct,
$variations,
@ -723,7 +729,7 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
$combination->variation_price_percentage,
$combination->variation_price,
$combination->variation_weight
) < 0)
) < 0)
{
return -1;
}