179 if (!empty($this->combination_price_levels) && $useCache) {
180 if ((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)) {
185 if (!is_array($this->combination_price_levels)
186 || empty($fk_price_level)
188 $this->combination_price_levels = array();
192 $combination_price_levels = $staticProductCombinationLevel->fetchAll($this->
id, $fk_price_level);
194 if (!is_array($combination_price_levels)) {
198 if (empty($combination_price_levels)) {
202 if ($fk_price_level > 0) {
206 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
212 $this->combination_price_levels = $combination_price_levels;
313 $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";
314 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_attribute_combination AS pac";
316 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product AS p ON p.rowid = pac.fk_product_child";
318 $sql .=
" WHERE pac.fk_product_parent = ".((int) $fk_product_parent).
" AND pac.entity IN (".
getEntity(
'product').
")";
320 $sql .= $this->db->order(
'p.ref',
'ASC');
323 $query = $this->db->query($sql);
331 while ($result = $this->db->fetch_object($query)) {
333 $tmp->id = $result->rowid;
334 $tmp->fk_product_parent = $result->fk_product_parent;
335 $tmp->fk_product_child = $result->fk_product_child;
336 $tmp->variation_price = $result->variation_price;
337 $tmp->variation_price_percentage = $result->variation_price_percentage;
338 $tmp->variation_weight = $result->variation_weight;
339 $tmp->variation_ref_ext = $result->variation_ref_ext;
342 $tmp->fetchCombinationPriceLevels();
523 $child =
new Product($this->db);
524 $child->fetch($this->fk_product_child);
526 $child->price_autogen = $parent->price_autogen;
527 $child->weight = $parent->weight;
529 if (is_object($parent->oldcopy) && !$parent->oldcopy->isEmpty() && ($parent->status != $parent->oldcopy->status)) {
530 $child->status = $parent->status;
532 if (is_object($parent->oldcopy) && !$parent->oldcopy->isEmpty() && ($parent->status_buy != $parent->oldcopy->status_buy)) {
533 $child->status_buy = $parent->status_buy;
536 if ($this->variation_weight) {
537 $child->weight = ($child->weight ? $child->weight : 0) + $this->variation_weight;
539 $child->weight_units = $parent->weight_units;
542 if ($child->label == $parent->label) {
545 $child->label = $parent->label.$varlabel;
549 if ($child->update($child->id, $user) > 0) {
550 $new_vat = $parent->tva_tx;
551 $new_npr = $parent->tva_npr;
556 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
557 if ($parent->multiprices[$i] !=
'' || isset($this->combination_price_levels[$i]->variation_price)) {
558 $new_type = empty($parent->multiprices_base_type[$i]) ?
'HT' : $parent->multiprices_base_type[$i];
559 $new_min_price = $parent->multiprices_min[$i];
560 $variation_price = (float) (!isset($this->combination_price_levels[$i]->variation_price) ? $this->variation_price : $this->combination_price_levels[$i]->variation_price);
561 $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);
563 if ($parent->prices_by_qty_list[$i]) {
569 if ($new_type ==
'TTC') {
570 $new_price = $parent->multiprices_ttc[$i];
572 $new_price = $parent->multiprices[$i];
575 if ($variation_price_percentage) {
576 if ($new_price != 0) {
577 $new_price *= 1 + ($variation_price / 100);
580 $new_price += $variation_price;
583 $ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, $i, $new_npr, $new_psq, 0, array(), $parent->default_vat_code);
586 $this->db->rollback();
587 $this->error = $child->error;
588 $this->errors = $child->errors;
594 $new_type = $parent->price_base_type;
595 $new_min_price = $parent->price_min;
596 $new_psq = $parent->price_by_qty;
598 if ($new_type ==
'TTC') {
599 $new_price = $parent->price_ttc;
601 $new_price = $parent->price;
604 if ($this->variation_price_percentage) {
605 if ($new_price != 0) {
606 $new_price *= 1 + ($this->variation_price / 100);
609 $new_price += $this->variation_price;
612 $ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, 1, $new_npr, $new_psq);
615 $this->db->rollback();
616 $this->error = $child->error;
617 $this->errors = $child->errors;
627 $this->db->rollback();
628 $this->error = $child->error;
629 $this->errors = $child->errors;
758 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 =
'')
762 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttribute.class.php';
763 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttributeValue.class.php';
767 $price_impact = array(1 => 0);
769 $forced_refvar = trim((
string) $forced_refvar);
771 if (!empty($forced_refvar) && $forced_refvar != $product->ref) {
772 $existingProduct =
new Product($this->db);
773 $result = $existingProduct->fetch(0, $forced_refvar);
775 $newproduct = $existingProduct;
777 $existingProduct =
false;
778 $newproduct = clone $product;
779 $newproduct->ref = $forced_refvar;
782 $forced_refvar =
false;
783 $existingProduct =
false;
784 $newproduct = clone $product;
788 $weight_impact = (float) $forced_weightvar;
791 if (!is_array($forced_pricevar)) {
792 $price_impact[1] = (float) $forced_pricevar;
794 $price_impact = $forced_pricevar;
797 if (!array($price_var_percent)) {
798 $price_var_percent[1] = (float) $price_var_percent;
802 $existingCombination = $newcomb->fetchByProductCombination2ValuePairs($product->id, $combinations);
804 if ($existingCombination) {
805 $newcomb = $existingCombination;
807 $newcomb->fk_product_parent = $product->id;
810 $result = $newcomb->create($user);
812 $this->error = $newcomb->error;
813 $this->errors = $newcomb->errors;
814 $this->db->rollback();
823 foreach ($combinations as $currcombattr => $currcombval) {
825 $prodattr->fetch($currcombattr);
826 $prodattrval->fetch($currcombval);
829 if (!$existingCombination) {
831 $tmp->fk_prod_attr = $currcombattr;
832 $tmp->fk_prod_attr_val = $currcombval;
833 $tmp->fk_prod_combination = $newcomb->id;
835 if ($tmp->create($user) < 0) {
836 $this->error = $tmp->error;
837 $this->errors = $tmp->errors;
838 $this->db->rollback();
842 if ($forced_weightvar ===
false) {
843 $weight_impact += (float)
price2num($variations[$currcombattr][$currcombval][
'weight']);
845 if ($forced_pricevar ===
false) {
846 $price_impact[1] += (float)
price2num($variations[$currcombattr][$currcombval][
'price']);
851 for ($i = 2; $i <= $produit_multiprices_limit; $i++) {
852 $price_impact[$i] += (float)
price2num($variations[$currcombattr][$currcombval][
'price']);
857 if ($forced_refvar ===
false) {
858 if (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) {
859 $newproduct->ref .=
getDolGlobalString(
'PRODUIT_ATTRIBUTES_SEPARATOR') . $prodattrval->ref;
861 $newproduct->ref .=
'_'.$prodattrval->ref;
866 if ($newproduct->description) {
867 $newproduct->description .=
'<br>';
869 $newproduct->description .=
'<strong>'.$prodattr->label.
':</strong> '.$prodattrval->value;
872 $newcomb->variation_price_percentage = $price_var_percent[1];
873 $newcomb->variation_price = $price_impact[1];
874 $newcomb->variation_weight = $weight_impact;
875 $newcomb->variation_ref_ext = $this->db->escape($ref_ext);
880 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
882 $productCombinationLevel->fk_product_attribute_combination = $newcomb->id;
883 $productCombinationLevel->fk_price_level = $i;
884 $productCombinationLevel->variation_price = $price_impact[$i];
886 if (is_array($price_var_percent)) {
887 $productCombinationLevel->variation_price_percentage = (empty($price_var_percent[$i]) ? false : $price_var_percent[$i]);
889 $productCombinationLevel->variation_price_percentage = $price_var_percent;
892 $newcomb->combination_price_levels[$i] = $productCombinationLevel;
897 $newproduct->weight += $weight_impact;
901 if ($existingProduct ===
false) {
903 $newproduct->price = 0;
904 $newproduct->price_ttc = 0;
905 $newproduct->price_min = 0;
906 $newproduct->price_min_ttc = 0;
909 $newproduct->barcode = -1;
910 $result = $newproduct->create($user);
914 if ($newproduct->error !=
'ErrorProductAlreadyExists') {
915 $this->error = $newproduct->error;
916 $this->errors = $newproduct->errors;
917 $this->db->rollback();
926 if ($newcomb->fk_product_child) {
927 $res = $newproduct->fetch($existingCombination->fk_product_child);
929 $orig_prod_ref = $newproduct->ref;
933 $newproduct->ref = $orig_prod_ref.$i;
934 $res = $newproduct->create($user);
936 if ($newproduct->error !=
'ErrorProductAlreadyExists') {
937 $this->errors[] = $newproduct->error;
946 $this->db->rollback();
951 $result = $newproduct->update($newproduct->id, $user);
953 $this->db->rollback();
958 $newcomb->fk_product_child = $newproduct->id;
960 if ($newcomb->update($user) < 0) {
961 $this->error = $newcomb->error;
962 $this->errors = $newcomb->errors;
963 $this->db->rollback();
968 return $newproduct->id;