INSERT INTO categories (category_code, category_name, description) VALUES
('CAT-COMP', 'Linh kiện máy tính', 'Chuột, bàn phím, RAM, SSD'),
('CAT-NET', 'Thiết bị mạng', 'Thiết bị kết nối và lưu trữ mạng'),
('CAT-ELEC', 'Thiết bị điện', 'Dây dẫn và thiết bị điện dân dụng');

INSERT INTO units (unit_code, unit_name, symbol) VALUES
('PCS', 'Cái', 'cái'), ('BOX', 'Hộp', 'hộp'), ('ROLL', 'Cuộn', 'cuộn');

INSERT INTO warehouses (warehouse_code, warehouse_name, address, description) VALUES
('KHO01', 'Kho chính', '12 Nguyễn Trãi, Quận 1, TP.HCM', 'Kho trung tâm'),
('KHO02', 'Kho phụ', '88 Điện Biên Phủ, Bình Thạnh, TP.HCM', 'Kho dự phòng'),
('KHO03', 'Kho cách ly', '20 Lê Lợi, Quận 1, TP.HCM', 'Hàng trả, hỏng, chờ xử lý');

INSERT INTO users (username, password_hash, full_name, email, role) VALUES
('admin', '$2b$10$Rzp7bvB1osDykg4gdeFB5OmAfYbBmzoypoQRq4p7Ku3GqrHKbnUo2', 'Quản trị viên', 'admin@stockflow.local', 'ADMIN'),
('warehouse01', '$2b$10$jJFkiRdYbQqNePHKbqGVXeJGuh63BSjfm4EQCcO1cXAyHJH4rJD9q', 'Nguyễn Văn Kho', 'warehouse01@stockflow.local', 'WAREHOUSE_STAFF'),
('warehouse02', '$2b$10$VLkq8Ox1qzdbB6sPccCPLe2Wm4jGt4IFt/m5bR64VF5SILXsbAE3q', 'Trần Thị Kho', 'warehouse02@stockflow.local', 'WAREHOUSE_STAFF');

INSERT INTO suppliers (supplier_code, supplier_name, tax_code, contact_name, phone, email, address) VALUES
('NCC001', 'Công ty Công nghệ Sao Việt', '0301000001', 'Lê Minh', '0281111111', 'sales@saoviet.local', 'TP.HCM'),
('NCC002', 'Nhà phân phối Phú Quý', '0102000002', 'Phạm An', '0282222222', 'contact@phuquy.local', 'Hà Nội'),
('NCC003', 'Thiết bị điện Đông Á', '3703000003', 'Võ Bình', '0283333333', 'hello@donga.local', 'Bình Dương');

INSERT INTO customers (customer_code, customer_name, tax_code, contact_name, phone, email, address) VALUES
('KH001', 'Công ty Minh Long', '0304000001', 'Nguyễn Minh', '0901000001', 'contact@minhlong.local', 'TP.HCM'),
('KH002', 'Công ty An Bình', '0304000002', 'Phạm Bình', '0901000002', 'contact@anbinh.local', 'Đồng Nai'),
('KH003', 'Đại lý Hoàng Gia', '0304000003', 'Lê Hoàng', '0901000003', 'contact@hoanggia.local', 'Bình Dương');

INSERT INTO products (product_code, product_name, category_id, unit_id, minimum_stock, import_price, export_price, description) VALUES
('SP001', 'Chuột Logitech G102', 1, 1, 10, 300000, 350000, 'Chuột máy tính'),
('SP002', 'Bàn phím Logitech K120', 1, 1, 8, 200000, 240000, 'Bàn phím máy tính'),
('SP003', 'RAM Kingston 16GB', 1, 1, 10, 800000, 900000, 'Bộ nhớ RAM'),
('SP004', 'SSD Samsung 500GB', 1, 1, 8, 1000000, 1150000, 'Ổ cứng SSD'),
('SP005', 'Bộ phát WiFi TP-Link', 2, 1, 5, 600000, 750000, 'Thiết bị WiFi'),
('SP006', 'Dây điện Cadivi 4.0', 3, 3, 20, 45000, 55000, 'Dây điện cuộn'),
('SP007', 'Router TP-Link AX23', 2, 1, 5, 850000, 1050000, 'Router WiFi'),
('SP008', 'Switch TP-Link 8 cổng', 2, 1, 5, 420000, 520000, 'Thiết bị chuyển mạch'),
('SP009', 'Ổ cắm điện 5 mét', 3, 1, 10, 120000, 150000, 'Ổ cắm điện'),
('SP010', 'Hộp đầu nối điện', 3, 2, 10, 80000, 100000, 'Phụ kiện điện');

