-- ============================================
-- 주문/판매/불출 관리 테이블 (Phase 4)
-- ============================================

-- 주문 접수 (P4-01~03)
CREATE TABLE IF NOT EXISTS `shop_order` (
  `so_idx`         INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `so_lg_idx`      INT UNSIGNED NOT NULL,
  `so_ds_idx`      INT UNSIGNED NULL COMMENT '지정판매소 FK',
  `so_ds_name`     VARCHAR(100) NOT NULL DEFAULT '' COMMENT '판매소명(스냅샷)',
  `so_order_date`  DATE NOT NULL COMMENT '접수일',
  `so_delivery_date` DATE NULL COMMENT '배달일',
  `so_payment_type` VARCHAR(20) NOT NULL DEFAULT '' COMMENT '이체/가상계좌',
  `so_paid`        TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '입금여부 1=예',
  `so_received`    TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '수령여부 1=예',
  `so_total_qty`   INT UNSIGNED NOT NULL DEFAULT 0,
  `so_total_amount` DECIMAL(14,2) NOT NULL DEFAULT 0,
  `so_status`      VARCHAR(10) NOT NULL DEFAULT 'normal' COMMENT 'normal/cancelled',
  `so_orderer_idx` INT UNSIGNED NULL,
  `so_regdate`     DATETIME NOT NULL,
  PRIMARY KEY (`so_idx`),
  KEY `idx_so_lg_idx` (`so_lg_idx`),
  KEY `idx_so_ds_idx` (`so_ds_idx`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='주문 접수';

-- 주문 상세
CREATE TABLE IF NOT EXISTS `shop_order_item` (
  `soi_idx`        INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `soi_so_idx`     INT UNSIGNED NOT NULL,
  `soi_bag_code`   VARCHAR(50) NOT NULL,
  `soi_bag_name`   VARCHAR(100) NOT NULL DEFAULT '',
  `soi_unit_price` DECIMAL(12,2) NOT NULL DEFAULT 0,
  `soi_qty`        INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '접수량(낱장)',
  `soi_amount`     DECIMAL(14,2) NOT NULL DEFAULT 0,
  `soi_box_count`  INT UNSIGNED NOT NULL DEFAULT 0,
  `soi_pack_count` INT UNSIGNED NOT NULL DEFAULT 0,
  `soi_sheet_count` INT UNSIGNED NOT NULL DEFAULT 0,
  PRIMARY KEY (`soi_idx`),
  KEY `idx_soi_so_idx` (`soi_so_idx`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='주문 상세';

-- 판매 (P4-04~07)
CREATE TABLE IF NOT EXISTS `bag_sale` (
  `bs_idx`        INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `bs_lg_idx`     INT UNSIGNED NOT NULL,
  `bs_so_idx`     INT UNSIGNED NULL COMMENT '주문 FK (NULL=직접판매)',
  `bs_ds_idx`     INT UNSIGNED NULL COMMENT '지정판매소 FK',
  `bs_ds_name`    VARCHAR(100) NOT NULL DEFAULT '',
  `bs_sale_date`  DATE NOT NULL,
  `bs_bag_code`   VARCHAR(50) NOT NULL,
  `bs_bag_name`   VARCHAR(100) NOT NULL DEFAULT '',
  `bs_qty`        INT NOT NULL DEFAULT 0 COMMENT '판매수량(낱장, 음수=반품)',
  `bs_unit_price` DECIMAL(12,2) NOT NULL DEFAULT 0,
  `bs_amount`     DECIMAL(14,2) NOT NULL DEFAULT 0,
  `bs_type`       VARCHAR(20) NOT NULL DEFAULT 'sale' COMMENT 'sale/return/cancel',
  `bs_regdate`    DATETIME NOT NULL,
  PRIMARY KEY (`bs_idx`),
  KEY `idx_bs_lg_idx` (`bs_lg_idx`),
  KEY `idx_bs_ds_idx` (`bs_ds_idx`),
  KEY `idx_bs_sale_date` (`bs_sale_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='판매/반품';

-- 불출 (P4-08~10)
CREATE TABLE IF NOT EXISTS `bag_issue` (
  `bi2_idx`        INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `bi2_lg_idx`     INT UNSIGNED NOT NULL,
  `bi2_year`       YEAR NOT NULL,
  `bi2_quarter`    TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '분기(1~4)',
  `bi2_issue_type` VARCHAR(20) NOT NULL DEFAULT '' COMMENT '무료용/공공용',
  `bi2_issue_date` DATE NOT NULL,
  `bi2_dest_type`  VARCHAR(20) NOT NULL DEFAULT '' COMMENT '불출처 구분(동사무소 등)',
  `bi2_dest_name`  VARCHAR(100) NOT NULL DEFAULT '' COMMENT '불출처명',
  `bi2_bag_code`   VARCHAR(50) NOT NULL,
  `bi2_bag_name`   VARCHAR(100) NOT NULL DEFAULT '',
  `bi2_qty`        INT NOT NULL DEFAULT 0 COMMENT '불출수량(낱장, 음수=취소)',
  `bi2_status`     VARCHAR(10) NOT NULL DEFAULT 'normal' COMMENT 'normal/cancelled',
  `bi2_regdate`    DATETIME NOT NULL,
  PRIMARY KEY (`bi2_idx`),
  KEY `idx_bi2_lg_idx` (`bi2_lg_idx`),
  KEY `idx_bi2_date` (`bi2_issue_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='무료용 불출';
