-- =============================================================
-- 酷冰甲 CMS · PSI 进销存：销售订单 / 采购订单 / 出库单 模块
-- 在已有库上执行一次即可（已存在则忽略）。
-- 说明：单据与明细分离，出库单通过 so_no / so_item_id 关联销售订单，
--       采购订单收货时写入 psi_purchases 并增加库存，出库单扣减库存。
-- =============================================================

CREATE TABLE IF NOT EXISTS `psi_sales_orders` (
  `id`            INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `order_no`      VARCHAR(40)  NOT NULL,
  `customer`      VARCHAR(120) NOT NULL DEFAULT '',
  `salesman`      VARCHAR(60)  NOT NULL DEFAULT '',
  `channel`       ENUM('domestic','export') NOT NULL DEFAULT 'domestic',
  `region`        VARCHAR(60)  NOT NULL DEFAULT '',
  `status`        ENUM('pending','partial','delivered','closed') NOT NULL DEFAULT 'pending',
  `delivery_date` DATE         NULL,
  `remark`        TEXT,
  `created_at`    DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_so_no` (`order_no`),
  KEY `idx_so_customer` (`customer`),
  KEY `idx_so_salesman` (`salesman`),
  KEY `idx_so_status` (`status`),
  KEY `idx_so_created` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `psi_sales_order_items` (
  `id`            INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `so_id`         INT UNSIGNED NOT NULL,
  `product_id`    INT UNSIGNED NOT NULL DEFAULT 0,
  `name`          VARCHAR(160) NOT NULL DEFAULT '',
  `spec`          VARCHAR(120) NOT NULL DEFAULT '',
  `unit`          VARCHAR(20)  NOT NULL DEFAULT '',
  `qty`           INT          NOT NULL DEFAULT 0,
  `price`         DECIMAL(12,2) NOT NULL DEFAULT 0,
  `amount`        DECIMAL(12,2) NOT NULL DEFAULT 0,
  `delivered_qty` INT          NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_so_item_so` (`so_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `psi_purchase_orders` (
  `id`            INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `order_no`      VARCHAR(40)  NOT NULL,
  `supplier_id`   INT UNSIGNED NOT NULL DEFAULT 0,
  `supplier_name` VARCHAR(120) NOT NULL DEFAULT '',
  `salesman`      VARCHAR(60)  NOT NULL DEFAULT '',
  `status`        ENUM('pending','partial','received','closed') NOT NULL DEFAULT 'pending',
  `expected_at`   DATE         NULL,
  `remark`        TEXT,
  `created_at`    DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_po_no` (`order_no`),
  KEY `idx_po_supplier` (`supplier_id`),
  KEY `idx_po_salesman` (`salesman`),
  KEY `idx_po_status` (`status`),
  KEY `idx_po_created` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `psi_purchase_order_items` (
  `id`            INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `po_id`         INT UNSIGNED NOT NULL,
  `item_type`     ENUM('material','product') NOT NULL DEFAULT 'material',
  `item_id`       INT UNSIGNED NOT NULL DEFAULT 0,
  `name`          VARCHAR(160) NOT NULL DEFAULT '',
  `spec`          VARCHAR(120) NOT NULL DEFAULT '',
  `unit`          VARCHAR(20)  NOT NULL DEFAULT '',
  `qty`           INT          NOT NULL DEFAULT 0,
  `price`         DECIMAL(12,2) NOT NULL DEFAULT 0,
  `amount`        DECIMAL(12,2) NOT NULL DEFAULT 0,
  `received_qty`  INT          NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_po_item_po` (`po_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `psi_outbounds` (
  `id`            INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `order_no`      VARCHAR(40)  NOT NULL,
  `so_no`         VARCHAR(40)  NOT NULL DEFAULT '',
  `customer`      VARCHAR(120) NOT NULL DEFAULT '',
  `salesman`      VARCHAR(60)  NOT NULL DEFAULT '',
  `warehouse`     VARCHAR(60)  NOT NULL DEFAULT '',
  `status`        ENUM('delivered','partial') NOT NULL DEFAULT 'delivered',
  `delivery_date` DATE         NULL,
  `remark`        TEXT,
  `created_at`    DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_ob_no` (`order_no`),
  KEY `idx_ob_so` (`so_no`),
  KEY `idx_ob_customer` (`customer`),
  KEY `idx_ob_salesman` (`salesman`),
  KEY `idx_ob_created` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `psi_outbound_items` (
  `id`            INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `ob_id`         INT UNSIGNED NOT NULL,
  `so_item_id`    INT UNSIGNED NOT NULL DEFAULT 0,
  `product_id`    INT UNSIGNED NOT NULL DEFAULT 0,
  `name`          VARCHAR(160) NOT NULL DEFAULT '',
  `spec`          VARCHAR(120) NOT NULL DEFAULT '',
  `unit`          VARCHAR(20)  NOT NULL DEFAULT '',
  `qty`           INT          NOT NULL DEFAULT 0,
  `price`         DECIMAL(12,2) NOT NULL DEFAULT 0,
  `amount`        DECIMAL(12,2) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_ob_item_ob` (`ob_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
