-- Disable foreign key checks
SET FOREIGN_KEY_CHECKS = 0;
-- Step 1: Delete dependent child rows first
DELETE FROM purchase_items;
DELETE FROM sale_items;

-- Step 2: Delete all items
DELETE FROM items;
-- Child tables first
DELETE FROM sale_items;
DELETE FROM purchase_items;
DELETE FROM bank_transactions;
DELETE FROM accounts_ledger;

-- Then parent transaction records
DELETE FROM sales;
DELETE FROM purchases;
DELETE FROM expenses;

-- Finally, core tables
DELETE FROM items;
DELETE FROM customers;
DELETE FROM suppliers;
DELETE FROM banks;
DELETE FROM categories;
DELETE FROM brands;

-- Reset AUTO_INCREMENT
ALTER TABLE sale_items AUTO_INCREMENT = 1;
ALTER TABLE purchase_items AUTO_INCREMENT = 1;
ALTER TABLE bank_transactions AUTO_INCREMENT = 1;
ALTER TABLE accounts_ledger AUTO_INCREMENT = 1;
ALTER TABLE sales AUTO_INCREMENT = 1;
ALTER TABLE purchases AUTO_INCREMENT = 1;
ALTER TABLE expenses AUTO_INCREMENT = 1;
ALTER TABLE items AUTO_INCREMENT = 1;
ALTER TABLE customers AUTO_INCREMENT = 1;
ALTER TABLE suppliers AUTO_INCREMENT = 1;
ALTER TABLE banks AUTO_INCREMENT = 1;
ALTER TABLE categories AUTO_INCREMENT = 1;
ALTER TABLE brands AUTO_INCREMENT = 1;

-- Re-enable foreign key checks
SET FOREIGN_KEY_CHECKS = 1;


-- Clear child logs first
DELETE FROM sale_audit_log;

-- Then child transactional data
DELETE FROM sale_items;
DELETE FROM purchase_items;

-- Then main tables
DELETE FROM sales;
DELETE FROM banks;
DELETE FROM sales;
DELETE FROM items;
DELETE FROM purchases;
DELETE FROM expenses;
DELETE FROM accounts_ledger;
DELETE FROM customer_payments;
DELETE FROM bank_transactions;

-- Optionally
DELETE FROM customers;
DELETE FROM suppliers;
DELETE FROM categories;
DELETE FROM expense_categories;
DELETE FROM brands;
DELETE FROM sizes;
DELETE FROM units;
-- DELETE FROM items;
