销售铜含量统计

drop procedure [dbo].[zp_z_BOM_FU]
drop function [dbo].[F_GetChar]


create  function [dbo].[F_GetChar]		--创建函数去掉非字符的数字
(
@No varchar(100)
)
RETURNS varchar(100)
AS
BEGIN
WHILE PATINDEX('%[^.]%',@No)>0
BEGIN
SET @No = STUFF(@No,PATINDEX('%[^.]%',@No),1,'') --删掉一个非字符的数字,循环结束,剩余的为数字部分
END
RETURN CONVERT(varchar(100),@No)
END

--select dbo.F_GetChar('wwww23')

/*--1、创建供应商维护价格视图z_view_SupplyRatePrice,代码如下:*/
create view [dbo].[z_view_SupplyRatePrice]
as
select 
u1.*,
t1.FValueAddRate,					--供应商表.增值税率
t2.FCoefficient,					--计量单位表.系数
t3.FExchangeRate					--币别表.汇率
from t_supplyentry u1				--供应商供货明细表
inner join t_supplier t1			--供应商表
	on t1.fitemid = u1.fsupid		--供应商表.供应商内码 = 供应商供货明细表.供应商内码
inner join t_MeasureUnit t2			--计量单位表
	on t2.FItemID = u1.Funitid		--计量单位表.单位内码 = 供应商供货明细表.单位内码
inner join t_currency t3			--币别表
	on t3.Fcurrencyid = u1.FCyID	--币别表.币别内码 = 供应商供货明细表.币别


/*2、创建实际入库价格视图z_view_OrderPrice,代码如下:*/

create  view
[dbo].[z_view_OrderPrice]
as
select 
t1.finterid,		--出入库单据表.单据内码
t1.fdate,			--出入库单据表.单据日期
case when Forderinterid > 0 then 1 else 0 end as Forderinterid, 
-- 出入库单据分录表.订单内码 大于0 则为1 否则为 0
t2.Fitemid,			--出入库单据分录表.物料内码
case t1.ftrantype when 1 then t2.Fprice 
				  when 5 then FProcessprice end as Fprice
-- 出入库单据表.单据类型 为 1 则取 出入库单据分录表.单价 ,为 5 则取委外加工入库增加加工单价
from
ICStockBill t1							--出入库单据表
inner join ICStockBillEntry t2			--出入库单据分录表
	on t1.finterid = t2.finterid			--出入库单据表.单据内码 = 出入库单据分录表.单据内码
inner join t_MeasureUnit t3				--计量单位表
	on t3.Fitemid = t2.Funitid			--计量单位表.单位内码 = 出入库单据分录表.单位内码
where t1.ftrantype in (1,5)				--出入库单据表.单据类型
and t1.FROB = 1							--出入库单据表.红蓝字  1 蓝字,-1 红字






/*--3、创建存储过程zp_z_JTT_HZ2019_FU,代码如下:*/

create procedure [dbo].[zp_z_JTT_HZ2019_FU]  

as  
set nocount on  
/*  
declare @Fitemid int  
set @Fitemid = (select top 1 Fitemid from t_icitem where Fnumber = @Fnumber)  
*/  
/**********************************************************************/  
  
   
/*********************************************************************/  

create table #z_productCost  --创建临时表:产品成本表
( 
Finterid int not null IDENTITY (1, 1) primary key,	--单据内码
FInterID_JT int,									--销售订单内码
FCustID varchar(255),								--购货单位
FBillNo varchar(255),								--销售订单编号
FDateZD datetime,									--单据日期
FStatus_JT smallint,								--状态 0-未审核,1-审核
FHeadSelfS0152 decimal(18,6),						--JT铜价
FEntryID_JT int,									--分录号
FOrderBillNo nvarchar (510),						--客户订单号
FItemID int,										--产品代码
FQty_JT decimal(18,6),								--数量
FUnitID_JT int,										--单位
FDate datetime,										--交货日期
FStockQty decimal(18,6),							--出库数量
FEntrySelfS0167 datetime,							--出货日期   
FSupplyAmount numeric(18,6) default 0,				--供应商维护金额
FStockAmount numeric(18,6) default 0,				--最新入库价金额
WorkingHours numeric(18,6) default 0,				--工时
LaborCost numeric(18,6) default 0,					--人工
JT_TL numeric(18,6) default 0,						--含铜量
JT_TJ numeric(18,6) default 0						--JT铜价
)
  
