From 41a515d9fcc8a6fa1501ea18eeaf5f69caefa40f Mon Sep 17 00:00:00 2001 From: w3houde Date: Sun, 14 Jun 2026 00:57:56 +0200 Subject: [PATCH] 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 Co-authored-by: Dolicraft --- htdocs/fourn/class/fournisseur.commande.class.php | 15 +++++++++++++++ htdocs/product/class/product.class.php | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/htdocs/fourn/class/fournisseur.commande.class.php b/htdocs/fourn/class/fournisseur.commande.class.php index 70b77f8c6da..38fb90e959b 100644 --- a/htdocs/fourn/class/fournisseur.commande.class.php +++ b/htdocs/fourn/class/fournisseur.commande.class.php @@ -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'); + } } } diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 0ff1cf98a63..18272e87df3 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -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 {