-- Add branch_id to transactions
ALTER TABLE `orders`
ADD COLUMN `branch_id` INT UNSIGNED DEFAULT 1 AFTER `user_id`;

ALTER TABLE `orders`
ADD CONSTRAINT `fk_orders_branch` FOREIGN KEY (`branch_id`) REFERENCES `branches` (`id`) ON DELETE SET NULL;

ALTER TABLE `orders` ADD INDEX `idx_branch` (`branch_id`);

ALTER TABLE `stock_mutations`
ADD COLUMN `branch_id` INT UNSIGNED DEFAULT 1 AFTER `product_id`;

ALTER TABLE `stock_mutations`
ADD CONSTRAINT `fk_mutations_branch` FOREIGN KEY (`branch_id`) REFERENCES `branches` (`id`) ON DELETE SET NULL;

ALTER TABLE `stock_mutations` ADD INDEX `idx_branch` (`branch_id`);

ALTER TABLE `financial_records`
ADD COLUMN `branch_id` INT UNSIGNED DEFAULT 1 AFTER `id`;

ALTER TABLE `financial_records`
ADD CONSTRAINT `fk_finance_branch` FOREIGN KEY (`branch_id`) REFERENCES `branches` (`id`) ON DELETE SET NULL;

ALTER TABLE `financial_records` ADD INDEX `idx_branch` (`branch_id`);