Skip to content

Commit

Permalink
Debug module variant
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Nov 26, 2017
1 parent 2c07886 commit ae90627
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 48 deletions.
2 changes: 2 additions & 0 deletions htdocs/langs/en_US/errors.lang
Expand Up @@ -204,6 +204,8 @@ ErrorOnlyInvoiceValidatedCanBeSentInMassAction=Only validated invoices can be se
ErrorChooseBetweenFreeEntryOrPredefinedProduct=You must choose if article is a predefined product or not
ErrorDiscountLargerThanRemainToPaySplitItBefore=The discount you try to apply is larger than remain to pay. Split the discount in 2 smaller discounts before.
ErrorFileNotFoundWithSharedLink=File was not found. May be the share key was modified or file was removed recently.
ErrorProductBarCodeAlreadyExists=The product barcode %s already exists on another product reference.

# Warnings
WarningPasswordSetWithNoAccount=A password was set for this member. However, no user account was created. So this password is stored but can't be used to login to Dolibarr. It may be used by an external module/interface but if you don't need to define any login nor password for a member, you can disable option "Manage a login for each member" from Member module setup. If you need to manage a login but don't need any password, you can keep this field empty to avoid this warning. Note: Email can also be used as a login if the member is linked to a user.
WarningMandatorySetupNotComplete=Mandatory setup parameters are not yet defined
Expand Down
13 changes: 8 additions & 5 deletions htdocs/product/class/product.class.php
Expand Up @@ -656,21 +656,23 @@ function verify()
$result = -2;
}

$rescode = $this->check_barcode($this->barcode,$this->barcode_type_code);
if ($rescode <> 0)
$rescode = $this->check_barcode($this->barcode, $this->barcode_type_code);
if ($rescode)
{
if ($rescode == -1)
{
$this->errors[] = 'ErrorBadBarCodeSyntax';
}
if ($rescode == -2)
elseif ($rescode == -2)
{
$this->errors[] = 'ErrorBarCodeRequired';
}
if ($rescode == -3)
elseif ($rescode == -3)
{
// Note: Common usage is to have barcode unique. For variants, we should have a different barcode.
$this->errors[] = 'ErrorBarCodeAlreadyUsed';
}

$result = -3;
}

