From 7541f0f69a3617251a26b4bbc75ede5cb03c6aa7 Mon Sep 17 00:00:00 2001 From: Yuri Yaryshev Date: Fri, 4 Dec 2026 15:17:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BA=D1=80=D0=B8=D0=BF=D1=82=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=D0=B2=D1=8F=D0=B7=D0=BA=D0=B8=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=BD=D0=B5=D1=81=D1=91=D0=BD=D0=BD=D1=8B=D1=85=20=D0=B8?= =?UTF-8?q?=D0=BD=D0=B2=D0=BE=D0=B9=D1=81=D0=BE=D0=B2=20=D0=BA=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=BE=D0=B4=D0=BA=D0=B0=D0=BC=20QuickBooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- .../scripts/old-invoices-link-qbo.sql | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 packages/accounting_automation/scripts/old-invoices-link-qbo.sql diff --git a/packages/accounting_automation/scripts/old-invoices-link-qbo.sql b/packages/accounting_automation/scripts/old-invoices-link-qbo.sql new file mode 100644 index 0000000..9c10153 --- /dev/null +++ b/packages/accounting_automation/scripts/old-invoices-link-qbo.sql @@ -0,0 +1,16 @@ +-- Привязка перенесённых из Excel инвойсов к проводкам QuickBooks (Purchase): тот же вендор и сумма, дата проводки в окне вокруг даты оплаты (или инвойса). +-- Привязываем только однозначные совпадения (одна проводка на инвойс и одна проводка не занята другим инвойсом). +begin; +with cand as ( + select i.id as inv, p.id as txn, + count(*) over (partition by i.id) as n_for_inv, + count(*) over (partition by p.id) as n_for_txn + from acc_invoices i + join qbo_purchase p on i.vendor_qbo_id is not null and p.entity_id = i.vendor_qbo_id and p.total_amt = i.amount + and p.txn_date between coalesce(i.paid_on, i.invoice_date) - 10 and coalesce(i.paid_on, i.invoice_date) + 21 + where i.conclusion ->> 'imported_from' = 'old_invoices' and i.qbo_txn_id is null + and not exists (select 1 from acc_invoices o where o.qbo_txn_id = p.id) +) +update acc_invoices a set qbo_txn_id = c.txn from cand c where a.id = c.inv and c.n_for_inv = 1 and c.n_for_txn = 1; +select count(*) filter (where qbo_txn_id is not null) as linked, count(*) as total from acc_invoices where conclusion ->> 'imported_from' = 'old_invoices'; +commit;