Repository navigation
Expand file tree
/
Copy pathimport-excel.mjs
More file actions
284 lines (256 loc) · 10 KB
/
Copy pathimport-excel.mjs
File metadata and controls
284 lines (256 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// OpenProcess — Excel Import Tool
// Kullanım: node import-excel.mjs dosya.xlsx
//
// Müşterinin mevcut Excel kayıtlarını OpenProcess veritabanına aktarır.
// Beklenen Excel kolonları:
// Tarih | Plaka | Müşteri Adı | Telefon | Araç Marka | Araç Model | Renk | Yapılan İşlem | Ücret | Ödeme Şekli | Notlar
import pg from "pg";
import dotenv from "dotenv";
dotenv.config();
const pool = new pg.Pool({
host: process.env.PG_HOST || "127.0.0.1",
port: parseInt(process.env.PG_PORT || "5432"),
database: process.env.PG_DATABASE || "openprocess_db",
user: process.env.PG_USER || "openprocess",
password: process.env.PG_PASSWORD || "",
});
// Simple XLSX parser (reads shared strings + sheet data)
// For production: npm install xlsx
async function parseXlsx(filePath) {
try {
// Try using xlsx package if available
const XLSX = (await import("xlsx")).default || (await import("xlsx"));
const workbook = XLSX.readFile(filePath);
const sheet = workbook.Sheets[workbook.SheetNames[0]];
const rows = XLSX.utils.sheet_to_json(sheet, { defval: "" });
return rows;
} catch {
console.error(
"❌ 'xlsx' paketi gerekli. Kurmak için: npm install xlsx",
);
process.exit(1);
}
}
// Normalize column names (handle variations)
function findColumn(row, ...possibleNames) {
for (const name of possibleNames) {
for (const key of Object.keys(row)) {
if (key.toLowerCase().includes(name.toLowerCase())) {
return row[key];
}
}
}
return null;
}
async function importExcel(filePath) {
console.log(`\n📊 Excel dosyası okunuyor: ${filePath}\n`);
const rows = await parseXlsx(filePath);
console.log(` ${rows.length} satır bulundu\n`);
if (rows.length === 0) {
console.log("❌ Excel dosyası boş veya okunamadı");
return;
}
// Show detected columns
console.log("📋 Algılanan kolonlar:", Object.keys(rows[0]).join(", "));
console.log("");
const client = await pool.connect();
let imported = 0;
let skipped = 0;
const customerCache = new Map(); // phone -> customer_id
const vehicleCache = new Map(); // plate -> vehicle_id
try {
await client.query("BEGIN");
for (const row of rows) {
const plate = findColumn(row, "plaka", "plate");
const customerName = findColumn(
row,
"müşteri",
"musteri",
"ad",
"isim",
"customer",
);
const phone = findColumn(
row,
"telefon",
"tel",
"phone",
"gsm",
"cep",
);
const brand = findColumn(row, "marka", "brand");
const model = findColumn(row, "model");
const color = findColumn(row, "renk", "color");
const service = findColumn(
row,
"işlem",
"islem",
"hizmet",
"service",
"yapılan",
);
const price = findColumn(
row,
"ücret",
"ucret",
"fiyat",
"tutar",
"price",
"₺",
);
const payment = findColumn(row, "ödeme", "odeme", "payment");
const notes = findColumn(row, "not", "notes", "açıklama");
const dateVal = findColumn(row, "tarih", "date");
// Skip rows without plate
if (
!plate ||
String(plate).trim() === "" ||
String(plate).includes("TOPLAM")
) {
skipped++;
continue;
}
const cleanPlate = String(plate).replace(/\s/g, "").toUpperCase();
const cleanPhone = String(phone || "").replace(/\D/g, "");
const cleanName = String(customerName || "Bilinmeyen").trim();
const cleanPrice =
parseFloat(
String(price || "0")
.replace(/[^\d.,]/g, "")
.replace(",", "."),
) || 0;
// Payment method mapping
let paymentMethod = "cash";
const payStr = String(payment || "").toLowerCase();
if (
payStr.includes("kart") ||
payStr.includes("kredi") ||
payStr.includes("card")
)
paymentMethod = "card";
else if (
payStr.includes("havale") ||
payStr.includes("eft") ||
payStr.includes("transfer")
)
paymentMethod = "transfer";
// Parse date
let jobDate;
if (dateVal instanceof Date) {
jobDate = dateVal;
} else if (typeof dateVal === "number") {
// Excel serial date
jobDate = new Date((dateVal - 25569) * 86400 * 1000);
} else {
const dStr = String(dateVal || "");
// Try DD.MM.YYYY format
const parts = dStr.split(/[.\/\-]/);
if (parts.length === 3) {
const [d, m, y] = parts;
jobDate = new Date(
parseInt(y),
parseInt(m) - 1,
parseInt(d),
);
} else {
jobDate = new Date(dStr);
}
}
if (isNaN(jobDate?.getTime())) jobDate = new Date();
// 1. Find or create customer
let customerId = customerCache.get(cleanPhone || cleanName);
if (!customerId) {
const existingCust = cleanPhone
? await client.query(
"SELECT id FROM customers WHERE phone = $1 LIMIT 1",
[cleanPhone],
)
: await client.query(
"SELECT id FROM customers WHERE name = $1 LIMIT 1",
[cleanName],
);
if (existingCust.rows.length > 0) {
customerId = existingCust.rows[0].id;
} else {
const newCust = await client.query(
"INSERT INTO customers (name, phone) VALUES ($1, $2) RETURNING id",
[cleanName, cleanPhone || "0000000000"],
);
customerId = newCust.rows[0].id;
}
customerCache.set(cleanPhone || cleanName, customerId);
}
// 2. Find or create vehicle
let vehicleId = vehicleCache.get(cleanPlate);
if (!vehicleId) {
const existingVeh = await client.query(
"SELECT id FROM vehicles WHERE UPPER(REPLACE(plate, ' ', '')) = $1 LIMIT 1",
[cleanPlate],
);
if (existingVeh.rows.length > 0) {
vehicleId = existingVeh.rows[0].id;
} else {
const newVeh = await client.query(
"INSERT INTO vehicles (customer_id, plate, brand, model, color) VALUES ($1, $2, $3, $4, $5) RETURNING id",
[
customerId,
cleanPlate,
brand || null,
model || null,
color || null,
],
);
vehicleId = newVeh.rows[0].id;
}
vehicleCache.set(cleanPlate, vehicleId);
}
// 3. Create job (as delivered/completed)
const jobResult = await client.query(
`INSERT INTO jobs (vehicle_id, customer_id, status, total_amount, payment_method, paid, notes, created_at, delivered_at)
VALUES ($1, $2, 'delivered', $3, $4, true, $5, $6, $6) RETURNING id`,
[
vehicleId,
customerId,
cleanPrice,
paymentMethod,
notes || null,
jobDate.toISOString(),
],
);
// 4. Create job service entry
const serviceName = String(service || "Genel Hizmet").trim();
await client.query(
"INSERT INTO job_services (job_id, service_name, price) VALUES ($1, $2, $3)",
[jobResult.rows[0].id, serviceName, cleanPrice],
);
// 5. Update customer stats
await client.query(
"UPDATE customers SET total_visits = total_visits + 1, total_spent = total_spent + $1 WHERE id = $2",
[cleanPrice, customerId],
);
imported++;
}
await client.query("COMMIT");
console.log("═══════════════════════════════════════");
console.log(`✅ Import tamamlandı!`);
console.log(` 📥 Aktarılan: ${imported} kayıt`);
console.log(` ⏭️ Atlanan: ${skipped} satır`);
console.log(` 👥 Müşteri: ${customerCache.size} benzersiz müşteri`);
console.log(` 🚗 Araç: ${vehicleCache.size} benzersiz araç`);
console.log("═══════════════════════════════════════\n");
} catch (err) {
await client.query("ROLLBACK");
console.error("❌ Import hatası:", err.message);
} finally {
client.release();
await pool.end();
}
}
// Run
const filePath = process.argv[2];
if (!filePath) {
console.log("\n📖 Kullanım: node import-excel.mjs <excel_dosyasi.xlsx>\n");
console.log("Örnek: node import-excel.mjs musteri_kayitlari.xlsx\n");
process.exit(1);
}
importExcel(filePath);