Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions scripts/006_fix_deposit_gl_entries.sql
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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',
Expand Down
44 changes: 44 additions & 0 deletions scripts/044_045_fix_deposit_items_relationships.sql
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 0 additions & 7 deletions scripts/044_fix_deposit_items_schema.sql

This file was deleted.

21 changes: 0 additions & 21 deletions scripts/045_fix_deposit_items_relationship.sql

This file was deleted.

Loading