ALTER PROCEDURE [dbo].[proc_Account]
@yymm CHAR(6),
@flag VARCHAR(10)
AS
DECLARE @sql varchar(5000)
DECLARE @opt_price INT
--SET @opt_price=(select isnull(sum(add_inDanga+((su-1)*add_optDanga*add_inDanga)),0) from Order_detail OD, order_options OO where OD.seq=OO.seq and OD.num=OO.num and OD.buy_code=@yymm + '-' + char(39) + ')
SET @sql='select Account_code, Account_no, '
IF @flag='buy' BEGIN
SET @sql=@sql +'(select buyCo_nameC from tbl_buyCo where buyCo_id=tbl_Account.Account_co) as make_co_name, 0 as co_tax, '
END
ELSE BEGIN
SET @sql=@sql +'case when Account_co=0 then ' + char(39) + '이미지사이트' + char(39) + ' when Account_co<>0 then (select custom_name from dbo.other_order where custom_code=tbl_Account.Account_co) end as make_co_name, '
SET @sql=@sql +'isnull((select co_tax from other_order where custom_code=tbl_Account.Account_co),0) as co_tax, '
END
SET @sql=@sql + 'Account_st, Account_en, '
IF @flag='buy' BEGIN
SET @sql=@sql + ' (isnull((select sum(case order_detail.ea_box when 0 then order_detail.in_danga*order_detail.su when 1 then order_detail.in_danga*order_detail.su*order_detail.box_su end) from order_detail,[order] where buy_code=' + char(39) +@yymm + '-' + char(39) + '+Convert(varchar(10), Account_no) and order_detail.seq=[order].seq and ( (left([order_detail].seq,1)<>' + Char(39) + 'C' + char(39) + ' and [order_detail].detail_flag in(70,80)) or (left([order_detail].seq,1)=' + Char(39) + 'C' + char(39) + ' and [order_detail].detail_flag>30) ) ),0))+(select isnull(sum(add_inDanga+((su-1)*add_optDanga*add_inDanga)),0) from Order_detail OD, order_options OO where OD.seq=OO.seq and OD.num=OO.num and OD.buy_code=' + char(39) +@yymm + '-' + char(39) + '+Convert(varchar(10), Account_no) ) as Account_price '
END
ELSE BEGIN
SET @sql=@sql + '((select sum(case when [order].custom_code=0 then total_pay when [order].custom_code<>0 then custom_danga*su end) from order_detail,[order] where order_detail.seq=[order].seq and sell_code=' + char(39) +@yymm + '-' + char(39) + '+Convert(varchar(10), Account_no) and ((left([order_detail].seq,1)<>' + Char(39) + 'C' + char(39) + ' and [order_detail].detail_flag in(70,80)) or (left([order_detail].seq,1)=' + Char(39) + 'C' + char(39) + ' and [order_detail].detail_flag>30) ) )+(select isnull(sum(add_sellDanga+((su-1)*add_optDanga*add_sellDanga)),0) from Order_detail OD, order_options OO where OD.seq=OO.seq and OD.num=OO.num and OD.sell_code=' + char(39) +@yymm + '-' + char(39) + '+Convert(varchar(10), Account_no)) ) as Account_price '
END
SET @sql=@sql + ', Convert(varchar(10),Account_date,112) as Account_date, Account_price as Account_price2, receive_pay, aFlag, (select Isnull(sum(Account_in-Account_out),0) from tbl_inOutPay where Account_idx=' + char(39) +@yymm + '-' + char(39) + '+Convert(varchar(10), Account_no)) as sum_pay, Account_memo, Account_tax, apply '
SET @sql=@sql + ', (select top 1 Summary from tbl_inOutPay where Account_idx= tbl_Account.Account_code + ' + Char(39) + '-' + Char(39) + ' + Convert(varchar(3),Account_no) and Account in(1,2)) as summary'
SET @sql=@sql + ' from tbl_Account '
SET @sql=@sql + ' where Account_flag=' + char(39) + @flag + char(39) + ' and Account_code=' + @yymm
SET @sql=@sql + ' order by Account_No asc'
--select @sql
exec(@sql)