-- ============================================
-- 발주/입고 관리 테이블 (Phase 3)
-- ============================================

-- 발주 (P3-01~04)
CREATE TABLE IF NOT EXISTS `bag_order` (
  `bo_idx`         INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `bo_uuid`        CHAR(36) NOT NULL COMMENT 'UUID v4',
  `bo_version`     INT UNSIGNED NOT NULL DEFAULT 1 COMMENT '발주 버전',
  `bo_lg_idx`      INT UNSIGNED NOT NULL COMMENT '지자체 FK',
  `bo_gugun_code`  VARCHAR(20) NOT NULL DEFAULT '' COMMENT '구/군 코드',
  `bo_dong_code`   VARCHAR(20) NOT NULL DEFAULT '' COMMENT '동 코드',
  `bo_company_idx` INT UNSIGNED NULL COMMENT '제작업체 FK (company)',
  `bo_agency_idx`  INT UNSIGNED NULL COMMENT '입고처(대행소) FK (sales_agency)',
  `bo_fee_rate`    DECIMAL(5,2) NOT NULL DEFAULT 0 COMMENT '수수료율(%)',
  `bo_order_date`  DATE NOT NULL COMMENT '발주일',
  `bo_bag_types`   TEXT NULL COMMENT '봉투 종류 목록(JSON)',
  `bo_unit_prices` TEXT NULL COMMENT '발주 단가 목록(JSON)',
  `bo_qty_boxes`   TEXT NULL COMMENT '발주 박스 수량 목록(JSON)',
  `bo_lot_no`      VARCHAR(50) NOT NULL DEFAULT '' COMMENT 'LOT 번호',
  `bo_hash`        VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'SHA-256 해시',
  `bo_status`      VARCHAR(10) NOT NULL DEFAULT 'normal' COMMENT 'normal/cancelled/deleted',
  `bo_orderer_idx` INT UNSIGNED NULL COMMENT '발주자 mb_idx',
  `bo_regdate`     DATETIME NOT NULL COMMENT '등록일시',
  `bo_moddate`     DATETIME NULL DEFAULT NULL COMMENT '수정일시',
  PRIMARY KEY (`bo_uuid`, `bo_version`),
  -- 코드/화면 로직은 bo_idx(숫자 PK)로 조회/취소/삭제하고 있어, bo_idx도 유니크로 유지한다.
  UNIQUE KEY `uk_bo_idx` (`bo_idx`),
  UNIQUE KEY `uk_bo_uuid_ver` (`bo_uuid`, `bo_version`),
  KEY `idx_bo_lg_idx` (`bo_lg_idx`),
  KEY `idx_bo_status` (`bo_status`),
  KEY `idx_bo_order_date` (`bo_order_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='발주';

-- 발주 상세 (봉투 종류별 수량)
CREATE TABLE IF NOT EXISTS `bag_order_item` (
  `boi_idx`        INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `boi_bo_idx`     INT UNSIGNED NOT NULL COMMENT 'bag_order FK',
  `boi_bag_code`   VARCHAR(50) NOT NULL COMMENT '봉투코드(code_detail O)',
  `boi_bag_name`   VARCHAR(100) NOT NULL DEFAULT '',
  `boi_unit_price` DECIMAL(12,2) NOT NULL DEFAULT 0 COMMENT '발주 단가',
  `boi_qty_box`    INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '발주 박스 수',
  `boi_qty_sheet`  INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '낱장 환산 수량',
  `boi_amount`     DECIMAL(14,2) NOT NULL DEFAULT 0 COMMENT '금액(단가*낱장수)',
  PRIMARY KEY (`boi_idx`),
  KEY `idx_boi_bo_idx` (`boi_bo_idx`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='발주 상세';

-- 입고 (P3-06~09)
CREATE TABLE IF NOT EXISTS `bag_receiving` (
  `br_idx`         INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `br_bo_idx`      INT UNSIGNED NOT NULL COMMENT 'bag_order FK',
  `br_lg_idx`      INT UNSIGNED NOT NULL,
  `br_bag_code`    VARCHAR(50) NOT NULL,
  `br_bag_name`    VARCHAR(100) NOT NULL DEFAULT '',
  `br_qty_box`     INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '입고 박스 수',
  `br_qty_sheet`   INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '입고 낱장 수',
  `br_receive_date` DATE NOT NULL,
  `br_receiver_idx` INT UNSIGNED NULL COMMENT '인수자 mb_idx',
  `br_sender_name` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '인계자명',
  `br_type`        VARCHAR(20) NOT NULL DEFAULT 'scanner' COMMENT 'scanner/batch',
  `br_regdate`     DATETIME NOT NULL,
  PRIMARY KEY (`br_idx`),
  KEY `idx_br_bo_idx` (`br_bo_idx`),
  KEY `idx_br_lg_idx` (`br_lg_idx`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='발주 입고';

-- 재고 (P3-10)
CREATE TABLE IF NOT EXISTS `bag_inventory` (
  `bi_idx`       INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `bi_lg_idx`    INT UNSIGNED NOT NULL,
  `bi_bag_code`  VARCHAR(50) NOT NULL,
  `bi_bag_name`  VARCHAR(100) NOT NULL DEFAULT '',
  `bi_qty`       INT NOT NULL DEFAULT 0 COMMENT '현재 재고(낱장)',
  `bi_updated_at` DATETIME NOT NULL,
  PRIMARY KEY (`bi_idx`),
  UNIQUE KEY `uk_bi_lg_bag` (`bi_lg_idx`, `bi_bag_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='봉투 재고';