INSERT INTO import_receipts (import_code, import_type, warehouse_id, supplier_id, created_by, delivery_person, source_name, import_date, reason, note, status) VALUES
('PN000001', 'PURCHASE', 1, 1, 2, 'Nguyễn Văn Giao', 'Công ty Công nghệ Sao Việt', '2026-09-01', 'Nhập mua theo hợp đồng', 'Đợt nhập tháng 9', 'CONFIRMED'),
('PN000002', 'PURCHASE', 1, 2, 2, 'Phạm Minh', 'Nhà phân phối Phú Quý', '2026-09-02', 'Nhập linh kiện máy tính', NULL, 'CONFIRMED'),
('PNTP000001', 'FINISHED_GOODS', 1, NULL, 2, NULL, 'Bộ phận nội bộ', '2026-09-03', 'Nhập thành phẩm', 'Nguồn nội bộ', 'CONFIRMED'),
('PNTR000001', 'CUSTOMER_RETURN', 3, NULL, 2, NULL, 'Công ty Minh Long', '2026-09-10', 'Khách trả hàng lỗi', 'Chờ kiểm tra chất lượng', 'CONFIRMED');

INSERT INTO import_details (import_id, product_id, document_quantity, actual_quantity, unit_price, note) VALUES
(1, 1, 50, 50, 300000, NULL), (1, 2, 30, 30, 200000, NULL),
(2, 3, 20, 20, 800000, NULL), (3, 5, 15, 15, 600000, 'Thành phẩm nội bộ'),
(4, 1, 2, 2, 300000, 'Hàng trả chờ phân loại');

INSERT INTO export_receipts (export_code, export_type, warehouse_id, customer_id, created_by, receiver_name, export_date, reason, note, status) VALUES
('PX000001', 'SALE', 1, 1, 2, 'Nguyễn Minh', '2026-09-05', 'Xuất bán hàng', NULL, 'CONFIRMED'),
('PX000002', 'SALE', 1, 2, 2, 'Phạm Bình', '2026-09-06', 'Xuất bán hàng', NULL, 'CONFIRMED'),
('PXTR000001', 'SUPPLIER_RETURN', 1, NULL, 2, 'NCC001', '2026-09-07', 'Trả hàng lỗi', 'Trả theo biên bản QC', 'CONFIRMED');

INSERT INTO export_details (export_id, product_id, requested_quantity, actual_quantity, unit_price, note) VALUES
(1, 1, 10, 10, 350000, NULL), (1, 2, 5, 5, 240000, NULL),
(2, 5, 3, 3, 750000, NULL), (3, 3, 2, 2, 900000, 'Trả NCC');

UPDATE import_receipts SET source_export_id = 1 WHERE import_id = 4;

INSERT INTO transfer_receipts (transfer_code, from_warehouse_id, to_warehouse_id, created_by, carrier_name, transfer_date, reason, note, status) VALUES
('CK000001', 1, 2, 2, 'Nguyễn Văn T', '2026-09-08', 'Bổ sung hàng cho kho phụ', NULL, 'CONFIRMED');

INSERT INTO transfer_details (transfer_id, product_id, requested_quantity, actual_quantity, note) VALUES
(1, 1, 5, 5, NULL);

INSERT INTO stock_checks (check_code, warehouse_id, created_by, approved_by, check_date, note, status) VALUES
('KK000001', 1, 2, 1, '2026-09-09', 'Kiểm kê định kỳ tháng 9', 'ADJUSTED');