create table #z_productCostEntry  --创建临时表:产品成本分录表
(
Finterid int not null,							--单据内码
sn nvarchar(100) default '1',					--层级 
FEntryID nvarchar(100) default '0' not null,	--BOM单分录号
fitemid int,									--物料内码 
Flevel nvarchar(100) default '0',				--层次  
fqty decimal(18,6) default '1',					--用量, 标准单位  
FAuxqty decimal(18,6),							--显示用量  
FunitID int,									--单位  
FErpClsID int default '0',						--物料属性
FMaterType int,									--领料类型  
FError int default 0,							--错误状态  
Fstatus int,									--审核状态  0.未审核 1.已审核  
FUseStatus int,									--使用状态  1072.已使用 1073.未使用  
FBOMSkip int,									--是否跳层  
FIsKeyItem bit,									--关键件  
FForbid int,									--禁用  
FEnterTime datetime,							--最近修改时间  
FSupplyPrice numeric(18,6) default 0,			--供应商维护单价
FSupplyAmount numeric(18,6) default 0,			--供应商维护金额
FSupplyPriceType int,							--供应商维护价格来源
FStockPrice numeric(18,6) default 0,			--最新入库单价
FStockAmount numeric(18,6) default 0,			--最新入库价金额
FStockPriceType int,							--入库价价格来源
FMinStockPrice numeric(18,6) default 0,			--最新入库单价
WorkingHours numeric(18,6) default 0,			--工时
LaborCost numeric(18,6) default 0,				--人工
JT_TL numeric(18,6) default 0,					--JT铜量
JT_TJ numeric(18,6) default 0					--JT铜价
)


create table #Product  --创建临时表:产品  
(  
FParentID int default 1,  
sn nvarchar(100) default '1',					--层级 
FEntryID nvarchar(100) default '0' not null,	--BOM单分录号
fitemid int,									--物料内码  
Flevel nvarchar(100) default '0',				--层次  
fqty decimal(18,6) default '1',					--用量, 标准单位  
FAuxqty decimal(18,6),							--显示用量  
FunitID int,									--单位  
FErpClsID int default '0',						--物料属性
FMaterType int,									--领料类型  
FError int default 0,							--错误状态  
Fstatus int,									--审核状态  0.未审核 1.已审核  
FUseStatus int,									--使用状态  1072.已使用 1073.未使用  
FBOMSkip int,									--是否跳层  
FIsKeyItem bit,									--关键件  
FForbid int,									--禁用  
FEnterTime datetime								--最近修改时间  
)  
   
create table #ProductEntry  --创建临时表:产品分录  
(  
FParentID int default 1,  
sn nvarchar(100) default '1',					--层级 
FEntryID nvarchar(100) default '0' not null,	--BOM单分录号
fitemid int,									--物料内码  
Flevel nvarchar(100) default '0',				--层次  
fqty decimal(18,6) default '1',					--用量, 标准单位  
FAuxqty decimal(18,6),							--显示用量  
FunitID int,									--单位  
FErpClsID int default '0',						--物料属性
FMaterType int,									--领料类型  
FError int default 0,							--错误状态  
Fstatus int,									--审核状态  0.未审核 1.已审核  
FUseStatus int,									--使用状态  1072.已使用 1073.未使用  
FBOMSkip int,									--是否跳层  
FIsKeyItem bit,									--关键件  
FForbid int,									--禁用  
FEnterTime datetime								--最近修改时间  
)
   
  
/**************************************************************************/  
  
insert into #z_productCost				--向临时表:产品成本 插入记录  
(
FInterID_JT,							--销售订单内码
FCustID,								--购货单位
FBillNo,								--销售订单编号
FDateZD,								--单据日期
FStatus_JT,								--状态 0-未审核,1-审核
FHeadSelfS0152,							--JT铜价
FEntryID_JT,							--分录号
FOrderBillNo,							--客户订单号
FItemID,								--产品代码
FQty_JT,								--数量
FUnitID_JT,								--单位
FDate,									--交货日期
FStockQty,								--出库数量
FEntrySelfS0167							--出货日期   
)
select   
U1.FInterID,					--销售订单内码
KH.FName,						--购货单位
U1.FBillNo,						--销售订单编号
U1.FDate,						--单据日期
U1.FStatus,						--状态 0-未审核,1-审核
U1.FHeadSelfS0152,				--JT铜价
U2.FEntryID,					--分录号
U2.FOrderBillNo,				--客户订单号
U2.FItemID,						--产品代码
U2.FQty,						--数量
U2.FUnitID,						--单位
U2.FDate,						--交货日期
U2.FStockQty,					--出库数量
U2.FEntrySelfS0167				--出货日期
from SEOrder U1
inner join
SEOrderEntry U2
on U1.FInterID = U2.FInterID
left outer join t_Organization KH	--客户表
on U1.FCustID = KH.FItemID
where U2.FMrpClosed = 0			--行业务关闭标志, 0-未关闭,1关闭
and U1.FCancellation = 0		--作废,0-未作废,1-作废
order by KH.FName, U1.FBillNo, U2.FEntryID, U1.FDate



 
insert into #Product		--向临时表:产品 插入记录
(FParentID,		--单据内码
sn,
Fqty, 
FItemid		--物料内码  
)
select
FInterid,	--单据内码
 '1', 
 1, 