Expand Down Expand Up @@ -997,6 +999,7 @@ function update($id, $user, $notrigger=false, $action='update')
{
if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
{
$langs->load("errors");
if (empty($conf->barcode->enabled)) $this->error=$langs->trans("Error")." : ".$langs->trans("ErrorProductAlreadyExists",$this->ref);
else $this->error=$langs->trans("Error")." : ".$langs->trans("ErrorProductBarCodeAlreadyExists",$this->barcode);
$this->errors[]=$this->error;
Expand Down Expand Up @@ -1119,7 +1122,7 @@ function delete(User $user, $notrigger=0)
}

//We also check if it is a child product
if (!$error && ($prodcomb->fetchByFkProductChild($id) > 0) && ($prodcomb->delete() < 0)) {
if (!$error && ($prodcomb->fetchByFkProductChild($id) > 0) && ($prodcomb->delete($user) < 0)) {
$error++;
$this->errors[] = 'Error deleting child combination';
}
Expand Down
12 changes: 9 additions & 3 deletions htdocs/variants/class/ProductAttribute.class.php
Expand Up @@ -224,6 +224,8 @@ public function countChildProducts()
*/
protected function reorderLines()
{
global $user;

$tmp_order = array();

$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'product_attribute WHERE rang = 0';
Expand All @@ -244,7 +246,7 @@ protected function reorderLines()
$tmp->fetch($rowid);
$tmp->rang = $order+1;

if ($tmp->update() < 0) {
if ($tmp->update($user) < 0) {
return -1;
}
}
Expand All @@ -260,6 +262,8 @@ protected function reorderLines()
*/
private function moveLine($type)
{
global $user;

if ($this->reorderLines() < 0) {
return -1;
}
Expand All @@ -281,7 +285,7 @@ private function moveLine($type)

$this->rang = $newrang;

if ($this->update() < 0) {
if ($this->update($user) < 0) {
$this->db->rollback();
return -1;
}
Expand Down Expand Up @@ -319,6 +323,8 @@ public function moveDown()
*/
public static function bulkUpdateOrder(DoliDB $db, array $order)
{
global $user;

$tmp = new ProductAttribute($db);

foreach ($order as $key => $attrid) {
Expand All @@ -328,7 +334,7 @@ public static function bulkUpdateOrder(DoliDB $db, array $order)

$tmp->rang = $key;

if ($tmp->update() < 0) {
if ($tmp->update($user) < 0) {
return -1;
}
}
Expand Down
41 changes: 26 additions & 15 deletions htdocs/variants/class/ProductCombination.class.php
Expand Up @@ -199,9 +199,10 @@ public function countNbOfCombinationForFkProductParent($fk_product_parent)
/**
* Creates a product attribute combination
*
* @return int
* @param User $user Object user
* @return int <0 if KO, >0 if OK
*/
public function create()
public function create($user)
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_attribute_combination
(fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight, entity)
Expand All @@ -223,9 +224,10 @@ public function create()
/**
* Updates a product combination
*
* @return int <0 KO, >0 OK
* @param User $user Object user
* @return int <0 KO, >0 OK
*/
public function update()
public function update(User $user)
{
$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.",
Expand Down Expand Up @@ -370,9 +372,9 @@ public function updateProperties(Product $parent)
/**
* Retrieves the combination that matches the given features.
*
* @param int $prodid Id of parent product
* @param array $features Format: [$attr] => $attr_val
* @return false|ProductCombination false if not found
* @param int $prodid Id of parent product
* @param array $features Format: [$attr] => $attr_val
* @return false|ProductCombination False if not found
*/
public function fetchByProductCombination2ValuePairs($prodid, array $features)
{
Expand Down Expand Up @@ -507,7 +509,7 @@ public function createProductCombination(Product $product, array $combinations,
} else {
$newcomb->fk_product_parent = $product->id;

if ($newcomb->create() < 0) {
if ($newcomb->create($user) < 0) { // Create 1 entry into product_attribute_combination (1 entry for all combinations)
$db->rollback();
return -1;
}
Expand All @@ -516,6 +518,8 @@ public function createProductCombination(Product $product, array $combinations,
$prodattr = new ProductAttribute($db);
$prodattrval = new ProductAttributeValue($db);

// $combination contains list of attributes pairs key->value. Example: array('id Color'=>id Blue, 'id Size'=>id Small, 'id Option'=>id val a, ...)
//var_dump($combinations);
foreach ($combinations as $currcombattr => $currcombval) {

//This was checked earlier, so no need to double check
Expand All @@ -529,7 +533,7 @@ public function createProductCombination(Product $product, array $combinations,
$tmp->fk_prod_attr_val = $currcombval;
$tmp->fk_prod_combination = $newcomb->id;

if ($tmp->create() < 0) {
if ($tmp->create($user) < 0) { // Create 1 entry into product_attribute_combination2val
$db->rollback();
return -1;
}
Expand Down Expand Up @@ -567,8 +571,14 @@ public function createProductCombination(Product $product, array $combinations,
$newproduct->price_min = 0;
$newproduct->price_min_ttc = 0;

if ($newproduct->create($user) < 0) {
// A new variant must use a new barcode (not same product)
$newproduct->barcode = -1;

// Now create the product
//print 'Create prod '.$newproduct->ref.'<br>'."\n";
$newprodid = $newproduct->create($user);
if ($newprodid < 0)
{
//In case the error is not related with an already existing product
if ($newproduct->error != 'ErrorProductAlreadyExists') {
$this->error[] = $newproduct->error;
Expand Down Expand Up @@ -611,7 +621,8 @@ public function createProductCombination(Product $product, array $combinations,

$newcomb->fk_product_child = $newproduct->id;

if ($newcomb->update() < 0) {
if ($newcomb->update($user) < 0)
{
$db->rollback();
return -1;
}
Expand All @@ -636,11 +647,10 @@ public function copyAll($origProductId, Product $destProduct)
return -1;
}

$prodcomb = new ProductCombination($this->db);
$prodcomb2val = new ProductCombination2ValuePair($this->db);

//Retrieve all product combinations
$combinations = $prodcomb->fetchAllByFkProductParent($origProductId);
$combinations = $this->fetchAllByFkProductParent($origProductId);

foreach ($combinations as $combination) {

Expand All @@ -650,14 +660,15 @@ public function copyAll($origProductId, Product $destProduct)
$variations[$tmp_pc2v->fk_prod_attr] = $tmp_pc2v->fk_prod_attr_val;
}

if (self::createProductCombination(
if ($this->createProductCombination(
$destProduct,
$variations,
array(),
$combination->variation_price_percentage,
$combination->variation_price,
$combination->variation_weight
) < 0) {
) < 0)
{
return -1;
}
}
Expand Down

0 comments on commit ae90627

Please sign in to comment.