Скрипт привязки перенесённых инвойсов к проводкам QuickBooks
Some checks failed
ci / deploy (push) Has been cancelled
ci / test (push) Has been cancelled

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-04 15:17:27 +03:00
parent 154d9be4ea
commit 7541f0f69a

View File

@@ -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;