Revert vendor->A/P posting rule (it collapsed the P&L)

The blanket rule stripped expense from direct-expense checks that have no bill,
leaving the P&L showing only a couple of accounts. Restore original
post_transaction_gl precedence (coded account before vendor). Re-posted affected
transactions to restore expense recognition. Double-count needs a bill-linkage fix.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-08 17:38:15 -04:00
parent fdb9174c45
commit f3b81eaeeb
@@ -0,0 +1,40 @@
-- Revert 20260608120000: the blanket "vendor -> A/P" rule stripped expense from
-- legitimate direct-expense checks (no bill), collapsing the P&L. Restore the
-- original precedence (coded account before vendor). The bill/check double-count
-- needs a surgical, bill-linkage fix instead — handled separately.
create or replace function accounting.post_transaction_gl(_txn_id uuid)
returns void
language plpgsql
security definer
set search_path to 'public', 'accounting'
as $function$
declare t accounting.transactions%rowtype; _counter uuid; _je uuid; _amt numeric;
begin
select * into t from accounting.transactions where id=_txn_id;
if not found then return; end if;
perform accounting._gl_clear(t.company_id, 'acmacc_txn', t.id::text);
if not accounting.gl_managed(t.company_id) then return; end if;
if t.transfer_id is not null or t.deposit_id is not null then return; end if;
if t.account_id is null then return; end if;
_amt := coalesce(t.amount,0);
if _amt = 0 then return; end if;
_counter := case
when t.customer_id is not null then accounting.coa_ar(t.company_id)
when t.coa_account_id is not null then t.coa_account_id
when t.vendor_id is not null then accounting.coa_ap(t.company_id)
else null end;
if _counter is null then return; end if;
insert into accounting.journal_entries (company_id, date, description, reference, external_source, external_id)
values (t.company_id, t.date, coalesce(nullif(t.description,''), 'Bank transaction'), t.reference, 'acmacc_txn', t.id::text)
returning id into _je;
if t.type = 'credit' then
insert into accounting.journal_entry_lines (journal_entry_id, account_id, debit, credit, description) values
(_je, t.account_id, _amt, 0, t.description),
(_je, _counter, 0, _amt, t.description);
else
insert into accounting.journal_entry_lines (journal_entry_id, account_id, debit, credit, description) values
(_je, _counter, _amt, 0, t.description),
(_je, t.account_id, 0, _amt, t.description);
end if;
end$function$;