FItemid		--物料内码  
from #z_ProductCost

/*			
insert into #Product		--向临时表:产品 插入记录
(FParentID, sn, Fitemid, Fqty, FerpClsID, FError)
select
FInterid, '1', Fitemid, 1, 2, FError
from #z_ProductCost
*/
/********************************************************************/
 /*  
select * from #Product  --调试代码  
select * from #ProductEntry  --调试代码  
select * from #z_ProductCost  --调试代码  
select * from #z_productCostEntry  --调试代码  
  
delete #Product  --调试代码  
delete #ProductEntry  --调试代码  
delete #z_ProductCost  --调试代码  
delete #z_productCostEntry  --调试代码  
  
drop table #Product  --调试代码  
drop table #ProductEntry  --调试代码  
drop table #z_ProductCost    --调试代码  
drop table #z_productCostEntry
*/  


declare @Level int  
set @Level = 1  
  
insert into #ProductEntry  --向产品分录 插入记录  
(  
FParentID,						--单据内码  
sn,								--层级  
FItemid,	--物料内码 
Flevel,		--第几层  
fqty,		--用量, 标准单位  
FAuxqty,	--显示用量  
FunitID,	--单位  
FErpClsID,	--物料属性  
FError,		--错误状态  
Fstatus,	--审核状态  0.未审核 1.已审核  
FUseStatus, --使用状态  1072.已使用 1073.未使用  
FBOMSkip,	--是否跳层  
FIsKeyItem, --关键件  
FForbid,	--禁用  
FEnterTime	--最近修改时间  
)  
select   
FParentID,						--单据内码
sn,								--层级  
FItemid,	--物料内码
Flevel,		--第几层  
fqty,		--用量, 标准单位  
FAuxqty,	--显示用量  
FunitID,	--单位  
FErpClsID,	--物料属性  
FError,		--错误状态  
Fstatus,	--审核状态  0.未审核 1.已审核  
FUseStatus, --使用状态  1072.已使用 1073.未使用  
FBOMSkip,	--是否跳层  
FIsKeyItem, --关键件  
FForbid,	--禁用  
FEnterTime	--最近修改时间  
 from #Product  --产品表  
--inner join icbom t1 on t1.fitemid = u1.fitemid and t1.FUsestatus = 1072  --BOM表 t1 物料内码,FUsestatus 使用状态  
--inner join icbom t2 on u1.fitemid = t2.fitemid  --BOM分录表 t2 所属BOM内码  
--inner join t_icitem t3 on t2.fitemid = t3.fitemid  --物料表 t3 物料内码  
where sn = '1'  

insert into #z_productCostEntry  --向临时表:产品成本分录表 插入记录  
(  
Finterid,								--单据内码
sn,										--层级  
FItemid,	--物料内码  
Flevel,		--第几层  
fqty,		--用量, 标准单位  
FAuxqty,	--显示用量  
FunitID,	--单位  
FErpClsID,	--物料属性  
FError,		--错误状态  
Fstatus,	--审核状态  0.未审核 1.已审核  
FUseStatus, --使用状态  1072.已使用 1073.未使用  
FBOMSkip,	--是否跳层  
FIsKeyItem, --关键件  
FForbid,	--禁用  
FEnterTime	--最近修改时间  
)  
select  
FParentID,								--单据内码
sn,			--层级  
FItemid,	--物料内码
Flevel,		--第几层  
fqty,		--用量, 标准单位  
FAuxqty,	--显示用量  
FunitID,	--单位  
FErpClsID,	--物料属性  
FError,		--错误状态  
Fstatus,	--审核状态  0.未审核 1.已审核  
FUseStatus, --使用状态  1072.已使用 1073.未使用  
FBOMSkip,	--是否跳层  
FIsKeyItem, --关键件  
FForbid,	--禁用  
FEnterTime	--最近修改时间  
from #ProductEntry  --产品分录表  

