From 063006a6a269d15a27972f9d9b840d2bb0cc4cfc Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Mon, 13 Apr 2026 05:45:19 +0000 Subject: [PATCH] update migrations 2 --- .../024_link_expenses_to_accounting.sql | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/db/migrations/024_link_expenses_to_accounting.sql b/db/migrations/024_link_expenses_to_accounting.sql index b03b89e..2cef5a1 100644 --- a/db/migrations/024_link_expenses_to_accounting.sql +++ b/db/migrations/024_link_expenses_to_accounting.sql @@ -1,23 +1,25 @@ -- 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 (Best effort based on Arabic account names vs English categories) --- Accounts: --- 13: مصروفات الرواتب (Salaries) --- 16: مصروفات التسويق (Marketing) --- 30: مصروفات الإيجار (Rent) --- 31: مصروفات المرافق (Utilities) --- 12: تكلفة البضاعة المباعة (COGS) +-- Seed default mappings using subqueries instead of hardcoded IDs. +-- The accounts are inserted by seed_accounts.php which uses Arabic names. --- Update 'Salaries' category -UPDATE expense_categories SET account_id = 13 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 '%Salaries%' OR name LIKE '%رواتب%'; --- Update 'Rent' category -UPDATE expense_categories SET account_id = 30 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 '%Rent%' OR name LIKE '%إيجار%'; --- Update 'Utilities' category -UPDATE expense_categories SET account_id = 31 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 '%Utilities%' OR name LIKE '%مرافق%'; --- Update 'Advertising' category -UPDATE expense_categories SET account_id = 16 WHERE name LIKE '%Advertising%' OR name LIKE '%Marketing%' 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 '%تسو%';