USE [TOPLED2015]
GO
/****** Object:  StoredProcedure [dbo].[SP_配置方案库存价]    Script Date: 09/21/2019 09:42:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--------------------------------------------------------------
ALTER PROCEDURE [dbo].[SP_配置方案库存价]
(
    @品号 CHAR(20),
    @配置方案 CHAR(15),
    @TD001 char(4),
    @TD002 char(11),
    @TD003 char(4),
    @TC009 numeric(16,6),
    @TD037 numeric(5,4)
) 
as
BEGIN
    SET NOCOUNT ON;  
-----------------------------------------------------------------------
declare @UDF62 numeric(16,6),@i int,@name nvarchar(50) = ''
--取配置方案明细#A
select 
COPTR.TR001 as 品号
,COPTR.TR002 as 配置方案
,COPTR.TR003 as 层级
,COPTR.TR004 as 上阶主件
,a.MB025 as 属性一
,COPTR.TR009 as 品号2
,b.MB025 as 属性二
,COPTR.TR010/COPTR.TR011 as 用量
,COPTR.TR015 as 生效日期
,COPTR.TR016 as 失效日期
,COPTR.TR017 as 选择
,COPTR.TR019 as 纳入成本
into #A
from COPTR 
     left join INVMB as a on TR004 = a.MB001  
     left join INVMB as b on TR009 = b.MB001          
where TR001 = @品号 and TR002 = @配置方案

--select * from #A
------------------------------------------------------------------------
--取配置件下的所有品号
select distinct 上阶主件,属性一 ,品号2,属性二,选择 into #B from #A where 属性一 ='C'
--select * from #B

--取配置件下的所有品号用于#A反向

--1256.077988200000000000

select * into #C from
(
select a.* from #A as a
left join 
(select * from #A as a  where a.上阶主件 in (select 品号2 from #B where 属性二 in ('Y','M'))
union all
select a.* from #A as a left join (select * from #B where #B.属性二 = 'P') as b on a.上阶主件 = b.上阶主件 and a.品号2 = b.品号2 where b.上阶主件 is not null) as b
on a.层级 = b.层级 
where b.上阶主件 is null and a.属性二 = 'P' and a.属性一 = 'M'
union all
------------------------------------------------------------------------
--取虚设件下的采购品号
select * from #A as a  where a.上阶主件 in (select 品号2 from #B where 属性二 in ('Y','M') and 选择 = 'Y')
union all
select a.* from #A as a left join #B as b on a.上阶主件 = b.上阶主件 and a.品号2 = b.品号2 where b.选择 = 'Y' and b.属性二 ='P'
) as a 
left join INVMB on a.品号2 = MB001
where  (a.失效日期 = '' or a.失效日期 >= CONVERT(char(8),GETDATE(),112)) and (a.生效日期 = '' or a.生效日期 <= CONVERT(char(8),GETDATE(),112)) and 纳入成本 = 'Y'
-----------------------------------------------------------------------------------

select @i = COUNT(1) from #C where MB050 = 0

if @i > 0
    BEGIN
        set @name = CONVERT(nvarchar(8),@i) + ' 个材料没最近进价。'
    end
ELSE
    begin
        set @name = 'OK,全都有最近进价。'
    end

select @UDF62 = sum(用量 * MB050) from #C  
update COPTD set UDF62 = @UDF62/@TC009*(1+@TD037), UDF12 = @name where TD001=@TD001 and TD002=@TD002 and TD003=@TD003

/*
if @i > 0
        BEGIN
            TRUNCATE TABLE COPTDLogLog
            select * from COPTDLogLog
            insert into COPTDLogLog select 品号,配置方案,品号2,MB002,用量 from #C  where MB050 = 0
            ------
            declare @SQL nvarchar(max) = CONVERT(char(8),GETDATE(),112)+CONVERT(char(15),GETDATE(),114)
            --set @SQL ='D:\文件\' + rtrim(replace(@SQL,':',''))+'.xls'
            set @SQL ='D:\文件\' + @TD001 + '-' + @TD002 + '-' + @TD003 + '(' + @配置方案 + ')' + rtrim(replace(@SQL,':',''))+'.xls'
            SET @SQL = 'EXEC master..xp_cmdshell'+'''bcp "select * from TOPLED2015..COPTDLogLog" queryout ' + @SQL + ' -c -q -T'''
            EXEC(@SQL)
        end
*/
if object_id('tempdb..#A') is not null drop table #A;
if object_id('tempdb..#B') is not null drop table #B;
if object_id('tempdb..#C') is not null drop table #C;
end