93 lines
5.3 KiB
JavaScript
93 lines
5.3 KiB
JavaScript
// Разбор Excel Анастасии «Payments_Constr List» в строки для таблицы old_invoices.
|
||
// Запуск: node old-invoices-parse.mjs <файл.xlsx> <выход.json> (на Drive: id 1gPOutfuVrdEJK3j1YoaOd3e_7gHBv4GM)
|
||
import fs from "node:fs";
|
||
import { readXlsx } from "./xlsx-read.mjs";
|
||
|
||
// три реестра с разной раскладкой столбцов: inv - номер инвойса, proj - проект (адрес)
|
||
const SHEETS = {
|
||
"KH Open Invoices": { inv: "G", proj: "F" },
|
||
"GK Open Invoices": { inv: "F", proj: "G" },
|
||
"KH BUILD": { inv: "G", proj: "F" },
|
||
};
|
||
const MON = { jan: 0, feb: 1, mar: 2, apr: 3, may: 4, jun: 5, jul: 6, aug: 7, sep: 8, oct: 9, nov: 10, dec: 11 };
|
||
const iso = (d) => d.toISOString().slice(0, 10);
|
||
const serial = (n) => new Date(Date.UTC(1899, 11, 30) + Math.round(n) * 86400000);
|
||
const s = (v) => (v === undefined || v === null ? null : String(v).replace(/\s+/g, " ").trim() || null);
|
||
|
||
function payNorm(t) {
|
||
const x = (t ?? "").toLowerCase().trim();
|
||
if (!x) return null;
|
||
if (/zelle/.test(x)) return "zelle";
|
||
if (/bill ?pay/.test(x)) return "bill_pay";
|
||
if (/wire/.test(x)) return "wire";
|
||
if (/bofa|transfer/.test(x)) return "bank_transfer";
|
||
if (/^(ach)/.test(x)) return "ach";
|
||
if (/paypal|paypall/.test(x)) return "paypal";
|
||
if (/^(cc|сс|card|cr card|credit card)|cc /.test(x)) return "card";
|
||
if (/check/.test(x)) return "check";
|
||
if (/cash/.test(x)) return "cash";
|
||
if (/qb/.test(x)) return "qb_link";
|
||
if (/link|portal|porta|site|website/.test(x)) return "portal_link";
|
||
return "other";
|
||
}
|
||
|
||
function parsePaid(k, invDate) {
|
||
if (k === undefined || k === null || k === "") return { raw: null, on: null, status: "unknown" };
|
||
if (typeof k === "number") return { raw: iso(serial(k)), on: iso(serial(k)), status: "paid" };
|
||
const raw = s(k);
|
||
let on = null;
|
||
let m = /\b(\d{1,2})\s+([A-Za-z]{3})[a-z]*\b/.exec(raw);
|
||
if (m && MON[m[2].toLowerCase()] !== undefined) {
|
||
const base = invDate ? new Date(`${invDate}T00:00:00Z`) : null;
|
||
if (base) {
|
||
let d = new Date(Date.UTC(base.getUTCFullYear(), MON[m[2].toLowerCase()], +m[1]));
|
||
if (d.getTime() < base.getTime() - 10 * 86400000) d = new Date(Date.UTC(d.getUTCFullYear() + 1, d.getUTCMonth(), d.getUTCDate()));
|
||
on = iso(d);
|
||
}
|
||
} else if ((m = /^(\d{1,2})\/(\d{1,2})\/(\d{4})/.exec(raw))) on = iso(new Date(Date.UTC(+m[3], +m[1] - 1, +m[2])));
|
||
if (on && !/not |waiting|approval|don't/i.test(raw)) return { raw, on, status: "paid" };
|
||
if (/^(paid|check|paper check|cash|reimburs|оплачено)/i.test(raw) || /paid/i.test(raw)) return { raw, on, status: "paid" };
|
||
if (/not approved|not yet|not finished|waiting|need to|не /i.test(raw)) return { raw, on: null, status: /waiting/i.test(raw) ? "waiting" : "not_approved" };
|
||
if (/approved/i.test(raw)) return { raw, on: null, status: "approved" };
|
||
return { raw, on: null, status: "other" };
|
||
}
|
||
|
||
const sheets = readXlsx(process.argv[2]);
|
||
const out = [];
|
||
const skipped = { no_vendor_or_amount: 0, empty: 0 };
|
||
const skippedSamples = [];
|
||
for (const [name, cfg] of Object.entries(SHEETS)) {
|
||
const sh = sheets.find((x) => x.name === name);
|
||
for (const r of sh.rows) {
|
||
const c = r.cells;
|
||
if (!Object.keys(c).length) { skipped.empty++; continue; }
|
||
const header = typeof c.B === "string" && c.B === "#";
|
||
if (header) continue;
|
||
if (!(typeof c.J === "number" && c.I)) {
|
||
if (typeof c.J === "number" || c.I) { skipped.no_vendor_or_amount++; if (skippedSamples.length < 12) skippedSamples.push({ sheet: name, row: r.n, cells: c }); }
|
||
continue;
|
||
}
|
||
const invDate = typeof c.C === "number" ? iso(serial(c.C)) : null;
|
||
const paid = parsePaid(c.K, invDate);
|
||
const note = s(c.L);
|
||
const total = note ? /total(?:\s*price)?\s*:?\s*\$?\s*\$?\s*([\d][\d,]*(?:\.\d+)?)/i.exec(note) : null;
|
||
const drive = note ? /drive\.google\.com\/(?:file\/d\/|open\?id=|drive\/folders\/)([\w-]+)/.exec(note) : null;
|
||
out.push({
|
||
source_sheet: name, source_row: r.n, invoice_date: invDate, pay_method_raw: s(c.E), pay_method: payNorm(c.E),
|
||
project_text: s(c[cfg.proj]), invoice_number_raw: s(c[cfg.inv]), purpose: s(c.H), vendor_name: s(c.I), amount: Math.round(c.J * 100) / 100,
|
||
paid_raw: paid.raw, paid_on: paid.on, paid_status: paid.status, note, contract_total: total ? +total[1].replace(/,/g, "") : null,
|
||
drive_file_id: drive ? drive[1] : null, section: s(c.A), raw: c,
|
||
});
|
||
}
|
||
}
|
||
fs.writeFileSync(process.argv[3], JSON.stringify(out));
|
||
const cnt = (a) => Object.entries(a.reduce((m, x) => ((m[x] = (m[x] || 0) + 1), m), {})).sort((a, b) => b[1] - a[1]);
|
||
console.log("rows", out.length, "skipped", skipped);
|
||
console.log("by sheet", cnt(out.map((x) => x.source_sheet)));
|
||
console.log("pay_method", cnt(out.map((x) => x.pay_method)));
|
||
console.log("paid_status", cnt(out.map((x) => x.paid_status)));
|
||
console.log("no date", out.filter((x) => !x.invoice_date).length, "no inv#", out.filter((x) => !x.invoice_number_raw).length, "no project", out.filter((x) => !x.project_text).length);
|
||
console.log("contract_total", out.filter((x) => x.contract_total).length, "drive link", out.filter((x) => x.drive_file_id).length, "neg", out.filter((x) => x.amount < 0).length);
|
||
console.log("paid_on null while status paid", out.filter((x) => x.paid_status === "paid" && !x.paid_on).length);
|
||
console.log("skipped samples", JSON.stringify(skippedSamples));
|