-- 4.9.6.1
alter table [Order] add TaxRatio numeric(18,4) null
alter table [Order] add TaxType int null
alter table [Order] add TaxAmount numeric(18,2) null
go

alter table Quotation add TaxRatio numeric(18,4) null
alter table Quotation add TaxType int null
alter table Quotation add TaxAmount numeric(18,2) null
go

alter table MaterialTechnic add CostPrice numeric(18,4) null
go

update MaterialTechnic set CostPrice=0
alter table MaterialTechnic alter column CostPrice numeric(18,4) not null
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) * 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 FactoryOrderNo from OrderFactoryOrder 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 FactoryOrderNo from OrderFactoryOrder 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

insert into SystemSetting(SettingName,SettingValue1,SettingValue2)
values 
	('CompanyConfiguration','EnableWorkFlow','') 
		
insert into CompanyConfiguration
	select 'EnableWorkFlow','true'
	
update configuration set SettingValue='4.9.6' where SettingName='DatabaseVersion'
 go
 
 