178 if (!empty($this->combination_price_levels) && $useCache) {
179 if ((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)) {
184 if (!is_array($this->combination_price_levels)
185 || empty($fk_price_level)
187 $this->combination_price_levels = array();
191 $combination_price_levels = $staticProductCombinationLevel->fetchAll($this->
id, $fk_price_level);
193 if (!is_array($combination_price_levels)) {
197 if (empty($combination_price_levels)) {
201 if ($fk_price_level > 0) {
205 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
211 $this->combination_price_levels = $combination_price_levels;
312 $sql =
"SELECT pac.rowid, pac.fk_product_parent, pac.fk_product_child, pac.variation_price, pac.variation_price_percentage, pac.variation_ref_ext, pac.variation_weight";
313 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_attribute_combination AS pac";
315 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product AS p ON p.rowid = pac.fk_product_child";
317 $sql .=
" WHERE pac.fk_product_parent = ".((int) $fk_product_parent).
" AND pac.entity IN (".
getEntity(
'product').
")";
319 $sql .= $this->db->order(
'p.ref',
'ASC');
322 $query = $this->db->query($sql);
330 while ($result = $this->db->fetch_object($query)) {
332 $tmp->id = $result->rowid;
333 $tmp->fk_product_parent = $result->fk_product_parent;
334 $tmp->fk_product_child = $result->fk_product_child;
335 $tmp->variation_price = $result->variation_price;
336 $tmp->variation_price_percentage = $result->variation_price_percentage;
337 $tmp->variation_weight = $result->variation_weight;
338 $tmp->variation_ref_ext = $result->variation_ref_ext;
341 $tmp->fetchCombinationPriceLevels();
535 $child =
new Product($this->db);
536 $child->fetch($this->fk_product_child);
538 $child->price_autogen = $parent->price_autogen;
539 $child->weight = $parent->weight;
541 if (is_object($parent->oldcopy) && !$parent->oldcopy->isEmpty() && ($parent->status != $parent->oldcopy->status)) {
542 $child->status = $parent->status;
544 if (is_object($parent->oldcopy) && !$parent->oldcopy->isEmpty() && ($parent->status_buy != $parent->oldcopy->status_buy)) {
545 $child->status_buy = $parent->status_buy;
548 if ($this->variation_weight) {
549 $child->weight = ($child->weight ? $child->weight : 0) + $this->variation_weight;
551 $child->weight_units = $parent->weight_units;
554 if ($child->label == $parent->label) {
557 $child->label = $parent->label.$varlabel;
561 if ($child->update($child->id, $user) > 0) {
562 $new_vat = $parent->tva_tx;
563 $new_npr = $parent->tva_npr;
567 $produit_multiprices_limit =
getDolGlobalInt(
'PRODUIT_MULTIPRICES_LIMIT');
568 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
569 if ($parent->multiprices[$i] !=
'' || isset($this->combination_price_levels[$i]->variation_price)) {
570 $new_type = empty($parent->multiprices_base_type[$i]) ?
'HT' : $parent->multiprices_base_type[$i];
571 $new_min_price = $parent->multiprices_min[$i];
572 $variation_price = (float) (!isset($this->combination_price_levels[$i]->variation_price) ? $this->variation_price : $this->combination_price_levels[$i]->variation_price);
573 $variation_price_percentage = (bool) (!isset($this->combination_price_levels[$i]->variation_price_percentage) ? $this->variation_price_percentage : $this->combination_price_levels[$i]->variation_price_percentage);
575 if ($parent->prices_by_qty_list[$i]) {
581 if ($new_type ==
'TTC') {
582 $new_price = $parent->multiprices_ttc[$i];
584 $new_price = $parent->multiprices[$i];
587 if ($variation_price_percentage) {
588 if ($new_price != 0) {
589 $new_price *= 1 + ($variation_price / 100);
592 $new_price += $variation_price;
595 $ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, $i, $new_npr, $new_psq, 0, array(), $parent->default_vat_code);
598 $this->db->rollback();
599 $this->error = $child->error;
600 $this->errors = $child->errors;
606 $new_type = $parent->price_base_type;
607 $new_min_price = $parent->price_min;
608 $new_psq = $parent->price_by_qty;
610 if ($new_type ==
'TTC') {
611 $new_price = $parent->price_ttc;
613 $new_price = $parent->price;
616 if ($this->variation_price_percentage) {
617 if ($new_price != 0) {
618 $new_price *= 1 + ($this->variation_price / 100);
621 $new_price += $this->variation_price;
624 $ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, 1, $new_npr, $new_psq);
627 $this->db->rollback();
628 $this->error = $child->error;
629 $this->errors = $child->errors;
639 $this->db->rollback();
640 $this->error = $child->error;
641 $this->errors = $child->errors;
770 public function createProductCombination(
User $user,
Product $product, array $combinations, array $variations, $price_var_percent =
false, $forced_pricevar =
false, $forced_weightvar =
false, $forced_refvar =
false, $ref_ext =
'')
774 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttribute.class.php';
775 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttributeValue.class.php';
779 $price_impact = array(1 => 0);
781 $forced_refvar = trim((
string) $forced_refvar);
783 if (!empty($forced_refvar) && $forced_refvar != $product->ref) {
784 $existingProduct =
new Product($this->db);
785 $result = $existingProduct->fetch(0, $forced_refvar);
787 $newproduct = $existingProduct;
789 $existingProduct =
false;
790 $newproduct = clone $product;
791 $newproduct->ref = $forced_refvar;
794 $forced_refvar =
false;
795 $existingProduct =
false;
796 $newproduct = clone $product;
800 $newproduct->context[
'createproductcombination'] =
'createproductcombination';
803 $weight_impact = (float) $forced_weightvar;
806 if (!is_array($forced_pricevar)) {
807 $price_impact[1] = (float) $forced_pricevar;
809 $price_impact = $forced_pricevar;
812 if (!is_array($price_var_percent)) {
813 $price_var_percent = array(1 => (
bool) $price_var_percent);
817 $existingCombination = $newcomb->fetchByProductCombination2ValuePairs($product->id, $combinations);
819 if ($existingCombination) {
820 $newcomb = $existingCombination;
822 $newcomb->fk_product_parent = $product->id;
825 $result = $newcomb->create($user);
827 $this->error = $newcomb->error;
828 $this->errors = $newcomb->errors;
829 $this->db->rollback();
838 foreach ($combinations as $currcombattr => $currcombval) {
840 $prodattr->fetch($currcombattr);
841 $prodattrval->fetch($currcombval);
844 if (!$existingCombination) {
846 $tmp->fk_prod_attr = $currcombattr;
847 $tmp->fk_prod_attr_val = $currcombval;
848 $tmp->fk_prod_combination = $newcomb->id;
850 if ($tmp->create($user) < 0) {
851 $this->error = $tmp->error;
852 $this->errors = $tmp->errors;
853 $this->db->rollback();
857 if ($forced_weightvar ===
false) {
858 $weight_impact += (float)
price2num($variations[$currcombattr][$currcombval][
'weight']);
860 if ($forced_pricevar ===
false) {
861 $price_impact[1] += (float)
price2num($variations[$currcombattr][$currcombval][
'price']);
866 for ($i = 2; $i <= $produit_multiprices_limit; $i++) {
867 $price_impact[$i] += (float)
price2num($variations[$currcombattr][$currcombval][
'price']);
872 if ($forced_refvar ===
false) {
873 if (isset(
$conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) {
874 $newproduct->ref .=
getDolGlobalString(
'PRODUIT_ATTRIBUTES_SEPARATOR') . $prodattrval->ref;
876 $newproduct->ref .=
'_'.$prodattrval->ref;
881 if ($newproduct->description) {
882 $newproduct->description .=
'<br>';
884 $newproduct->description .=
'<strong>'.$prodattr->label.
':</strong> '.$prodattrval->value;
887 $newcomb->variation_price_percentage = (bool) $price_var_percent[1];
888 $newcomb->variation_price = $price_impact[1];
889 $newcomb->variation_weight = $weight_impact;
890 $newcomb->variation_ref_ext = $this->db->escape($ref_ext);
894 $produit_multiprices_limit =
getDolGlobalInt(
'PRODUIT_MULTIPRICES_LIMIT');
895 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
897 $productCombinationLevel->fk_product_attribute_combination = $newcomb->id;
898 $productCombinationLevel->fk_price_level = $i;
899 $productCombinationLevel->variation_price = $price_impact[$i];
901 if (is_array($price_var_percent)) {
902 $productCombinationLevel->variation_price_percentage = (bool) $price_var_percent[$i] ;
904 $productCombinationLevel->variation_price_percentage = $price_var_percent;
907 $newcomb->combination_price_levels[$i] = $productCombinationLevel;
912 $newproduct->weight += $weight_impact;
916 if ($existingProduct ===
false) {
918 $newproduct->price = 0;
919 $newproduct->price_ttc = 0;
920 $newproduct->price_min = 0;
921 $newproduct->price_min_ttc = 0;
924 $newproduct->barcode = -1;
925 $result = $newproduct->create($user);
929 if ($newproduct->error !=
'ErrorProductAlreadyExists') {
930 $this->error = $newproduct->error;
931 $this->errors = $newproduct->errors;
932 $this->db->rollback();
941 if ($newcomb->fk_product_child) {
942 $res = $newproduct->fetch($existingCombination->fk_product_child);
944 $orig_prod_ref = $newproduct->ref;
948 $newproduct->ref = $orig_prod_ref.$i;
949 $res = $newproduct->create($user);
951 if ($newproduct->error !=
'ErrorProductAlreadyExists') {
952 $this->errors[] = $newproduct->error;
961 $this->db->rollback();
966 $result = $newproduct->update($newproduct->id, $user);
968 $this->db->rollback();
973 $newcomb->fk_product_child = $newproduct->id;
975 if ($newcomb->update($user) < 0) {
976 $this->error = $newcomb->error;
977 $this->errors = $newcomb->errors;
978 $this->db->rollback();
983 return $newproduct->id;
996 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination2ValuePair.class.php';
999 if ($origProductId == $destProduct->id) {
1008 foreach ($combinations as $combination) {
1009 $variations = array();
1011 foreach ($prodcomb2val->fetchByFkCombination($combination->id) as $tmp_pc2v) {
1012 $variations[$tmp_pc2v->fk_prod_attr] = $tmp_pc2v->fk_prod_attr_val;
1015 $variation_price_percentage = $combination->variation_price_percentage;
1016 $variation_price = $combination->variation_price;
1019 $variation_price_percentage = [ ];
1020 $variation_price = [ ];
1022 foreach ($combination->combination_price_levels as $productCombinationLevel) {
1023 $variation_price_percentage[$productCombinationLevel->fk_price_level] = $productCombinationLevel->variation_price_percentage;
1024 $variation_price[$productCombinationLevel->fk_price_level] = $productCombinationLevel->variation_price;
1033 $variation_price_percentage,
1035 $combination->variation_weight