dolibarr/htdocs/variants/class/ProductCombination.class.php

1357 lines
40 KiB
PHP
Raw Permalink Normal View History

2016-07-23 14:37:21 +00:00
<?php
2024-04-20 21:45:52 +00:00
/* Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2018 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2022 Open-Dsi <support@open-dsi.fr>
* Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
2025-03-05 17:36:34 +00:00
* Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2025 William Mead <william@m34d.com>
2016-07-23 14:37:21 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 19:55:30 +00:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2016-07-23 14:37:21 +00:00
*/
2024-04-27 02:20:26 +00:00
/**
* \file htdocs/variants/class/ProductCombination.class.php
* \ingroup variants
* \brief File of the ProductCombination class
*/
/**
* Class ProductCombination
2024-04-27 02:20:26 +00:00
* Used to represent the relation between a product and one of its variants.
*
* Example: a product "shirt" has two variants "shirt XL white" and "shirt XL grey".
* This is represented with two ProductCombination objects:
* - One for "shirt XL white":
* * $object->fk_product_parent ID of the Product object "shirt"
* * $object->fk_product_child ID of the Product object "shirt XL white"
* - Another for "shirt XL grey":
* * $object->fk_product_parent ID of the Product object "shirt"
* * $object->fk_product_child ID of the Product object "shirt XL grey"
*/
2016-07-23 14:37:21 +00:00
class ProductCombination
{
/**
* Database handler
* @var DoliDB
*/
2020-09-19 21:30:29 +00:00
public $db;
2016-07-23 14:37:21 +00:00
/**
2024-04-27 02:20:26 +00:00
* Rowid of this ProductCombination
2016-07-23 14:37:21 +00:00
* @var int
*/
public $id;
/**
2024-04-27 02:20:26 +00:00
* Rowid of the parent Product
2016-07-23 14:37:21 +00:00
* @var int
*/
public $fk_product_parent;
/**
2024-04-27 02:20:26 +00:00
* Rowid of the variant Product
2016-07-23 14:37:21 +00:00
* @var int
*/
public $fk_product_child;
/**
* Price variation
* @var float
*/
public $variation_price;
/**
2024-04-27 02:20:26 +00:00
* Is the price variation a relative variation?
* Can be an array if multiprice feature per level is enabled.
* @var bool
2016-07-23 14:37:21 +00:00
*/
public $variation_price_percentage = false;
/**
* Weight variation
* @var float
*/
public $variation_weight;
/**
* Combination entity
* @var int
*/
public $entity;
2020-08-12 08:14:29 +00:00
/**
* Combination price level
* @var ProductCombinationLevel[]
*/
public $combination_price_levels;
/**
* External ref
* @var string
*/
public $variation_ref_ext = '';
2020-08-12 08:14:29 +00:00
2023-02-20 19:43:23 +00:00
/**
2024-04-27 02:20:26 +00:00
* Error message
* @var string
2023-02-20 19:43:23 +00:00
*/
public $error;
/**
2024-04-27 02:20:26 +00:00
* Array of error messages
* @var string[]
2023-02-20 19:43:23 +00:00
*/
public $errors = array();
2020-05-23 11:53:47 +00:00
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct(DoliDB $db)
2016-07-23 14:37:21 +00:00
{
global $conf;
$this->db = $db;
$this->entity = $conf->entity;
}
/**
2024-04-27 02:20:26 +00:00
* Retrieves a ProductCombination by its rowid
2016-07-23 14:37:21 +00:00
*
2024-07-31 11:09:10 +00:00
* @param int $rowid ID of the ProductCombination
* @return int<-1,1> -1 if KO, 1 if OK
2016-07-23 14:37:21 +00:00
*/
public function fetch($rowid)
{
2021-03-22 10:30:18 +00:00
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight, variation_ref_ext FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE rowid = ".((int) $rowid)." AND entity IN (".getEntity('product').")";
2016-07-23 14:37:21 +00:00
$query = $this->db->query($sql);
if (!$query) {
return -1;
}
if (!$this->db->num_rows($query)) {
return -1;
}
2019-11-12 09:11:30 +00:00
$obj = $this->db->fetch_object($query);
$this->id = $obj->rowid;
$this->fk_product_parent = $obj->fk_product_parent;
$this->fk_product_child = $obj->fk_product_child;
$this->variation_price = $obj->variation_price;
$this->variation_price_percentage = $obj->variation_price_percentage;
$this->variation_weight = $obj->variation_weight;
$this->variation_ref_ext = $obj->variation_ref_ext;
2016-07-23 14:37:21 +00:00
2023-11-27 11:08:48 +00:00
if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
2020-08-12 08:14:29 +00:00
$this->fetchCombinationPriceLevels();
}
2016-07-23 14:37:21 +00:00
return 1;
}
2020-08-12 08:14:29 +00:00
/**
* Retrieves combination price levels
*
* @param int $fk_price_level The price level to fetch, use 0 for all
* @param bool $useCache To use cache or not
* @return int<-1,1> -1 if KO, 1 if OK
2020-08-12 08:14:29 +00:00
*/
public function fetchCombinationPriceLevels($fk_price_level = 0, $useCache = true)
{
// Check cache
if (!empty($this->combination_price_levels) && $useCache) {
if ((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)) {
2020-08-12 08:14:29 +00:00
return 1;
}
}
2020-08-13 09:48:28 +00:00
if (!is_array($this->combination_price_levels)
|| empty($fk_price_level) // if fetch an unique level don't erase all already fetched
) {
2020-08-12 08:14:29 +00:00
$this->combination_price_levels = array();
}
$staticProductCombinationLevel = new ProductCombinationLevel($this->db);
$combination_price_levels = $staticProductCombinationLevel->fetchAll($this->id, $fk_price_level);
if (!is_array($combination_price_levels)) {
2020-08-12 08:14:29 +00:00
return -1;
}
if (empty($combination_price_levels)) {
/**
* for auto retrocompatibility with last behavior
*/
if ($fk_price_level > 0) {
$combination_price_levels[$fk_price_level] = ProductCombinationLevel::createFromParent($this->db, $this, $fk_price_level);
} else {
$produit_multiprices_limit = getDolGlobalString('PRODUIT_MULTIPRICES_LIMIT');
for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
$combination_price_levels[$i] = ProductCombinationLevel::createFromParent($this->db, $this, $i);
}
}
}
2020-08-12 08:14:29 +00:00
$this->combination_price_levels = $combination_price_levels;
return 1;
}
/**
* Retrieves combination price levels
*
* @param int $clean Levels of PRODUIT_MULTIPRICES_LIMIT
2023-12-06 14:46:39 +00:00
* @return int Return integer <0 KO, >0 OK
2020-08-12 08:14:29 +00:00
*/
public function saveCombinationPriceLevels($clean = 1)
{
global $conf;
2020-08-17 03:54:22 +00:00
$error = 0;
2020-08-12 08:14:29 +00:00
$staticProductCombinationLevel = new ProductCombinationLevel($this->db);
// Delete all
if (empty($this->combination_price_levels)) {
2020-08-12 08:14:29 +00:00
return $staticProductCombinationLevel->deleteAllForCombination($this->id);
}
// Clean not needed price levels (level higher than number max defined into setup)
if ($clean) {
2020-08-12 08:14:29 +00:00
$res = $staticProductCombinationLevel->clean($this->id);
if ($res < 0) {
2020-08-12 08:14:29 +00:00
$this->errors[] = 'Fail to clean not needed price levels';
return -1;
}
}
foreach ($this->combination_price_levels as $fk_price_level => $combination_price_level) {
2020-08-12 08:14:29 +00:00
$res = $combination_price_level->save();
if ($res < 1) {
$this->error = 'Error saving combination price level '.$fk_price_level.' : '.$combination_price_level->error;
2020-08-12 08:14:29 +00:00
$this->errors[] = $this->error;
2020-08-17 03:54:22 +00:00
$error++;
break;
2020-08-12 08:14:29 +00:00
}
}
if ($error) {
2020-08-17 03:54:22 +00:00
return $error * -1;
} else {
2020-08-12 08:14:29 +00:00
return 1;
}
}
2016-07-23 14:37:21 +00:00
/**
2020-09-04 12:05:35 +00:00
* Retrieves information of a variant product and ID of its parent product.
2016-07-23 14:37:21 +00:00
*
2020-09-04 12:05:35 +00:00
* @param int $productid Product ID of variant
* @param int $donotloadpricelevel Avoid loading price impact for each level. If PRODUIT_MULTIPRICES is not set, this has no effect.
2023-12-06 14:46:39 +00:00
* @return int Return integer <0 if KO, 0 if product ID is not ID of a variant product (so parent not found), >0 if OK (ID of parent)
2016-07-23 14:37:21 +00:00
*/
2020-09-04 12:05:35 +00:00
public function fetchByFkProductChild($productid, $donotloadpricelevel = 0)
2016-07-23 14:37:21 +00:00
{
2020-08-12 08:14:29 +00:00
global $conf;
2020-09-04 12:05:35 +00:00
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight";
$sql .= " FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_child = ".((int) $productid)." AND entity IN (".getEntity('product').")";
2016-07-23 14:37:21 +00:00
$query = $this->db->query($sql);
if (!$query) {
return -1;
}
if (!$this->db->num_rows($query)) {
2020-09-04 12:05:35 +00:00
return 0;
2016-07-23 14:37:21 +00:00
}
$result = $this->db->fetch_object($query);
$this->id = $result->rowid;
$this->fk_product_parent = $result->fk_product_parent;
$this->fk_product_child = $result->fk_product_child;
$this->variation_price = $result->variation_price;
$this->variation_price_percentage = $result->variation_price_percentage;
$this->variation_weight = $result->variation_weight;
2023-11-27 11:08:48 +00:00
if (empty($donotloadpricelevel) && getDolGlobalString('PRODUIT_MULTIPRICES')) {
2020-08-12 08:14:29 +00:00
$this->fetchCombinationPriceLevels();
}
2020-09-12 13:53:18 +00:00
return (int) $this->fk_product_parent;
2016-07-23 14:37:21 +00:00
}
/**
* Retrieves all product combinations by the product parent row id
*
* @param int $fk_product_parent Rowid of parent product
* @param bool $sort_by_ref Sort result by product child reference
* @return int|ProductCombination[] Return integer <0 KO
2016-07-23 14:37:21 +00:00
*/
public function fetchAllByFkProductParent($fk_product_parent, $sort_by_ref = false)
2016-07-23 14:37:21 +00:00
{
$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";
$sql .= " FROM ".MAIN_DB_PREFIX."product_attribute_combination AS pac";
if ($sort_by_ref) {
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product AS p ON p.rowid = pac.fk_product_child";
}
$sql .= " WHERE pac.fk_product_parent = ".((int) $fk_product_parent)." AND pac.entity IN (".getEntity('product').")";
if ($sort_by_ref) {
$sql .= $this->db->order('p.ref', 'ASC');
}
2016-07-23 14:37:21 +00:00
$query = $this->db->query($sql);
if (!$query) {
return -1;
}
$return = array();
while ($result = $this->db->fetch_object($query)) {
$tmp = new ProductCombination($this->db);
$tmp->id = $result->rowid;
$tmp->fk_product_parent = $result->fk_product_parent;
$tmp->fk_product_child = $result->fk_product_child;
$tmp->variation_price = $result->variation_price;
$tmp->variation_price_percentage = $result->variation_price_percentage;
$tmp->variation_weight = $result->variation_weight;
$tmp->variation_ref_ext = $result->variation_ref_ext;
2016-07-23 14:37:21 +00:00
2023-11-27 11:08:48 +00:00
if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
2020-08-12 08:14:29 +00:00
$tmp->fetchCombinationPriceLevels();
}
2016-07-23 14:37:21 +00:00
$return[] = $tmp;
}
return $return;
}
/**
* Retrieves all product combinations by the product parent row id
*
* @param int $fk_product_parent Id of parent product
* @return int Nb of record
*/
public function countNbOfCombinationForFkProductParent($fk_product_parent)
{
2020-05-23 11:53:47 +00:00
$nb = 0;
2021-03-22 10:30:18 +00:00
$sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_parent = ".((int) $fk_product_parent)." AND entity IN (".getEntity('product').")";
2017-07-28 07:45:23 +00:00
2020-05-23 11:53:47 +00:00
$resql = $this->db->query($sql);
if ($resql) {
$obj = $this->db->fetch_object($resql);
if ($obj) {
$nb = (int) $obj->nb;
}
2020-05-23 11:53:47 +00:00
}
2020-05-23 11:53:47 +00:00
return $nb;
}
2017-07-28 07:45:23 +00:00
2016-07-23 14:37:21 +00:00
/**
* Creates a product attribute combination
*
2017-11-26 17:43:43 +00:00
* @param User $user Object user
2023-12-01 18:51:32 +00:00
* @return int Return integer <0 if KO, >0 if OK
2016-07-23 14:37:21 +00:00
*/
2017-11-26 17:43:43 +00:00
public function create($user)
2016-07-23 14:37:21 +00:00
{
/* $this->fk_product_child may be empty and will be filled later after subproduct has been created */
2016-07-23 14:37:21 +00:00
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_attribute_combination";
2020-09-10 11:56:58 +00:00
$sql .= " (fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight, variation_ref_ext, entity)";
$sql .= " VALUES (".((int) $this->fk_product_parent).", ".((int) $this->fk_product_child).",";
$sql .= (float) $this->variation_price.", ".(int) $this->variation_price_percentage.",";
$sql .= (float) $this->variation_weight.", '".$this->db->escape($this->variation_ref_ext)."', ".(int) $this->entity.")";
2016-07-23 14:37:21 +00:00
$resql = $this->db->query($sql);
if ($resql) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'product_attribute_combination');
} else {
$this->error = $this->db->lasterror();
2016-07-23 14:37:21 +00:00
return -1;
}
2023-11-27 11:08:48 +00:00
if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
2020-08-12 08:14:29 +00:00
$res = $this->saveCombinationPriceLevels();
if ($res < 0) {
2020-08-12 08:14:29 +00:00
return -2;
}
}
2016-07-23 14:37:21 +00:00
return 1;
}
/**
* Updates a product combination
*
2017-11-26 17:43:43 +00:00
* @param User $user Object user
2023-12-06 14:46:39 +00:00
* @return int Return integer <0 KO, >0 OK
2016-07-23 14:37:21 +00:00
*/
2017-11-26 17:43:43 +00:00
public function update(User $user)
2016-07-23 14:37:21 +00:00
{
$sql = "UPDATE ".MAIN_DB_PREFIX."product_attribute_combination";
$sql .= " SET fk_product_parent = ".(int) $this->fk_product_parent.", fk_product_child = ".(int) $this->fk_product_child.",";
$sql .= " variation_price = ".(float) $this->variation_price.", variation_price_percentage = ".(int) $this->variation_price_percentage.",";
$sql .= " variation_ref_ext = '".$this->db->escape($this->variation_ref_ext)."',";
$sql .= " variation_weight = ".(float) $this->variation_weight." WHERE rowid = ".((int) $this->id);
2016-07-23 14:37:21 +00:00
$resql = $this->db->query($sql);
if (!$resql) {
2016-07-23 14:37:21 +00:00
return -1;
}
2023-11-27 11:08:48 +00:00
if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
2020-08-12 08:14:29 +00:00
$res = $this->saveCombinationPriceLevels();
if ($res < 0) {
2020-08-12 08:14:29 +00:00
return -2;
}
}
2016-07-23 14:37:21 +00:00
$parent = new Product($this->db);
$parent->fetch($this->fk_product_parent);
Fix #32372 surface variant updateProperties failure instead of price=0 (#38541) ProductCombination::update() called $this->updateProperties($parent, $user) at line 439 and discarded the return value. updateProperties() walks the parent's price levels, computes the variant price from the combination's variation_price / variation_price_percentage and calls Product::updatePrice() on the child product. updatePrice() refuses to set a price below the configured price_min (returns -1 with 'ErrorPriceCantBeLowerThanMinPrice'), but ProductCombination::update() still returned 1 because the failure was thrown away. createProductCombination() initialises the new variant product with price = price_ttc = price_min = price_min_ttc = 0 to keep a clean price history, then commits the combination by calling $newcomb->update($user). When the parent product has a non-zero minimum price and the variation drops the computed price below it, updateProperties() returns -1, update() reports success anyway, the combination is committed and the variant product stays at price 0. The card displays price 0 and minimum 0 with no error message, which is exactly the behaviour the reporter described and that @JonBendtsen confirmed was still present on develop after the v22 release. Capture updateProperties()'s return value and propagate the negative result so the calling createProductCombination() sees the failure, rolls back the transaction and surfaces the underlying 'ErrorPriceCantBeLowerThanMinPrice' message. The variant is no longer silently created with a 0 price; users get either the legitimate adjusted price (when the variation is permissible) or a clear error prompting them to drop the parent's minimum price first. Signed-off-by: Dolicraft <contact@dolicraft.com> Co-authored-by: Dolicraft <contact@dolicraft.com>
2026-06-09 16:36:54 +00:00
// Propagate failure of updateProperties (otherwise an update where the variant
// percentage drives the new price below the parent product's price_min returns
// success but leaves the variant product at the price=0 it had right after
// createProductCombination, see issue #32372).
$result = $this->updateProperties($parent, $user);
if ($result < 0) {
return $result;
}
2016-07-23 14:37:21 +00:00
return 1;
}
/**
* Deletes a product combination
*
2017-07-31 23:28:07 +00:00
* @param User $user Object user
2023-12-01 18:51:32 +00:00
* @return int Return integer <0 if KO, >0 if OK
2016-07-23 14:37:21 +00:00
*/
2017-07-28 07:45:23 +00:00
public function delete(User $user)
2016-07-23 14:37:21 +00:00
{
$this->db->begin();
$comb2val = new ProductCombination2ValuePair($this->db);
$comb2val->deleteByFkCombination($this->id);
2020-08-12 08:14:29 +00:00
// remove combination price levels
2020-08-13 09:48:28 +00:00
if (!$this->db->query("DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination_price_level WHERE fk_product_attribute_combination = ".(int) $this->id)) {
2020-08-12 08:14:29 +00:00
$this->db->rollback();
return -1;
}
2016-07-23 14:37:21 +00:00
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE rowid = ".(int) $this->id;
if ($this->db->query($sql)) {
$this->db->commit();
return 1;
}
$this->db->rollback();
return -1;
}
/**
* Deletes all product combinations of a parent product
*
* @param User $user Object user
* @param int $fk_product_parent Rowid of parent product
* @return int Return integer <0 if KO, >0 if OK
2016-07-23 14:37:21 +00:00
*/
public function deleteByFkProductParent($user, $fk_product_parent)
2016-07-23 14:37:21 +00:00
{
2025-04-12 14:24:29 +00:00
$arrayofparent = $this->fetchAllByFkProductParent($fk_product_parent);
2016-07-23 14:37:21 +00:00
if (!is_array($arrayofparent)) { // No combinations found, return success
return 1;
}
2016-07-23 14:37:21 +00:00
$this->db->begin();
foreach ($arrayofparent as $prodcomb) {
2016-07-23 14:37:21 +00:00
$prodstatic = new Product($this->db);
$res = $prodstatic->fetch($prodcomb->fk_product_child);
2025-04-12 14:24:29 +00:00
2016-07-23 14:37:21 +00:00
if ($res > 0) {
$res = $prodcomb->delete($user);
2016-07-23 14:37:21 +00:00
}
if ($res > 0 && !$prodstatic->isObjectUsed($prodstatic->id)) {
$res = $prodstatic->delete($user);
2016-07-23 14:37:21 +00:00
}
if ($res < 0) {
$this->db->rollback();
return -1;
}
}
$this->db->commit();
return 1;
}
/**
2021-02-26 12:22:13 +00:00
* Updates the weight of the child product. The price must be updated using Product::updatePrices.
* This method is called by the update() of a product.
2016-07-23 14:37:21 +00:00
*
2021-02-26 12:22:13 +00:00
* @param Product $parent Parent product
2019-11-09 13:13:14 +00:00
* @param User $user Object user
2021-02-26 12:22:13 +00:00
* @return int >0 if OK, <0 if KO
2016-07-23 14:37:21 +00:00
*/
2019-11-09 13:13:14 +00:00
public function updateProperties(Product $parent, User $user)
2016-07-23 14:37:21 +00:00
{
$this->db->begin();
$child = new Product($this->db);
$child->fetch($this->fk_product_child);
2020-05-23 11:53:47 +00:00
2016-07-23 14:37:21 +00:00
$child->price_autogen = $parent->price_autogen;
2020-05-23 11:46:37 +00:00
$child->weight = $parent->weight;
// Only when Parent Status are updated
if (is_object($parent->oldcopy) && !empty($parent->oldcopy->id) && ($parent->status != $parent->oldcopy->status)) {
2021-11-02 13:20:41 +00:00
$child->status = $parent->status;
}
if (is_object($parent->oldcopy) && !empty($parent->oldcopy->id) && ($parent->status_buy != $parent->oldcopy->status_buy)) {
2021-11-02 13:20:41 +00:00
$child->status_buy = $parent->status_buy;
}
2020-05-23 11:46:37 +00:00
if ($this->variation_weight) { // If we must add a delta on weight
$child->weight = ($child->weight ? $child->weight : 0) + $this->variation_weight;
}
$child->weight_units = $parent->weight_units;
2020-05-23 11:53:47 +00:00
// Don't update the child label if the user has already modified it.
if ($child->label == $parent->label) {
// This will trigger only at variant creation time
$varlabel = $this->getCombinationLabel($this->fk_product_child);
2022-12-28 15:20:35 +00:00
$child->label = $parent->label.$varlabel;
2020-05-23 11:53:47 +00:00
}
2016-07-23 14:37:21 +00:00
2020-08-12 08:14:29 +00:00
2016-07-23 14:37:21 +00:00
if ($child->update($child->id, $user) > 0) {
$new_vat = $parent->tva_tx;
$new_npr = $parent->tva_npr;
// MultiPrix
2023-11-27 11:08:48 +00:00
if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
$produit_multiprices_limit = getDolGlobalInt('PRODUIT_MULTIPRICES_LIMIT');
for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
if ((isset($parent->multiprices[$i]) && $parent->multiprices[$i] != '') || isset($this->combination_price_levels[$i]->variation_price)) {
2021-10-31 16:42:43 +00:00
$new_type = empty($parent->multiprices_base_type[$i]) ? 'HT' : $parent->multiprices_base_type[$i];
$new_min_price = isset($parent->multiprices_min[$i]) ? $parent->multiprices_min[$i] : 0;
2023-12-27 11:12:20 +00:00
$variation_price = (float) (!isset($this->combination_price_levels[$i]->variation_price) ? $this->variation_price : $this->combination_price_levels[$i]->variation_price);
$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);
2020-08-12 08:14:29 +00:00
if (!empty($parent->prices_by_qty_list[$i])) {
$new_psq = 1;
} else {
$new_psq = 0;
}
if ($new_type == 'TTC') {
$new_price = isset($parent->multiprices_ttc[$i]) ? $parent->multiprices_ttc[$i] : 0;
} else {
$new_price = isset($parent->multiprices[$i]) ? $parent->multiprices[$i] : 0;
}
2020-08-12 08:14:29 +00:00
if ($variation_price_percentage) {
if ($new_price != 0) {
2020-08-12 08:14:29 +00:00
$new_price *= 1 + ($variation_price / 100);
}
} else {
2020-08-12 08:14:29 +00:00
$new_price += $variation_price;
}
$ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, $i, $new_npr, $new_psq, 0, array(), $parent->default_vat_code);
if ($ret < 0) {
$this->db->rollback();
$this->error = $child->error;
$this->errors = $child->errors;
return $ret;
}
}
2016-07-23 14:37:21 +00:00
}
} else {
$new_type = $parent->price_base_type;
$new_min_price = $parent->price_min;
$new_psq = $parent->price_by_qty;
if ($new_type == 'TTC') {
$new_price = $parent->price_ttc;
} else {
$new_price = $parent->price;
}
if ($this->variation_price_percentage) {
if ($new_price != 0) {
$new_price *= 1 + ($this->variation_price / 100);
}
} else {
$new_price += $this->variation_price;
}
2016-07-23 14:37:21 +00:00
2025-03-05 17:36:34 +00:00
$ret = $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, 1, $new_npr, $new_psq, 0, [], $parent->default_vat_code);
if ($ret < 0) {
$this->db->rollback();
$this->error = $child->error;
$this->errors = $child->errors;
return $ret;
}
}
2016-07-23 14:37:21 +00:00
$this->db->commit();
return 1;
}
$this->db->rollback();
$this->error = $child->error;
$this->errors = $child->errors;
2016-07-23 14:37:21 +00:00
return -1;
}
/**
* Retrieves the combination that matches the given features.
*
2017-11-26 17:43:43 +00:00
* @param int $prodid Id of parent product
* @param array<int|string,int|string> $features Format: [$attr] => $attr_val
2017-11-26 17:43:43 +00:00
* @return false|ProductCombination False if not found
2016-07-23 14:37:21 +00:00
*/
public function fetchByProductCombination2ValuePairs($prodid, array $features)
{
require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php';
2016-07-23 14:37:21 +00:00
$actual_comp = array();
$prodcomb2val = new ProductCombination2ValuePair($this->db);
$prodcomb = new ProductCombination($this->db);
$features = array_filter(
$features,
/**
* @param mixed $v Feature information of a product.
* @return bool
*/
static function ($v) {
return !empty($v);
}
);
2016-07-23 14:37:21 +00:00
foreach ($features as $attr => $attr_val) {
$actual_comp[$attr] = $attr_val;
}
foreach ($prodcomb->fetchAllByFkProductParent($prodid) as $prc) {
$values = array();
foreach ($prodcomb2val->fetchByFkCombination($prc->id) as $value) {
$values[$value->fk_prod_attr] = $value->fk_prod_attr_val;
}
$check1 = count(array_diff_assoc($values, $actual_comp));
$check2 = count(array_diff_assoc($actual_comp, $values));
if (!$check1 && !$check2) {
return $prc;
}
}
return false;
}
/**
* Retrieves all unique attributes for a parent product
* (filtered on its 'to sell' variants)
2016-07-23 14:37:21 +00:00
*
* @param int $productid Parent Product rowid
* @return array<object{id:int,ref:string,label:string,values:ProductAttributeValue[]}> Array of attributes
2016-07-23 14:37:21 +00:00
*/
public function getUniqueAttributesAndValuesByFkProductParent($productid)
{
require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php';
require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttributeValue.class.php';
2016-07-23 14:37:21 +00:00
// Attributes
// Select all unique attributes of the variants (which are to sell) of a given parent product.
$sql = "SELECT DISTINCT c2v.fk_prod_attr, a.position";
$sql .= " FROM ".MAIN_DB_PREFIX."product_attribute_combination2val c2v";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_attribute_combination c";
$sql .= " ON c2v.fk_prod_combination = c.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product p";
$sql .= " ON p.rowid = c.fk_product_child";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_attribute a";
$sql .= " ON a.rowid = fk_prod_attr";
$sql .= " WHERE c.fk_product_parent = ".((int) $productid);
$sql .= " AND p.tosell = 1";
$sql .= $this->db->order('a.position', 'asc');
2016-07-23 14:37:21 +00:00
$resql = $this->db->query($sql);
2016-07-23 14:37:21 +00:00
// Values
$variants = array();
while ($obj = $this->db->fetch_object($resql)) {
2016-07-23 14:37:21 +00:00
$attr = new ProductAttribute($this->db);
$attr->fetch($obj->fk_prod_attr);
2016-07-23 14:37:21 +00:00
$tmp = new stdClass();
$tmp->id = $attr->id;
$tmp->ref = $attr->ref;
$tmp->label = $attr->label;
$tmp->values = array();
$attrval = new ProductAttributeValue($this->db);
// fetch only the used values of this attribute
foreach ($attrval->fetchAllByProductAttribute($attr->id, true) as $val) {
$tmpValue = new stdClass();
$tmpValue->id = $val->id;
$tmpValue->fk_product_attribute = $val->fk_product_attribute;
$tmpValue->ref = $val->ref;
$tmpValue->value = $val->value;
$tmp->values[] = $tmpValue;
2016-07-23 14:37:21 +00:00
}
$variants[] = $tmp;
2016-07-23 14:37:21 +00:00
}
return $variants;
2016-07-23 14:37:21 +00:00
}
/**
* Creates a product combination. Check usages to find more about its use
* Format of $combinations array:
* array(
* 0 => array(
* attr => value,
* attr2 => value
* [...]
* ),
* [...]
* )
*
* @param User $user User
* @param Product $product Parent Product
* @param array<int,int> $combinations Attribute and value combinations.
* @param array<int,array<int,array{weight:string|float,price:string|float}>> $variations Price and weight variations (example: $variations[fk_product_attribute][fk_product_attribute_value]['weight'])
* @param bool|bool[] $price_var_percent Is the price variation value a relative variation (in %)? (it is an array if global constant "PRODUIT_MULTIPRICES" is on)
* @param false|float|float[] $forced_pricevar Value of the price variation if it is forced ; in currency or percent. (it is an array if global constant "PRODUIT_MULTIPRICES" is on)
* @param false|float $forced_weightvar Value of the weight variation if it is forced
* @param false|string $forced_refvar Value of the reference if it is forced
* @param string $ref_ext External reference
* @param bool $clone_categories Add parent product categories to the created variant
* @return int<-1,1> Return integer <0 KO, >0 OK
2016-07-23 14:37:21 +00:00
*/
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 = '', $clone_categories = false)
2016-07-23 14:37:21 +00:00
{
2020-09-21 10:35:20 +00:00
global $conf;
2016-07-23 14:37:21 +00:00
require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php';
require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttributeValue.class.php';
2016-07-23 14:37:21 +00:00
2020-09-19 21:30:29 +00:00
$this->db->begin();
2016-07-23 14:37:21 +00:00
$price_impact = array(1 => 0); // init level price impact
2020-08-12 08:14:29 +00:00
2024-03-19 21:40:43 +00:00
$forced_refvar = trim((string) $forced_refvar);
2019-11-23 15:23:31 +00:00
if (!empty($forced_refvar) && $forced_refvar != $product->ref) {
2020-09-19 21:30:29 +00:00
$existingProduct = new Product($this->db);
$result = $existingProduct->fetch(0, $forced_refvar);
2019-11-23 15:23:31 +00:00
if ($result > 0) {
$newproduct = $existingProduct;
} else {
$existingProduct = false;
$newproduct = clone $product;
$newproduct->ref = $forced_refvar;
}
} else {
2020-05-23 11:53:47 +00:00
$forced_refvar = false;
$existingProduct = false;
$newproduct = clone $product;
}
2016-07-23 14:37:21 +00:00
// To avoid warning with unique extrafields values
$newproduct->context['createproductcombination'] = 'createproductcombination';
2016-07-23 14:37:21 +00:00
//Final weight impact
$weight_impact = (float) $forced_weightvar; // If false, return 0
2016-07-23 14:37:21 +00:00
//Final price impact
if (!is_array($forced_pricevar)) {
$price_impact[1] = (float) $forced_pricevar; // If false, return 0
} else {
2020-08-12 08:14:29 +00:00
$price_impact = $forced_pricevar;
}
if (!is_array($price_var_percent)) {
2025-01-07 16:37:30 +00:00
$price_var_percent = array(1 => (bool) $price_var_percent);
}
2020-09-19 21:30:29 +00:00
$newcomb = new ProductCombination($this->db);
2016-07-23 14:37:21 +00:00
$existingCombination = $newcomb->fetchByProductCombination2ValuePairs($product->id, $combinations);
if ($existingCombination) {
$newcomb = $existingCombination;
} else {
$newcomb->fk_product_parent = $product->id;
// Create 1 entry into product_attribute_combination (1 entry for each combinations). This init also $newcomb->id
$result = $newcomb->create($user);
if ($result < 0) {
$this->error = $newcomb->error;
$this->errors = $newcomb->errors;
2020-09-19 21:30:29 +00:00
$this->db->rollback();
2016-07-23 14:37:21 +00:00
return -1;
}
}
2020-09-19 21:30:29 +00:00
$prodattr = new ProductAttribute($this->db);
$prodattrval = new ProductAttributeValue($this->db);
2016-07-23 14:37:21 +00:00
2017-11-26 17:43:43 +00:00
// $combination contains list of attributes pairs key->value. Example: array('id Color'=>id Blue, 'id Size'=>id Small, 'id Option'=>id val a, ...)
2016-07-23 14:37:21 +00:00
foreach ($combinations as $currcombattr => $currcombval) {
//This was checked earlier, so no need to double check
$prodattr->fetch($currcombattr);
$prodattrval->fetch($currcombval);
//If there is an existing combination, there is no need to duplicate the valuepair
if (!$existingCombination) {
2020-09-19 21:30:29 +00:00
$tmp = new ProductCombination2ValuePair($this->db);
2016-07-23 14:37:21 +00:00
$tmp->fk_prod_attr = $currcombattr;
$tmp->fk_prod_attr_val = $currcombval;
$tmp->fk_prod_combination = $newcomb->id;
2017-11-26 17:43:43 +00:00
if ($tmp->create($user) < 0) { // Create 1 entry into product_attribute_combination2val
$this->error = $tmp->error;
$this->errors = $tmp->errors;
2020-09-19 21:30:29 +00:00
$this->db->rollback();
2016-07-23 14:37:21 +00:00
return -1;
}
}
if ($forced_weightvar === false) {
$weight_impact += (float) price2num($variations[$currcombattr][$currcombval]['weight']);
}
if ($forced_pricevar === false) {
2020-08-12 08:14:29 +00:00
$price_impact[1] += (float) price2num($variations[$currcombattr][$currcombval]['price']);
// Manage Price levels
if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
$produit_multiprices_limit = getDolGlobalString('PRODUIT_MULTIPRICES_LIMIT');
for ($i = 2; $i <= $produit_multiprices_limit; $i++) {
2020-08-12 08:14:29 +00:00
$price_impact[$i] += (float) price2num($variations[$currcombattr][$currcombval]['price']);
}
}
2016-07-23 14:37:21 +00:00
}
2019-11-23 15:23:31 +00:00
if ($forced_refvar === false) {
2025-05-04 08:49:27 +00:00
$newproduct->ref .= getDolGlobalString('PRODUIT_ATTRIBUTES_SEPARATOR', '_') . $prodattrval->ref;
}
2016-07-23 14:37:21 +00:00
//The first one should not contain a linebreak
if ($newproduct->description) {
$newproduct->description .= '<br>';
}
$newproduct->description .= '<strong>'.$prodattr->label.':</strong> '.$prodattrval->value;
}
$newcomb->variation_price_percentage = (bool) $price_var_percent[1];
2020-08-12 08:14:29 +00:00
$newcomb->variation_price = $price_impact[1];
2016-07-23 14:37:21 +00:00
$newcomb->variation_weight = $weight_impact;
$newcomb->variation_ref_ext = $this->db->escape($ref_ext);
2016-07-23 14:37:21 +00:00
2020-08-12 08:14:29 +00:00
// Init price level
if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
$produit_multiprices_limit = getDolGlobalInt('PRODUIT_MULTIPRICES_LIMIT');
for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
2020-08-12 08:14:29 +00:00
$productCombinationLevel = new ProductCombinationLevel($this->db);
$productCombinationLevel->fk_product_attribute_combination = $newcomb->id;
2020-08-12 08:14:29 +00:00
$productCombinationLevel->fk_price_level = $i;
$productCombinationLevel->variation_price = $price_impact[$i];
$productCombinationLevel->variation_price_percentage = (bool) $price_var_percent[$i] ;
2020-08-12 08:14:29 +00:00
$newcomb->combination_price_levels[$i] = $productCombinationLevel;
}
}
//var_dump($newcomb->combination_price_levels);
2020-08-12 08:14:29 +00:00
2016-07-23 14:37:21 +00:00
$newproduct->weight += $weight_impact;
2017-11-26 17:43:43 +00:00
// Now create the product
//print 'Create prod '.$newproduct->ref.'<br>'."\n";
if ($existingProduct === false) {
2020-05-23 11:53:47 +00:00
//To avoid wrong information in price history log
$newproduct->price = 0;
$newproduct->price_ttc = 0;
$newproduct->price_min = 0;
$newproduct->price_min_ttc = 0;
// A new variant must use a new barcode (not same product)
$newproduct->barcode = -1;
$result = $newproduct->create($user);
if ($result < 0) {
2020-05-23 11:53:47 +00:00
//In case the error is not related with an already existing product
if ($newproduct->error != 'ErrorProductAlreadyExists') {
$this->error = $newproduct->error;
2020-05-23 11:53:47 +00:00
$this->errors = $newproduct->errors;
2020-09-19 21:30:29 +00:00
$this->db->rollback();
2020-05-23 11:53:47 +00:00
return -1;
}
/**
* If there is an existing combination, then we update the prices and weight
* Otherwise, we try adding a random number to the ref
*/
if ($newcomb->fk_product_child) {
$res = $newproduct->fetch($existingCombination->fk_product_child);
} else {
$orig_prod_ref = $newproduct->ref;
$i = 1;
do {
$newproduct->ref = $orig_prod_ref.$i;
$res = $newproduct->create($user);
if ($newproduct->error != 'ErrorProductAlreadyExists') {
$this->errors[] = $newproduct->error;
break;
}
$i++;
} while ($res < 0);
}
if ($res < 0) {
2020-09-19 21:30:29 +00:00
$this->db->rollback();
2020-05-23 11:53:47 +00:00
return -1;
}
}
} else {
2020-05-23 11:53:47 +00:00
$result = $newproduct->update($newproduct->id, $user);
if ($result < 0) {
2020-09-19 21:30:29 +00:00
$this->db->rollback();
2020-05-23 11:53:47 +00:00
return -1;
}
2016-07-23 14:37:21 +00:00
}
$newcomb->fk_product_child = $newproduct->id;
if ($clone_categories) {
$clone_result = $newproduct->cloneCategories($product->id, $newproduct->id);
if ($clone_result < 0) {
$this->error = $newproduct->error;
$this->errors = $newproduct->errors;
$this->db->rollback();
return -1;
}
}
if ($newcomb->update($user) < 0) {
$this->error = $newcomb->error;
$this->errors = $newcomb->errors;
2020-09-19 21:30:29 +00:00
$this->db->rollback();
2016-07-23 14:37:21 +00:00
return -1;
}
2020-09-19 21:30:29 +00:00
$this->db->commit();
return $newproduct->id;
2016-07-23 14:37:21 +00:00
}
2020-05-23 11:53:47 +00:00
/**
* Copies all product combinations from the origin product to the destination product
*
2019-11-17 00:24:07 +00:00
* @param User $user Object user
2020-05-23 11:53:47 +00:00
* @param int $origProductId Origin product id
* @param Product $destProduct Destination product
* @return int >0 OK <0 KO
*/
2019-11-17 00:24:07 +00:00
public function copyAll(User $user, $origProductId, Product $destProduct)
2016-07-23 14:37:21 +00:00
{
require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php';
2016-07-23 14:37:21 +00:00
//To prevent a loop
if ($origProductId == $destProduct->id) {
return -1;
}
$prodcomb2val = new ProductCombination2ValuePair($this->db);
//Retrieve all product combinations
$combinationObjects = $this->fetchAllByFkProductParent($origProductId);
2016-07-23 14:37:21 +00:00
foreach ($combinationObjects as $combinationObject) {
$combinations = array();
2016-07-23 14:37:21 +00:00
foreach ($prodcomb2val->fetchByFkCombination($combinationObject->id) as $tmp_pc2v) {
$combinations[$tmp_pc2v->fk_prod_attr] = $tmp_pc2v->fk_prod_attr_val;
2016-07-23 14:37:21 +00:00
}
$variation_price_percentage = $combinationObject->variation_price_percentage;
$variation_price = $combinationObject->variation_price;
if (getDolGlobalInt('PRODUIT_MULTIPRICES') && getDolGlobalInt('PRODUIT_MULTIPRICES_LIMIT') > 1) {
$variation_price_percentage = [ ];
$variation_price = [ ];
foreach ($combinationObject->combination_price_levels as $productCombinationLevel) {
$variation_price_percentage[$productCombinationLevel->fk_price_level] = $productCombinationLevel->variation_price_percentage;
$variation_price[$productCombinationLevel->fk_price_level] = $productCombinationLevel->variation_price;
}
}
2020-05-23 11:53:47 +00:00
if ($this->createProductCombination(
2019-11-17 00:24:07 +00:00
$user,
2016-07-23 14:37:21 +00:00
$destProduct,
$combinations,
2016-07-23 14:37:21 +00:00
array(),
$variation_price_percentage,
$variation_price,
$combinationObject->variation_weight
) < 0) {
2016-07-23 14:37:21 +00:00
return -1;
}
}
return 1;
}
/**
* Return label for combinations
2019-02-10 09:45:49 +00:00
* @param int $prod_child id of child
* @return string combination label
*/
public function getCombinationLabel($prod_child)
{
$label = '';
$sql = 'SELECT pav.value AS label';
$sql .= ' FROM '.MAIN_DB_PREFIX.'product_attribute_combination pac';
$sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'product_attribute_combination2val pac2v ON pac2v.fk_prod_combination=pac.rowid';
$sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'product_attribute_value pav ON pav.rowid=pac2v.fk_prod_attr_val';
2021-08-28 01:09:18 +00:00
$sql .= ' WHERE pac.fk_product_child='.((int) $prod_child);
$resql = $this->db->query($sql);
if ($resql) {
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < $num) {
$obj = $this->db->fetch_object($resql);
if ($obj->label) {
$label .= ' '.$obj->label;
}
$i++;
}
}
return $label;
}
}
2020-08-12 08:14:29 +00:00
/**
* Class ProductCombinationLevel
* Used to represent a product combination Level
*/
class ProductCombinationLevel
{
/**
* Database handler
* @var DoliDB
*/
2020-09-19 21:30:29 +00:00
public $db;
2020-08-12 08:14:29 +00:00
/**
* @var string Name of table without prefix where object is stored
*/
public $table_element = 'product_attribute_combination_price_level';
/**
* Rowid of combination
* @var int
*/
public $id;
/**
* Rowid of parent product combination
* @var int
*/
public $fk_product_attribute_combination;
/**
* Combination price level
* @var int
*/
public $fk_price_level;
/**
* Price variation
* @var float
*/
public $variation_price;
/**
* Is the price variation a relative variation?
* @var bool
*/
public $variation_price_percentage = false;
2023-02-20 19:43:23 +00:00
/**
* @var string error
*/
public $error;
/**
* @var string[] array of errors
*/
public $errors = array();
2020-08-12 08:14:29 +00:00
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct(DoliDB $db)
{
$this->db = $db;
}
/**
* Retrieves a combination level by its rowid
*
* @param int $rowid Row id
2023-12-06 14:46:39 +00:00
* @return int Return integer <0 KO, >0 OK
2020-08-12 08:14:29 +00:00
*/
public function fetch($rowid)
{
2022-05-10 12:38:40 +00:00
$sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage";
$sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql .= " WHERE rowid = ".(int) $rowid;
2020-08-12 08:14:29 +00:00
2022-05-10 12:38:40 +00:00
$resql = $this->db->query($sql);
if ($resql) {
$obj = $this->db->fetch_object($resql);
if ($obj) {
return $this->fetchFormObj($obj);
}
2020-08-12 08:14:29 +00:00
}
return -1;
}
/**
* Retrieves combination price levels
*
2020-08-17 03:09:27 +00:00
* @param int $fk_product_attribute_combination Id of product combination
* @param int $fk_price_level The price level to fetch, use 0 for all
* @return mixed self[] | -1 on KO
2020-08-12 08:14:29 +00:00
*/
public function fetchAll($fk_product_attribute_combination, $fk_price_level = 0)
{
2021-11-07 17:14:49 +00:00
$result = array();
2020-08-12 08:14:29 +00:00
2020-08-17 03:54:22 +00:00
$sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage";
$sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql .= " WHERE fk_product_attribute_combination = ".intval($fk_product_attribute_combination);
if (!empty($fk_price_level)) {
$sql .= ' AND fk_price_level = '.intval($fk_price_level);
2020-08-12 08:14:29 +00:00
}
2021-11-07 17:14:49 +00:00
$res = $this->db->query($sql);
if ($res) {
if ($this->db->num_rows($res) > 0) {
while ($obj = $this->db->fetch_object($res)) {
$productCombinationLevel = new ProductCombinationLevel($this->db);
$productCombinationLevel->fetchFormObj($obj);
$result[$obj->fk_price_level] = $productCombinationLevel;
}
}
2021-11-07 17:14:49 +00:00
} else {
return -1;
2020-08-12 08:14:29 +00:00
}
return $result;
}
/**
2021-11-07 17:14:49 +00:00
* Assign vars form an stdclass like sql obj
2020-08-12 08:14:29 +00:00
*
2023-02-20 19:43:23 +00:00
* @param Object $obj Object resultset
2023-12-06 14:46:39 +00:00
* @return int Return integer <0 KO, >0 OK
2020-08-12 08:14:29 +00:00
*/
public function fetchFormObj($obj)
{
if (!$obj) {
return -1;
}
$this->id = $obj->rowid;
$this->fk_product_attribute_combination = (int) $obj->fk_product_attribute_combination;
2020-08-12 08:14:29 +00:00
$this->fk_price_level = intval($obj->fk_price_level);
2023-12-27 11:12:20 +00:00
$this->variation_price = (float) $obj->variation_price;
2020-08-13 09:48:28 +00:00
$this->variation_price_percentage = (bool) $obj->variation_price_percentage;
2020-08-12 08:14:29 +00:00
return 1;
}
/**
* Save a price impact of a product combination for a price level
2020-08-12 08:14:29 +00:00
*
2023-12-06 14:46:39 +00:00
* @return int Return integer <0 KO, >0 OK
2020-08-12 08:14:29 +00:00
*/
public function save()
{
if (($this->id > 0 && empty($this->fk_product_attribute_combination)) || empty($this->fk_price_level)) {
2020-08-12 08:14:29 +00:00
return -1;
}
// Check if level exist in DB before add
if ($this->fk_product_attribute_combination > 0 && empty($this->id)) {
2020-08-17 03:09:27 +00:00
$sql = "SELECT rowid id";
$sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
2020-08-17 03:09:27 +00:00
$sql .= " WHERE fk_product_attribute_combination = ".(int) $this->fk_product_attribute_combination;
2022-05-10 12:38:40 +00:00
$sql .= ' AND fk_price_level = '.((int) $this->fk_price_level);
2020-08-12 08:14:29 +00:00
2022-05-10 12:38:40 +00:00
$resql = $this->db->query($sql);
if ($resql) {
$obj = $this->db->fetch_object($resql);
if ($obj) {
$this->id = $obj->id;
}
2020-08-12 08:14:29 +00:00
}
}
// Update
2020-08-13 09:48:28 +00:00
if (!empty($this->id)) {
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2023-12-27 11:12:20 +00:00
$sql .= ' SET variation_price = '.(float) $this->variation_price.' , variation_price_percentage = '.intval($this->variation_price_percentage);
2020-08-17 03:26:32 +00:00
$sql .= ' WHERE rowid = '.((int) $this->id);
2020-08-12 08:14:29 +00:00
$res = $this->db->query($sql);
if ($res > 0) {
2020-08-12 08:14:29 +00:00
return $this->id;
} else {
2020-08-12 08:14:29 +00:00
$this->error = $this->db->error();
$this->errors[] = $this->error;
return -1;
}
} else {
// Add
$sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
2020-08-17 03:26:32 +00:00
$sql .= "fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage";
$sql .= ") VALUES (";
$sql .= (int) $this->fk_product_attribute_combination;
$sql .= ", ".intval($this->fk_price_level);
2023-12-27 11:12:20 +00:00
$sql .= ", ".(float) $this->variation_price;
2020-08-17 03:26:32 +00:00
$sql .= ", ".intval($this->variation_price_percentage);
2020-08-17 03:09:27 +00:00
$sql .= ")";
2020-08-12 08:14:29 +00:00
$res = $this->db->query($sql);
if ($res) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
} else {
2020-08-12 08:14:29 +00:00
$this->error = $this->db->error();
$this->errors[] = $this->error;
return -1;
}
}
return $this->id;
}
/**
* delete
*
2023-12-06 14:46:39 +00:00
* @return int Return integer <0 KO, >0 OK
2020-08-12 08:14:29 +00:00
*/
public function delete()
{
2020-08-17 03:09:27 +00:00
$sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".(int) $this->id;
$res = $this->db->query($sql);
2020-08-12 08:14:29 +00:00
return $res ? 1 : -1;
}
/**
* delete all for a combination
*
2020-08-17 03:54:22 +00:00
* @param int $fk_product_attribute_combination Id of combination
2023-12-06 14:46:39 +00:00
* @return int Return integer <0 KO, >0 OK
2020-08-12 08:14:29 +00:00
*/
public function deleteAllForCombination($fk_product_attribute_combination)
{
2020-08-17 03:09:27 +00:00
$sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE fk_product_attribute_combination = ".(int) $fk_product_attribute_combination;
$res = $this->db->query($sql);
2020-08-12 08:14:29 +00:00
return $res ? 1 : -1;
}
/**
* Clean not needed price levels for a combination
*
2020-08-17 03:54:22 +00:00
* @param int $fk_product_attribute_combination Id of combination
2023-12-06 14:46:39 +00:00
* @return int Return integer <0 KO, >0 OK
2020-08-12 08:14:29 +00:00
*/
public function clean($fk_product_attribute_combination)
{
global $conf;
2020-08-17 03:54:22 +00:00
$sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql .= " WHERE fk_product_attribute_combination = ".(int) $fk_product_attribute_combination;
$sql .= " AND fk_price_level > ".(int) getDolGlobalString('PRODUIT_MULTIPRICES_LIMIT');
2020-08-17 03:54:22 +00:00
$res = $this->db->query($sql);
2020-08-12 08:14:29 +00:00
return $res ? 1 : -1;
}
/**
* Create new Product Combination Price level from Parent
*
2020-09-03 10:37:30 +00:00
* @param DoliDB $db Database handler
* @param ProductCombination $productCombination Product combination
* @param int $fkPriceLevel Price level
* @return ProductCombinationLevel
*/
public static function createFromParent(DoliDB $db, ProductCombination $productCombination, $fkPriceLevel)
{
$productCombinationLevel = new self($db);
$productCombinationLevel->fk_price_level = $fkPriceLevel;
$productCombinationLevel->fk_product_attribute_combination = $productCombination->id;
$productCombinationLevel->variation_price = $productCombination->variation_price;
2020-09-14 02:23:14 +00:00
$productCombinationLevel->variation_price_percentage = (bool) $productCombination->variation_price_percentage;
return $productCombinationLevel;
}
2020-08-12 08:14:29 +00:00
}