--- /dev/null
+create procedure disposable_analysis_old
+ @year_month nvarchar(7) -- "YYYY-MM" 형식의 월별 입력값
+as
+begin
+ -- 시작일과 종료일 계산
+ declare @start_date datetime;
+ declare @end_date datetime;
+
+ set @start_date = convert(datetime, @year_month + '-01');
+ set @end_date = dateadd(month, 1, @start_date);
+
+ -- 임시 테이블 생성 (판매개수 컬럼으로 변경)
+ create table #product_sales (
+ product_name varchar(100),
+ product_sales varchar(50), -- 문자열로 저장 (반점 포함)
+ product_ratio varchar(10), -- 문자열로 저장
+ product_qty int -- 판매개수 컬럼
+ );
+
+ -- 매출 계산
+ declare @totalamount numeric(18, 0);
+ declare @disposableamount numeric(18, 0);
+
+ select
+ @totalamount = sum(ps_amt + ps_vat),
+ @disposableamount = sum(case when kind.kind_code in (1099, 1105, 1106, 1107, 1108) then ps_amt + ps_vat else 0 end)
+ from ps
+ join pr on ps.ps_prcode = pr.pr_code
+ join kind on pr.kind_code = kind.kind_code
+ where ps.ps_date >= @start_date and ps.ps_date < @end_date;
+
+ declare @totalamount_str varchar(50);
+ declare @disposableamount_str varchar(50);
+
+ set @totalamount_str = replace(convert(varchar(50), cast(@totalamount as money), 1), '.00', '');
+ set @disposableamount_str = replace(convert(varchar(50), cast(@disposableamount as money), 1), '.00', '');
+
+ insert into #product_sales
+ values (@year_month + ' ' + '매출', @totalamount_str, null, null);
+
+ insert into #product_sales
+ values (@year_month + ' ' + '일회용매출', @disposableamount_str, null, null);
+
+ declare @disposable_ratio varchar(10);
+ if @totalamount > 0
+ begin
+ set @disposable_ratio = cast(cast(@disposableamount * 100.0 / @totalamount as numeric(5, 2)) as varchar(10)) + '%';
+ end
+ else
+ begin
+ set @disposable_ratio = '0%';
+ end
+
+ update #product_sales
+ set product_ratio = @disposable_ratio
+ where product_name = @year_month + ' ' + '일회용매출';
+
+ insert into #product_sales
+ select
+ pr.pr_name as product_name,
+ replace(convert(varchar(50), cast(sum(ps.ps_amt + ps.ps_vat) as money), 1), '.00', '') as product_sales,
+ cast(cast(sum(ps.ps_amt + ps.ps_vat) * 100.0 / nullif(@disposableamount, 0) as numeric(5, 2)) as varchar(10)) + '%' as product_ratio,
+ sum(ps.ps_qty) as product_qty
+ from ps
+ join pr on ps.ps_prcode = pr.pr_code
+ join kind on pr.kind_code = kind.kind_code
+ where ps.ps_date >= @start_date and ps.ps_date < @end_date
+ and kind.kind_code in (1099, 1105, 1106, 1107, 1108)
+ group by pr.pr_name
+ having sum(ps.ps_amt + ps.ps_vat) > 0;
+
+ declare @total_qty int;
+ select @total_qty = sum(ps.ps_qty)
+ from ps
+ join pr on ps.ps_prcode = pr.pr_code
+ join kind on pr.kind_code = kind.kind_code
+ where ps.ps_date >= @start_date and ps.ps_date < @end_date
+ and kind.kind_code in (1099, 1105, 1106, 1107, 1108);
+
+ update #product_sales
+ set product_qty = @total_qty
+ where product_name = @year_month + ' ' + '일회용매출';
+
+ -- 최종 결과 출력: 매출 → 일회용매출 → 특정 상품들 → 나머지
+ select
+ product_name as 상품명,
+ product_sales as 매출,
+ product_ratio as 비율,
+ product_qty as 개수
+ from #product_sales
+ order by
+ case product_name
+ when @year_month + ' ' + '매출' then 1
+ when @year_month + ' ' + '일회용매출' then 2
+ when '와카' then 3
+ when '쥬피터' then 4
+ when '에이스' then 5
+ when '크리에이터' then 6
+ else 7
+ end,
+ product_name;
+
+ drop table #product_sales;
+end;
+
+-- 실행 예제
+EXEC disposable_analysis @year_month = '2025-01';
+
+
+
+
+create procedure disposable_analysis_new
+ @year_month nvarchar(7)
+as
+begin
+ declare @start_date datetime;
+ declare @end_date datetime;
+ declare @color1 varchar(50);
+ declare @color2 varchar(50);
+ declare @color3 varchar(50);
+
+ set @start_date = convert(datetime, @year_month + '-01');
+ set @end_date = dateadd(month, 1, @start_date);
+ set @color1= 'LightYellow';
+ set @color2= 'LightPink';
+ set @color3= 'PeachPuff';
+
+ create table #product_sales (
+ product_name varchar(100),
+ product_sales varchar(50),
+ product_ratio varchar(10),
+ product_qty int,
+ row_color varchar(20)
+ );
+
+ declare @totalamount numeric(18, 0);
+ declare @disposableamount numeric(18, 0);
+
+ select
+ @totalamount = sum(ps_amt + ps_vat)
+ from ps
+ join pr on ps.ps_prcode = pr.pr_code
+ join kind on pr.kind_code = kind.kind_code
+ where ps.ps_date >= @start_date and ps.ps_date < @end_date;
+
+ select
+ @disposableamount = sum(ps_amt + ps_vat)
+ from ps
+ join pr on ps.ps_prcode = pr.pr_code
+ join kind on pr.kind_code = kind.kind_code
+ join disposable_analysis_info on disposable_analysis_info.gubun in ('wevape', 'etc') and kind.kind_code= disposable_analysis_info.kind_code
+ where ps.ps_date >= @start_date and ps.ps_date < @end_date;
+
+ declare @totalamount_str varchar(50);
+ declare @disposableamount_str varchar(50);
+
+ set @totalamount_str = replace(convert(varchar(50), cast(@totalamount as money), 1), '.00', '');
+ set @disposableamount_str = replace(convert(varchar(50), cast(@disposableamount as money), 1), '.00', '');
+
+ insert into #product_sales values (@year_month + ' 매출', @totalamount_str, null, null, @color1);
+ insert into #product_sales values (@year_month + ' 일회용매출', @disposableamount_str, null, null, @color1);
+ insert into #product_sales values ('----- 일회용매출 -----', null, null, null, null);
+
+ declare @disposable_ratio varchar(10);
+ if @totalamount > 0
+ set @disposable_ratio = cast(cast(@disposableamount * 100.0 / @totalamount as numeric(5, 2)) as varchar(10)) + '%';
+ else
+ set @disposable_ratio = '0%';
+
+ update #product_sales
+ set product_ratio = @disposable_ratio
+ where product_name = @year_month + ' 일회용매출';
+
+ -- kind별 통합 매출 (매출 없어도 항상 표시되도록 수정)
+ insert into #product_sales
+ select
+ k.kind_name + ' 총 매출',
+ replace(convert(varchar(50), cast(isnull(sum(ps.ps_amt + ps.ps_vat), 0) as money), 1), '.00', ''),
+ case when @disposableamount > 0 then
+ cast(cast(isnull(sum(ps.ps_amt + ps.ps_vat), 0) * 100.0 / nullif(@disposableamount, 0) as numeric(5, 2)) as varchar(10)) + '%'
+ else '0%' end,
+ isnull(sum(ps.ps_qty), 0),
+ @color2
+ from (
+ select kind_code, kind_name
+ from kind
+ where kind_code in (select kind_code from disposable_analysis_info where gubun in ('wevape'))
+ ) k
+ left join pr on k.kind_code = pr.kind_code
+ left join ps on ps.ps_prcode = pr.pr_code and ps.ps_date >= @start_date and ps.ps_date < @end_date
+ group by k.kind_name;
+
+ -- '그 외 매출 요약' 추가
+ declare @sum_known_kind numeric(18, 0);
+ declare @etc_qty int;
+
+ select @etc_qty= sum(ps.ps_qty) from ps join pr on ps.ps_prcode= pr.pr_code join kind on kind.kind_code= 1099 and kind.kind_code= pr.kind_code where ps.ps_date >= @start_date and ps.ps_date < @end_date
+
+ select @sum_known_kind = sum(amount)
+ from (
+ select sum(ps.ps_amt + ps.ps_vat) as amount
+ from ps
+ join pr on ps.ps_prcode = pr.pr_code
+ join kind k on pr.kind_code = k.kind_code
+ where ps.ps_date >= @start_date and ps.ps_date < @end_date
+ and k.kind_code in (select kind_code from disposable_analysis_info where gubun in ('wevape'))
+ group by k.kind_code
+ ) as subquery;
+
+ declare @other_amount numeric(18, 0);
+ set @other_amount = @disposableamount - isnull(@sum_known_kind, 0);
+
+ insert into #product_sales
+ values (
+ '그 외 매출',
+ replace(convert(varchar(50), cast(isnull(@other_amount, 0) as money), 1), '.00', ''),
+ case when @disposableamount > 0 then cast(cast(isnull(@other_amount, 0) * 100.0 / nullif(@disposableamount, 0) as numeric(5, 2)) as varchar(10)) + '%' else '0%' end,
+ @etc_qty,
+ @color3
+ );
+
+ -- 상세 매출내용 시작
+ insert into #product_sales
+ values ('----- 상세내역 -----', null, null, null, null);
+
+ -- 상품별 상세 매출 삽입
+ insert into #product_sales
+ select
+ pr.pr_name,
+ replace(convert(varchar(50), cast(sum(ps.ps_amt + ps.ps_vat) as money), 1), '.00', ''),
+ cast(cast(sum(ps.ps_amt + ps.ps_vat) * 100.0 / nullif(@disposableamount, 0) as numeric(5, 2)) as varchar(10)) + '%',
+ sum(ps.ps_qty),
+ null
+ from ps
+ join pr on ps.ps_prcode = pr.pr_code
+ join kind on pr.kind_code = kind.kind_code
+ where ps.ps_date >= @start_date and ps.ps_date < @end_date
+ and kind.kind_code in (select kind_code from disposable_analysis_info where gubun in ('wevape', 'etc'))
+ group by pr.pr_name
+ having sum(ps.ps_amt + ps.ps_vat) > 0;
+
+ -- 총 수량 계산해서 일회용매출에 반영
+ declare @total_qty int;
+ select @total_qty = sum(ps.ps_qty)
+ from ps
+ join pr on ps.ps_prcode = pr.pr_code
+ join kind on pr.kind_code = kind.kind_code
+ where ps.ps_date >= @start_date and ps.ps_date < @end_date
+ and kind.kind_code in (select kind_code from disposable_analysis_info where gubun in ('wevape', 'etc'));
+
+ update #product_sales
+ set product_qty = @total_qty
+ where product_name = @year_month + ' 일회용매출';
+
+ -- 최종 결과
+ select
+ product_name as 상품명,
+ product_sales as 매출,
+ product_ratio as 비율,
+ product_qty as 개수,
+ row_color
+ from #product_sales
+ order by
+ case
+ when product_name = @year_month + ' 매출' then 1
+ when product_name = @year_month + ' 일회용매출' then 2
+ when product_name = '----- 일회용매출 -----' then 3
+ when (product_name like '% 총 매출') then 4
+ when product_name = '그 외 매출' then 5
+ when product_name = '----- 상세내역 -----' then 6
+ else 7
+ end,
+ product_name;
+
+ drop table #product_sales;
+end;
+
+
+
+
+
+
+
+create procedure disposable_analysis
+ @year_month nvarchar(7) -- "YYYY-MM" 형식
+as
+begin
+ -- 변수 선언
+ declare @standard_month nvarchar(7);
+ set @standard_month = '2025-05';
+
+ if @year_month < @standard_month
+ begin
+ -- 기준월 이전이면 old 프로시저 호출
+ exec disposable_analysis_old @year_month;
+ end
+ else
+ begin
+ -- 기준월 이후거나 같으면 new 프로시저 호출
+ exec disposable_analysis_new @year_month;
+ end
+end;
+
+
+
+
+
+EXEC disposable_analysis_old @year_month = '2025-05';
+EXEC disposable_analysis_new @year_month = '2025-05';
+EXEC disposable_analysis @year_month = '2025-05';
+drop procedure disposable_analysis_new;
\ No newline at end of file