ALTER procedure [dbo].[AutoGetOrderCostFee]	
	@OrderNo nvarchar(50),		
	@UpdateType int,
	@Fee numeric(18,2) output,
	@TotalCost numeric(18,2) output,
	@Message nvarchar(150) output
as
begin

	set @Fee=null
	set @TotalCost=null
	set @Message=null
	if not exists (select OrderNo  from [Order] where OrderNo=@OrderNo)
	begin		
		return
	end
	
	if not exists (select Id from OrderCost where OrderNo=@OrderNo)	
		insert into OrderCost(OrderNo) values (@OrderNo)
		
			
	if @UpdateType=2
	begin
		select @Fee=SUM(ISNULL(MaterialCostPrice,0) * Number) from OrderDetail where OrderNo=@OrderNo
		update OrderCost set FeeMaterial=ISNULL(@Fee,0) where OrderNo=@OrderNo
		set @Fee=ISNULL(@Fee,0)*100
	end	
	else if @UpdateType=4
	begin
		select @Fee=SUM(ISNULL(Amount,0)) from FactoryOrder 
			where FactoryOrderNo in (select OrderFactoryOrder.FactoryOrderNo from OrderFactoryOrder 
			inner join FactoryOrder on OrderFactoryOrder.FactoryOrderNo=FactoryOrder.FactoryOrderNo and FactoryOrder.IsDeleted=0 
			where OrderNo=@OrderNo)			
		update OrderCost set FeeFactory=ISNULL(@Fee,0) where OrderNo=@OrderNo
		
		declare @fOrderNo nvarchar(50)
		select @fOrderNo=FactoryOrderNo from OrderFactoryOrder 
			 where FactoryOrderNo in (select OrderFactoryOrder.FactoryOrderNo from OrderFactoryOrder 
			 inner join FactoryOrder on OrderFactoryOrder.FactoryOrderNo=FactoryOrder.FactoryOrderNo and FactoryOrder.IsDeleted=0 
			 where OrderNo=@OrderNo)
			 group by FactoryOrderNo having COUNT(FactoryOrderNo)>1
		
		if @fOrderNo is not null
			set @Message='提示：外协单['+@fOrderNo+']关联了多个客户订单，自动获取的外协费用可能不准确。'
			
		set @Fee=ISNULL(@Fee,0)*100
	end 
	else if @UpdateType=8
	begin
		select @Fee=SUM(ISNULL(FeeTax,0)) from [Order] where OrderNo=@OrderNo
		update OrderCost set FeeTax=ISNULL(@Fee,0) where OrderNo=@OrderNo
		set @Fee=ISNULL(@Fee,0)*100
	end
	
	select @TotalCost=TotalFee*100 from OrderCost where OrderNo=@OrderNo	 
	 
end
go


ALTER procedure [dbo].[AutoGetOrderCostFee]	
	@OrderNo nvarchar(50),		
	@UpdateType int,
	@Fee numeric(18,2) output,
	@TotalCost numeric(18,2) output,
	@Message nvarchar(150) output
as
begin

	set @Fee=null
	set @TotalCost=null
	set @Message=null
	if not exists (select OrderNo  from [Order] where OrderNo=@OrderNo)
	begin		
		return
	end
	
	if not exists (select Id from OrderCost where OrderNo=@OrderNo)	
		insert into OrderCost(OrderNo) values (@OrderNo)
		
			
	if @UpdateType=2
	begin
		select @Fee=SUM(ISNULL(MaterialCostPrice,0) * ISNULL(StockNumber,Number)) from OrderDetail where OrderNo=@OrderNo
		update OrderCost set FeeMaterial=ISNULL(@Fee,0) where OrderNo=@OrderNo
		set @Fee=ISNULL(@Fee,0)*100
	end	
	else if @UpdateType=4
	begin
		select @Fee=SUM(ISNULL(Amount,0)) from FactoryOrder 
			where FactoryOrderNo in (select OrderFactoryOrder.FactoryOrderNo from OrderFactoryOrder 
			inner join FactoryOrder on OrderFactoryOrder.FactoryOrderNo=FactoryOrder.FactoryOrderNo and FactoryOrder.IsDeleted=0 
			where OrderNo=@OrderNo)			
		update OrderCost set FeeFactory=ISNULL(@Fee,0) where OrderNo=@OrderNo
		
		declare @fOrderNo nvarchar(50)
		select @fOrderNo=FactoryOrderNo from OrderFactoryOrder 
			 where FactoryOrderNo in (select OrderFactoryOrder.FactoryOrderNo from OrderFactoryOrder 
			 inner join FactoryOrder on OrderFactoryOrder.FactoryOrderNo=FactoryOrder.FactoryOrderNo and FactoryOrder.IsDeleted=0 
			 where OrderNo=@OrderNo)
			 group by FactoryOrderNo having COUNT(FactoryOrderNo)>1
		
		if @fOrderNo is not null
			set @Message='提示：外协单['+@fOrderNo+']关联了多个客户订单，自动获取的外协费用可能不准确。'
			
		set @Fee=ISNULL(@Fee,0)*100
	end 
	else if @UpdateType=8
	begin
		select @Fee=SUM(ISNULL(FeeTax,0)) from [Order] where OrderNo=@OrderNo
		update OrderCost set FeeTax=ISNULL(@Fee,0) where OrderNo=@OrderNo
		set @Fee=ISNULL(@Fee,0)*100
	end
	
	select @TotalCost=TotalFee*100 from OrderCost where OrderNo=@OrderNo	 
	 
end

go

update Permission set PermissionName='订单收款流水' where PermissionName='订单收款明细'
update configuration set SettingValue='4.9.9' where SettingName='DatabaseVersion'
 go
 
 