-- ============================================
-- 봉투 파기(입고분 폐기) + 5월 테스트 시드
-- 반품/파기 현황: 입출고=입고 시 bag_dispose 조회
-- ============================================

CREATE TABLE IF NOT EXISTS `bag_dispose` (
  `bd_idx`          INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `bd_lg_idx`       INT UNSIGNED NOT NULL,
  `bd_dispose_date` DATE NOT NULL COMMENT '파기일',
  `bd_location`     VARCHAR(100) NOT NULL DEFAULT '' COMMENT '파기처(창고·처리장 등)',
  `bd_bag_code`     VARCHAR(50) NOT NULL,
  `bd_bag_name`     VARCHAR(100) NOT NULL DEFAULT '',
  `bd_qty`          INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '파기수량(매)',
  `bd_reason`       VARCHAR(200) NOT NULL DEFAULT '' COMMENT '사유',
  `bd_regdate`      DATETIME NOT NULL,
  PRIMARY KEY (`bd_idx`),
  KEY `idx_bd_lg_date` (`bd_lg_idx`, `bd_dispose_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='봉투 파기(입고분)';

-- 지정판매소 반품 스캔(없으면 생성) — designated-return 과 동일 구조
CREATE TABLE IF NOT EXISTS `bag_return_scan_code` (
  `brsc_idx` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `brsc_lg_idx` INT UNSIGNED NOT NULL,
  `brsc_so_idx` INT UNSIGNED NOT NULL,
  `brsc_ds_idx` INT UNSIGNED NOT NULL,
  `brsc_bag_code` VARCHAR(50) NOT NULL DEFAULT '',
  `brsc_bag_name` VARCHAR(100) NOT NULL DEFAULT '',
  `brsc_code` VARCHAR(120) NOT NULL,
  `brsc_unit` VARCHAR(10) NOT NULL DEFAULT '',
  `brsc_qty` INT UNSIGNED NOT NULL DEFAULT 0,
  `brsc_unit_price` DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  `brsc_amount` DECIMAL(14,2) NOT NULL DEFAULT 0.00,
  `brsc_return_date` DATE NOT NULL,
  `brsc_state` VARCHAR(20) NOT NULL DEFAULT 'returned',
  `brsc_regdate` DATETIME NOT NULL,
  PRIMARY KEY (`brsc_idx`),
  KEY `idx_brsc_lg_return_date` (`brsc_lg_idx`, `brsc_return_date`),
  KEY `idx_brsc_ds_idx` (`brsc_ds_idx`),
  KEY `idx_brsc_state` (`brsc_state`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='지정판매소 반품 스캔 코드';

SET @LG_IDX = (SELECT lg_idx FROM local_government WHERE lg_code = '110204' LIMIT 1);
SET @DS1 = COALESCE(
  (SELECT ds_idx FROM designated_shop WHERE ds_lg_idx = @LG_IDX AND ds_name = 'CU 대명점' LIMIT 1),
  (SELECT MIN(ds_idx) FROM designated_shop WHERE ds_lg_idx = @LG_IDX)
);
SET @DS2 = COALESCE(
  (SELECT ds_idx FROM designated_shop WHERE ds_lg_idx = @LG_IDX AND ds_name = 'GS25 앞산점' LIMIT 1),
  (SELECT MIN(ds_idx) + 1 FROM designated_shop WHERE ds_lg_idx = @LG_IDX)
);
SET @DS4 = COALESCE(
  (SELECT ds_idx FROM designated_shop WHERE ds_lg_idx = @LG_IDX AND ds_name = '봉덕슈퍼' LIMIT 1),
  (SELECT MAX(ds_idx) FROM designated_shop WHERE ds_lg_idx = @LG_IDX)
);
SET @SO1 = (SELECT so_idx FROM shop_order WHERE so_lg_idx = @LG_IDX ORDER BY so_idx ASC LIMIT 1);

DELETE FROM `bag_dispose`
 WHERE `bd_lg_idx` = @LG_IDX AND `bd_dispose_date` BETWEEN '2026-05-01' AND '2026-05-31';
DELETE FROM `bag_return_scan_code`
 WHERE `brsc_lg_idx` = @LG_IDX AND `brsc_code` LIKE 'RET-202605%';

-- 2026년 5월 파기(입고) — 반품/파기 현황 입고 조회용
INSERT INTO `bag_dispose` (`bd_lg_idx`, `bd_dispose_date`, `bd_location`, `bd_bag_code`, `bd_bag_name`, `bd_qty`, `bd_reason`, `bd_regdate`) VALUES
(@LG_IDX, '2026-05-05', '남구 종량제 물류창고', '10152', '일반용 20L', 120, '유통기한 경과', '2026-05-05 10:00:00'),
(@LG_IDX, '2026-05-12', '남구 종량제 물류창고', '60102', '음식물 2L',   80, '오염·훼손',     '2026-05-12 11:30:00'),
(@LG_IDX, '2026-05-19', '남구 종량제 물류창고', '10172', '일반용 50L',   45, '재고 정리',     '2026-05-19 09:15:00');

-- 2026년 5월 지정판매소 반품(designated-return) — 출고 조회용
INSERT INTO `bag_return_scan_code`
  (`brsc_lg_idx`, `brsc_so_idx`, `brsc_ds_idx`, `brsc_bag_code`, `brsc_bag_name`, `brsc_code`, `brsc_unit`, `brsc_qty`, `brsc_unit_price`, `brsc_amount`, `brsc_return_date`, `brsc_state`, `brsc_regdate`)
SELECT @LG_IDX, IFNULL(@SO1, 0), @DS1, '10152', '일반용 20L', 'RET-20260508-DS1-001', '매', 25, 670.00, 16750.00, '2026-05-08', 'returned', '2026-05-08 14:20:00'
 WHERE @DS1 IS NOT NULL
UNION ALL SELECT @LG_IDX, IFNULL(@SO1, 0), @DS1, '10152', '일반용 20L', 'RET-20260508-DS1-002', '매', 10, 670.00, 6700.00, '2026-05-08', 'returned', '2026-05-08 14:21:00'
 WHERE @DS1 IS NOT NULL
UNION ALL SELECT @LG_IDX, IFNULL(@SO1, 0), @DS2, '10132', '일반용 10L', 'RET-20260515-DS2-001', '매', 40, 340.00, 13600.00, '2026-05-15', 'returned', '2026-05-15 16:00:00'
 WHERE @DS2 IS NOT NULL
UNION ALL SELECT @LG_IDX, IFNULL(@SO1, 0), @DS4, '10162', '일반용 30L', 'RET-20260522-DS4-001', '매', 15, 1080.00, 16200.00, '2026-05-22', 'returned', '2026-05-22 11:45:00'
 WHERE @DS4 IS NOT NULL;

-- E2E·북구(lg_idx=1) 등 designated_shop 이 있는 지자체
SET @LG_T = 1;
SET @DS_T1 = (SELECT MIN(ds_idx) FROM designated_shop WHERE ds_lg_idx = @LG_T);
SET @DS_T2 = (SELECT MIN(ds_idx) + 1 FROM designated_shop WHERE ds_lg_idx = @LG_T);
SET @SO_T = (SELECT so_idx FROM shop_order WHERE so_lg_idx = @LG_T ORDER BY so_idx ASC LIMIT 1);

DELETE FROM `bag_dispose`
 WHERE `bd_lg_idx` = @LG_T AND `bd_dispose_date` BETWEEN '2026-05-01' AND '2026-05-31';
DELETE FROM `bag_return_scan_code`
 WHERE `brsc_lg_idx` = @LG_T AND `brsc_code` LIKE 'RET-202605-LG1-%';

INSERT INTO `bag_dispose` (`bd_lg_idx`, `bd_dispose_date`, `bd_location`, `bd_bag_code`, `bd_bag_name`, `bd_qty`, `bd_reason`, `bd_regdate`) VALUES
(@LG_T, '2026-05-07', '북구 종량제 물류창고', '10152', '일반용 20L', 60, '유통기한 경과', '2026-05-07 10:00:00'),
(@LG_T, '2026-05-14', '북구 종량제 물류창고', '10132', '일반용 10L', 35, '오염·훼손',     '2026-05-14 11:00:00');

INSERT INTO `bag_return_scan_code`
  (`brsc_lg_idx`, `brsc_so_idx`, `brsc_ds_idx`, `brsc_bag_code`, `brsc_bag_name`, `brsc_code`, `brsc_unit`, `brsc_qty`, `brsc_unit_price`, `brsc_amount`, `brsc_return_date`, `brsc_state`, `brsc_regdate`)
SELECT @LG_T, IFNULL(@SO_T, 0), @DS_T1, '10152', '일반용 20L', 'RET-202605-LG1-001', '매', 20, 670.00, 13400.00, '2026-05-09', 'returned', '2026-05-09 14:00:00'
 WHERE @DS_T1 IS NOT NULL
UNION ALL SELECT @LG_T, IFNULL(@SO_T, 0), @DS_T2, '10132', '일반용 10L', 'RET-202605-LG1-002', '매', 30, 340.00, 10200.00, '2026-05-16', 'returned', '2026-05-16 15:00:00'
 WHERE @DS_T2 IS NOT NULL;

-- 테스터 지자체관리자(mb_lg_idx=10 등) 환경: 파기만 시드(판매소 없을 수 있음)
SET @LG_T = 10;
DELETE FROM `bag_dispose`
 WHERE `bd_lg_idx` = @LG_T AND `bd_dispose_date` BETWEEN '2026-05-01' AND '2026-05-31';
INSERT INTO `bag_dispose` (`bd_lg_idx`, `bd_dispose_date`, `bd_location`, `bd_bag_code`, `bd_bag_name`, `bd_qty`, `bd_reason`, `bd_regdate`) VALUES
(@LG_T, '2026-05-06', '중구 종량제 물류창고', '10152', '일반용 20L', 50, '유통기한 경과', '2026-05-06 10:00:00');
