181 if (!empty($this->combination_price_levels) && $useCache) {
182 if ((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)) {
187 if (!is_array($this->combination_price_levels)
188 || empty($fk_price_level)
190 $this->combination_price_levels = array();
194 $combination_price_levels = $staticProductCombinationLevel->fetchAll($this->
id, $fk_price_level);
196 if (!is_array($combination_price_levels)) {
200 if (empty($combination_price_levels)) {
204 if ($fk_price_level > 0) {
208 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
214 $this->combination_price_levels = $combination_price_levels;
315 $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";
316 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_attribute_combination AS pac";
318 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product AS p ON p.rowid = pac.fk_product_child";
320 $sql .=
" WHERE pac.fk_product_parent = ".((int) $fk_product_parent).
" AND pac.entity IN (".
getEntity(
'product').
")";
322 $sql .= $this->db->order(
'p.ref',
'ASC');
325 $query = $this->db->query($sql);
333 while ($result = $this->db->fetch_object($query)) {
335 $tmp->id = $result->rowid;
336 $tmp->fk_product_parent = $result->fk_product_parent;
337 $tmp->fk_product_child = $result->fk_product_child;
338 $tmp->variation_price = $result->variation_price;
339 $tmp->variation_price_percentage = $result->variation_price_percentage;
340 $tmp->variation_weight = $result->variation_weight;
341 $tmp->variation_ref_ext = $result->variation_ref_ext;
344 $tmp->fetchCombinationPriceLevels();
525 $child =
new Product($this->db);
526 $child->fetch($this->fk_product_child);
528 $child->price_autogen = $parent->price_autogen;
529 $child->weight = $parent->weight;
531 if (is_object($parent->oldcopy) && !$parent->oldcopy->isEmpty() && ($parent->status != $parent->oldcopy->status)) {
532 $child->status = $parent->status;
534 if (is_object($parent->oldcopy) && !$parent->oldcopy->isEmpty() && ($parent->status_buy != $parent->oldcopy->status_buy)) {
535 $child->status_buy = $parent->status_buy;
538 if ($this->variation_weight) {
539 $child->weight = ($child->weight ? $child->weight : 0) + $this->variation_weight;
541 $child->weight_units = $parent->weight_units;
544 if ($child->label == $parent->label) {
547 $child->label = $parent->label.$varlabel;
551 if ($child->update($child->id, $user) > 0) {
552 $new_vat = $parent->tva_tx;
553 $new_npr = $parent->tva_npr;
558 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
559 if ($parent->multiprices[$i] !=
'' || isset($this->combination_price_levels[$i]->variation_price)) {
560 $new_type = empty($parent->multiprices_base_type[$i]) ?
'HT' : $parent->multiprices_base_type[$i];
561 $new_min_price = $parent->multiprices_min[$i];
562 $variation_price = (float) (!isset($this->combination_price_levels[$i]->variation_price) ? $this->variation_price : $this->combination_price_levels[$i]->variation_price);
563 $variation_price_percentage = (float) (!isset($this->combination_price_levels[$i]->variation_price_percentage) ? $this->variation_price_percentage : $this->combination_price_levels[$i]->variation_price_percentage);
565 if ($parent->prices_by_qty_list[$i]) {
571 if ($new_type ==
'TTC') {
572 $new_price = $parent->multiprices_ttc[$i];
574 $new_price = $parent->multiprices[$i];
577 if ($variation_price_percentage) {
578 if ($new_price != 0) {
579 $new_price *= 1 + ($variation_price / 100);
582 $new_price += $variation_price;
585 $ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, $i, $new_npr, $new_psq, 0, array(), $parent->default_vat_code);
588 $this->db->rollback();
589 $this->error = $child->error;
590 $this->errors = $child->errors;
596 $new_type = $parent->price_base_type;
597 $new_min_price = $parent->price_min;
598 $new_psq = $parent->price_by_qty;
600 if ($new_type ==
'TTC') {
601 $new_price = $parent->price_ttc;
603 $new_price = $parent->price;
606 if ($this->variation_price_percentage) {
607 if ($new_price != 0) {
608 $new_price *= 1 + ($this->variation_price / 100);
611 $new_price += $this->variation_price;
614 $ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, 1, $new_npr, $new_psq);
617 $this->db->rollback();
618 $this->error = $child->error;
619 $this->errors = $child->errors;
629 $this->db->rollback();
630 $this->error = $child->error;
631 $this->errors = $child->errors;
760 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 =
'')
764 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttribute.class.php';
765 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttributeValue.class.php';
769 $price_impact = array(1 => 0);
771 $forced_refvar = trim((
string) $forced_refvar);
773 if (!empty($forced_refvar) && $forced_refvar != $product->ref) {
774 $existingProduct =
new Product($this->db);
775 $result = $existingProduct->fetch(0, $forced_refvar);
777 $newproduct = $existingProduct;
779 $existingProduct =
false;
780 $newproduct = clone $product;
781 $newproduct->ref = $forced_refvar;
784 $forced_refvar =
false;
785 $existingProduct =
false;
786 $newproduct = clone $product;
790 $weight_impact = (float) $forced_weightvar;
793 if (!is_array($forced_pricevar)) {
794 $price_impact[1] = (float) $forced_pricevar;
796 $price_impact = $forced_pricevar;
799 if (!array($price_var_percent)) {
800 $price_var_percent[1] = (float) $price_var_percent;
804 $existingCombination = $newcomb->fetchByProductCombination2ValuePairs($product->id, $combinations);
806 if ($existingCombination) {
807 $newcomb = $existingCombination;
809 $newcomb->fk_product_parent = $product->id;
812 $result = $newcomb->create($user);
814 $this->error = $newcomb->error;
815 $this->errors = $newcomb->errors;
816 $this->db->rollback();
825 foreach ($combinations as $currcombattr => $currcombval) {
827 $prodattr->fetch($currcombattr);
828 $prodattrval->fetch($currcombval);
831 if (!$existingCombination) {
833 $tmp->fk_prod_attr = $currcombattr;
834 $tmp->fk_prod_attr_val = $currcombval;
835 $tmp->fk_prod_combination = $newcomb->id;
837 if ($tmp->create($user) < 0) {
838 $this->error = $tmp->error;
839 $this->errors = $tmp->errors;
840 $this->db->rollback();
844 if ($forced_weightvar ===
false) {
845 $weight_impact += (float)
price2num($variations[$currcombattr][$currcombval][
'weight']);
847 if ($forced_pricevar ===
false) {
848 $price_impact[1] += (float)
price2num($variations[$currcombattr][$currcombval][
'price']);
853 for ($i = 2; $i <= $produit_multiprices_limit; $i++) {
854 $price_impact[$i] += (float)
price2num($variations[$currcombattr][$currcombval][
'price']);
859 if ($forced_refvar ===
false) {
860 if (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) {
861 $newproduct->ref .=
getDolGlobalString(
'PRODUIT_ATTRIBUTES_SEPARATOR') . $prodattrval->ref;
863 $newproduct->ref .=
'_'.$prodattrval->ref;
868 if ($newproduct->description) {
869 $newproduct->description .=
'<br>';
871 $newproduct->description .=
'<strong>'.$prodattr->label.
':</strong> '.$prodattrval->value;
874 $newcomb->variation_price_percentage = $price_var_percent[1];
875 $newcomb->variation_price = $price_impact[1];
876 $newcomb->variation_weight = $weight_impact;
877 $newcomb->variation_ref_ext = $this->db->escape($ref_ext);
882 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
884 $productCombinationLevel->fk_product_attribute_combination = $newcomb->id;
885 $productCombinationLevel->fk_price_level = $i;
886 $productCombinationLevel->variation_price = $price_impact[$i];
888 if (is_array($price_var_percent)) {
889 $productCombinationLevel->variation_price_percentage = (empty($price_var_percent[$i]) ? false : $price_var_percent[$i]);
891 $productCombinationLevel->variation_price_percentage = $price_var_percent;
894 $newcomb->combination_price_levels[$i] = $productCombinationLevel;
899 $newproduct->weight += $weight_impact;
903 if ($existingProduct ===
false) {
905 $newproduct->price = 0;
906 $newproduct->price_ttc = 0;
907 $newproduct->price_min = 0;
908 $newproduct->price_min_ttc = 0;
911 $newproduct->barcode = -1;
912 $result = $newproduct->create($user);
916 if ($newproduct->error !=
'ErrorProductAlreadyExists') {
917 $this->error = $newproduct->error;
918 $this->errors = $newproduct->errors;
919 $this->db->rollback();
928 if ($newcomb->fk_product_child) {
929 $res = $newproduct->fetch($existingCombination->fk_product_child);
931 $orig_prod_ref = $newproduct->ref;
935 $newproduct->ref = $orig_prod_ref.$i;
936 $res = $newproduct->create($user);
938 if ($newproduct->error !=
'ErrorProductAlreadyExists') {
939 $this->errors[] = $newproduct->error;
948 $this->db->rollback();
953 $result = $newproduct->update($newproduct->id, $user);
955 $this->db->rollback();
960 $newcomb->fk_product_child = $newproduct->id;
962 if ($newcomb->update($user) < 0) {
963 $this->error = $newcomb->error;
964 $this->errors = $newcomb->errors;
965 $this->db->rollback();
970 return $newproduct->id;