fix: Totalpreis statt Stückpreis für Mindestmengen laden (v4.3)

Problem:
- Code lud unitprice (0,08€) statt price (0,94€)
- Berechnete dann 0,08€ × 12 = 0,96€ statt direkt 0,94€
- Führte zu Rundungsfehlern

Lösung - RICHTIGE Daten aus DB:
- price: Totalpreis für Mindestmenge (0,94€)
- quantity: Mindestmenge (12)
- unitprice: Wird berechnet, nicht direkt verwendet

Beispiel Lüsterklemme:
- Vorher: unitprice=0,08€ × 12 = 0,96€ (Rundungsfehler!)
- Nachher: price=0,94€ für 12 (korrekt!)

MERKEN: IMMER price + quantity holen, NICHT unitprice alleine!

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Eduard Wisch 2026-03-03 08:57:46 +01:00
parent bfcc5c66db
commit 87931e981e

View file

@ -1893,8 +1893,9 @@ if ($action == 'previewdatanorm' && $id > 0) {
$productCopperSurcharge = 0;
$currentSupplierPriceId = 0;
$currentSupplierMinQty = 1;
$currentSupplierTotalPrice = 0;
if ($existingProductId > 0) {
$sqlExisting = "SELECT pf.rowid, pf.fk_soc, pf.unitprice, pf.quantity FROM ".MAIN_DB_PREFIX."product_fournisseur_price pf";
$sqlExisting = "SELECT pf.rowid, pf.fk_soc, pf.price, pf.quantity FROM ".MAIN_DB_PREFIX."product_fournisseur_price pf";
$sqlExisting .= " WHERE pf.fk_product = ".(int)$existingProductId;
$resExisting = $db->query($sqlExisting);
if ($resExisting) {
@ -1902,9 +1903,10 @@ if ($action == 'previewdatanorm' && $id > 0) {
$existingPriceSuppliers[$objEx->fk_soc] = true;
// Load current invoice supplier's Dolibarr price for comparison
if ($objEx->fk_soc == $import->fk_soc) {
$currentDolibarrPrice = (float)$objEx->unitprice;
$currentSupplierPriceId = $objEx->rowid;
$currentSupplierTotalPrice = (float)$objEx->price;
$currentSupplierMinQty = max(1, $objEx->quantity);
$currentDolibarrPrice = $currentSupplierMinQty > 0 ? $currentSupplierTotalPrice / $currentSupplierMinQty : $currentSupplierTotalPrice;
$currentSupplierPriceId = $objEx->rowid;
}
}
}
@ -1979,6 +1981,7 @@ if ($action == 'previewdatanorm' && $id > 0) {
'datanorm_price_unit' => $datanorm->price_unit,
'datanorm_ean' => $datanorm->ean,
'purchase_price' => $currentDolibarrPrice, // Current Dolibarr unit price
'purchase_total_price' => $currentSupplierTotalPrice, // Dolibarr total price for min qty
'purchase_min_qty' => $currentSupplierMinQty, // Dolibarr minimum quantity
'purchase_copper_surcharge' => $productCopperSurcharge, // Dolibarr copper surcharge (for min qty)
'datanorm_purchase_price' => $purchasePrice, // New Datanorm unit price
@ -3035,9 +3038,9 @@ if ($action == 'edit' && $import->id > 0) {
// Current Dolibarr price for minimum quantity
print '<td class="right nowraponall">';
if (!empty($match['purchase_price']) && $match['purchase_price'] > 0) {
if (!empty($match['purchase_total_price']) && $match['purchase_total_price'] > 0) {
$dolibarrMinQty = !empty($match['purchase_min_qty']) ? $match['purchase_min_qty'] : 1;
$dolibarrMaterialTotal = $match['purchase_price'] * $dolibarrMinQty;
$dolibarrMaterialTotal = $match['purchase_total_price'];
$dolibarrCopper = !empty($match['purchase_copper_surcharge']) ? $match['purchase_copper_surcharge'] : 0;
// Show breakdown if copper exists