-- Sync stock from products table to branch_stocks
-- This will populate branch_stocks with current stock from products

USE superpos;

-- Insert stock for all products into branch_stocks (branch_id = 1 for Head Office)
INSERT INTO
    branch_stocks (
        product_id,
        branch_id,
        stock_quantity
    )
SELECT
    id as product_id,
    1 as branch_id,
    stock_current as stock_quantity
FROM products
WHERE
    product_type = 'Barang'
    AND is_active = 1
ON DUPLICATE KEY UPDATE
    stock_quantity = VALUES(stock_quantity);

-- Show results
SELECT
    p.sku,
    p.name,
    p.stock_current as product_stock,
    bs.stock_quantity as branch_stock
FROM
    products p
    LEFT JOIN branch_stocks bs ON p.id = bs.product_id
    AND bs.branch_id = 1
WHERE
    p.product_type = 'Barang'
    AND p.is_active = 1
LIMIT 10;

SELECT 'Stock synchronized to branch_stocks!' as Status;