Fix #38783 enforce supplier qty_min on PRODUCT_USE_SUPPLIER_PACKAGING rounding (#38799)

CommandeFournisseur::addline rounds the line quantity to the next packaging
multiple but never enforces pfp.quantity (the supplier minimum purchase
quantity). A line with packaging=3 and qty_min=15 could therefore be saved
at qty=6 (only multiple of packaging) - the user-facing scenario from
#38783 with the table of expected vs actual values.

Two small changes:

1. htdocs/product/class/product.class.php get_buyprice() already maintains
   $prod_supplier->fourn_qty for the dynamic-price code path at lines 2622
   and 2691 but never propagates the same value to $this->fourn_qty (which
   is the public property documented as MOQ at line 738). Assign $this->fourn_qty
   in both branches of get_buyprice so callers can read it after the call.

2. htdocs/fourn/class/fournisseur.commande.class.php addline now applies a
   second rounding pass against $prod->fourn_qty after the packaging multiple
   step. If qty is still below qty_min, round qty_min up to the next packaging
   multiple - the smallest qty that satisfies both constraints simultaneously.

Signed-off-by: Dolicraft <contact@dolicraft.com>
Co-authored-by: Dolicraft <contact@dolicraft.com>
This commit is contained in:
w3houde 2026-06-14 00:57:56 +02:00 committed by GitHub
parent 718ad088a2
commit 41a515d9fc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 0 deletions

View file

@ -2162,6 +2162,21 @@ class CommandeFournisseur extends CommonOrder
setEventMessages($langs->trans('QtyRecalculatedWithPackaging'), null, 'warnings');
}
}
// Enforce the supplier minimum purchase quantity on top of packaging
// rounding. The packaging block above only ensures qty is a packaging
// multiple, not that it satisfies pfp.quantity (=qty_min). If the line
// is below the supplier minimum, round qty_min itself up to the next
// packaging multiple so we end up with the smallest valid order qty (#38783).
if (!empty($prod->fourn_qty) && abs((float) $qty) < (float) $prod->fourn_qty) {
if (!empty($prod->packaging) && (float) price2num(fmod((float) $prod->fourn_qty, (float) $prod->packaging), 'MS')) {
$coeff = intval((float) $prod->fourn_qty / (float) $prod->packaging) + 1;
$qty = (float) price2num((float) $prod->packaging * $coeff, 'MS');
} else {
$qty = (float) $prod->fourn_qty;
}
setEventMessages($langs->trans('QtyRecalculatedWithPackaging'), null, 'mesgs');
}
}
}

View file

@ -2644,6 +2644,11 @@ class Product extends CommonObject
if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
$this->packaging = (float) $obj->packaging;
}
// Expose the supplier minimum purchase quantity so callers can enforce qty_min
// on top of packaging-multiple rounding (#38783). pfp.quantity is the lowest
// qty for which this price line is valid; for "below-min" callers this is the
// floor they must round up to.
$this->fourn_qty = $obj->quantity;
$result = $obj->fk_product;
return $result;
} else { // If not found
@ -2709,6 +2714,9 @@ class Product extends CommonObject
if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
$this->packaging = (float) $obj->packaging;
}
// Expose pfp.quantity so callers can enforce qty_min on top of packaging
// rounding (#38783).
$this->fourn_qty = $obj->quantity;
$result = $obj->fk_product;
return $result;
} else {