beginTransaction(); try { $stmt = db()->prepare("INSERT INTO suppliers (tenant_id, name, type, contact_info) VALUES (?, ?, ?, ?)"); while (($row = fgetcsv($handle)) !== false) { $rowCount++; $data = []; foreach ($expected as $col) { $data[$col] = isset($columnMap[$col]) && isset($row[$columnMap[$col]]) ? trim($row[$columnMap[$col]]) : ''; } if (empty($data['name'])) { $skippedCount++; $results[] = "Row $rowCount: Skipped (Missing Name)"; continue; } // Default type if invalid if (!in_array($data['type'], ['supplier', 'contractor'])) { $data['type'] = 'supplier'; } $stmt->execute([ 1, // Hardcoded tenant_id $data['name'], $data['type'], $data['contact_info'] ]); $importCount++; } db()->commit(); $message = "Import completed successfully. $importCount suppliers imported, $skippedCount skipped."; } catch (Exception $e) { db()->rollBack(); $error = 'Database error: ' . $e->getMessage(); } } fclose($handle); } } include 'includes/header.php'; ?>