161 if (!empty($this->combination_price_levels) && $useCache) {
162 if ((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)) {
167 if (!is_array($this->combination_price_levels)
168 || empty($fk_price_level)
170 $this->combination_price_levels = array();
174 $combination_price_levels = $staticProductCombinationLevel->fetchAll($this->
id, $fk_price_level);
176 if (!is_array($combination_price_levels)) {
180 if (empty($combination_price_levels)) {
184 if ($fk_price_level > 0) {
187 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
193 $this->combination_price_levels = $combination_price_levels;
497 $child =
new Product($this->db);
498 $child->fetch($this->fk_product_child);
500 $child->price_autogen = $parent->price_autogen;
501 $child->weight = $parent->weight;
503 if (!empty($parent->oldcopy) && ($parent->status != $parent->oldcopy->status)) {
504 $child->status = $parent->status;
506 if (!empty($parent->oldcopy) && ($parent->status_buy != $parent->oldcopy->status_buy)) {
507 $child->status_buy = $parent->status_buy;
510 if ($this->variation_weight) {
511 $child->weight = ($child->weight ? $child->weight : 0) + $this->variation_weight;
513 $child->weight_units = $parent->weight_units;
516 if ($child->label == $parent->label) {
519 $child->label = $parent->label.$varlabel;
523 if ($child->update($child->id, $user) > 0) {
524 $new_vat = $parent->tva_tx;
525 $new_npr = $parent->tva_npr;
529 $produit_multiprices_limit =
getDolGlobalInt(
'PRODUIT_MULTIPRICES_LIMIT');
530 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
531 if ($parent->multiprices[$i] !=
'' || isset($this->combination_price_levels[$i]->variation_price)) {
532 $new_type = empty($parent->multiprices_base_type[$i]) ?
'HT' : $parent->multiprices_base_type[$i];
533 $new_min_price = $parent->multiprices_min[$i];
534 $variation_price = (float) (!isset($this->combination_price_levels[$i]->variation_price) ? $this->variation_price : $this->combination_price_levels[$i]->variation_price);
535 $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);
537 if ($parent->prices_by_qty_list[$i]) {
543 if ($new_type ==
'TTC') {
544 $new_price = $parent->multiprices_ttc[$i];
546 $new_price = $parent->multiprices[$i];
549 if ($variation_price_percentage) {
550 if ($new_price != 0) {
551 $new_price *= 1 + ($variation_price / 100);
554 $new_price += $variation_price;
557 $ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, $i, $new_npr, $new_psq, 0, array(), $parent->default_vat_code);
560 $this->db->rollback();
561 $this->error = $child->error;
562 $this->errors = $child->errors;
568 $new_type = $parent->price_base_type;
569 $new_min_price = $parent->price_min;
570 $new_psq = $parent->price_by_qty;
572 if ($new_type ==
'TTC') {
573 $new_price = $parent->price_ttc;
575 $new_price = $parent->price;
578 if ($this->variation_price_percentage) {
579 if ($new_price != 0) {
580 $new_price *= 1 + ($this->variation_price / 100);
583 $new_price += $this->variation_price;
586 $ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, 1, $new_npr, $new_psq);
589 $this->db->rollback();
590 $this->error = $child->error;
591 $this->errors = $child->errors;
601 $this->db->rollback();
602 $this->error = $child->error;
603 $this->errors = $child->errors;
717 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 =
'')
721 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttribute.class.php';
722 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttributeValue.class.php';
726 $price_impact = array(1=>0);
728 $forced_refvar = trim($forced_refvar);
730 if (!empty($forced_refvar) && $forced_refvar != $product->ref) {
731 $existingProduct =
new Product($this->db);
732 $result = $existingProduct->fetch(
'', $forced_refvar);
734 $newproduct = $existingProduct;
736 $existingProduct =
false;
737 $newproduct = clone $product;
738 $newproduct->ref = $forced_refvar;
741 $forced_refvar =
false;
742 $existingProduct =
false;
743 $newproduct = clone $product;
747 $weight_impact = (float) $forced_weightvar;
750 if (!is_array($forced_pricevar)) {
751 $price_impact[1] = (float) $forced_pricevar;
753 $price_impact = $forced_pricevar;
756 if (!is_array($price_var_percent)) {
757 $price_var_percent = array(1 => (
bool) $price_var_percent);
761 $existingCombination = $newcomb->fetchByProductCombination2ValuePairs($product->id, $combinations);
763 if ($existingCombination) {
764 $newcomb = $existingCombination;
766 $newcomb->fk_product_parent = $product->id;
769 $result = $newcomb->create($user);
771 $this->error = $newcomb->error;
772 $this->errors = $newcomb->errors;
773 $this->db->rollback();
783 foreach ($combinations as $currcombattr => $currcombval) {
785 $prodattr->fetch($currcombattr);
786 $prodattrval->fetch($currcombval);
789 if (!$existingCombination) {
791 $tmp->fk_prod_attr = $currcombattr;
792 $tmp->fk_prod_attr_val = $currcombval;
793 $tmp->fk_prod_combination = $newcomb->id;
795 if ($tmp->create($user) < 0) {
796 $this->error = $tmp->error;
797 $this->errors = $tmp->errors;
798 $this->db->rollback();
803 if ($forced_weightvar ===
false) {
804 $weight_impact += (float)
price2num($variations[$currcombattr][$currcombval][
'weight']);
806 if ($forced_pricevar ===
false) {
807 $price_impact[1] += (float)
price2num($variations[$currcombattr][$currcombval][
'price']);
810 if ($conf->global->PRODUIT_MULTIPRICES) {
811 for ($i = 2; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
812 $price_impact[$i] += (float)
price2num($variations[$currcombattr][$currcombval][
'price']);
817 if ($forced_refvar ===
false) {
818 if (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) {
819 $newproduct->ref .=
getDolGlobalString(
'PRODUIT_ATTRIBUTES_SEPARATOR') . $prodattrval->ref;
821 $newproduct->ref .=
'_'.$prodattrval->ref;
826 if ($newproduct->description) {
827 $newproduct->description .=
'<br>';
829 $newproduct->description .=
'<strong>'.$prodattr->label.
':</strong> '.$prodattrval->value;
832 $newcomb->variation_price_percentage = (bool) $price_var_percent[1];
833 $newcomb->variation_price = $price_impact[1];
834 $newcomb->variation_weight = $weight_impact;
835 $newcomb->variation_ref_ext = $this->db->escape($ref_ext);
839 $produit_multiprices_limit =
getDolGlobalInt(
'PRODUIT_MULTIPRICES_LIMIT');
840 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
842 $productCombinationLevel->fk_product_attribute_combination = $newcomb->id;
843 $productCombinationLevel->fk_price_level = $i;
844 $productCombinationLevel->variation_price = $price_impact[$i];
846 if (is_array($price_var_percent)) {
847 $productCombinationLevel->variation_price_percentage = (bool) $price_var_percent[$i] ;
849 $productCombinationLevel->variation_price_percentage = $price_var_percent;
852 $newcomb->combination_price_levels[$i] = $productCombinationLevel;
857 $newproduct->weight += $weight_impact;
861 if ($existingProduct ===
false) {
863 $newproduct->price = 0;
864 $newproduct->price_ttc = 0;
865 $newproduct->price_min = 0;
866 $newproduct->price_min_ttc = 0;
869 $newproduct->barcode = -1;
870 $result = $newproduct->create($user);
874 if ($newproduct->error !=
'ErrorProductAlreadyExists') {
875 $this->error[] = $newproduct->error;
876 $this->errors = $newproduct->errors;
877 $this->db->rollback();
886 if ($newcomb->fk_product_child) {
887 $res = $newproduct->fetch($existingCombination->fk_product_child);
889 $orig_prod_ref = $newproduct->ref;
893 $newproduct->ref = $orig_prod_ref.$i;
894 $res = $newproduct->create($user);
896 if ($newproduct->error !=
'ErrorProductAlreadyExists') {
897 $this->errors[] = $newproduct->error;
906 $this->db->rollback();
911 $result = $newproduct->update($newproduct->id, $user);
913 $this->db->rollback();
918 $newcomb->fk_product_child = $newproduct->id;
920 if ($newcomb->update($user) < 0) {
921 $this->error = $newcomb->error;
922 $this->errors = $newcomb->errors;
923 $this->db->rollback();
928 return $newproduct->id;
941 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination2ValuePair.class.php';
944 if ($origProductId == $destProduct->id) {
953 foreach ($combinations as $combination) {
954 $variations = array();
956 foreach ($prodcomb2val->fetchByFkCombination($combination->id) as $tmp_pc2v) {
957 $variations[$tmp_pc2v->fk_prod_attr] = $tmp_pc2v->fk_prod_attr_val;
960 $variation_price_percentage = $combination->variation_price_percentage;
961 $variation_price = $combination->variation_price;
964 $variation_price_percentage = [ ];
965 $variation_price = [ ];
967 foreach ($combination->combination_price_levels as $productCombinationLevel) {
968 $variation_price_percentage[$productCombinationLevel->fk_price_level] = $productCombinationLevel->variation_price_percentage;
969 $variation_price[$productCombinationLevel->fk_price_level] = $productCombinationLevel->variation_price;
978 $variation_price_percentage,
980 $combination->variation_weight