diff --git a/import.php b/import.php index d7e6143..bf7085a 100755 --- a/import.php +++ b/import.php @@ -1881,9 +1881,10 @@ if ($action == 'previewdatanorm' && $id > 0) { // Load existing product's copper surcharge for price comparison $productCopperSurcharge = 0; + $currentSupplierPriceId = 0; if ($existingProductId > 0) { - $sqlExisting = "SELECT fk_soc, unitprice FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; - $sqlExisting .= " WHERE fk_product = ".(int)$existingProductId; + $sqlExisting = "SELECT pf.rowid, pf.fk_soc, pf.unitprice FROM ".MAIN_DB_PREFIX."product_fournisseur_price pf"; + $sqlExisting .= " WHERE pf.fk_product = ".(int)$existingProductId; $resExisting = $db->query($sqlExisting); if ($resExisting) { while ($objEx = $db->fetch_object($resExisting)) { @@ -1891,18 +1892,21 @@ if ($action == 'previewdatanorm' && $id > 0) { // Load current invoice supplier's Dolibarr price for comparison if ($objEx->fk_soc == $import->fk_soc) { $currentDolibarrPrice = (float)$objEx->unitprice; + $currentSupplierPriceId = $objEx->rowid; } } } - // Load copper surcharge from product extrafields - $sqlCopper = "SELECT kupferzuschlag FROM ".MAIN_DB_PREFIX."product_extrafields"; - $sqlCopper .= " WHERE fk_object = ".(int)$existingProductId; - $resCopper = $db->query($sqlCopper); - if ($resCopper && $db->num_rows($resCopper) > 0) { - $objCopper = $db->fetch_object($resCopper); - if (!empty($objCopper->kupferzuschlag) && $objCopper->kupferzuschlag > 0) { - $productCopperSurcharge = (float)$objCopper->kupferzuschlag; + // Load copper surcharge from supplier price extrafields (not product extrafields!) + if ($currentSupplierPriceId > 0) { + $sqlCopper = "SELECT kupferzuschlag FROM ".MAIN_DB_PREFIX."product_fournisseur_price_extrafields"; + $sqlCopper .= " WHERE fk_object = ".(int)$currentSupplierPriceId; + $resCopper = $db->query($sqlCopper); + if ($resCopper && $db->num_rows($resCopper) > 0) { + $objCopper = $db->fetch_object($resCopper); + if (!empty($objCopper->kupferzuschlag) && $objCopper->kupferzuschlag > 0) { + $productCopperSurcharge = (float)$objCopper->kupferzuschlag; + } } } } @@ -1916,15 +1920,19 @@ if ($action == 'previewdatanorm' && $id > 0) { $altSupplier = new Societe($db); $altSupplier->fetch($altResult['fk_soc']); - // Datanorm base price (per unit) - $altPurchasePrice = $altResult['price']; - if ($altResult['price_unit'] > 1) { - $altPurchasePrice = $altResult['price'] / $altResult['price_unit']; - } + // Calculate unit price from Datanorm + $priceUnit = ($altResult['price_unit'] > 0) ? $altResult['price_unit'] : 1; + $materialPrice = $altResult['price']; - // Add copper surcharge from existing product for comparison - // Datanorm has only material price, not copper surcharge - $altPurchasePriceWithCopper = $altPurchasePrice + $productCopperSurcharge; + // Datanorm has NO metal surcharge - use copper surcharge from Dolibarr supplier price instead + // $productCopperSurcharge is already loaded from extrafields above + $metalSurcharge = $productCopperSurcharge; + + // Total price for price unit (e.g. for 100 pieces) + $totalPriceForUnit = $materialPrice + $metalSurcharge; + + // Unit price (price per 1 piece/meter) + $altPurchasePrice = $totalPriceForUnit / $priceUnit; $supplierAlternatives[] = array( 'datanorm_id' => $altResult['id'], @@ -1934,9 +1942,10 @@ if ($action == 'previewdatanorm' && $id > 0) { 'short_text1' => $altResult['short_text1'], 'price' => $altResult['price'], 'price_unit' => $altResult['price_unit'], - 'purchase_price' => $altPurchasePriceWithCopper, - 'datanorm_base_price' => $altPurchasePrice, // Without copper - 'copper_surcharge' => $productCopperSurcharge, + 'effective_price_unit' => $priceUnit, // Actual divisor used + 'purchase_price' => $altPurchasePrice, // Calculated unit price (Datanorm + Copper) + 'datanorm_base_price' => $materialPrice / $priceUnit, // Material only per unit + 'metal_surcharge' => $metalSurcharge / $priceUnit, // Copper surcharge per unit (from Dolibarr) 'ean' => $altResult['ean'], 'manufacturer_ref' => $altResult['manufacturer_ref'], 'is_invoice_supplier' => ($altResult['fk_soc'] == $import->fk_soc), @@ -2590,18 +2599,20 @@ if ($action == 'edit' && $import->id > 0) { // Fehlende Lieferantenpreise aus anderen Katalogen sammeln (Anzeige weiter unten) if ($import->fk_soc > 0 && getDolGlobalString('IMPORTZUGFERD_DATANORM_SEARCH_ALL')) { // Alle vorhandenen Lieferantenpreise fuer dieses Produkt laden - $sqlExistingPrices = "SELECT fk_soc, price, unitprice, barcode FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; + $sqlExistingPrices = "SELECT fk_soc, price, unitprice, quantity, barcode FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; $sqlExistingPrices .= " WHERE fk_product = ".(int)$line->fk_product; $resExistingPrices = $db->query($sqlExistingPrices); $existingSupplierIds = array(); $currentSupplierPrice = 0; + $currentSupplierQty = 1; $supplierEan = ''; if ($resExistingPrices) { while ($objPrice = $db->fetch_object($resExistingPrices)) { $existingSupplierIds[$objPrice->fk_soc] = true; - // Stueckpreis und EAN vom Rechnungslieferanten merken + // Stueckpreis, Mindestmenge und EAN vom Rechnungslieferanten merken if ($objPrice->fk_soc == $import->fk_soc) { $currentSupplierPrice = $objPrice->unitprice; + $currentSupplierQty = max(1, $objPrice->quantity); if (!empty($objPrice->barcode)) { $supplierEan = $objPrice->barcode; } @@ -2613,10 +2624,12 @@ if ($action == 'edit' && $import->id > 0) { $datanormSearch = new Datanorm($db); $allCatalogResults = array(); - // Load copper surcharge from product extrafields for price comparison + // Load copper surcharge from current supplier price extrafields for price comparison $productCopperSurcharge = 0; - $sqlCopper = "SELECT kupferzuschlag FROM ".MAIN_DB_PREFIX."product_extrafields"; - $sqlCopper .= " WHERE fk_object = ".(int)$line->fk_product; + $sqlCopper = "SELECT pfe.kupferzuschlag FROM ".MAIN_DB_PREFIX."product_fournisseur_price pf"; + $sqlCopper .= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price_extrafields pfe ON pfe.fk_object = pf.rowid"; + $sqlCopper .= " WHERE pf.fk_product = ".(int)$line->fk_product; + $sqlCopper .= " AND pf.fk_soc = ".(int)$import->fk_soc; $resCopper = $db->query($sqlCopper); if ($resCopper && $db->num_rows($resCopper) > 0) { $objCopper = $db->fetch_object($resCopper); @@ -2638,15 +2651,19 @@ if ($action == 'edit' && $import->id > 0) { $altSupplier = new Societe($db); $altSupplier->fetch($catalogResult['fk_soc']); - // Datanorm base price (per unit) - $altPurchasePrice = $catalogResult['price']; - if ($catalogResult['price_unit'] > 1) { - $altPurchasePrice = $catalogResult['price'] / $catalogResult['price_unit']; - } + // Calculate unit price from Datanorm (same logic as supplier_alternatives) + $priceUnit = ($catalogResult['price_unit'] > 0) ? $catalogResult['price_unit'] : 1; + $materialPrice = $catalogResult['price']; - // Add copper surcharge from existing product for comparison - // Datanorm has only material price, not copper surcharge - $altPurchasePriceWithCopper = $altPurchasePrice + $productCopperSurcharge; + // Datanorm has NO metal surcharge - use copper surcharge from Dolibarr supplier price + // $productCopperSurcharge is already loaded from extrafields above + $metalSurcharge = $productCopperSurcharge; + + // Total price for price unit + $totalPriceForUnit = $materialPrice + $metalSurcharge; + + // Unit price (price per 1 piece/meter) + $altPurchasePrice = $totalPriceForUnit / $priceUnit; $missingSuppliers[] = array( 'datanorm_id' => $catalogResult['id'], @@ -2655,9 +2672,10 @@ if ($action == 'edit' && $import->id > 0) { 'article_number' => $catalogResult['article_number'], 'price' => $catalogResult['price'], 'price_unit' => $catalogResult['price_unit'], - 'purchase_price' => $altPurchasePriceWithCopper, - 'datanorm_base_price' => $altPurchasePrice, // Without copper - 'copper_surcharge' => $productCopperSurcharge, + 'effective_price_unit' => $priceUnit, + 'purchase_price' => $altPurchasePrice, // Calculated unit price (Datanorm + Copper) + 'datanorm_base_price' => $materialPrice / $priceUnit, // Material only per unit + 'metal_surcharge' => $metalSurcharge / $priceUnit, // Copper surcharge per unit (from Dolibarr) 'ean' => $catalogResult['ean'], ); } @@ -2680,8 +2698,13 @@ if ($action == 'edit' && $import->id > 0) { print '
'.dol_escape_htmltag($missing['article_number']).'';
- print ' @ '.price($missing['purchase_price']).'';
+ print ' @ '.price($totalPriceForUnit);
+ if ($priceUnit > 1) {
+ print '/'.$priceUnit;
+ }
+ print '';
+ if ($priceUnit > 1) {
+ print ' ('.price($missing['purchase_price']).'/Stk.)';
+ }
print $priceDiffHtml;
+ // Show current Dolibarr price for comparison
+ if ($currentSupplierPrice > 0) {
+ $currentTotalPrice = $currentSupplierPrice * $currentSupplierQty;
+ print ' | Aktuell: '.price($currentTotalPrice);
+ if ($currentSupplierQty > 1) {
+ print '/'.$currentSupplierQty;
+ }
+ print '';
+ }
print '';
print '