delete #ProductEntry  --清空产品分录表  
  
/*  
select * from #Product  --调试代码  
select * from #ProductEntry  --调试代码  
select * from #z_productCostEntry  --调试代码  
select * from #z_productCostEntry  --调试代码  
  
delete #Product  --调试代码  
delete #ProductEntry --调试代码  
drop table #z_productCostEntry  --调试代码  
 */  
   
while @level<20 and exists(select * from #Product)  
   
begin  
   
insert into #ProductEntry  --向产品分录 插入记录  
(  
FParentID,  --单据内码 
sn,			--层级  
Flevel,		--第几层
FEntryID,	--BOM单分录号
Fitemid,	--物料内码  
fqty,		--用量, 标准单位  
FAuxqty,	--显示用量  
FunitID,	--单位  
FerpClsID,  --物料属性
FMaterType,	--领料类型  
FError,		--错误状态  
Fstatus,	--审核状态  0.未审核 1.已审核  
FUseStatus, --使用状态  1072.已使用 1073.未使用  
FBOMSkip,	--是否跳层  
FIsKeyItem, --关键件  
FForbid,	--是否禁用  
FEnterTime  --最近修改时间  
)  
select   
u1.FParentID,  
u1.sn+'.'+right('000'+cast(t2.fentryid as nvarchar),3),  
--u1.sn+right('.'+cast(t2.fentryid as nvarchar),3),				 --层级  
dbo.F_GetChar (u1.Flevel) + '.' + convert(varchar(100),@Level),   --层次 
t2.FEntryID,		--BOM单分录号
t2.fitemid,			--子项物料内码  
u1.fqty*t2.fqty,	--*(1+FScrap/100),  --用量 FScrap:损耗率
--t2.fqty,			--用量, 标准单位  
t2.FAuxqty,			--显示用量  
t2.FunitID ,		--单位内码  
t3.FerpClsID,		--物料属性
t2.FMaterielType,	--领料类型   
u1.FError,			--错误状态  
t1.Fstatus,			--审核状态  0.未审核 1.已审核  
t1.FUseStatus,		--使用状态  1072.已使用 1073.未使用  
t1.FBOMSkip,		--是否跳层  
t3.FIsKeyItem,		--关键件  
t1.FForbid,			--是否禁用  
t1.FEnterTime		--最近修改时间  
 from #Product u1	--产品表  
inner join icbom t1 --BOM表
on t1.fitemid = u1.fitemid --and t1.FUsestatus = 1072  --BOM表 t1 物料内码,FUsestatus 使用状态  
inner join icbomchild t2	--BOM分录表
on t1.finterid = t2.finterid  --BOM分录表 t2 所属BOM内码  
inner join t_icitem t3		--物料表
on t2.fitemid = t3.fitemid	--物料表 t3 物料内码  
  
update #ProductEntry  --更新 产品分录   
set FError = 1  --错误状态  
where not exists(select * 
				from icbom u1 
				where u1.fitemid = #ProductEntry.fitemid 
				and u1.FUsestatus = 1072)  
 and FerpClsid in (2,3)  
   
   
insert into #z_productCostEntry  --向临时表:产品成本分录表 插入记录  
(  
Finterid,  
sn,			--层级 
FEntryID,	--BOM单分录号 
fitemid,	--物料内码  
Flevel,		--第几层  
fqty,		--用量, 标准单位  
FAuxqty,	--显示用量  
FunitID,	--单位  
FErpClsID,	--物料属性
FMaterType,	--领料类型   
FError,		--错误状态  
Fstatus,	--审核状态  0.未审核 1.已审核  
FUseStatus, --使用状态  1072.已使用 1073.未使用  
FBOMSkip,	--是否跳层  
FIsKeyItem, --关键件  
FForbid,	--禁用  
FEnterTime,	--最近修改时间
JT_TL		--含铜量
)  
select  
FParentID,  
sn,			--层级
FEntryID,	--BOM单分录号  
fitemid,	--物料内码  
Flevel,		--第几层   
fqty,		--用量, 标准单位  
FAuxqty,	--显示用量  
FunitID,	--单位  
FErpClsID,	--物料属性
FMaterType,	--领料类型   
FError,		--错误状态  
Fstatus,	--审核状态  0.未审核 1.已审核  
FUseStatus, --使用状态  1072.已使用 1073.未使用  
FBOMSkip,	--是否跳层  
FIsKeyItem, --关键件  
FForbid,	--禁用  
FEnterTime,	--最近修改时间
JT_TL = (CASE fitemid when '201431' then FQty else 0 end)  
from #ProductEntry  --产品分录表  
 /*  
select * from #Product  --调试代码  
select * from #ProductEntry  --调试代码  
select * from #z_ProductCost  --调试代码  
select * from #z_productCostEntry  --调试代码  
  
delete #Product  --调试代码  
delete #ProductEntry  --调试代码  
delete #z_ProductCost  --调试代码  
delete #z_productCostEntry  --调试代码  
  
drop table #Product  --调试代码  
drop table #ProductEntry  --调试代码  
drop table #z_productCostEntry    --调试代码  
  
*/  
delete #Product  ---清空产品表  
   
insert into #Product  --向临时表:产品成本分录表 插入记录  
(  
FParentID,  
sn,			--层级 
FEntryID,	--BOM单分录号 
fitemid,	--物料内码  
Flevel,		--第几层  
fqty,		--用量, 标准单位  
FAuxqty,	--显示用量  
FunitID,	--单位  
FErpClsID,	--物料属性
FMaterType,	--领料类型  
FError,		--错误状态  
Fstatus,	--审核状态  0.未审核 1.已审核  
FUseStatus, --使用状态  1072.已使用 1073.未使用  
FBOMSkip,	--是否跳层  
FIsKeyItem, --关键件  
FForbid,	--禁用  
FEnterTime	--最近修改时间  
)  
select  
FParentID,  
sn,			--层级
FEntryID,	--BOM单分录号  
fitemid,	--物料内码  
dbo.F_GetChar (Flevel),  --第几层  
fqty,		--用量, 标准单位  
FAuxqty,	--显示用量  
FunitID,	--单位  
FErpClsID,	--物料属性
FMaterType,	--领料类型   
FError,		--错误状态  
Fstatus,	--审核状态  0.未审核 1.已审核  
FUseStatus, --使用状态  1072.已使用 1073.未使用  
FBOMSkip,	--是否跳层  
FIsKeyItem, --关键件  
FForbid,	--禁用  
FEnterTime	--最近修改时间  
from #ProductEntry  --产品分录表  
--where FerpClsid in (2,3,5) and Ferror = 0  
/*  
select * from #Product --调试代码  
select * from #z_productCostEntry --调试代码  
*/  
   
delete #ProductEntry  --清空产品分录表  
   
set @level = @level+1  --@level 加1  
end  
  
  
/***********************************************************************/   
  
drop table #Product  --删除 产品表  
drop table #ProductEntry  --删除产品分录表  
/***********************************************************************/ 

 /*  
select * from #Product  --调试代码  
select * from #ProductEntry  --调试代码  
select * from #z_ProductCost  --调试代码  
select * from #z_productCostEntry  --调试代码  
  
delete #Product  --调试代码  
delete #ProductEntry  --调试代码  
delete #z_ProductCost  --调试代码  
delete #z_productCostEntry  --调试代码  
  
drop table #Product  --调试代码  
drop table #ProductEntry  --调试代码  
drop table #z_productCostEntry    --调试代码  
  
*/   

 /*更新供应商维护单价*/
 
 update #z_productCostEntry			--更新产品成本分录表 供应商维护单价
 set FSupplyPrice  = 		--供应商维护单价
 (select top 1 (FPrice * FExchangeRate / (1 + FValueAddRate / 100) / FCoefficient )
 		-- 单价 * 币别表.汇率 / ( 1 + 供应商表.增值税率 / 100 ) / 计量单位表.系数
	from z_view_SupplyRatePrice t9		--供应商维护价格视图
	where t9.FItemID = t1.fitemid		--供应商维护价格视图.物料内码 = BOM.物料内码
	and FDisableDate > GETDATE ()		--供应商维护价格视图.失效日期 > 当前日期
	and FQuoteTime  < GETDATE ()		--供应商维护价格视图.生效日期 < 当前日期
	order by FQuoteTime desc),			--按 生效日期 降序排序
	FSupplyPriceType = 1				----供应商维护价格来源,1.供应商维护单价  2.最新入库价  3.计划单价  4.其它  无单价
	
from #z_productCostEntry t1
	where t1.FSupplyPrice = 0			--产品成本分录.供应商价格 = 0
	and FMaterType = 371				--领料类型
	and FerpClsid in (1,3)
	and exists(select top 1 FPrice		--判断有无最新单价
				from z_view_SupplyRatePrice t10		--供应商维护价格视图
				where t10.FItemID = t1.fitemid		--供应商维护价格视图.物料内码 = BOM.物料内码
				and FDisableDate > GETDATE ()		--供应商维护价格视图.失效日期 > 当前日期
				and FQuoteTime < GETDATE ())		--供应商维护价格视图.生效日期 < 当前日期
   
   
update #z_productCostEntry			--更新产品成本分录表 供应商维护单价
 set FSupplyPrice  = 		--供应商维护单价
 (select top 1 FPrice 
	from z_view_OrderPrice t9			--实际入库价格视图
	where t9.FItemID = t1.fitemid		--实际入库价格视图.物料内码 = BOM.物料内码
	order by Fdate desc),				--按 单据日期 降序排序
	FSupplyPriceType = 2				--供应商维护价格来源,1.供应商维护单价  2.最新入库价  3.计划单价  4.其它  无单价
from #z_productCostEntry t1
	where t1.FSupplyPrice = 0			--产品成本分录.供应商价格 = 0
	and FMaterType = 371				--领料类型
	and FerpClsid in (1,3)
	and exists(select top 1 FPrice		--判断有无最新单价
				from z_view_SupplyRatePrice t10		--供应商维护价格视图
				where t10.FItemID = t1.fitemid)		--实际入库价格视图.物料内码 = BOM.物料内码
	
  /*更新最新入库单价*/

update #z_productCostEntry			--更新产品成本分录表 最新入库单价
 set FStockPrice  = 		--最新入库单价
 (select top 1 FPrice 
	from z_view_OrderPrice t9			--实际入库价格视图
	where t9.FItemID = t1.fitemid		--实际入库价格视图.物料内码 = BOM.物料内码
	order by Forderinterid desc, fdate desc),
	--排序 按 出入库单据分录表.订单内码 降序,单据日期 降序排序
	FStockPriceType = 2				----入库价格来源,1.供应商维护单价  2.最新入库价  3.计划单价  4.其它  无单价
from #z_productCostEntry t1
	where t1.FStockPrice = 0			--产品成本分录表.最新入库单价 = 0
	and FMaterType = 371				--领料类型
	and FerpClsid in (1,3)
	and exists(select top 1 FPrice		--判断有无最新入库单价
				from z_view_OrderPrice t10			--实际入库价格视图
				where t10.FItemID = t1.fitemid)		--实际入库价格视图.物料内码 = BOM.物料内码

   
update #z_productCostEntry			--更新产品成本分录表 最新入库单价
 set FStockPrice  = 		--最新入库单价
 (select top 1 FPrice * FExchangeRate /(1 + FValueAddRate / 100) / FCoefficient
		-- 单价 * 币别表.汇率 / ( 1 + 供应商表.增值税率 / 100 ) / 计量单位表.系数
	from z_view_SupplyRatePrice t9		--供应商维护价格视图
	where t9.FItemID = t1.fitemid		--供应商维护价格视图.物料内码 = BOM.物料内码
	and FDisableDate > GETDATE ()		--供应商维护价格视图.失效日期 > 当前日期
	and FQuoteTime  < GETDATE ()		--供应商维护价格视图.生效日期 < 当前日期
	order by FQuoteTime  desc),			--按 生效日期 降序排序
	FStockPriceType = 1				--入库价格来源,1.供应商维护单价  2.最新入库价  3.计划单价  4.其它  无单价
from #z_productCostEntry t1
	where t1.FStockPrice = 0			--产品成本分录表.最新入库单价 = 0
	and FMaterType = 371				--领料类型
	and FerpClsid in (1,3)
	and exists(select top 1 FPrice		--判断有无最新单价
				from z_view_SupplyRatePrice t10		--供应商维护价格视图
				where t10.FItemID = t1.fitemid		--实际入库价格视图.物料内码 = BOM.物料内码
				and FDisableDate > GETDATE ()		--供应商维护价格视图.失效日期 > 当前日期
				and FQuoteTime < GETDATE ())		--供应商维护价格视图.失效日期 > 当前日期
  
  

 
  
/*计算金额*/  
update #z_productCostEntry						--更新产品成本分录表
set FSupplyAmount = fqty * FSupplyPrice,		--供应商维护金额  = 
	FStockAmount = fqty * FStockPrice			--最新入库价金额  = 
from #z_productCostEntry 


/*更新工时*/ 
update #z_productCostEntry			--产品成本分录表
set WorkingHours = d.f1,			--标准工时
	LaborCost = d.f1 * 20.3			--人工
from #z_productCostEntry a
join (select R1.FItemID, c.FInterID, c.f1					--物料内码,表头内码,汇总工时
		from t_Routing R1									--工艺路线表
		join(select FInterID, SUM(FPersonStdTimeRun) f1		--按表头内码汇总标准人工运行工时
				from t_RoutingOper							--工艺路线表体
				group by FInterID ) c						--表头内码
		on R1.FInterID = c.FInterID ) D
	on a.fitemid = d.FItemID								--BOM.物料内码 = 物料内码
	

 /*****************************************************************/
 
update #z_ProductCost		--更新产品成本
/*汇总供应商维护金额更新到产品成本表*/
set FSupplyAmount = (		--		--供应商维护金额
					select top 1 FSupplyAmount		--供应商维护金额
					from (
							select 
							finterid,
							sum(FSupplyAmount) as FSupplyAmount,		--供应商维护金额
							sum(FStockAmount) as FStockAmount,			--最新入库价金额
							sum(WorkingHours) as WorkingHours,			--标准工时
							SUM(LaborCost) as LaborCost,				--人工
							SUM(JT_TL) as JT_TL,						--铜量
							min(FSupplyPrice) as FMinSupplyPrice,		--供应商维护单价
							min (FStockPrice) as FminStockPrice			--最新入库单价
							from #z_productCostEntry					--产品成本分录表
							where FMaterType = 371 and FerpClsid in (1,3)
							group by finterid) t1						--单据内码
					where t1.Finterid = #z_ProductCost.Finterid),		--单据内码

/*汇总最新入库金额更新到产品成本表*/
FStockAmount = (
				select top 1 FStockAmount				--最新入库金额
				from (		
						select 
						finterid,
						sum(FSupplyAmount) as FSupplyAmount,		--供应商维护金额
						sum(FStockAmount) as FStockAmount,			--最新入库价金额
						sum(WorkingHours) as WorkingHours,			--标准工时
						SUM(LaborCost) as LaborCost,				--人工
						SUM(JT_TL) as JT_TL,						--铜量
						min(FSupplyPrice) as FMinSupplyPrice,		--供应商维护单价
						min (FStockPrice) as FminStockPrice			--最新入库单价
						from #z_productCostEntry		--产品成本分录表
						where FMaterType = 371 and FerpClsid in (1,3)
						group by finterid) t1 
				where t1.Finterid = #z_ProductCost.Finterid),


/*汇总标准工时更新到产品成本表*/
WorkingHours  = (
					select top 1 WorkingHours				--标准工时
					from (		
							select 
							finterid,
							sum(FSupplyAmount) as FSupplyAmount,		--供应商维护金额
							sum(FStockAmount) as FStockAmount,			--最新入库价金额
							sum(WorkingHours) as WorkingHours,			--标准工时
							SUM(LaborCost) as LaborCost,				--人工
							SUM(JT_TL) as JT_TL,						--铜量
							min(FSupplyPrice) as FMinSupplyPrice,		--供应商维护单价
							min (FStockPrice) as FminStockPrice			--最新入库单价
							from #z_productCostEntry		--产品成本分录表
							--where FMaterType = 371 --and FerpClsid in (1,3)
							group by finterid) t1 
					where t1.Finterid = #z_ProductCost.Finterid),

/*汇总人工更新到产品成本表*/
LaborCost  = (
				select top 1 LaborCost		--人工
				from (		
						select 
						finterid,
						sum(FSupplyAmount) as FSupplyAmount,		--供应商维护金额
						sum(FStockAmount) as FStockAmount,			--最新入库价金额
						sum(WorkingHours) as WorkingHours,			--标准工时
						SUM(LaborCost) as LaborCost,				--人工
						SUM(JT_TL) as JT_TL,						--铜量
						min(FSupplyPrice) as FMinSupplyPrice,		--供应商维护单价
						min (FStockPrice) as FminStockPrice			--最新入库单价
						from #z_productCostEntry		--产品成本分录表
						--where FMaterType = 371 and FerpClsid in (1,3)
						GROUP BY finterid) t1 
				where t1.Finterid = #z_ProductCost.Finterid),

/*汇总含铜量更新到产品成本表*/
JT_TL  = (
				select top 1 JT_TL		--含铜量
				from (		
						select 
						finterid,
						sum(FSupplyAmount) as FSupplyAmount,		--供应商维护金额
						sum(FStockAmount) as FStockAmount,			--最新入库价金额
						sum(WorkingHours) as WorkingHours,			--标准工时
						SUM(LaborCost) as LaborCost,				--人工
						SUM(JT_TL) as JT_TL,						--铜量
						min(FSupplyPrice) as FMinSupplyPrice,		--供应商维护单价
						min (FStockPrice) as FminStockPrice			--最新入库单价
						from #z_productCostEntry		--产品成本分录表
						--where FMaterType = 371 and FerpClsid in (1,3)
						GROUP BY finterid) t1 
				where t1.Finterid = #z_ProductCost.Finterid)
				

				


 
 
  /*  
select * from #Product  --调试代码  
select * from #ProductEntry  --调试代码  
select * from #z_ProductCost  --调试代码  
select * from #z_productCostEntry  --调试代码  
  
delete #Product  --调试代码  
delete #ProductEntry  --调试代码  
delete #z_ProductCost  --调试代码  
delete #z_productCostEntry  --调试代码  
  
drop table #Product  --调试代码
drop table #z_ProductCost 
drop table #ProductEntry  --调试代码  
drop table #z_productCostEntry    --调试代码  
  
*/   

 /*****************************************************************/
/*
update t1
set t1.FError = t1.FError+2		--错误状态
from #z_ProductCost t1 ,#z_productCostEntry t2
where t1.finterid = t2.finterid and t2.FMinStockPrice = 0
*/

select
U1.Finterid 序号,								--单据内码
U1.FCustID 购货单位,							--购货单位
U1.FBillNo JT编号,								--销售订单编号
U1.FDate 日期,									--单据日期
U1.FStatus_JT 状态,								--状态 0-未审核,1-审核
U1.FHeadSelfS0152 铜价,							--JT铜价
U1.FEntryID_JT 行号,							--分录号
U1.FOrderBillNo 客户订单号,						--客户订单号
F1.FNumber 产品代码,							--产品代码
F1.FName 产品名称,								--产品名称
U1.FQty_JT 数量,								--数量
D1.FName 单位,									--单位
U1.FDate 交货日期,								--交货日期
U1.FStockQty 出库数量,							--出库数量
U1.FEntrySelfS0167 出货日期,					--出货日期
U1.FQty_JT - U1.FStockQty 未出库数量,			--未出库数量
u1.WorkingHours 工时,
u1.LaborCost 人工,
--u1.FSupplyPrice 供应商维护单价,
u1.FSupplyAmount 供应商维护金额,
--u1.FStockPrice 最新入库单价,
u1.FStockAmount 最新入库价金额,
JT_TL 含铜量,						--JT铜量
JT_TJ JT铜价						--JT铜价
into #result
from
#z_ProductCost U1					--产品成本表
left outer join t_ICItem F1			--物料表
on U1.FItemID = F1.FItemID			--产品表.物料内码 = 物料表.物料内码
left outer join t_MeasureUnit D1	--计量单位表
on U1.FUnitID_JT = D1.FMeasureUnitID	--产品表.单位 = 单位表.单位内码



insert into #result
select
9999,		--序号
'',		--购货单位
'',		--JT编号
'',		--日期
'',		--状态
0,		--铜价
'',		--行号
'',		--客户订单号
'合计:',--产品代码
'',		--产品名称
0,		--数量
'',		--单位
'',		--交货日期
0,		--出库数量
'',		--出货日期
0,		--未出库数量
SUM(u1.WorkingHours),		--工时
SUM(u1.LaborCost),			--人工
SUM(u1.fsupplyamount),		--供应商维护金额
SUM(u1.fstockamount),		--最新入库金额
SUM(u1.JT_TL),				--含铜量
0
from #z_ProductCost u1

select * from #result
ORDER BY 序号 


drop table #result
drop table #z_productCost
drop table #z_productCostEntry



/*--K3查询分析工具调用存储过程,代码如下:*/

exec zp_z_JTT_HZ2019_FU 

drop procedure zp_z_JTT_HZ2019_FU




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值