177 if (!empty($this->combination_price_levels) && $useCache) {
178 if ((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)) {
183 if (!is_array($this->combination_price_levels)
184 || empty($fk_price_level)
186 $this->combination_price_levels = array();
190 $combination_price_levels = $staticProductCombinationLevel->fetchAll($this->
id, $fk_price_level);
192 if (!is_array($combination_price_levels)) {
196 if (empty($combination_price_levels)) {
200 if ($fk_price_level > 0) {
204 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
210 $this->combination_price_levels = $combination_price_levels;
311 $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";
312 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_attribute_combination AS pac";
314 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product AS p ON p.rowid = pac.fk_product_child";
316 $sql .=
" WHERE pac.fk_product_parent = ".((int) $fk_product_parent).
" AND pac.entity IN (".
getEntity(
'product').
")";
318 $sql .= $this->db->order(
'p.ref',
'ASC');
321 $query = $this->db->query($sql);
329 while ($result = $this->db->fetch_object($query)) {
331 $tmp->id = $result->rowid;
332 $tmp->fk_product_parent = $result->fk_product_parent;
333 $tmp->fk_product_child = $result->fk_product_child;
334 $tmp->variation_price = $result->variation_price;
335 $tmp->variation_price_percentage = $result->variation_price_percentage;
336 $tmp->variation_weight = $result->variation_weight;
337 $tmp->variation_ref_ext = $result->variation_ref_ext;
340 $tmp->fetchCombinationPriceLevels();
521 $child =
new Product($this->db);
522 $child->fetch($this->fk_product_child);
524 $child->price_autogen = $parent->price_autogen;
525 $child->weight = $parent->weight;
527 if (is_object($parent->oldcopy) && !$parent->oldcopy->isEmpty() && ($parent->status != $parent->oldcopy->status)) {
528 $child->status = $parent->status;
530 if (is_object($parent->oldcopy) && !$parent->oldcopy->isEmpty() && ($parent->status_buy != $parent->oldcopy->status_buy)) {
531 $child->status_buy = $parent->status_buy;
534 if ($this->variation_weight) {
535 $child->weight = ($child->weight ? $child->weight : 0) + $this->variation_weight;
537 $child->weight_units = $parent->weight_units;
540 if ($child->label == $parent->label) {
543 $child->label = $parent->label.$varlabel;
547 if ($child->update($child->id, $user) > 0) {
548 $new_vat = $parent->tva_tx;
549 $new_npr = $parent->tva_npr;
554 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
555 if ($parent->multiprices[$i] !=
'' || isset($this->combination_price_levels[$i]->variation_price)) {
556 $new_type = empty($parent->multiprices_base_type[$i]) ?
'HT' : $parent->multiprices_base_type[$i];
557 $new_min_price = $parent->multiprices_min[$i];
558 $variation_price = (float) (!isset($this->combination_price_levels[$i]->variation_price) ? $this->variation_price : $this->combination_price_levels[$i]->variation_price);
559 $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);
561 if ($parent->prices_by_qty_list[$i]) {
567 if ($new_type ==
'TTC') {
568 $new_price = $parent->multiprices_ttc[$i];
570 $new_price = $parent->multiprices[$i];
573 if ($variation_price_percentage) {
574 if ($new_price != 0) {
575 $new_price *= 1 + ($variation_price / 100);
578 $new_price += $variation_price;
581 $ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, $i, $new_npr, $new_psq, 0, array(), $parent->default_vat_code);
584 $this->db->rollback();
585 $this->error = $child->error;
586 $this->errors = $child->errors;
592 $new_type = $parent->price_base_type;
593 $new_min_price = $parent->price_min;
594 $new_psq = $parent->price_by_qty;
596 if ($new_type ==
'TTC') {
597 $new_price = $parent->price_ttc;
599 $new_price = $parent->price;
602 if ($this->variation_price_percentage) {
603 if ($new_price != 0) {
604 $new_price *= 1 + ($this->variation_price / 100);
607 $new_price += $this->variation_price;
610 $ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, 1, $new_npr, $new_psq);
613 $this->db->rollback();
614 $this->error = $child->error;
615 $this->errors = $child->errors;
625 $this->db->rollback();
626 $this->error = $child->error;
627 $this->errors = $child->errors;
756 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 =
'')
760 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttribute.class.php';
761 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttributeValue.class.php';
765 $price_impact = array(1 => 0);
767 $forced_refvar = trim((
string) $forced_refvar);
769 if (!empty($forced_refvar) && $forced_refvar != $product->ref) {
770 $existingProduct =
new Product($this->db);
771 $result = $existingProduct->fetch(0, $forced_refvar);
773 $newproduct = $existingProduct;
775 $existingProduct =
false;
776 $newproduct = clone $product;
777 $newproduct->ref = $forced_refvar;
780 $forced_refvar =
false;
781 $existingProduct =
false;
782 $newproduct = clone $product;
786 $weight_impact = (float) $forced_weightvar;
789 if (!is_array($forced_pricevar)) {
790 $price_impact[1] = (float) $forced_pricevar;
792 $price_impact = $forced_pricevar;
795 if (!array($price_var_percent)) {
796 $price_var_percent[1] = (float) $price_var_percent;
800 $existingCombination = $newcomb->fetchByProductCombination2ValuePairs($product->id, $combinations);
802 if ($existingCombination) {
803 $newcomb = $existingCombination;
805 $newcomb->fk_product_parent = $product->id;
808 $result = $newcomb->create($user);
810 $this->error = $newcomb->error;
811 $this->errors = $newcomb->errors;
812 $this->db->rollback();
821 foreach ($combinations as $currcombattr => $currcombval) {
823 $prodattr->fetch($currcombattr);
824 $prodattrval->fetch($currcombval);
827 if (!$existingCombination) {
829 $tmp->fk_prod_attr = $currcombattr;
830 $tmp->fk_prod_attr_val = $currcombval;
831 $tmp->fk_prod_combination = $newcomb->id;
833 if ($tmp->create($user) < 0) {
834 $this->error = $tmp->error;
835 $this->errors = $tmp->errors;
836 $this->db->rollback();
840 if ($forced_weightvar ===
false) {
841 $weight_impact += (float)
price2num($variations[$currcombattr][$currcombval][
'weight']);
843 if ($forced_pricevar ===
false) {
844 $price_impact[1] += (float)
price2num($variations[$currcombattr][$currcombval][
'price']);
849 for ($i = 2; $i <= $produit_multiprices_limit; $i++) {
850 $price_impact[$i] += (float)
price2num($variations[$currcombattr][$currcombval][
'price']);
855 if ($forced_refvar ===
false) {
856 if (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) {
857 $newproduct->ref .=
getDolGlobalString(
'PRODUIT_ATTRIBUTES_SEPARATOR') . $prodattrval->ref;
859 $newproduct->ref .=
'_'.$prodattrval->ref;
864 if ($newproduct->description) {
865 $newproduct->description .=
'<br>';
867 $newproduct->description .=
'<strong>'.$prodattr->label.
':</strong> '.$prodattrval->value;
870 $newcomb->variation_price_percentage = $price_var_percent[1];
871 $newcomb->variation_price = $price_impact[1];
872 $newcomb->variation_weight = $weight_impact;
873 $newcomb->variation_ref_ext = $this->db->escape($ref_ext);
878 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
880 $productCombinationLevel->fk_product_attribute_combination = $newcomb->id;
881 $productCombinationLevel->fk_price_level = $i;
882 $productCombinationLevel->variation_price = $price_impact[$i];
884 if (is_array($price_var_percent)) {
885 $productCombinationLevel->variation_price_percentage = (empty($price_var_percent[$i]) ? false : $price_var_percent[$i]);
887 $productCombinationLevel->variation_price_percentage = $price_var_percent;
890 $newcomb->combination_price_levels[$i] = $productCombinationLevel;
895 $newproduct->weight += $weight_impact;
899 if ($existingProduct ===
false) {
901 $newproduct->price = 0;
902 $newproduct->price_ttc = 0;
903 $newproduct->price_min = 0;
904 $newproduct->price_min_ttc = 0;
907 $newproduct->barcode = -1;
908 $result = $newproduct->create($user);
912 if ($newproduct->error !=
'ErrorProductAlreadyExists') {
913 $this->error = $newproduct->error;
914 $this->errors = $newproduct->errors;
915 $this->db->rollback();
924 if ($newcomb->fk_product_child) {
925 $res = $newproduct->fetch($existingCombination->fk_product_child);
927 $orig_prod_ref = $newproduct->ref;
931 $newproduct->ref = $orig_prod_ref.$i;
932 $res = $newproduct->create($user);
934 if ($newproduct->error !=
'ErrorProductAlreadyExists') {
935 $this->errors[] = $newproduct->error;
944 $this->db->rollback();
949 $result = $newproduct->update($newproduct->id, $user);
951 $this->db->rollback();
956 $newcomb->fk_product_child = $newproduct->id;
958 if ($newcomb->update($user) < 0) {
959 $this->error = $newcomb->error;
960 $this->errors = $newcomb->errors;
961 $this->db->rollback();
966 return $newproduct->id;