38808-vm/db/migrations/024_link_expenses_to_accounting.sql
2026-04-13 05:45:19 +00:00

26 lines
1.3 KiB
SQL

-- Add account_id to expense_categories
-- We rely on the migration runner's exception handling to skip if the column or constraint already exists
ALTER TABLE expense_categories ADD COLUMN account_id INT DEFAULT NULL;
ALTER TABLE expense_categories ADD CONSTRAINT fk_expense_category_account FOREIGN KEY (account_id) REFERENCES accounting_accounts(id) ON DELETE SET NULL;
-- Seed default mappings using subqueries instead of hardcoded IDs.
-- The accounts are inserted by seed_accounts.php which uses Arabic names.
UPDATE expense_categories
SET account_id = (SELECT id FROM accounting_accounts WHERE name LIKE '%مصروفات الرواتب%' LIMIT 1)
WHERE name LIKE '%Salaries%' OR name LIKE '%رواتب%';
UPDATE expense_categories
SET account_id = (SELECT id FROM accounting_accounts WHERE name LIKE '%مصروفات الإيجار%' LIMIT 1)
WHERE name LIKE '%Rent%' OR name LIKE '%إيجار%';
UPDATE expense_categories
SET account_id = (SELECT id FROM accounting_accounts WHERE name LIKE '%مصروفات المرافق%' LIMIT 1)
WHERE name LIKE '%Utilities%' OR name LIKE '%مرافق%';
UPDATE expense_categories
SET account_id = (SELECT id FROM accounting_accounts WHERE name LIKE '%مصروفات التسويق%' LIMIT 1)
WHERE name LIKE '%Advertising%' OR name LIKE '%Marketing%' OR name LIKE '%تسو%';