Merge pull request #14465 from atm-john/develop_declinaison_price_multiple_merge
NEW : Declinaison price level compatibility
This commit is contained in:
commit
816aeadcba
@ -175,6 +175,16 @@ ALTER TABLE llx_recruitment_recruitmentcandidature_extrafields ADD INDEX idx_fk_
|
||||
|
||||
|
||||
|
||||
CREATE TABLE llx_product_attribute_combination_price_level
|
||||
(
|
||||
rowid INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
|
||||
fk_product_attribute_combination INTEGER DEFAULT 1 NOT NULL,
|
||||
fk_price_level INTEGER DEFAULT 1 NOT NULL,
|
||||
variation_price DOUBLE(24,8) NOT NULL,
|
||||
variation_price_percentage INTEGER NULL
|
||||
)ENGINE=innodb;
|
||||
|
||||
ALTER TABLE llx_product_attribute_combination_price_level ADD UNIQUE( fk_product_attribute_combination, fk_price_level);
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
-- ============================================================================
|
||||
-- Copyright (C) 2020 John BOTELLA <john.botella@atm-consulting.fr>
|
||||
--
|
||||
-- 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
|
||||
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
--
|
||||
-- ============================================================================
|
||||
|
||||
CREATE TABLE llx_product_attribute_combination_price_level
|
||||
(
|
||||
rowid INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
|
||||
fk_product_attribute_combination INTEGER DEFAULT 1 NOT NULL,
|
||||
fk_price_level INTEGER DEFAULT 1 NOT NULL,
|
||||
variation_price DOUBLE(24,8) NOT NULL,
|
||||
variation_price_percentage INTEGER NULL
|
||||
)ENGINE=innodb;
|
||||
|
||||
ALTER TABLE llx_product_attribute_combination_price_level ADD UNIQUE( fk_product_attribute_combination, fk_price_level);
|
||||
@ -362,6 +362,9 @@ SelectCombination=Select combination
|
||||
ProductCombinationGenerator=Variants generator
|
||||
Features=Features
|
||||
PriceImpact=Price impact
|
||||
ImpactOnPriceLevel=Impact on price level %s
|
||||
ApplyToAllPriceImpactLevel= Apply to all levels
|
||||
ApplyToAllPriceImpactLevelHelp=By clicking here you set the same price impact on all levels
|
||||
WeightImpact=Weight impact
|
||||
NewProductAttribute=New attribute
|
||||
NewProductAttributeValue=New attribute value
|
||||
|
||||
@ -1077,7 +1077,7 @@ class Product extends CommonObject
|
||||
$comb = new ProductCombination($this->db);
|
||||
|
||||
foreach ($comb->fetchAllByFkProductParent($this->id) as $currcomb) {
|
||||
$currcomb->updateProperties($this, $user);
|
||||
$currcomb->updateProperties($this, $user);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -71,6 +71,12 @@ class ProductCombination
|
||||
*/
|
||||
public $entity;
|
||||
|
||||
/**
|
||||
* Combination price level
|
||||
* @var ProductCombinationLevel[]
|
||||
*/
|
||||
public $combination_price_levels;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
@ -92,6 +98,8 @@ class ProductCombination
|
||||
*/
|
||||
public function fetch($rowid)
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE rowid = ".(int) $rowid." AND entity IN (".getEntity('product').")";
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
@ -113,9 +121,118 @@ class ProductCombination
|
||||
$this->variation_price_percentage = $obj->variation_price_percentage;
|
||||
$this->variation_weight = $obj->variation_weight;
|
||||
|
||||
if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
|
||||
$this->fetchCombinationPriceLevels();
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 <0 KO, >0 OK
|
||||
*/
|
||||
public function fetchCombinationPriceLevels($fk_price_level = 0, $useCache = true)
|
||||
{
|
||||
global $conf;
|
||||
|
||||
// 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)){
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_array($this->combination_price_levels)
|
||||
|| empty($fk_price_level) // if fetch an unique level dont erase all already fetched
|
||||
){
|
||||
$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)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (empty($combination_price_levels)){
|
||||
|
||||
/**
|
||||
* for auto retrocompatibility with last behavior
|
||||
*/
|
||||
$productCombinationLevel = new ProductCombinationLevel($this->db);
|
||||
$productCombinationLevel->fk_price_level = intval($fk_price_level);
|
||||
$productCombinationLevel->fk_product_attribute_combination = $this->id;
|
||||
$productCombinationLevel->variation_price = $this->variation_price;
|
||||
$productCombinationLevel->variation_price_percentage = $this->variation_price_percentage;
|
||||
|
||||
if ($fk_price_level>0){
|
||||
$combination_price_levels[$fk_price_level] = $productCombinationLevel;
|
||||
}
|
||||
else {
|
||||
for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++){
|
||||
$combination_price_levels[$i] = $productCombinationLevel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->combination_price_levels = $combination_price_levels;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves combination price levels
|
||||
*
|
||||
* @param int $clean levels off PRODUIT_MULTIPRICES_LIMIT
|
||||
* @return int <0 KO, >0 OK
|
||||
*/
|
||||
public function saveCombinationPriceLevels($clean = 1)
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$errors = 0;
|
||||
|
||||
$staticProductCombinationLevel = new ProductCombinationLevel($this->db);
|
||||
|
||||
// Delete all
|
||||
if (empty($this->combination_price_levels)){
|
||||
return $staticProductCombinationLevel->deleteAllForCombination($this->id);
|
||||
}
|
||||
|
||||
// Clean not needed price levels
|
||||
if ($clean){
|
||||
$res = $staticProductCombinationLevel->clean($this->id);
|
||||
|
||||
if ($res<0){
|
||||
$this->errors[] = 'Fail to clean not needed price levels';
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->combination_price_levels as $fk_price_level => $combination_price_level){
|
||||
$res = $combination_price_level->save();
|
||||
if ($res<1){
|
||||
$this->error = 'save combination price level '.$fk_price_level . ' '.$combination_price_level->error;
|
||||
$this->errors[] = $this->error;
|
||||
$errors ++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($errors > 0){
|
||||
return $errors*-1;
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves a product combination by a child product row id
|
||||
*
|
||||
@ -124,6 +241,8 @@ class ProductCombination
|
||||
*/
|
||||
public function fetchByFkProductChild($fk_child)
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_child = ".(int) $fk_child." AND entity IN (".getEntity('product').")";
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
@ -145,6 +264,10 @@ class ProductCombination
|
||||
$this->variation_price_percentage = $result->variation_price_percentage;
|
||||
$this->variation_weight = $result->variation_weight;
|
||||
|
||||
if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
|
||||
$this->fetchCombinationPriceLevels();
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -156,6 +279,8 @@ class ProductCombination
|
||||
*/
|
||||
public function fetchAllByFkProductParent($fk_product_parent)
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_parent = ".(int) $fk_product_parent." AND entity IN (".getEntity('product').")";
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
@ -175,6 +300,10 @@ class ProductCombination
|
||||
$tmp->variation_price_percentage = $result->variation_price_percentage;
|
||||
$tmp->variation_weight = $result->variation_weight;
|
||||
|
||||
if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
|
||||
$tmp->fetchCombinationPriceLevels();
|
||||
}
|
||||
|
||||
$return[] = $tmp;
|
||||
}
|
||||
|
||||
@ -209,6 +338,8 @@ class ProductCombination
|
||||
*/
|
||||
public function create($user)
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_attribute_combination
|
||||
(fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight, entity)
|
||||
VALUES (".(int) $this->fk_product_parent.", ".(int) $this->fk_product_child.",
|
||||
@ -221,6 +352,13 @@ class ProductCombination
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
|
||||
$res = $this->saveCombinationPriceLevels();
|
||||
if ($res<0){
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
||||
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'product_attribute_combination');
|
||||
|
||||
return 1;
|
||||
@ -234,6 +372,8 @@ class ProductCombination
|
||||
*/
|
||||
public function update(User $user)
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$sql = "UPDATE ".MAIN_DB_PREFIX."product_attribute_combination
|
||||
SET fk_product_parent = ".(int) $this->fk_product_parent.", fk_product_child = ".(int) $this->fk_product_child.",
|
||||
variation_price = ".(float) $this->variation_price.", variation_price_percentage = ".(int) $this->variation_price_percentage.",
|
||||
@ -245,6 +385,14 @@ class ProductCombination
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
|
||||
$res = $this->saveCombinationPriceLevels();
|
||||
if ($res<0){
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
||||
$parent = new Product($this->db);
|
||||
$parent->fetch($this->fk_product_parent);
|
||||
|
||||
@ -266,6 +414,12 @@ class ProductCombination
|
||||
$comb2val = new ProductCombination2ValuePair($this->db);
|
||||
$comb2val->deleteByFkCombination($this->id);
|
||||
|
||||
// remove combination price levels
|
||||
if (!$this->db->query("DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination_price_level WHERE fk_product_attribute_combination = ".(int) $this->id)) {
|
||||
$this->db->rollback();
|
||||
return -1;
|
||||
}
|
||||
|
||||
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE rowid = ".(int) $this->id;
|
||||
|
||||
if ($this->db->query($sql)) {
|
||||
@ -341,6 +495,7 @@ class ProductCombination
|
||||
$child->label = $parent->label.$varlabel;;
|
||||
}
|
||||
|
||||
|
||||
if ($child->update($child->id, $user) > 0) {
|
||||
$new_vat = $parent->tva_tx;
|
||||
$new_npr = $parent->tva_npr;
|
||||
@ -349,9 +504,12 @@ class ProductCombination
|
||||
if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
|
||||
for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++)
|
||||
{
|
||||
if ($parent->multiprices[$i] != '') {
|
||||
if ($parent->multiprices[$i] != '' || isset($this->combination_price_levels[$i]->variation_price)) {
|
||||
$new_type = $parent->multiprices_base_type[$i];
|
||||
$new_min_price = $parent->multiprices_min[$i];
|
||||
$variation_price = doubleval(!isset($this->combination_price_levels[$i]->variation_price) ? $this->variation_price : $this->combination_price_levels[$i]->variation_price);
|
||||
$variation_price_percentage = doubleval(!isset($this->combination_price_levels[$i]->variation_price_percentage) ? $this->variation_price_percentage : $this->combination_price_levels[$i]->variation_price_percentage);
|
||||
|
||||
if ($parent->prices_by_qty_list[$i]) {
|
||||
$new_psq = 1;
|
||||
} else {
|
||||
@ -364,12 +522,12 @@ class ProductCombination
|
||||
$new_price = $parent->multiprices[$i];
|
||||
}
|
||||
|
||||
if ($this->variation_price_percentage) {
|
||||
if ($variation_price_percentage) {
|
||||
if ($new_price != 0) {
|
||||
$new_price *= 1 + ($this->variation_price / 100);
|
||||
$new_price *= 1 + ($variation_price / 100);
|
||||
}
|
||||
} else {
|
||||
$new_price += $this->variation_price;
|
||||
$new_price += $variation_price;
|
||||
}
|
||||
|
||||
$child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, $i, $new_npr, $new_psq);
|
||||
@ -508,7 +666,7 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
||||
* @param Product $product Parent product
|
||||
* @param array $combinations Attribute and value combinations.
|
||||
* @param array $variations Price and weight variations
|
||||
* @param bool $price_var_percent Is the price variation a relative variation?
|
||||
* @param bool|array $price_var_percent Is the price variation a relative variation?
|
||||
* @param bool|float $forced_pricevar If the price variation is forced
|
||||
* @param bool|float $forced_weightvar If the weight variation is forced
|
||||
* @param bool|string $forced_refvar If the reference is forced
|
||||
@ -523,6 +681,8 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
||||
|
||||
$db->begin();
|
||||
|
||||
$price_impact = array(1=>0); // init level price impact
|
||||
|
||||
$forced_refvar = trim($forced_refvar);
|
||||
|
||||
if (!empty($forced_refvar) && $forced_refvar != $product->ref) {
|
||||
@ -545,7 +705,12 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
||||
$weight_impact = (float) $forced_weightvar; // If false, return 0
|
||||
|
||||
//Final price impact
|
||||
$price_impact = (float) $forced_pricevar; // If false, return 0
|
||||
if (!is_array($forced_pricevar)){
|
||||
$price_impact[1] = (float) $forced_pricevar; // If false, return 0
|
||||
}
|
||||
else {
|
||||
$price_impact = $forced_pricevar;
|
||||
}
|
||||
|
||||
$newcomb = new ProductCombination($db);
|
||||
$existingCombination = $newcomb->fetchByProductCombination2ValuePairs($product->id, $combinations);
|
||||
@ -587,7 +752,15 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
||||
$weight_impact += (float) price2num($variations[$currcombattr][$currcombval]['weight']);
|
||||
}
|
||||
if ($forced_pricevar === false) {
|
||||
$price_impact += (float) price2num($variations[$currcombattr][$currcombval]['price']);
|
||||
$price_impact[1] += (float) price2num($variations[$currcombattr][$currcombval]['price']);
|
||||
|
||||
// Manage Price levels
|
||||
if ($conf->global->PRODUIT_MULTIPRICES){
|
||||
for ($i = 2; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++)
|
||||
{
|
||||
$price_impact[$i] += (float) price2num($variations[$currcombattr][$currcombval]['price']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($forced_refvar === false) {
|
||||
@ -606,9 +779,27 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
||||
}
|
||||
|
||||
$newcomb->variation_price_percentage = $price_var_percent;
|
||||
$newcomb->variation_price = $price_impact;
|
||||
$newcomb->variation_price = $price_impact[1];
|
||||
$newcomb->variation_weight = $weight_impact;
|
||||
|
||||
// Init price level
|
||||
if ($conf->global->PRODUIT_MULTIPRICES){
|
||||
for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++){
|
||||
$productCombinationLevel = new ProductCombinationLevel($this->db);
|
||||
$productCombinationLevel->fk_product_attribute_combination = 0;
|
||||
$productCombinationLevel->fk_price_level = $i;
|
||||
$productCombinationLevel->variation_price = $price_impact[$i];
|
||||
|
||||
if (is_array($price_var_percent)){
|
||||
$productCombinationLevel->variation_price_percentage = !empty($price_var_percent[$i]) ? $price_var_percent[$i] : 0;
|
||||
}else {
|
||||
$productCombinationLevel->variation_price_percentage = $price_var_percent;
|
||||
}
|
||||
|
||||
$newcomb->combination_price_levels[$i] = $productCombinationLevel;
|
||||
}
|
||||
}
|
||||
|
||||
$newproduct->weight += $weight_impact;
|
||||
|
||||
// Now create the product
|
||||
@ -764,3 +955,260 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
||||
return $label;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Class ProductCombinationLevel
|
||||
* Used to represent a product combination Level
|
||||
*/
|
||||
class ProductCombinationLevel
|
||||
{
|
||||
/**
|
||||
* Database handler
|
||||
* @var DoliDB
|
||||
*/
|
||||
private $db;
|
||||
|
||||
/**
|
||||
* @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;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return int <0 KO, >0 OK
|
||||
*/
|
||||
public function fetch($rowid)
|
||||
{
|
||||
$sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage FROM " . MAIN_DB_PREFIX . $this->table_element." WHERE rowid = " . (int) $rowid;
|
||||
|
||||
$obj = $this->db->getRow($sql);
|
||||
|
||||
if ($obj){
|
||||
return $this->fetchFormObj($obj);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves combination price levels
|
||||
*
|
||||
* @param int $fk_product_attribute_combination
|
||||
* @param int $fk_price_level the price level to fetch, use 0 for all
|
||||
* @return self[] | -1 on KO
|
||||
*/
|
||||
public function fetchAll($fk_product_attribute_combination, $fk_price_level = 0)
|
||||
{
|
||||
|
||||
$sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage"
|
||||
." FROM ".MAIN_DB_PREFIX.$this->table_element
|
||||
." WHERE fk_product_attribute_combination = ".intval($fk_product_attribute_combination);
|
||||
|
||||
if (!empty($fk_price_level)){
|
||||
$sql.= ' AND fk_price_level = '.intval($fk_price_level);
|
||||
}
|
||||
|
||||
$combination_price_levels = $this->db->getRows($sql);
|
||||
|
||||
if (!is_array($combination_price_levels)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
$result = array();
|
||||
|
||||
if (!empty($combination_price_levels)) {
|
||||
// For more simple usage set level as array key
|
||||
foreach ($combination_price_levels as $k => $row){
|
||||
$productCombinationLevel = new ProductCombinationLevel($this->db);
|
||||
$productCombinationLevel->fetchFormObj($row);
|
||||
$result[$row->fk_price_level] = $productCombinationLevel;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* assign vars form an stdclass like sql obj
|
||||
*
|
||||
* @param int $rowid Row id
|
||||
* @return int <0 KO, >0 OK
|
||||
*/
|
||||
public function fetchFormObj($obj)
|
||||
{
|
||||
if (!$obj) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
$this->id = $obj->rowid;
|
||||
$this->fk_product_attribute_combination = doubleval($obj->fk_product_attribute_combination);
|
||||
$this->fk_price_level = intval($obj->fk_price_level);
|
||||
$this->variation_price = doubleval($obj->variation_price);
|
||||
$this->variation_price_percentage = (bool) $obj->variation_price_percentage;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* save
|
||||
*
|
||||
* @return int <0 KO, >0 OK
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$errors = 0;
|
||||
|
||||
|
||||
if (empty($this->fk_product_attribute_combination) || empty($this->fk_price_level)){
|
||||
return -1;
|
||||
}
|
||||
|
||||
// check if level exist in DB before add
|
||||
if (empty($this->id)){
|
||||
$sql = "SELECT rowid id"
|
||||
." FROM ".MAIN_DB_PREFIX . $this->table_element
|
||||
." WHERE fk_product_attribute_combination = ".(int) $this->fk_product_attribute_combination
|
||||
.' AND fk_price_level = '.intval($this->fk_price_level);
|
||||
|
||||
$existObj = $this->db->getRow($sql);
|
||||
if ($existObj){
|
||||
$this->id = $existObj->id;
|
||||
}
|
||||
}
|
||||
|
||||
// Update
|
||||
if (!empty($this->id)) {
|
||||
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET '
|
||||
. ' variation_price = '.doubleval($this->variation_price)
|
||||
. ' , variation_price_percentage = '.intval($this->variation_price_percentage)
|
||||
. ' WHERE rowid = '.intval($this->id);
|
||||
|
||||
$res = $this->db->query($sql);
|
||||
if ($res>0){
|
||||
return $this->id;
|
||||
}
|
||||
else {
|
||||
$this->error = $this->db->error();
|
||||
$this->errors[] = $this->error;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// ADD
|
||||
$sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . " ("
|
||||
. " fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage"
|
||||
. " ) VALUES ( "
|
||||
. intval($this->fk_product_attribute_combination)
|
||||
. ' , '.intval($this->fk_price_level)
|
||||
. ' , '.doubleval($this->variation_price)
|
||||
. ' , '.intval($this->variation_price_percentage)
|
||||
. " )";
|
||||
|
||||
$res = $this->db->query($sql);
|
||||
if ($res){
|
||||
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
|
||||
}
|
||||
else {
|
||||
$this->error = $this->db->error();
|
||||
$this->errors[] = $this->error;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* delete
|
||||
*
|
||||
* @return int <0 KO, >0 OK
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$res = $this->db->query("DELETE FROM ".MAIN_DB_PREFIX.$this->table_element
|
||||
." WHERE rowid = ".(int) $this->id);
|
||||
|
||||
return $res ? 1 : -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* delete all for a combination
|
||||
*
|
||||
* @param $fk_product_attribute_combination
|
||||
* @return int <0 KO, >0 OK
|
||||
*/
|
||||
public function deleteAllForCombination($fk_product_attribute_combination)
|
||||
{
|
||||
$res = $this->db->query("DELETE FROM ".MAIN_DB_PREFIX.$this->table_element
|
||||
." WHERE fk_product_attribute_combination = ".(int) $fk_product_attribute_combination);
|
||||
|
||||
return $res ? 1 : -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clean not needed price levels for a combination
|
||||
*
|
||||
* @param $fk_product_attribute_combination
|
||||
* @return int <0 KO, >0 OK
|
||||
*/
|
||||
public function clean($fk_product_attribute_combination)
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$res = $this->db->query("DELETE FROM ".MAIN_DB_PREFIX.$this->table_element
|
||||
. " WHERE fk_product_attribute_combination = ".(int) $fk_product_attribute_combination
|
||||
. " AND fk_price_level > ".intval($conf->global->PRODUIT_MULTIPRICES_LIMIT) );
|
||||
|
||||
|
||||
return $res ? 1 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,6 +33,10 @@ $ref = GETPOST('ref', 'alpha');
|
||||
$weight_impact = GETPOST('weight_impact', 'alpha');
|
||||
$price_impact = GETPOST('price_impact', 'alpha');
|
||||
$price_impact_percent = (bool) GETPOST('price_impact_percent');
|
||||
|
||||
$level_price_impact = GETPOST('level_price_impact', 'array');
|
||||
$level_price_impact_percent = GETPOST('level_price_impact_percent', 'array');
|
||||
|
||||
$reference = GETPOST('reference', 'alpha');
|
||||
$form = new Form($db);
|
||||
|
||||
@ -112,6 +116,18 @@ if ($_POST) {
|
||||
}
|
||||
$weight_impact = price2num($weight_impact);
|
||||
$price_impact = price2num($price_impact);
|
||||
|
||||
// for conf PRODUIT_MULTIPRICES
|
||||
if ($conf->global->PRODUIT_MULTIPRICES) {
|
||||
$level_price_impact = array_map('price2num', $level_price_impact);
|
||||
$level_price_impact_percent = array_map('price2num', $level_price_impact_percent);
|
||||
}
|
||||
else {
|
||||
$level_price_impact = array(1 => $weight_impact);
|
||||
$level_price_impact_percent = array(1 => $price_impact_percent);
|
||||
}
|
||||
|
||||
|
||||
$sanit_features = array();
|
||||
|
||||
//First, sanitize
|
||||
@ -141,11 +157,10 @@ if ($_POST) {
|
||||
// sanit_feature is an array with 1 (and only 1) value per attribute.
|
||||
// For example: Color->blue, Size->Small, Option->2
|
||||
//var_dump($sanit_features);
|
||||
//var_dump($productCombination2ValuePairs1); exit;
|
||||
|
||||
if (!$prodcomb->fetchByProductCombination2ValuePairs($id, $sanit_features))
|
||||
{
|
||||
$result = $prodcomb->createProductCombination($user, $object, $sanit_features, array(), $price_impact_percent, $price_impact, $weight_impact, $reference);
|
||||
$result = $prodcomb->createProductCombination($user, $object, $sanit_features, array(), $level_price_impact_percent, $level_price_impact, $weight_impact, $reference);
|
||||
if ($result > 0)
|
||||
{
|
||||
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
|
||||
@ -227,6 +242,32 @@ if ($_POST) {
|
||||
$prodcomb->variation_price = $price_impact;
|
||||
$prodcomb->variation_weight = $weight_impact;
|
||||
|
||||
// for conf PRODUIT_MULTIPRICES
|
||||
if ($conf->global->PRODUIT_MULTIPRICES) {
|
||||
$level_price_impact = array_map('price2num', $level_price_impact);
|
||||
$level_price_impact_percent = array_map(function ($a) {
|
||||
return !empty($a);}, $level_price_impact_percent);
|
||||
|
||||
$prodcomb->variation_price = $level_price_impact[1];
|
||||
$prodcomb->variation_price_percentage = (bool) $level_price_impact_percent[1];
|
||||
}
|
||||
else {
|
||||
$level_price_impact = array(1 => $weight_impact);
|
||||
$level_price_impact_percent = array(1 => $price_impact_percent);
|
||||
}
|
||||
|
||||
if ($conf->global->PRODUIT_MULTIPRICES){
|
||||
$prodcomb->combination_price_levels = array();
|
||||
for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++){
|
||||
$productCombinationLevel = new ProductCombinationLevel($db);
|
||||
$productCombinationLevel->fk_product_attribute_combination = $prodcomb->id;
|
||||
$productCombinationLevel->fk_price_level = $i;
|
||||
$productCombinationLevel->variation_price = $level_price_impact[$i];
|
||||
$productCombinationLevel->variation_price_percentage = $level_price_impact_percent[$i];
|
||||
$prodcomb->combination_price_levels[$i] = $productCombinationLevel;
|
||||
}
|
||||
}
|
||||
|
||||
if ($prodcomb->update($user) > 0) {
|
||||
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
|
||||
header('Location: '.dol_buildpath('/variants/combinations.php?id='.$id, 2));
|
||||
@ -594,12 +635,33 @@ if (!empty($id) || !empty($ref))
|
||||
<td><label for="reference"><?php echo $langs->trans('Reference') ?></label></td>
|
||||
<td><input type="text" id="reference" name="reference" value="<?php echo trim($reference) ?>"></td>
|
||||
</tr>
|
||||
<?php if (empty($conf->global->PRODUIT_MULTIPRICES)){ ?>
|
||||
<tr>
|
||||
<td><label for="price_impact"><?php echo $langs->trans('PriceImpact') ?></label></td>
|
||||
<td><input type="text" id="price_impact" name="price_impact" value="<?php echo price($price_impact) ?>">
|
||||
<input type="checkbox" id="price_impact_percent" name="price_impact_percent" <?php echo $price_impact_percent ? ' checked' : '' ?>> <label for="price_impact_percent"><?php echo $langs->trans('PercentageVariation') ?></label></td>
|
||||
<input type="checkbox" id="price_impact_percent" name="price_impact_percent" <?php echo $price_impact_percent ? ' checked' : '' ?>> <label for="price_impact_percent"><?php echo $langs->trans('PercentageVariation') ?></label>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
<?php }
|
||||
else {
|
||||
$prodcomb->fetchCombinationPriceLevels();
|
||||
|
||||
for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++)
|
||||
{
|
||||
print '<tr>';
|
||||
print '<td><label for="level_price_impact_'.$i.'">'.$langs->trans('ImpactOnPriceLevel', $i).'</label>';
|
||||
if ($i===1){
|
||||
print ' <a id="apply-price-impact-to-all-level" class="classfortooltip" href="#" title="'.$langs->trans('ApplyToAllPriceImpactLevelHelp').'">('.$langs->trans('ApplyToAllPriceImpactLevel').')</a>';
|
||||
}
|
||||
print '</td>';
|
||||
print '<td><input type="text" class="level_price_impact" id="level_price_impact_'.$i.'" name="level_price_impact['.$i.']" value="'.price($prodcomb->combination_price_levels[$i]->variation_price).'">';
|
||||
print '<input type="checkbox" class="level_price_impact_percent" id="level_price_impact_percent_'.$i.'" name="level_price_impact_percent['.$i.']" '. (!empty($prodcomb->combination_price_levels[$i]->variation_price_percentage) ? ' checked' : '' ).'> <label for="level_price_impact_percent_'.$i.'">'.$langs->trans('PercentageVariation').'</label>';
|
||||
|
||||
print '</td>';
|
||||
print '</tr>';
|
||||
}
|
||||
}
|
||||
|
||||
if ($object->isProduct()) {
|
||||
print '<tr>';
|
||||
print '<td><label for="weight_impact">'.$langs->trans('WeightImpact').'</label></td>';
|
||||
@ -609,9 +671,30 @@ if (!empty($id) || !empty($ref))
|
||||
print '</table>';
|
||||
}
|
||||
|
||||
dol_fiche_end();
|
||||
?>
|
||||
if (!empty($conf->global->PRODUIT_MULTIPRICES)){
|
||||
?>
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
// Apply level 1 impact to all prices impact levels
|
||||
$('body').on('click', '#apply-price-impact-to-all-level', function(e) {
|
||||
e.preventDefault();
|
||||
let priceImpact = $( "#level_price_impact_1" ).val();
|
||||
let priceImpactPrecent = $( "#level_price_impact_percent_1" ).prop("checked");
|
||||
|
||||
var multipricelimit = <?php print intval($conf->global->PRODUIT_MULTIPRICES_LIMIT); ?>
|
||||
|
||||
for (let i = 2; i <= multipricelimit; i++) {
|
||||
$( "#level_price_impact_" + i ).val(priceImpact);
|
||||
$( "#level_price_impact_percent_" + i ).prop("checked", priceImpactPrecent);
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
dol_fiche_end();
|
||||
?>
|
||||
<div style="text-align: center">
|
||||
<input type="submit" name="create" <?php if (!is_array($productCombination2ValuePairs1)) print ' disabled="disabled"'; ?> value="<?php echo $action == 'add' ? $langs->trans('Create') : $langs->trans('Save') ?>" class="button">
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user