ALTER procedure [dbo].[sp_NGprices_DONGd](@dongpriceindex varchar(32))
as begin
declare @pricemonth datetime
declare @priceday datetime
declare @surrogate int
declare @Iindex varchar(32)
declare @Lindex varchar(32)
declare @DCindex varchar(32)
declare @Pconstant float
declare @Cconstant float
declare @DCconstant float
declare @Iconstant float
declare @Lconstant float
declare @Iweight float
declare @ITweight float
declare @Lweight float
declare @DCweight float
declare @Cweight float
declare @DCavg float
declare @Iavg float
declare @Lavg float
declare @I float
declare @L float
declare @DC float
declare @ILDC float
declare @C float
declare @Pn float
declare @price float
declare @constants table(constant varchar(32),description varchar(32),value float)
declare @calc table(surrogate int identity(1,1),pricedate datetime,price float)
--GET CONSTANTS
insert into @constants(constant,description,value)
select constant,description,value
from dongpriceconstant
where dongpriceindex=@dongpriceindex
set @Pconstant= (select value from @constants where constant='Po')
set @Cconstant= (select value from @constants where constant='Co')
set @DCconstant= (select value from @constants where constant='DCo')
set @Iconstant= (select value from @constants where constant='Io')
set @Lconstant= (select value from @constants where constant='Lo')
set @Cweight= (select value from @constants where constant='Weight Co')
set @DCweight= (select value from @constants where constant='Weight DC')
set @Iweight= (select value from @constants where constant='Weight I')
set @ITweight= (select value from @constants where constant='Weight I (t)')
set @Lweight= (select value from @constants where constant='Weight L')
set @DCindex= (select description from @constants where constant='DC Index')
set @Iindex= (select description from @constants where constant='I Index')
set @Lindex= (select description from @constants where constant='L index')
--GET START DATE
set @pricemonth=(select dateadd(day,1,max(pricedate)) from pricevalue where priceindex=@dongpriceindex)
--FOR EACH QUARTER
while @pricemonth<=getdate()
begin
--CALCULATE THE PRICE
set @Iavg=dbo.fn_DONGAvgPrice(@Iindex,@pricemonth,6,3)
set @Iavg=round(@Iavg,dbo.fn_eroundfactor(@Iavg))
set @Lavg=dbo.fn_DONGAvgPrice(@Lindex,@pricemonth,6,3)
set @Lavg=round(@Lavg,dbo.fn_eroundfactor(@Lavg))
set @DCavg=dbo.fn_DONGAvgPrice(@DCindex,@pricemonth,6,3)
set @DCavg=round(@DCavg,dbo.fn_eroundfactor(@DCavg))
set @I = round(@Iavg/@Iconstant,dbo.fn_eroundfactor(@Iavg/@Iconstant))
set @I = round(@Iweight*@I,dbo.fn_eroundfactor(@Iweight*@I))
set @L = round(@Lavg/@Lconstant,dbo.fn_eroundfactor(@Lavg/@Lconstant))
set @L = round(@Lweight*@L,dbo.fn_eroundfactor(@Lweight*@L))
set @DC = round(@DCavg/@DCconstant,dbo.fn_eroundfactor(@DCavg/@DCconstant))
set @DC = round(@DCweight*@DC,dbo.fn_eroundfactor(@DCweight*@DC))
set @ILDC = round(@I+@L+@DC,dbo.fn_eroundfactor(@I+@L+@DC))
set @Pn = round(@Pconstant*@ILDC,dbo.fn_eroundfactor(@Pconstant*@ILDC))
set @I = round(@Iavg/@Iconstant,dbo.fn_eroundfactor(@Iavg/@Iconstant))
set @I = round(@Cconstant*@ITweight*@I,dbo.fn_eroundfactor(@Cconstant*@ITweight*@I))
set @C = round(@Cconstant*@Cweight,dbo.fn_eroundfactor(@Cconstant*@Cweight))
set @C = round(@C+ @I,dbo.fn_eroundfactor(@C+ @I))
set @price = @Pn-@C
set @price = round(@Price,dbo.fn_eroundfactor(@Price))
insert into DONGcalculationsD(pricemonth,Pconstant,Cconstant,DCconstant,Iconstant,Lconstant,Cweight,DCweight,Iweight,ITweight,Lweight,
Iavg,Lavg,DCavg,Icalc,Lcalc,DCcalc,Ccalc,Pn,price,creationname,creationdate)
values (@pricemonth,@Pconstant,@Cconstant,@DCconstant,@Iconstant,@Lconstant,@Cweight,@DCweight,@Iweight,@ITweight,@Lweight,
@Iavg,@Lavg,@DCavg,@I,@L,@DC,@C,@Pn,@price,'Interface',getdate())
set @pricemonth=dateadd(q,1,@pricemonth)
end
end