diff --git a/index.php b/index.php index 36aa69c..04c4434 100644 --- a/index.php +++ b/index.php @@ -1188,9 +1188,16 @@ function getPromotionalPrice($item) { $firstLine = fgets($handle); rewind($handle); + $seps = [",", ";", "\t", "|"]; $sep = ","; - if (strpos($firstLine, ";") !== false && strpos($firstLine, ",") === false) $sep = ";"; - elseif (strpos($firstLine, "\t") !== false) $sep = "\t"; + $maxCount = -1; + foreach ($seps as $s) { + $count = substr_count($firstLine, $s); + if ($count > $maxCount) { + $maxCount = $count; + $sep = $s; + } + } fgetcsv($handle, 0, $sep); // Skip header $count = 0; $errors = 0; @@ -1203,14 +1210,26 @@ function getPromotionalPrice($item) { $val = mb_convert_encoding($val, 'UTF-8', 'Windows-1252'); } $val = mb_convert_encoding($val, 'UTF-8', 'UTF-8'); + $val = trim($val); } + + // Map: 0:sku, 1:name_en, 2:name_ar, 3:sale_price, 4:cost_price + $sku = $data[0] ?? ''; + $name_en = $data[1] ?? ''; + $name_ar = $data[2] ?? ''; + $sale_price = (float)($data[3] ?? 0); + $purchase_price = (float)($data[4] ?? 0); + + if (empty($sku) && empty($name_en)) continue; + db()->prepare("INSERT INTO stock_items (sku, name_en, name_ar, sale_price, purchase_price) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name_en=VALUES(name_en), name_ar=VALUES(name_ar), sale_price=VALUES(sale_price), purchase_price=VALUES(purchase_price)") - ->execute([$data[0] ?? '', $data[1] ?? '', $data[2] ?? '', (float)($data[3] ?? 0), (float)($data[4] ?? 0)]); + ->execute([$sku, $name_en, $name_ar, $sale_price, $purchase_price]); $count++; } catch (Throwable $e) { + error_log("Import error: " . $e->getMessage()); $errors++; } }