Files
acmcc/supabase/migrations/20260602130000_accounting_gl_managed_flag.sql
admin f9c1b8af44 Fix balance sheet: recognize transfers & deposits (unify cash on bank register)
Root cause: accounting.transactions (deposits, transfers, bank payments) never
posted to the GL, and the GL's cash came only from synthesized legs that
hard-coded Undeposited Funds. So the balance sheet ignored transfers (#1) and
showed directly-deposited funds as Undeposited (#2), with banks going negative.

- Post the bank register to the GL: customer receipt -> Dr bank/Cr A/R, vendor
  payment -> Dr A/P/Cr bank, categorized -> Dr/Cr bank+COA, transfer -> Dr dest/
  Cr source, deposit -> Dr bank/Cr Undeposited; payments_received -> Undeposited.
  Retire the synthesized invoice/bill cash legs (acmacc_invpay/billpay) so cash is
  sourced once, from the register. Triggers on transactions/deposits keep it live.
- Fix gl_managed: make it an explicit accounting.companies.gl_auto_post flag
  instead of inferring from null-source journal entries (a single manual journal
  entry had silently disabled all automation for Ashley Manor).

Verified (Ashley Manor): transfer reflected (Cogent +47,127 vs prior -3,521),
deposits land in BOA (Undeposited cleared), R1=0, R7=0, no negative banks.
Imported companies (Bridgewater/Bent Oak) untouched; their residuals stay
surfaced in the Reconciliation report.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-02 01:08:37 -04:00

21 lines
1.1 KiB
PL/PgSQL

-- Make GL auto-posting an explicit per-company flag instead of inferring it from
-- the presence of non-acmacc journal entries. The old heuristic
-- (accounting.gl_managed = "no foreign/imported journal entries") broke the moment
-- a normal MANUAL journal entry (external_source null) was added to a managed
-- company — it silently flipped the company to "unmanaged" and disabled all GL
-- automation. Companies with a large imported GL are flagged off; platform-managed
-- companies default on.
alter table accounting.companies add column if not exists gl_auto_post boolean not null default true;
update accounting.companies c set gl_auto_post = false
where (
select count(*) from accounting.journal_entries je
where je.company_id = c.id
and (je.external_source is null or je.external_source not like 'acmacc_%')
) > 5;
create or replace function accounting.gl_managed(_company_id uuid)
returns boolean language sql stable security definer set search_path to 'public','accounting' as $$
select coalesce((select gl_auto_post from accounting.companies where id = _company_id), true);
$$;