From 6855b42be58f6e8b2005bb3f34d06b3e24a15702 Mon Sep 17 00:00:00 2001 From: Yuri Yaryshev Date: Fri, 4 Dec 2026 14:53:52 +0300 Subject: [PATCH] =?UTF-8?q?acc=5Finvoices:=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=D1=81=20=D1=82=D0=B5=D0=BA=D1=83=D1=89=D0=B5=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=80=D0=B5=D0=B5=D1=81=D1=82=D1=80=D0=B0=20Excel=20?= =?UTF-8?q?=D0=B2=20acc=5Finvoices=20(=D1=81=D0=BA=D1=80=D0=B8=D0=BF=D1=82?= =?UTF-8?q?),=20=D1=83=20=D0=BE=D0=BF=D0=BB=D0=B0=D1=87=D0=B5=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D1=85=20=D0=BD=D0=B5=D1=82=20=D0=BE=D1=88=D0=B8=D0=B1?= =?UTF-8?q?=D0=BA=D0=B8=20=C2=AB=D0=BD=D0=B5=D1=82=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D0=B0=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- packages/acc_invoices/src/rules.ts | 2 +- .../scripts/old-invoices-to-acc.sql | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 packages/accounting_automation/scripts/old-invoices-to-acc.sql diff --git a/packages/acc_invoices/src/rules.ts b/packages/acc_invoices/src/rules.ts index c6bd7df..33e95e5 100644 --- a/packages/acc_invoices/src/rules.ts +++ b/packages/acc_invoices/src/rules.ts @@ -72,7 +72,7 @@ export function errorsOf(r: InvoiceRow, now: Date, bankConfirmHours: number): st const e: string[] = []; if (r.is_draft) return e; // у черновика ошибок процесса нет if (!r.cancelled) { - if (!r.invoice_url) e.push("Нет файла инвойса (ссылки на Drive)"); + if (!r.invoice_url && !r.paid_on) e.push("Нет файла инвойса (ссылки на Drive)"); if (r.llm_result && !isOk(r.llm_result)) e.push(`Проверка агента: ${r.llm_result}`); if (r.manual_result && !isOk(r.manual_result)) e.push(`Проверка сотрудника: ${r.manual_result}`); if (r.approves_rejected > 0) e.push("Согласование отклонено"); diff --git a/packages/accounting_automation/scripts/old-invoices-to-acc.sql b/packages/accounting_automation/scripts/old-invoices-to-acc.sql new file mode 100644 index 0000000..c49ffae --- /dev/null +++ b/packages/accounting_automation/scripts/old-invoices-to-acc.sql @@ -0,0 +1,34 @@ +-- Перенос текущего реестра Анастасии (old_invoices, лист «KH BUILD») в рабочую таблицу acc_invoices, чтобы WebUI acc_invoices показывал продуктивные данные. +-- Повторный запуск безопасен (по conclusion->>'old_id'). Откат: delete from acc_invoices where conclusion->>'imported_from' = 'old_invoices'. +-- conclusion заполнен намеренно: invoice-check такие строки не берёт. Файлов инвойсов в архиве нет (drive_file_id пуст), поэтому у открытых строк будет ошибка «нет файла». +begin; +create temp table _imp on commit drop as +select gen_random_uuid() as new_id, o.*, + case o.paid_status when 'paid' then 'paid' when 'approved' then 'approved' when 'waiting' then 'wait' else 'checking' end as new_stage + from old_invoices o + where o.source_sheet = 'KH BUILD' + and o.invoice_date is not null and coalesce(btrim(o.vendor_name), '') <> '' + and not exists (select 1 from acc_invoices a where a.conclusion ->> 'old_id' = o.id::text); + +insert into acc_invoices (id, vendor_name, vendor_qbo_id, invoice_number, invoice_date, amount, project_customer_id, stage, conclusion, decided_by, paid_on, + pay_method, is_approved, llm_result, manual_result, is_paid_manual, paid_manual_at, created_at) +select new_id, vendor_name, vendor_qbo_id, nullif(btrim(invoice_number_raw), ''), invoice_date, amount, project_customer_id, new_stage, + jsonb_build_object('imported_from', 'old_invoices', 'old_id', id::text, 'paid_raw', paid_raw, 'note', note, 'project_text', project_text, 'excel_row', source_row), + case when new_stage in ('paid', 'approved') then 'excel' end, + case when new_stage = 'paid' then paid_on end, + pay_method, + new_stage in ('paid', 'approved'), + case when new_stage in ('paid', 'approved') then 'OK' end, + case when new_stage in ('paid', 'approved') then 'OK' + when paid_status in ('not_approved', 'waiting') then null + else 'Статус из Excel не определён: ' || coalesce(nullif(btrim(paid_raw), ''), '(пусто)') end, + new_stage = 'paid' and paid_on is not null, + case when new_stage = 'paid' then paid_on::timestamptz end, + coalesce(invoice_date::timestamptz, now()) + from _imp; + +insert into acc_invoices_details (invoice_id, line_no, description, amount) +select new_id, 1, purpose, amount from _imp where nullif(btrim(purpose), '') is not null; + +select new_stage, count(*) from _imp group by 1 order by 1; +commit;