diff --git a/scripts/006_fix_deposit_gl_entries.sql b/scripts/006_fix_deposit_gl_entries.sql index e44c939..a7adab4 100644 --- a/scripts/006_fix_deposit_gl_entries.sql +++ b/scripts/006_fix_deposit_gl_entries.sql @@ -1,19 +1,28 @@ +-- 006_fix_deposit_gl_entries.sql -- Fix missing GL entries for existing deposits -- This script creates GL entries for deposits that were recorded but didn't create GL entries --- First, add 'deposit' to the allowed reference_type values -ALTER TABLE general_ledger +-- 1) Ensure 'deposit' is allowed as a reference_type +ALTER TABLE general_ledger DROP CONSTRAINT IF EXISTS general_ledger_reference_type_check; -ALTER TABLE general_ledger -ADD CONSTRAINT general_ledger_reference_type_check -CHECK (reference_type IN ('tenant_payment', 'landlord_payment', 'maintenance', 'expense', 'journal_entry', 'deposit')); +ALTER TABLE general_ledger +ADD CONSTRAINT general_ledger_reference_type_check +CHECK ( + reference_type IN ( + 'tenant_payment', + 'landlord_payment', + 'maintenance', + 'expense', + 'journal_entry', + 'deposit' + ) +); --- Now create the GL entries for deposits +-- 2) Create GL entries for deposits that do not have them DO $$ DECLARE deposit_record RECORD; - bank_gl_account_id UUID; undeposited_funds_account_id UUID; BEGIN -- Get the Undeposited Funds account @@ -22,9 +31,13 @@ BEGIN WHERE account_code = '1015' LIMIT 1; + IF undeposited_funds_account_id IS NULL THEN + RAISE EXCEPTION 'Undeposited Funds account (1015) not found in chart_of_accounts'; + END IF; + -- Loop through all deposits that don't have GL entries - FOR deposit_record IN - SELECT + FOR deposit_record IN + SELECT pd.id as deposit_id, pd.bank_account_id, pd.deposit_date, @@ -35,18 +48,25 @@ BEGIN FROM payment_deposits pd JOIN bank_accounts ba ON pd.bank_account_id = ba.id WHERE NOT EXISTS ( - SELECT 1 FROM general_ledger gl - WHERE gl.reference_type = 'deposit' + SELECT 1 FROM general_ledger gl + WHERE gl.reference_type = 'deposit' AND gl.reference_id = pd.id ) ORDER BY pd.deposit_date LOOP - RAISE NOTICE 'Creating GL entries for deposit % to % (Amount: %)', - deposit_record.deposit_reference, + + IF deposit_record.bank_gl_account_id IS NULL THEN + RAISE NOTICE 'Skipping deposit % because bank account has no gl_account_id', + deposit_record.deposit_reference; + CONTINUE; + END IF; + + RAISE NOTICE 'Creating GL entries for deposit % to % (Amount: %)', + deposit_record.deposit_reference, deposit_record.bank_name, deposit_record.total_amount; - -- Create GL entry to debit the bank account (increase bank balance) + -- Debit the bank account (increase bank balance) INSERT INTO general_ledger ( account_id, transaction_date, @@ -58,14 +78,14 @@ BEGIN ) VALUES ( deposit_record.bank_gl_account_id, deposit_record.deposit_date, - 'Deposit: ' || deposit_record.deposit_reference, + 'Deposit: ' || COALESCE(deposit_record.deposit_reference, ''), deposit_record.total_amount, 0, 'deposit', deposit_record.deposit_id ); - -- Create GL entry to credit Undeposited Funds (decrease undeposited funds) + -- Credit Undeposited Funds (decrease undeposited funds) INSERT INTO general_ledger ( account_id, transaction_date, @@ -77,7 +97,7 @@ BEGIN ) VALUES ( undeposited_funds_account_id, deposit_record.deposit_date, - 'Deposit: ' || deposit_record.deposit_reference, + 'Deposit: ' || COALESCE(deposit_record.deposit_reference, ''), 0, deposit_record.total_amount, 'deposit', diff --git a/scripts/044_045_fix_deposit_items_relationships.sql b/scripts/044_045_fix_deposit_items_relationships.sql new file mode 100644 index 0000000..77297cc --- /dev/null +++ b/scripts/044_045_fix_deposit_items_relationships.sql @@ -0,0 +1,44 @@ +-- 044_045_fix_deposit_items_relationships.sql +-- Combined fix for deposit_items schema + relationships. +-- +-- deposit_items.payment_id is polymorphic: +-- - tenant_payments.id +-- - landlord_payments.id +-- Therefore payment_id MUST NOT have an FK to tenant_payments. +-- +-- deposit_items should instead relate via: +-- - tenant_id -> tenants(id) +-- - landlord_id -> owners(id) + +BEGIN; + +-- 1) Remove incorrect FK constraints (safe no matter what exists) +ALTER TABLE deposit_items +DROP CONSTRAINT IF EXISTS fk_deposit_items_tenant_payment; + +ALTER TABLE deposit_items +DROP CONSTRAINT IF EXISTS deposit_items_tenant_payment_fkey; + +-- 2) Drop existing tenant/landlord constraints so we can recreate cleanly +ALTER TABLE deposit_items +DROP CONSTRAINT IF EXISTS deposit_items_tenant_fkey; + +ALTER TABLE deposit_items +DROP CONSTRAINT IF EXISTS deposit_items_landlord_fkey; + +-- 3) Add correct FK for tenant_id +ALTER TABLE deposit_items +ADD CONSTRAINT deposit_items_tenant_fkey + FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE SET NULL; + +-- 4) Add correct FK for landlord_id +ALTER TABLE deposit_items +ADD CONSTRAINT deposit_items_landlord_fkey + FOREIGN KEY (landlord_id) REFERENCES owners(id) ON DELETE SET NULL; + +-- 5) Add useful indexes +CREATE INDEX IF NOT EXISTS idx_deposit_items_payment_id ON deposit_items(payment_id); +CREATE INDEX IF NOT EXISTS idx_deposit_items_tenant_id ON deposit_items(tenant_id); +CREATE INDEX IF NOT EXISTS idx_deposit_items_landlord_id ON deposit_items(landlord_id); + +COMMIT; diff --git a/scripts/044_fix_deposit_items_schema.sql b/scripts/044_fix_deposit_items_schema.sql deleted file mode 100644 index 0318828..0000000 --- a/scripts/044_fix_deposit_items_schema.sql +++ /dev/null @@ -1,7 +0,0 @@ --- Fix the deposit_items table to properly reference tenant payments -ALTER TABLE deposit_items -ADD CONSTRAINT fk_deposit_items_tenant_payment -FOREIGN KEY (payment_id) REFERENCES tenant_payments(id) ON DELETE CASCADE; - --- Create index for better query performance -CREATE INDEX IF NOT EXISTS idx_deposit_items_payment_id ON deposit_items(payment_id); diff --git a/scripts/045_fix_deposit_items_relationship.sql b/scripts/045_fix_deposit_items_relationship.sql deleted file mode 100644 index cc98792..0000000 --- a/scripts/045_fix_deposit_items_relationship.sql +++ /dev/null @@ -1,21 +0,0 @@ --- Correct the foreign key relationships to match actual schema --- deposit_items table already has tenant_id and landlord_id columns --- Just add proper foreign key constraints - --- Drop incorrect constraints if they exist -ALTER TABLE IF EXISTS deposit_items -DROP CONSTRAINT IF EXISTS deposit_items_tenant_payment_fkey; - --- Add correct foreign key for tenant_id -ALTER TABLE deposit_items -ADD CONSTRAINT deposit_items_tenant_fkey - FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE SET NULL; - --- Add foreign key for landlord_id -ALTER TABLE deposit_items -ADD CONSTRAINT deposit_items_landlord_fkey - FOREIGN KEY (landlord_id) REFERENCES owners(id) ON DELETE SET NULL; - --- Add index for faster queries -CREATE INDEX IF NOT EXISTS idx_deposit_items_tenant ON deposit_items(tenant_id); -CREATE INDEX IF NOT EXISTS idx_deposit_items_landlord ON deposit_items(landlord_id);