Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog DynamicsPrices

## 2.0.1
- Correction du déclenchement des recalculs de prix lors des événements de prix d'achat/vente quand l'identifiant produit n'est pas directement porté par l'objet trigger. / Fixed price recalculation trigger execution on buy/sell price events when the product identifier is not directly available on the trigger object.

## 2.0.0
- Ajout du support des kits : le prix d'un kit est recalculé après ses composants pour éviter les doublons et refléter le coût cumulé. / Added kit support: a kit price is recalculated after its components to avoid duplicates and reflect the cumulative cost.
- Correction des mises à jour intempestives des services : seuls les produits physiques sont recalculés (`fk_product_type = 0`). / Fixed unintended service updates: only physical products are recalculated (`fk_product_type = 0`).
Expand Down
4 changes: 2 additions & 2 deletions core/modules/modDynamicsPrices.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public function __construct($db)
$this->editor_url = 'lesmetiersdubatiment.fr'; // Must be an external online web site
$this->editor_squarred_logo = 'logo.png@dynamicsprices'; // Must be image filename into the module/img directory followed with @modulename. Example: 'myimage.png@dynamicsprices'

// Possible values for version are: 'development', 'experimental', 'dolibarr', 'dolibarr_deprecated', 'experimental_deprecated' or a version string like 'x.y.z'
$this->version = '2.0.0';
// Possible values for version are: 'development', 'experimental', 'dolibarr', 'dolibarr_deprecated', 'experimental_deprecated' or a version string like 'x.y.z'
$this->version = '2.0.1';
// Url to the file with your last numberversion of this module
//$this->url_last_version = 'http://www.example.com/versionmodule.txt';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,17 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
require_once __DIR__.'/../../lib/dynamicsprices.lib.php';
$updateFunction = getDolGlobalString('LMDB_COST_PRICE_ONLY') ? 'update_customer_prices_from_cost_price' : 'update_customer_prices_from_suppliers';
$affectedActions = array('SUPPLIER_PRODUCT_BUYPRICE_CREATE', 'SUPPLIER_PRODUCT_BUYPRICE_MODIFY', 'SUPPLIER_PRODUCT_BUYPRICE_DELETE', 'PRODUCT_MODIFY', 'PRODUCT_CREATE', 'PRODUCT_CLONE', 'PRODUCT_PRICE_CREATE', 'PRODUCT_PRICE_MODIFY', 'PRODUCT_PRICE_DELETE', 'PRODUCT_BUYPRICE_CREATE', 'PRODUCT_BUYPRICE_MODIFY', 'PRODUCT_BUYPRICE_DELETE', 'PRODUCT_SUBPRODUCT_ADD', 'PRODUCT_SUBPRODUCT_UPDATE', 'PRODUCT_SUBPRODUCT_DELETE');
//var_dump($updateFunction);
//var_dump($action);
if (getDolGlobalString('LMDB_SUPPLIER_BUYPRICE_ALTERED') && in_array($action, $affectedActions, true)) {
dol_include_once('/product/class/product.class.php');
$productId = !empty($object->fk_product) ? $object->fk_product : (isset($object->id) ? $object->id : 0);
$product = new Product($db);
if ($productId > 0 && $product->fetch($productId) > 0) {
if ((int) $product->type !== Product::TYPE_PRODUCT) {
return 0;
}
}
if ($productId > 0) {
call_user_func($updateFunction, $db, $user, $langs, $conf, $productId);
if (getDolGlobalString('LMDB_SUPPLIER_BUYPRICE_ALTERED') && in_array($action, $affectedActions, true)) {
dol_include_once('/product/class/product.class.php');
$productId = $this->resolveProductIdFromTriggerAction($db, $action, $object);
$product = new Product($db);
if ($productId > 0 && $product->fetch($productId) > 0) {
if ((int) $product->type !== Product::TYPE_PRODUCT) {
return 0;
}
}
if ($productId > 0) {
call_user_func($updateFunction, $db, $user, $langs, $conf, $productId);
$parentKits = dynamicsprices_get_parent_kits($db, $productId);
foreach ($parentKits as $kitId) {
call_user_func($updateFunction, $db, $user, $langs, $conf, $kitId);
Expand Down Expand Up @@ -335,4 +333,56 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf

return 0;
}

/**
* Resolve related product id from trigger context.
*
* @param DoliDB $db Database handler
* @param string $action Trigger action
* @param CommonObject $object Trigger object
* @return int
*/
private function resolveProductIdFromTriggerAction($db, $action, $object)
{
if (!empty($object->fk_product)) {
return (int) $object->fk_product;
}

if (in_array($action, array('PRODUCT_CREATE', 'PRODUCT_MODIFY', 'PRODUCT_CLONE', 'PRODUCT_SUBPRODUCT_ADD', 'PRODUCT_SUBPRODUCT_UPDATE', 'PRODUCT_SUBPRODUCT_DELETE'), true) && !empty($object->id)) {
return (int) $object->id;
}

if (in_array($action, array('PRODUCT_PRICE_CREATE', 'PRODUCT_PRICE_MODIFY', 'PRODUCT_PRICE_DELETE'), true) && !empty($object->id)) {
$sql = "SELECT fk_product FROM ".MAIN_DB_PREFIX."product_price WHERE rowid = ".((int) $object->id);
$resql = $db->query($sql);
if ($resql) {
$obj = $db->fetch_object($resql);
if (!empty($obj->fk_product)) {
return (int) $obj->fk_product;
}
}
}

if (in_array($action, array('SUPPLIER_PRODUCT_BUYPRICE_CREATE', 'SUPPLIER_PRODUCT_BUYPRICE_MODIFY', 'SUPPLIER_PRODUCT_BUYPRICE_DELETE', 'PRODUCT_BUYPRICE_CREATE', 'PRODUCT_BUYPRICE_MODIFY', 'PRODUCT_BUYPRICE_DELETE'), true)) {
$priceRowId = 0;
if (!empty($object->product_fourn_price_id)) {
$priceRowId = (int) $object->product_fourn_price_id;
} elseif (!empty($object->id)) {
$priceRowId = (int) $object->id;
}

if ($priceRowId > 0) {
$sql = "SELECT fk_product FROM ".MAIN_DB_PREFIX."product_fournisseur_price WHERE rowid = ".$priceRowId;
$resql = $db->query($sql);
if ($resql) {
$obj = $db->fetch_object($resql);
if (!empty($obj->fk_product)) {
return (int) $obj->fk_product;
}
}
}
}

return 0;
}
}
Loading