INSERT INTO stock_check_details (check_id, product_id, system_quantity, actual_quantity, system_damaged_quantity, system_obsolete_quantity, good_quantity, damaged_quantity, obsolete_quantity, note) VALUES
(1, 1, 35, 34, 0, 0, 34, 0, 0, 'Thiếu 1 sản phẩm'),
(1, 2, 25, 25, 0, 0, 25, 0, 0, NULL);

INSERT INTO adjustment_receipts (adjustment_code, warehouse_id, check_id, created_by, approved_by, adjustment_date, reason, note, status) VALUES
('DC000001', 1, 1, 2, 1, '2026-09-09', 'Điều chỉnh theo kết quả kiểm kê', NULL, 'CONFIRMED');

INSERT INTO adjustment_details (adjustment_id, product_id, system_quantity, actual_quantity, quantity_change, damaged_change, obsolete_change, note) VALUES
(1, 1, 35, 34, -1, 0, 0, 'Thiếu 1 sản phẩm'),
(1, 2, 25, 25, 0, 0, 0, 'Không chênh lệch');

INSERT INTO inventory (warehouse_id, product_id, quantity, damaged_quantity, obsolete_quantity) VALUES
(1, 1, 34, 0, 0), (1, 2, 25, 0, 0), (1, 3, 18, 0, 0),
(1, 5, 12, 0, 0), (2, 1, 5, 0, 0), (3, 1, 2, 0, 0);

INSERT INTO inventory_transactions (warehouse_id, product_id, created_by, transaction_type, reference_type, reference_id, quantity_before, quantity_change, quantity_after, transaction_date, note) VALUES
(1, 1, 2, 'IMPORT', 'IMPORT_RECEIPT', 1, 0, 50, 50, '2026-09-01 09:00:00', 'Nhập mua PN000001'),
(1, 2, 2, 'IMPORT', 'IMPORT_RECEIPT', 1, 0, 30, 30, '2026-09-01 09:00:00', 'Nhập mua PN000001'),
(1, 3, 2, 'IMPORT', 'IMPORT_RECEIPT', 2, 0, 20, 20, '2026-09-02 09:00:00', 'Nhập mua PN000002'),
(1, 5, 2, 'IMPORT', 'IMPORT_RECEIPT', 3, 0, 15, 15, '2026-09-03 09:00:00', 'Nhập thành phẩm PNTP000001'),
(1, 1, 2, 'EXPORT', 'EXPORT_RECEIPT', 1, 50, -10, 40, '2026-09-05 09:00:00', 'Xuất bán PX000001'),
(1, 2, 2, 'EXPORT', 'EXPORT_RECEIPT', 1, 30, -5, 25, '2026-09-05 09:00:00', 'Xuất bán PX000001'),
(1, 5, 2, 'EXPORT', 'EXPORT_RECEIPT', 2, 15, -3, 12, '2026-09-06 09:00:00', 'Xuất bán PX000002'),
(1, 3, 2, 'EXPORT', 'EXPORT_RECEIPT', 3, 20, -2, 18, '2026-09-07 09:00:00', 'Trả NCC PXTR000001'),
(1, 1, 2, 'TRANSFER_OUT', 'TRANSFER_RECEIPT', 1, 40, -5, 35, '2026-09-08 09:00:00', 'Chuyển kho CK000001'),
(2, 1, 2, 'TRANSFER_IN', 'TRANSFER_RECEIPT', 1, 0, 5, 5, '2026-09-08 09:00:00', 'Nhận chuyển kho CK000001'),
(1, 1, 2, 'ADJUSTMENT', 'ADJUSTMENT_RECEIPT', 1, 35, -1, 34, '2026-09-09 10:00:00', 'Điều chỉnh theo KK000001'),
(3, 1, 2, 'IMPORT', 'IMPORT_RECEIPT', 4, 0, 2, 2, '2026-09-10 09:00:00', 'Khách trả PNTR000001');

