Files
khb_microservices/packages/accounting_automation/scripts/old-invoices-load.mjs

33 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Загрузка разобранных строк (результат old-invoices-parse.mjs) в old_invoices; повторный запуск обновляет те же строки.
// Запуск в контейнере: node old-invoices-load.mjs <rows.json> <file_id>; соединение с БД - как у сервиса (khb_common).
import fs from "node:fs";
import { createPool, loadServiceSettings } from "khb_common";
import { accountingSchema } from "../dist/settings.js";
const rows = JSON.parse(fs.readFileSync(process.argv[2], "utf8"));
const fileId = process.argv[3];
const { common } = loadServiceSettings("accounting_automation", accountingSchema);
const pool = createPool(common.database.url, 2);
let n = 0;
for (const r of rows) {
await pool.query(
`insert into old_invoices (source_file_id, source_sheet, source_row, invoice_date, pay_method_raw, pay_method, project_text, invoice_number_raw, purpose,
vendor_name, amount, paid_raw, paid_on, paid_status, note, contract_total, drive_file_id, section, raw)
values ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19::jsonb)
on conflict (source_file_id, source_sheet, source_row) do update set invoice_date = excluded.invoice_date, pay_method_raw = excluded.pay_method_raw,
pay_method = excluded.pay_method, project_text = excluded.project_text, invoice_number_raw = excluded.invoice_number_raw, purpose = excluded.purpose,
vendor_name = excluded.vendor_name, amount = excluded.amount, paid_raw = excluded.paid_raw, paid_on = excluded.paid_on, paid_status = excluded.paid_status,
note = excluded.note, contract_total = excluded.contract_total, drive_file_id = excluded.drive_file_id, section = excluded.section, raw = excluded.raw, loaded_at = now()`,
[fileId, r.source_sheet, r.source_row, r.invoice_date, r.pay_method_raw, r.pay_method, r.project_text, r.invoice_number_raw, r.purpose,
r.vendor_name, r.amount, r.paid_raw, r.paid_on, r.paid_status, r.note, r.contract_total, r.drive_file_id, r.section, JSON.stringify(r.raw)],
);
n++;
}
// сопоставление поставщика с QuickBooks по имени без регистра и пунктуации (только однозначное совпадение)
await pool.query(`
update old_invoices o set vendor_qbo_id = v.id
from (select lower(regexp_replace(display_name, '[^a-zA-Z0-9]+', '', 'g')) k, min(id) id, count(*) c from qbo_vendor group by 1) v
where o.vendor_qbo_id is null and v.c = 1 and v.k = lower(regexp_replace(o.vendor_name, '[^a-zA-Z0-9]+', '', 'g')) and v.k <> ''`);
console.log("loaded", n);
await pool.end();