-- Архив инвойсов из Excel Анастасии («Payments_Constr List», Drive id 1gPOutfuVrdEJK3j1YoaOd3e_7gHBv4GM): только история, в рабочий реестр acc_invoices не входит. -- Загрузка повторяемая (уникальность: файл + лист + строка), скрипт packages/accounting_automation/scripts/old-invoices-load.mjs. create table if not exists old_invoices ( id bigserial primary key, source_file_id text not null, source_sheet text not null, -- KH Open Invoices / GK Open Invoices / KH BUILD (три реестра = три компании/периода) source_row int not null, invoice_date date, pay_method_raw text, -- как в Excel: portal, ACH, zelle, BofA transfer, QB link ... pay_method text, -- нормализованный: ach, zelle, wire, bill_pay, bank_transfer, portal_link, qb_link, check, card, cash, paypal, other project_text text, -- адрес как в Excel (в acc_invoices - project_customer_id) project_customer_id text, -- заполняется сопоставлением с QuickBooks, если адрес совпал однозначно invoice_number_raw text, -- номер вместе с мусором: «Inv 9846», «INVOICE # 5013», «Contract», «DrReq 4» purpose text, vendor_name text, vendor_qbo_id text, -- заполняется сопоставлением с qbo_vendor по имени amount numeric(14,2) not null, -- сумма платежа по строке (частичные оплаты - отдельными строками) paid_raw text, -- столбец «дата оплаты / статус» как в Excel paid_on date, paid_status text, -- paid / approved / not_approved / waiting / other / unknown note text, -- последний столбец: «total $…», ссылки, комментарии contract_total numeric(14,2), -- «total $X» из примечания: полная сумма договора/инвойса, если платёж частичный drive_file_id text, -- если в примечании была ссылка на Drive section text, raw jsonb not null, -- все ячейки строки по буквам столбцов loaded_at timestamptz not null default now(), unique (source_file_id, source_sheet, source_row) ); create index if not exists old_invoices_vendor_idx on old_invoices (vendor_qbo_id, amount); create index if not exists old_invoices_date_idx on old_invoices (invoice_date); alter table old_invoices enable row level security; -- Папка файла инвойса на Drive (Инвойсы/<способ оплаты>/<Месяц ГГГГ>); пусто - файл ещё в «Без оплаты» или в старой раскладке alter table acc_invoices add column if not exists drive_folder text;