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 {