INSERT INTO vat_invoices (invoice_number, invoice_type, invoice_date, supplier_id, import_id, seller_name, seller_tax_code, buyer_name, buyer_tax_code, subtotal, vat_rate, vat_amount, total_amount, status, created_by) VALUES
('HDV000001', 'INPUT', '2026-09-01', 1, 1, 'Công ty Công nghệ Sao Việt', '0301000001', 'StockFlow', '0309000001', 21000000, 10, 2100000, 23100000, 'ISSUED', 1),
('HDV000002', 'INPUT', '2026-09-02', 2, 2, 'Nhà phân phối Phú Quý', '0102000002', 'StockFlow', '0309000001', 16000000, 10, 1600000, 17600000, 'ISSUED', 1);

INSERT INTO vat_invoice_details (invoice_id, product_id, quantity, unit_price, subtotal, vat_rate, vat_amount, total_amount) VALUES
(1, 1, 50, 300000, 15000000, 10, 1500000, 16500000), (1, 2, 30, 200000, 6000000, 10, 600000, 6600000),
(2, 3, 20, 800000, 16000000, 10, 1600000, 17600000);

INSERT INTO vat_invoices (invoice_number, invoice_type, invoice_date, customer_id, export_id, seller_name, seller_tax_code, buyer_name, buyer_tax_code, subtotal, vat_rate, vat_amount, total_amount, status, created_by) VALUES
('HDRA000001', 'OUTPUT', '2026-09-05', 1, 1, 'StockFlow', '0309000001', 'Công ty Minh Long', '0304000001', 4700000, 10, 470000, 5170000, 'ISSUED', 1),
('HDRA000002', 'OUTPUT', '2026-09-06', 2, 2, 'StockFlow', '0309000001', 'Công ty An Bình', '0304000002', 2250000, 10, 225000, 2475000, 'ISSUED', 1);

INSERT INTO vat_invoice_details (invoice_id, product_id, quantity, unit_price, subtotal, vat_rate, vat_amount, total_amount) VALUES
(3, 1, 10, 350000, 3500000, 10, 350000, 3850000), (3, 2, 5, 240000, 1200000, 10, 120000, 1320000),
(4, 5, 3, 750000, 2250000, 10, 225000, 2475000);

CREATE OR REPLACE VIEW vw_inventory_summary AS
SELECT
    i.inventory_id,
    w.warehouse_id,
    w.warehouse_code,
    w.warehouse_name,
    p.product_id,
    p.product_code,
    p.product_name,
    c.category_name,
    u.unit_name,
    u.symbol AS unit_symbol,
    i.quantity,
    i.damaged_quantity,
    i.obsolete_quantity,
    i.quantity - i.damaged_quantity - i.obsolete_quantity AS available_quantity,
    p.minimum_stock,
    CASE
        WHEN i.quantity - i.damaged_quantity - i.obsolete_quantity <= 0 THEN 'OUT_OF_STOCK'
        WHEN i.quantity - i.damaged_quantity - i.obsolete_quantity <= p.minimum_stock THEN 'LOW_STOCK'
        ELSE 'IN_STOCK'
    END AS stock_status
FROM inventory i
JOIN warehouses w ON w.warehouse_id = i.warehouse_id
JOIN products p ON p.product_id = i.product_id
JOIN categories c ON c.category_id = p.category_id
JOIN units u ON u.unit_id = p.unit_id;

CREATE OR REPLACE VIEW vw_stock_card AS
SELECT
    t.transaction_id,
    t.warehouse_id,
    w.warehouse_name,
    t.product_id,
    p.product_code,
    p.product_name,
    t.transaction_date,
    t.transaction_type,
    t.reference_type,
    t.reference_id,
    CASE WHEN t.quantity_change > 0 THEN t.quantity_change ELSE 0 END AS quantity_in,
    CASE WHEN t.quantity_change < 0 THEN ABS(t.quantity_change) ELSE 0 END AS quantity_out,
    t.quantity_before,
    t.quantity_after,
    t.note
FROM inventory_transactions t
JOIN warehouses w ON w.warehouse_id = t.warehouse_id
JOIN products p ON p.product_id = t.product_id;

SET FOREIGN_KEY_CHECKS = 1;
