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;
528 if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
529 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
530 if ($parent->multiprices[$i] !=
'' || isset($this->combination_price_levels[$i]->variation_price)) {
531 $new_type = empty($parent->multiprices_base_type[$i]) ?
'HT' : $parent->multiprices_base_type[$i];
532 $new_min_price = $parent->multiprices_min[$i];
533 $variation_price = floatval(!isset($this->combination_price_levels[$i]->variation_price) ? $this->variation_price : $this->combination_price_levels[$i]->variation_price);
534 $variation_price_percentage = floatval(!isset($this->combination_price_levels[$i]->variation_price_percentage) ? $this->variation_price_percentage : $this->combination_price_levels[$i]->variation_price_percentage);
536 if ($parent->prices_by_qty_list[$i]) {
542 if ($new_type ==
'TTC') {
543 $new_price = $parent->multiprices_ttc[$i];
545 $new_price = $parent->multiprices[$i];
548 if ($variation_price_percentage) {
549 if ($new_price != 0) {
550 $new_price *= 1 + ($variation_price / 100);
553 $new_price += $variation_price;
556 $ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, $i, $new_npr, $new_psq, 0, array(), $parent->default_vat_code);
559 $this->db->rollback();
560 $this->error = $child->error;
561 $this->errors = $child->errors;
567 $new_type = $parent->price_base_type;
568 $new_min_price = $parent->price_min;
569 $new_psq = $parent->price_by_qty;
571 if ($new_type ==
'TTC') {
572 $new_price = $parent->price_ttc;
574 $new_price = $parent->price;
577 if ($this->variation_price_percentage) {
578 if ($new_price != 0) {
579 $new_price *= 1 + ($this->variation_price / 100);
582 $new_price += $this->variation_price;
585 $ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, 1, $new_npr, $new_psq);
588 $this->db->rollback();
589 $this->error = $child->error;
590 $this->errors = $child->errors;
600 $this->db->rollback();
601 $this->error = $child->error;
602 $this->errors = $child->errors;
716 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 =
'')
720 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttribute.class.php';
721 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttributeValue.class.php';
725 $price_impact = array(1=>0);
727 $forced_refvar = trim($forced_refvar);
729 if (!empty($forced_refvar) && $forced_refvar != $product->ref) {
730 $existingProduct =
new Product($this->db);
731 $result = $existingProduct->fetch(
'', $forced_refvar);
733 $newproduct = $existingProduct;
735 $existingProduct =
false;
736 $newproduct = clone $product;
737 $newproduct->ref = $forced_refvar;
740 $forced_refvar =
false;
741 $existingProduct =
false;
742 $newproduct = clone $product;
746 $weight_impact = (float) $forced_weightvar;
749 if (!is_array($forced_pricevar)) {
750 $price_impact[1] = (float) $forced_pricevar;
752 $price_impact = $forced_pricevar;
755 if (!array($price_var_percent)) {
756 $price_var_percent[1] = (float) $price_var_percent;
760 $existingCombination = $newcomb->fetchByProductCombination2ValuePairs($product->id, $combinations);
762 if ($existingCombination) {
763 $newcomb = $existingCombination;
765 $newcomb->fk_product_parent = $product->id;
768 $result = $newcomb->create($user);
770 $this->error = $newcomb->error;
771 $this->errors = $newcomb->errors;
772 $this->db->rollback();
782 foreach ($combinations as $currcombattr => $currcombval) {
784 $prodattr->fetch($currcombattr);
785 $prodattrval->fetch($currcombval);
788 if (!$existingCombination) {
790 $tmp->fk_prod_attr = $currcombattr;
791 $tmp->fk_prod_attr_val = $currcombval;
792 $tmp->fk_prod_combination = $newcomb->id;
794 if ($tmp->create($user) < 0) {
795 $this->error = $tmp->error;
796 $this->errors = $tmp->errors;
797 $this->db->rollback();
802 if ($forced_weightvar ===
false) {
803 $weight_impact += (float)
price2num($variations[$currcombattr][$currcombval][
'weight']);
805 if ($forced_pricevar ===
false) {
806 $price_impact[1] += (float)
price2num($variations[$currcombattr][$currcombval][
'price']);
809 if ($conf->global->PRODUIT_MULTIPRICES) {
810 for ($i = 2; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
811 $price_impact[$i] += (float)
price2num($variations[$currcombattr][$currcombval][
'price']);
816 if ($forced_refvar ===
false) {
817 if (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) {
818 $newproduct->ref .= $conf->global->PRODUIT_ATTRIBUTES_SEPARATOR.$prodattrval->ref;
820 $newproduct->ref .=
'_'.$prodattrval->ref;
825 if ($newproduct->description) {
826 $newproduct->description .=
'<br>';
828 $newproduct->description .=
'<strong>'.$prodattr->label.
':</strong> '.$prodattrval->value;
831 $newcomb->variation_price_percentage = $price_var_percent[1];
832 $newcomb->variation_price = $price_impact[1];
833 $newcomb->variation_weight = $weight_impact;
834 $newcomb->variation_ref_ext = $this->db->escape($ref_ext);
837 if ($conf->global->PRODUIT_MULTIPRICES) {
838 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
840 $productCombinationLevel->fk_product_attribute_combination = $newcomb->id;
841 $productCombinationLevel->fk_price_level = $i;
842 $productCombinationLevel->variation_price = $price_impact[$i];
844 if (is_array($price_var_percent)) {
845 $productCombinationLevel->variation_price_percentage = (empty($price_var_percent[$i]) ? false : $price_var_percent[$i]);
847 $productCombinationLevel->variation_price_percentage = $price_var_percent;
850 $newcomb->combination_price_levels[$i] = $productCombinationLevel;
855 $newproduct->weight += $weight_impact;
859 if ($existingProduct ===
false) {
861 $newproduct->price = 0;
862 $newproduct->price_ttc = 0;
863 $newproduct->price_min = 0;
864 $newproduct->price_min_ttc = 0;
867 $newproduct->barcode = -1;
868 $result = $newproduct->create($user);
872 if ($newproduct->error !=
'ErrorProductAlreadyExists') {
873 $this->error[] = $newproduct->error;
874 $this->errors = $newproduct->errors;
875 $this->db->rollback();
884 if ($newcomb->fk_product_child) {
885 $res = $newproduct->fetch($existingCombination->fk_product_child);
887 $orig_prod_ref = $newproduct->ref;
891 $newproduct->ref = $orig_prod_ref.$i;
892 $res = $newproduct->create($user);
894 if ($newproduct->error !=
'ErrorProductAlreadyExists') {
895 $this->errors[] = $newproduct->error;
904 $this->db->rollback();
909 $result = $newproduct->update($newproduct->id, $user);
911 $this->db->rollback();
916 $newcomb->fk_product_child = $newproduct->id;
918 if ($newcomb->update($user) < 0) {
919 $this->error = $newcomb->error;
920 $this->errors = $newcomb->errors;
921 $this->db->rollback();
926 return $newproduct->id;