items import 5
This commit is contained in:
parent
cecfc338ef
commit
52e805d9d1
53
index.php
53
index.php
@ -1178,19 +1178,31 @@ function getPromotionalPrice($item) {
|
||||
}
|
||||
|
||||
if (isset($_POST['import_items'])) {
|
||||
error_log("Import items triggered. POST: " . print_r($_POST, true));
|
||||
if (isset($_FILES['excel_file']) && $_FILES['excel_file']['error'] === 0) {
|
||||
$tmpPath = $_FILES['excel_file']['tmp_name'];
|
||||
error_log("File uploaded to: $tmpPath");
|
||||
$firstBytes = file_get_contents($tmpPath, false, null, 0, 4);
|
||||
if ($firstBytes === "PK\x03\x04") {
|
||||
$message = "Error: It looks like you uploaded an Excel (.xlsx) file. Please save it as CSV (UTF-8) and try again.";
|
||||
} else {
|
||||
$handle = fopen($tmpPath, "r");
|
||||
// Check for BOM and skip it
|
||||
if (substr($firstBytes, 0, 3) === "\xEF\xBB\xBF") {
|
||||
$handle = fopen($tmpPath, "r");
|
||||
fseek($handle, 3);
|
||||
} else {
|
||||
$handle = fopen($tmpPath, "r");
|
||||
}
|
||||
|
||||
$firstLine = fgets($handle);
|
||||
rewind($handle);
|
||||
if (substr($firstBytes, 0, 3) === "\xEF\xBB\xBF") fseek($handle, 3);
|
||||
|
||||
error_log("First line of CSV: " . $firstLine);
|
||||
|
||||
$seps = [",", ";", "\t", "|"];
|
||||
$sep = ",";
|
||||
$maxCount = -1;
|
||||
$maxCount = 0;
|
||||
foreach ($seps as $s) {
|
||||
$count = substr_count($firstLine, $s);
|
||||
if ($count > $maxCount) {
|
||||
@ -1198,10 +1210,26 @@ function getPromotionalPrice($item) {
|
||||
$sep = $s;
|
||||
}
|
||||
}
|
||||
error_log("Detected separator: '$sep' (count: $maxCount)");
|
||||
|
||||
// Try to detect if first line is header
|
||||
$firstRow = fgetcsv($handle, 0, $sep);
|
||||
$isHeader = true;
|
||||
// If the first row's 4th or 5th columns are numeric, it's probably NOT a header
|
||||
if (isset($firstRow[3]) && is_numeric(str_replace(',', '', trim($firstRow[3])))) $isHeader = false;
|
||||
if (isset($firstRow[4]) && is_numeric(str_replace(',', '', trim($firstRow[4])))) $isHeader = false;
|
||||
|
||||
if (!$isHeader) {
|
||||
rewind($handle);
|
||||
if (substr($firstBytes, 0, 3) === "\xEF\xBB\xBF") fseek($handle, 3);
|
||||
error_log("No header detected, starting from first row.");
|
||||
} else {
|
||||
error_log("Header detected and skipped: " . print_r($firstRow, true));
|
||||
}
|
||||
|
||||
fgetcsv($handle, 0, $sep); // Skip header
|
||||
$count = 0; $errors = 0;
|
||||
while (($data = fgetcsv($handle, 0, $sep)) !== FALSE) {
|
||||
if (count($data) < 2) continue; // Skip empty or single-column rows that aren't SKUs
|
||||
if (empty($data[0]) && empty($data[1]) && empty($data[2])) continue;
|
||||
try {
|
||||
foreach ($data as &$val) {
|
||||
@ -1214,28 +1242,33 @@ function getPromotionalPrice($item) {
|
||||
}
|
||||
|
||||
// Map: 0:sku, 1:name_en, 2:name_ar, 3:sale_price, 4:cost_price
|
||||
$sku = $data[0] ?? '';
|
||||
$sku = substr(trim($data[0] ?? ''), 0, 100);
|
||||
$name_en = $data[1] ?? '';
|
||||
$name_ar = $data[2] ?? '';
|
||||
$sale_price = (float)($data[3] ?? 0);
|
||||
$purchase_price = (float)($data[4] ?? 0);
|
||||
$sale_price = str_replace(',', '', $data[3] ?? 0);
|
||||
$purchase_price = str_replace(',', '', $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)
|
||||
$stmt = 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([$sku, $name_en, $name_ar, $sale_price, $purchase_price]);
|
||||
sale_price=VALUES(sale_price), purchase_price=VALUES(purchase_price)");
|
||||
$stmt->execute([$sku, $name_en, $name_ar, (float)$sale_price, (float)$purchase_price]);
|
||||
$count++;
|
||||
} catch (Throwable $e) {
|
||||
error_log("Import error: " . $e->getMessage());
|
||||
error_log("Import row error: " . $e->getMessage() . " Data: " . print_r($data, true));
|
||||
$errors++;
|
||||
}
|
||||
}
|
||||
fclose($handle);
|
||||
$message = "Import completed! $count items processed." . ($errors > 0 ? " ($errors rows skipped due to data errors.)" : "");
|
||||
error_log("Import finished. Processed: $count, Errors: $errors");
|
||||
}
|
||||
} else {
|
||||
$errCode = $_FILES['excel_file']['error'] ?? 'no file';
|
||||
error_log("File upload failed or missing. Error code: $errCode");
|
||||
$message = "Error: File upload failed. (Error code: $errCode)";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user