Add scripts for importing prices and sale contract fees; update purchase fees script

- Implemented `import_prices.py` to import price index values from a CSV file with migration mapping.
- Created `import_sale_fees.py` for importing sale contract line fees, including detailed logging and error handling.
- Modified `import_purchase_fees.py` to change fee type from 'ordered' to 'budgeted' and added fee ID logging.
This commit is contained in:
AzureAD\SylvainDUVERNAY
2026-03-24 14:13:23 +01:00
parent 078843f991
commit 4bac53b01c
28 changed files with 9619 additions and 249 deletions

View File

@@ -1,6 +1,5 @@
-- To be imported
--Forward Curves
--Mtm formula LINKED TO physical contract
@@ -299,8 +298,8 @@ SELECT
Bd.Description AS [name],
'SERVICES' AS [category],
'Mt' AS [uom],
0 as [sale_price],
0 as [cost_price],
0 AS [sale_price],
0 AS [cost_price],
'' AS [description]
FROM dbo.BaseDefinition AS BD
WHERE BD.ClassType = 'CostType'
@@ -333,7 +332,9 @@ SELECT
FROM dbo.BaseDefinition AS BD
WHERE BD.ClassType = 'QuoteSource'
SELECT * FROM price.Symbol AS S
SELECT * FROM price.Maturity AS M
SELECT * FROM price.Quote AS Q
-- Terminal
SELECT
@@ -438,6 +439,87 @@ WHERE D.Status <> 'Cancelled'
WHERE 1=1
AND DPT.PayTermRangeSize > 0
GROUP BY D.Id , BD.Caption, DPT.PayTermRangeSize
),
Geo AS (
SELECT
G.CountryName,
G.ContinentName,
G.BusinessArea
FROM singa.VW_DIM_GEOGRAPHY AS G
GROUP BY
G.CountryName,
G.ContinentName,
G.BusinessArea
),
PriceMtM AS
(
SELECT
ERMtM.DealId,
ISNULL(M.MaturityYear * 10000 + M.MaturityPeriod * 100 + ISNULL(M.MaturityDay, 1), 0) AS Maturity ,
ISNULL(S.Description, '') AS PriceBenchmark,
M.MaturityYear AS maturity_year,
M.MaturityPeriod AS maturity_month,
M.MaturityDay AS maturity_day
FROM dbo.ExpenseRevenue AS ERMtM
INNER JOIN dbo.PriceFormulaElement AS PFEMtM ON PFEMtM.ExpenseRevenueId = ERMtM.Id AND PFEMtM.FormulaType <> 'Adjustment'
INNER JOIN price.Maturity AS M ON PFEMtM.MaturityId = M.Id
INNER JOIN price.Symbol AS S ON M.SymbolId = S.Id
WHERE 1=1
AND ERMtM.FormulaGroup = 2
AND PFEMtM.FormulaType LIKE 'MtM%'
AND S.Description NOT LIKE 'Freight%'
AND S.Description NOT LIKE 'Discount%'
AND S.Description NOT LIKE 'BAF%'
),
Price AS
(
SELECT
C.ContractNumber,
C.CostDescription AS pricing_curve,
AVG(C.Price) AS price
FROM singa.VW_REP_PHYSICAL_CONTRACT_COSTS AS C
WHERE 1=1
AND C.FormulaGroup = 1
AND C.CostDescription NOT IN ( 'Adjustment' )
AND C.CostDescription NOT LIKE ('%Discount%')
GROUP BY
C.ContractNumber,
C.CostDescription
),
Pricing AS
(
SELECT
D.Id AS dealID,
D.Reference AS deal_number,
MAX(
IIF(
S.Caption = 'Fixed Margin',
'Priced',
CASE WHEN ISNULL(PFE.FormulaType, '') <> 'Fixed' AND VMP.DealId IS NULL THEN 'Unpriced'
ELSE 'Priced' END
)
)
AS pricing,
ISNULL(M.MaturityYear * 10000 + M.MaturityPeriod * 100 + ISNULL(M.MaturityDay, 1), 0) AS maturity,
M.MaturityYear AS maturity_year,
M.MaturityPeriod AS maturity_month,
M.MaturityDay AS maturity_day
FROM dbo.Deal AS D
INNER JOIN dbo.ExpenseRevenue AS ER ON ER.DealId = d.Id AND ER.FormulaGroup NOT IN (2, 4)
LEFT JOIN dbo.PriceFormulaElement AS PFE ON PFE.ExpenseRevenueId = ER.Id AND PFE.FormulaType <> 'Adjustment'
LEFT JOIN singa.VW_MVT_PRICING AS VMP ON D.Id = VMP.DealId
INNER JOIN dbo.Strategy AS S ON D.StrategyId = S.Id --All deals in Fixed Margin strategy must appear as Fixed
LEFT JOIN price.Maturity AS M ON PFE.MaturityId = M.Id
LEFT JOIN price.Symbol AS SY ON M.SymbolId = SY.Id
GROUP BY
D.Id,
D.Reference,
ISNULL(M.MaturityYear * 10000 + M.MaturityPeriod * 100 + ISNULL(M.MaturityDay, 1), 0),
M.MaturityYear,
M.MaturityPeriod,
M.MaturityDay
)
SELECT
D.Id AS source_id
@@ -475,7 +557,8 @@ SELECT
, ISNULL(C2.CountryName,'') AS origin
, ROUND(D.Quantity , 2) AS line_quantity
, 'Mt' AS line_unit_code
, ISNULL(PRICE.AvgPrice,0) AS line_price
, ISNULL(ERV.Price ,0) AS line_price
--, ISNULL(OLD_PRICE.AvgPrice,0) AS line_old_price
, CONCAT(
D.Quantity , ' Mt of sulphuric acid - ',
CASE
@@ -500,6 +583,22 @@ SELECT
, ISNULL(D.PumpingHourlyMTRate,0) AS pumping_rate
, D.UseOnlyMinAndMax AS use_only_min_max
, D.DropRemainingQuantity AS drop_remaining_quantity
, C_CP.CountryName AS counterparty_country
, GEO.BusinessArea AS business_area
, ISNULL(PRC.pricing,'') AS pricing_status
, ISNULL(ERV.pricing_curve, '') AS price_curve
, CASE
WHEN PRC.maturity = 0 THEN NULL
ELSE
CAST(CONCAT( PRC.maturity_year, '-', PRC.maturity_month, '-', ISNULL(PRC.maturity_day,'1')) AS DATE)
END AS price_maturity_date
, ISNULL(MtM.PriceBenchmark, '') AS mtm_curve
, CASE
WHEN Mtm.maturity = 0 THEN NULL
ELSE
CAST(CONCAT( Mtm.maturity_year, '-', Mtm.maturity_month, '-', ISNULL(Mtm.maturity_day,'1')) AS DATE)
END AS mtm_maturity_date
, CAST(D.EstimatedDateOfBL AS DATE) AS estimated_BL_date
FROM dbo.Deal AS D
LEFT JOIN dbo.Book AS B ON D.BookId = B.Id
LEFT JOIN dbo.ProductContainer AS PC ON D.Id = PC.Id
@@ -509,6 +608,7 @@ SELECT
LEFT JOIN geo.Country AS C_D ON DIS.CountryId = C_D.Id
LEFT JOIN counterpart.Company AS C ON D.CounterpartId = C.Id
LEFT JOIN geo.Country AS C_CP ON C.CountryId = C_CP.Id
LEFT JOIN Geo AS GEO ON C_CP.CountryName = GEO.CountryName
LEFT JOIN dbo.Product AS P ON D.ProductId = P.Id
LEFT JOIN dbo.BaseDefinition AS INCO ON D.IncotermId = INCO.Id
LEFT JOIN dbo.Strategy AS S ON D.StrategyId = S.Id
@@ -516,58 +616,21 @@ SELECT
LEFT JOIN (
SELECT DealId, AVG(MvTPrice) AS AvgPrice
FROM [singa].[VW_MVT_PRICING]
GROUP BY DealId) AS PRICE ON D.Id = PRICE.DealId
GROUP BY DealId) AS OLD_PRICE ON D.Id = OLD_PRICE.DealId
LEFT JOIN Price AS ERV ON D.Reference = ERV.ContractNumber
LEFT JOIN Pricing AS PRC ON D.Id = PRC.dealID
LEFT JOIN profiles.XUser AS TRD ON D.TraderId = TRD.Id
LEFT JOIN profiles.XUser AS OP ON D.OperatorId = OP.Id
LEFT JOIN dbo.BaseDefinition AS BD ON D.DeliveryPeriodId = BD.Id
LEFT JOIN geo.Country AS C2 ON D.CountryOfOriginId = C2.Id
LEFT JOIN PriceMtM AS MTM ON D.Id = MTM.DealId
WHERE 1=1
AND D.Status <> 'Cancelled'
AND D.BuyOrSell = 1 -- Purchase contracts
AND ISNULL(D.OtherReference, '') NOT LIKE '%ACCT Matching%'
AND B.Description LIKE '%2025%'
--AND D.Reference = 2093
ORDER BY 3,1
-- Purchase contract costs
SELECT
D.Reference AS contract_number,
LTRIM(RTRIM( ISNULL(D.OtherReference, '') )) AS contract_ref,
1 AS line_sequence,
CASE
WHEN CT.Caption = 'Commision' THEN 'Commission'
WHEN CT.Caption = 'Freight' THEN 'Maritime Freight'
ELSE CT.Caption
END AS product,
LTRIM(RTRIM( ISNULL(C.Name , 'TBD Supplier') )) AS supplier,
ER.CurrencyCode AS currency,
CASE ER.IsRevenue
WHEN 1 THEN 'REC'
ELSE 'PAY'
END AS p_r,
'Per qt' AS mode,
ROUND (
(IIF(PFE.Quantity IS NULL, PFE.WeightBalance / 100, 1) * PFE.OutputPriceValue),
2 ) AS price,
ER.UnitReference AS unit
FROM dbo.Deal AS D
LEFT JOIN dbo.Book AS B ON D.BookId = B.Id
INNER JOIN dbo.ExpenseRevenue AS ER ON D.Id = ER.DealId
INNER JOIN dbo.PriceFormulaElement AS PFE ON PFE.ExpenseRevenueId = ER.Id
INNER JOIN dbo.BaseDefinition AS CT ON ER.CostTypeId = CT.Id
LEFT JOIN counterpart.Company AS C ON ER.CounterpartId = c.Id
WHERE 1=1
AND D.Status <> 'Cancelled'
AND D.BuyOrSell = 1 -- Purchase contracts
AND ISNULL(D.OtherReference, '') NOT LIKE '%ACCT Matching%'
AND B.Description LIKE '%2025%'
AND ER.FormulaGroup NOT IN (1,2) -- Not Price or MtM
ORDER BY 2
-- Sale contracts - Copy/Paste results of this query in CSV file
@@ -579,8 +642,87 @@ ORDER BY 2
WHERE 1=1
AND DPT.PayTermRangeSize > 0
GROUP BY D.Id , BD.Caption, DPT.PayTermRangeSize
)
),
Geo AS (
SELECT
G.CountryName,
G.ContinentName,
G.BusinessArea
FROM singa.VW_DIM_GEOGRAPHY AS G
GROUP BY
G.CountryName,
G.ContinentName,
G.BusinessArea
),
PriceMtM AS
(
SELECT ERMtM.DealId,
ISNULL(M.MaturityYear * 10000 + M.MaturityPeriod * 100 + ISNULL(M.MaturityDay, 1), 0) AS maturity ,
ISNULL(S.Description, '') AS PriceBenchmark,
M.MaturityYear AS maturity_year,
M.MaturityPeriod AS maturity_month,
M.MaturityDay AS maturity_day
FROM dbo.ExpenseRevenue AS ERMtM
INNER JOIN dbo.PriceFormulaElement AS PFEMtM ON PFEMtM.ExpenseRevenueId = ERMtM.Id AND PFEMtM.FormulaType <> 'Adjustment'
INNER JOIN price.Maturity AS M ON PFEMtM.MaturityId = M.Id
INNER JOIN price.Symbol AS S ON M.SymbolId = S.Id
WHERE 1=1
AND ERMtM.FormulaGroup = 2
AND PFEMtM.FormulaType LIKE 'MtM%'
AND S.Description NOT LIKE 'Freight%'
AND S.Description NOT LIKE 'Discount%'
AND S.Description NOT LIKE 'BAF%'
),
Price AS
(
SELECT
C.ContractNumber,
C.CostDescription AS pricing_curve,
AVG(C.Price) AS price
FROM singa.VW_REP_PHYSICAL_CONTRACT_COSTS AS C
WHERE 1=1
AND C.FormulaGroup = 1
AND C.CostDescription NOT IN ( 'Adjustment' )
AND C.CostDescription NOT LIKE ('%Discount%')
GROUP BY
C.ContractNumber,
C.CostDescription
),
Pricing AS
(
SELECT
D.Id AS dealID,
D.Reference AS deal_number,
MAX(
IIF(
S.Caption = 'Fixed Margin',
'Priced',
CASE WHEN ISNULL(PFE.FormulaType, '') <> 'Fixed' AND VMP.DealId IS NULL THEN 'Unpriced'
ELSE 'Priced' END
)
)
AS pricing,
ISNULL(M.MaturityYear * 10000 + M.MaturityPeriod * 100 + ISNULL(M.MaturityDay, 1), 0) AS maturity,
M.MaturityYear AS maturity_year,
M.MaturityPeriod AS maturity_month,
M.MaturityDay AS maturity_day
FROM dbo.Deal AS D
INNER JOIN dbo.ExpenseRevenue AS ER ON ER.DealId = d.Id AND ER.FormulaGroup NOT IN (2, 4)
LEFT JOIN dbo.PriceFormulaElement AS PFE ON PFE.ExpenseRevenueId = ER.Id AND PFE.FormulaType <> 'Adjustment'
LEFT JOIN singa.VW_MVT_PRICING AS VMP ON D.Id = VMP.DealId
INNER JOIN dbo.Strategy AS S ON D.StrategyId = S.Id --All deals in Fixed Margin strategy must appear as Fixed
LEFT JOIN price.Maturity AS M ON PFE.MaturityId = M.Id
LEFT JOIN price.Symbol AS SY ON M.SymbolId = SY.Id
GROUP BY
D.Id,
D.Reference,
ISNULL(M.MaturityYear * 10000 + M.MaturityPeriod * 100 + ISNULL(M.MaturityDay, 1), 0),
M.MaturityYear,
M.MaturityPeriod,
M.MaturityDay
)
SELECT
D.Id AS source_id
, D.Id AS source_line_id
@@ -618,7 +760,8 @@ SELECT
, ISNULL(C2.CountryName,'') AS origin
, ROUND(D.Quantity , 2) AS line_quantity
, 'Mt' AS line_unit_code
, ISNULL(PRICE.AvgPrice,0) AS line_price
, ISNULL(ERV.Price ,0) AS line_price
--, ISNULL(OLD_PRICE.AvgPrice,0) AS line_old_price
, CONCAT(
D.Quantity , ' Mt of sulphuric acid - ',
CASE
@@ -643,32 +786,220 @@ SELECT
, ISNULL(D.PumpingHourlyMTRate,0) AS pumping_rate
, D.UseOnlyMinAndMax AS use_only_min_max
, D.DropRemainingQuantity AS drop_remaining_quantity
, C_CP.CountryName AS counterparty_country
, GEO.BusinessArea AS business_area
, ISNULL(PRC.pricing,'') AS pricing_status
, ISNULL(ERV.pricing_curve, '') AS price_curve
, CASE
WHEN PRC.maturity = 0 THEN NULL
ELSE
CAST(CONCAT( PRC.maturity_year, '-', PRC.maturity_month, '-', ISNULL(PRC.maturity_day,'1')) AS DATE)
END AS price_maturity_date
, ISNULL(MtM.PriceBenchmark, '') AS mtm_curve
, CASE
WHEN Mtm.maturity = 0 THEN NULL
ELSE
CAST(CONCAT( Mtm.maturity_year, '-', Mtm.maturity_month, '-', ISNULL(Mtm.maturity_day,'1')) AS DATE)
END AS mtm_maturity_date
, CAST(D.EstimatedDateOfBL AS DATE) AS estimated_BL_date
FROM dbo.Deal AS D
LEFT JOIN dbo.Book AS B ON D.BookId = B.Id
LEFT JOIN dbo.ProductContainer AS PC ON D.Id = PC.Id
LEFT JOIN geo.Location AS LOA ON D.LoadLocationId = LOA.Id
LEFT JOIN geo.Country AS C_L ON LOA.CountryId = C_L.Id
LEFT JOIN geo.Location AS DIS ON D.DeliveryLocationId = DIS.Id
LEFT JOIN geo.Country AS C_D ON DIS.CountryId = C_D.Id
LEFT JOIN counterpart.Company AS C ON D.CounterpartId = C.Id
LEFT JOIN geo.Country AS C_CP ON C.CountryId = C_CP.Id
LEFT JOIN dbo.Product AS P ON D.ProductId = P.Id
LEFT JOIN dbo.BaseDefinition AS INCO ON D.IncotermId = INCO.Id
LEFT JOIN dbo.Strategy AS S ON D.StrategyId = S.Id
LEFT JOIN PaymentTerm AS PT ON D.Id = PT.DealId
LEFT JOIN dbo.Book AS B ON D.BookId = B.Id
LEFT JOIN dbo.ProductContainer AS PC ON D.Id = PC.Id
LEFT JOIN geo.Location AS LOA ON D.LoadLocationId = LOA.Id
LEFT JOIN geo.Country AS C_L ON LOA.CountryId = C_L.Id
LEFT JOIN geo.Location AS DIS ON D.DeliveryLocationId = DIS.Id
LEFT JOIN geo.Country AS C_D ON DIS.CountryId = C_D.Id
LEFT JOIN counterpart.Company AS C ON D.CounterpartId = C.Id
LEFT JOIN geo.Country AS C_CP ON C.CountryId = C_CP.Id
LEFT JOIN Geo AS GEO ON C_CP.CountryName = GEO.CountryName
LEFT JOIN dbo.Product AS P ON D.ProductId = P.Id
LEFT JOIN dbo.BaseDefinition AS INCO ON D.IncotermId = INCO.Id
LEFT JOIN dbo.Strategy AS S ON D.StrategyId = S.Id
LEFT JOIN PaymentTerm AS PT ON D.Id = PT.DealId
LEFT JOIN (
SELECT DealId, AVG(MvTPrice) AS AvgPrice
FROM [singa].[VW_MVT_PRICING]
GROUP BY DealId) AS PRICE ON D.Id = PRICE.DealId
LEFT JOIN profiles.XUser AS TRD ON D.TraderId = TRD.Id
LEFT JOIN profiles.XUser AS OP ON D.OperatorId = OP.Id
GROUP BY DealId) AS OLD_PRICE ON D.Id = OLD_PRICE.DealId
LEFT JOIN Price AS ERV ON D.Reference = ERV.ContractNumber
LEFT JOIN Pricing AS PRC ON D.Id = PRC.dealID
LEFT JOIN profiles.XUser AS TRD ON D.TraderId = TRD.Id
LEFT JOIN profiles.XUser AS OP ON D.OperatorId = OP.Id
LEFT JOIN dbo.BaseDefinition AS BD ON D.DeliveryPeriodId = BD.Id
LEFT JOIN geo.Country AS C2 ON D.CountryOfOriginId = C2.Id
LEFT JOIN PriceMtM AS MTM ON D.Id = MTM.DealId
WHERE 1=1
AND D.Status <> 'Cancelled'
AND D.BuyOrSell = -1 -- Sale contracts
AND ISNULL(D.OtherReference, '') NOT LIKE '%ACCT Matching%'
AND B.Description LIKE '%2025%'
ORDER BY 2,1
AND (B.Description LIKE '%2025%' OR B.Description LIKE '%2026%')
--AND D.Reference = 2112
ORDER BY 3,1
-- Purchase contract costs
SELECT
D.Reference AS contract_number,
--B.Description,
--CASE D.BuyOrSell
-- WHEN '1' THEN 'Purchase'
-- ELSE 'Sale'
-- END AS contract_type,
LTRIM(RTRIM( ISNULL(D.OtherReference, '') )) AS contract_ref,
1 AS line_sequence,
CASE
WHEN CT.Caption = 'Commision' THEN 'Commission'
WHEN CT.Caption = 'Freight' THEN 'Maritime Freight'
ELSE CT.Caption
END AS product,
LTRIM(RTRIM( ISNULL(C.Name , 'TBD Supplier') )) AS supplier,
ER.CurrencyCode AS currency,
CASE ER.IsRevenue
WHEN 1 THEN 'REC'
ELSE 'PAY'
END AS p_r,
'Per qt' AS mode,
ROUND (
(IIF(PFE.Quantity IS NULL, PFE.WeightBalance / 100, 1) * PFE.OutputPriceValue),
2 ) AS price,
ER.UnitReference AS unit
FROM dbo.Deal AS D
LEFT JOIN dbo.Book AS B ON D.BookId = B.Id
INNER JOIN dbo.ExpenseRevenue AS ER ON D.Id = ER.DealId
INNER JOIN dbo.PriceFormulaElement AS PFE ON PFE.ExpenseRevenueId = ER.Id
INNER JOIN dbo.BaseDefinition AS CT ON ER.CostTypeId = CT.Id
LEFT JOIN counterpart.Company AS C ON ER.CounterpartId = c.Id
WHERE 1=1
AND D.Status <> 'Cancelled'
AND D.BuyOrSell = 1 -- Purchase contracts
AND ISNULL(D.OtherReference, '') NOT LIKE '%ACCT Matching%'
--AND (B.Description LIKE '%2025%')
AND (B.Description LIKE '%2025%' OR B.Description LIKE '%2026%')
AND ER.FormulaGroup NOT IN (1,2) -- Not Price or MtM
ORDER BY 1
-- Sale contract costs
SELECT
D.Reference AS contract_number,
--B.Description,
--CASE D.BuyOrSell
-- WHEN '1' THEN 'Purchase'
-- ELSE 'Sale'
-- END AS contract_type,
LTRIM(RTRIM( ISNULL(D.OtherReference, '') )) AS contract_ref,
1 AS line_sequence,
CASE
WHEN CT.Caption = 'Commision' THEN 'Commission'
WHEN CT.Caption = 'Freight' THEN 'Maritime Freight'
ELSE CT.Caption
END AS product,
LTRIM(RTRIM( ISNULL(C.Name , 'TBD Supplier') )) AS supplier,
ER.CurrencyCode AS currency,
CASE ER.IsRevenue
WHEN 1 THEN 'REC'
ELSE 'PAY'
END AS p_r,
'Per qt' AS mode,
ROUND (
(IIF(PFE.Quantity IS NULL, PFE.WeightBalance / 100, 1) * PFE.OutputPriceValue),
2 ) AS price,
ER.UnitReference AS unit
FROM dbo.Deal AS D
LEFT JOIN dbo.Book AS B ON D.BookId = B.Id
INNER JOIN dbo.ExpenseRevenue AS ER ON D.Id = ER.DealId
INNER JOIN dbo.PriceFormulaElement AS PFE ON PFE.ExpenseRevenueId = ER.Id
INNER JOIN dbo.BaseDefinition AS CT ON ER.CostTypeId = CT.Id
LEFT JOIN counterpart.Company AS C ON ER.CounterpartId = c.Id
WHERE 1=1
AND D.Status <> 'Cancelled'
AND D.BuyOrSell <> 1 -- Sale contracts
AND ISNULL(D.OtherReference, '') NOT LIKE '%ACCT Matching%'
AND (B.Description LIKE '%2025%' OR B.Description LIKE '%2026%')
AND ER.FormulaGroup NOT IN (1,2) -- Not Price or MtM
ORDER BY 1
-- Run that script to generate CSV for price_curve MTM used in physical contract in 2025 and 2026
SELECT DISTINCT
CONCAT(
ISNULL(S.Description, '') , ' ' ,
M.MaturityYear , '-',
RIGHT(CONCAT('0', M.MaturityPeriod),2)
)AS price_index,
Q.QuoteDate AS price_date,
Q.Value AS high_price,
Q.Value AS low_price,
Q.Value AS open_price,
Q.Value AS price_value
FROM dbo.ExpenseRevenue AS ERMtM
INNER JOIN dbo.Deal AS D ON ERMtM.DealId = D.Id
INNER JOIN dbo.PriceFormulaElement AS PFEMtM ON PFEMtM.ExpenseRevenueId = ERMtM.Id AND PFEMtM.FormulaType <> 'Adjustment'
INNER JOIN price.Maturity AS M ON PFEMtM.MaturityId = M.Id
INNER JOIN price.Symbol AS S ON M.SymbolId = S.Id
INNER JOIN dbo.Book AS B ON D.BookId = B.Id
LEFT JOIN price.Quote AS Q ON M.Id = Q.MaturityId
WHERE 1=1
AND ERMtM.FormulaGroup = 2
AND PFEMtM.FormulaType LIKE 'MtM%'
AND S.Description NOT LIKE 'Freight%'
AND S.Description NOT LIKE 'Discount%'
AND S.Description NOT LIKE 'BAF%'
AND (B.Description LIKE '%2025%' OR B.Description LIKE '%2026%')
ORDER BY 1,2
SELECT DISTINCT formulaType FROM PriceFormulaElement
Trigger, RangeDate
-- Run that script to generate CSV for price_curve MTM used in physical contract in 2025 and 2026
SELECT DISTINCT
D.Reference,
PFEMtM.FormulaType,
CONCAT(
ISNULL(S.Description, '') , ' ' ,
M.MaturityYear , '-',
RIGHT(CONCAT('0', M.MaturityPeriod),2)
)AS price_index,
--Q.QuoteDate AS price_date,
--Q.Value AS high_price,
--Q.Value AS low_price,
--Q.Value AS open_price,
--Q.Value AS price_value,
PFEMtM.FirstDate,
PFEMtM.LastDate,
PFEMtM.TriggerDate,
PFEMtM.NbDaysBefore,
PFEMtM.NbDaysAfter,
PFEMtM.*
FROM dbo.ExpenseRevenue AS ERMtM
INNER JOIN dbo.Deal AS D ON ERMtM.DealId = D.Id
INNER JOIN dbo.PriceFormulaElement AS PFEMtM ON PFEMtM.ExpenseRevenueId = ERMtM.Id AND PFEMtM.FormulaType <> 'Adjustment'
INNER JOIN price.Maturity AS M ON PFEMtM.MaturityId = M.Id
INNER JOIN price.Symbol AS S ON M.SymbolId = S.Id
INNER JOIN dbo.Book AS B ON D.BookId = B.Id
LEFT JOIN price.Quote AS Q ON M.Id = Q.MaturityId
WHERE 1=1
AND ERMtM.FormulaGroup = 1
AND PFEMtM.FormulaType NOT LIKE 'MtM%'
AND S.Description NOT LIKE 'Freight%'
AND S.Description NOT LIKE 'Discount%'
AND S.Description NOT LIKE 'BAF%'
AND (B.Description LIKE '%2025%' OR B.Description LIKE '%2026%')
--AND D.Reference = 2097
ORDER BY 1,2