;WITH #SearchHistory (OrderDetail_ID, RelatedOrderDetail_ID, OrigOrderDetail_ID, TimesRolled) AS
(
SELECT od.OrderDetail_ID
,od.RelatedOrderDetail_ID
,0
,0
FROM #SearchFilter f1
INNER JOIN RLHistory.dbo.TRRawDetail od (NOLOCK)
ON od.ConfirmationNo LIKE f1.OrigConfirmationNo + '%'
AND od.ConfirmationNo <> f1.ConfirmationNo
AND od.ItemType_ID IN (14,27)
UNION ALL
SELECT od.OrderDetail_ID
,od.RelatedOrderDetail_ID
,0
,0
FROM #SearchFilter f1
INNER JOIN RueschLink.dbo.ClientOrder co (NOLOCK)
ON co.ConfirmationNo LIKE f1.OrigConfirmationNo + '%'
AND co.ConfirmationNo <> f1.ConfirmationNo
INNER JOIN RueschLink.dbo.OrderDetail od (NOLOCK)
ON od.ClientOrder_ID = co.ClientOrder_ID
AND od.ItemType_ID IN (14,27)
UNION ALL
SELECT od.OrderDetail_ID
,od.RelatedOrderDetail_ID
,CASE WHEN ISNULL(f2.RelatedOrderDetail_ID, 0) = 0 THEN f2.OrderDetail_ID ELSE f2.OrigOrderDetail_ID END
,f2.TimesRolled + 1 - (CASE WHEN ItemType_ID IN (14,27) THEN 0 ELSE 1 END)
FROM #SearchHistory AS f2
INNER JOIN RueschLink.dbo.OrderDetail od (NOLOCK)
ON od.RelatedOrderDetail_ID = f2.OrderDetail_ID
AND od.ItemType_ID IN (14,27,82,83)
UNION ALL
SELECT od.OrderDetail_ID
,od.RelatedOrderDetail_ID
,CASE WHEN ISNULL(f2.RelatedOrderDetail_ID, 0) = 0 THEN f2.OrderDetail_ID ELSE f2.OrigOrderDetail_ID END
,f2.TimesRolled + 1 - (CASE WHEN ItemType_ID IN (14,27) THEN 0 ELSE 1 END)
FROM #SearchHistory AS f2
INNER JOIN RLHistory.dbo.TRRawDetail od (NOLOCK)
ON od.RelatedOrderDetail_ID = f2.OrderDetail_ID
AND od.ItemType_ID IN (14,27,82,83)
)
-- create the report list and use it to collect more details
INSERT INTO #ReportList
(OrderDetail_ID
,RelatedOrderDetail_ID
,OrigOrderDetail_ID
,ConfirmationNo
,OrigConfirmationNo
,TimesRolled
,Reissue)
SELECT f1.OrderDetail_ID
,f1.RelatedOrderDetail_ID
,MAX(f2.OrigOrderDetail_ID)
,f1.ConfirmationNo
,f1.OrigConfirmationNo
,ISNULL(MAX(f2.TimesRolled), 0)
,CASE WHEN ISNULL(MAX(f2.TimesRolled), 0) > 0 THEN 'Y' ELSE 'N' END
FROM #SearchFilter f1
LEFT JOIN #SearchHistory f2
ON f1.OrderDetail_ID = f2.OrderDetail_ID
GROUP BY f1.ConfirmationNo, f1.OrigConfirmationNo, f1.OrderDetail_ID, f1.RelatedOrderDetail_ID
ORDER BY f1.ConfirmationNo
CREATE TABLE #ReportDetails
(
ClientOrder_ID INT
,ConfirmationNo VARCHAR(17)
,ConfirmationNo1 VARCHAR(100)
,OrigConfirmationNo VARCHAR(17)
,ClientName VARCHAR(90)
,Account VARCHAR(100)
,CurrencyCode VARCHAR(17)
,Amount VARCHAR(30)
,SettlementCurrencyCode VARCHAR(17)
,SettlementAmount VARCHAR(30)
,ItemRate VARCHAR(15)
,NewValueDate DATE
,OrigValueDate DATE
,OrderDate DATE
,OrderType VARCHAR(100)
,BeneficiaryName VARCHAR(140)
,OrderSource VARCHAR(100)
,Enteredby VARCHAR(100)
,Reissue VARCHAR(3) DEFAULT 'N'
,TimesRolled INT
,NumberofDaysRolled INT
,Trader VARCHAR(90)
,Trader_ID INT
,Comment VARCHAR(100)
,OrderDetail_ID INT
,WeightedAverage CHAR(1) DEFAULT ''
,OrigOrderDetail_ID INT
,OptionID Varchar(100)
,OriginalAmount VARCHAR(30)
,NewCloseDate Date
,CompletedTenor varchar(30)
,OriginalBookingDate Date
)
--start building the report
INSERT INTO #ReportDetails
(ClientOrder_ID
,ConfirmationNo
,ConfirmationNo1
,OrigConfirmationNo
,ClientName
,Account
,CurrencyCode
,Amount
,SettlementCurrencyCode
,SettlementAmount
,ItemRate
,NewValueDate
,OrderDate
,OrderType
,BeneficiaryName
,OrderSource
,Enteredby
,Reissue
,TimesRolled
,Trader_ID
,Comment
,OrderDetail_ID
,OrigOrderDetail_ID
,OptionID
,OriginalAmount
,NewCloseDate
,CompletedTenor
,OriginalBookingDate)
SELECT
od.ClientOrder_ID
,rl.ConfirmationNo
,('6' + ',' + ltrim(rtrim(Str(ISNULL(od.OrderDetail_ID, 0)))) + ',' + ltrim(rtrim(Str(ISNULL(od.ClientOrder_ID, 0)))) + ',' + ltrim(rtrim(od.ConfirmationNo)) + ',%' + ltrim(rtrim(od.ItemNo))) --link confirmation no
,rl.OrigConfirmationNo
,c.DESCRIPTION -- ClientName
,UPPER(co.Account) -- Account
,od.CurrencyCode -- CurrencyCode
,LTRIM(STR(od.ForeignAmount, 30, ISNULL(od.ForeignAmt_NDec, 2)))-- Amount
,co.Settlement_SWIFT-- SettlementCurrencyCode
,LTRIM(STR(od.Extension, 30, ISNULL(co.SettlementAmt_NDec, 2))) --Settlement Amount
,LTRIM(STR(od.ItemRate, 15, ISNULL(od.ItemRate_NDec, 2))) -- ItemRate
,COALESCE(trdvd.RequestedValueDate, od.ReleaseDate) -- NewValueDate
,co.Ordered -- OrderDate
,ot.DESCRIPTION -- OrderType
,od.BeneName -- BeneficiaryName
,COALESCE(a.Description, a2.Description, @RepoOrderSource) -- OrderSource
,co.UserName -- Enteredby
,rl.Reissue
,rl.TimesRolled
,COALESCE(qh.updid, qh.User_ID, qh.initid, q.updid, q.User_ID, q.initid, co.initid) -- Trader_ID
,rsr.Instructions
,CASE WHEN od.FundedBy = @FundedByMultiple THEN od.OrderDetail_ID ELSE 0 END
,rl.OrigOrderDetail_ID
,(CASE WHEN co.ordertype_id in (6,45,5,11) THEN odOrg.OptionContractId ELSE NULL END) --new column added for PBR-1192
,(CASE WHEN co.ordertype_id in (6,45,5,11) THEN LTRIM(STR(odOrg.ForeignAmount, 30, ISNULL(odOrg.ForeignAmt_NDec, 2))) ELSE NULL END)--new column added for PBR-1192
,(CASE WHEN co.ordertype_id in (6,45,5,11) THEN od.ReleaseDate + od.WindowLength ELSE null END) --new column added for PBR-1192
,(CASE WHEN co.ordertype_id in (6,45,5,11) THEN DateDiff(day,coOrg.Ordered,(od.ReleaseDate + isnull(od.WindowLength,0))) ELSE NULL END) --new column added for PBR-1192
,(CASE WHEN co.ordertype_id in (6,45,5,11) THEN coOrg.Ordered ELSE NULL END) --new column added for PBR-1192
FROM #ReportList rl
INNER JOIN RLHistory.dbo.TRRawDetail od (NOLOCK) ON rl.OrderDetail_ID = od.OrderDetail_ID
INNER JOIN RLHistory.dbo.TRRawHeader co (NOLOCK) ON co.ClientOrder_ID = od.ClientOrder_ID
INNER JOIN RueschLink.dbo.Client AS c (NOLOCK) ON co.Client_ID = c.Client_ID
INNER JOIN RueschLink.dbo.OrderType AS ot (NOLOCK) ON co.OrderType_ID = ot.OrderType_ID
LEFT OUTER JOIN RueschLink.dbo.Application AS a (NOLOCK) ON (co.Application_ID = a.Application_ID AND a.Application_ID = 3)-- a.Description = @FwdOrderSource)
LEFT OUTER JOIN VMaRS.dbo.RSRepurchase AS rsr (NOLOCK) ON rsr.ClientOrder_ID = od.ClientOrder_ID
LEFT OUTER JOIN RueschLink.dbo.Application AS a2 (NOLOCK) ON a2.Application_ID = rsr.ApplicationID
LEFT OUTER JOIN crs..Quote q (NOLOCK) ON od.Quote_ID = q.Quote_ID
LEFT OUTER JOIN crs..QuoteHistory qh (NOLOCK) ON od.Quote_ID = qh.Quote_ID
LEFT OUTER JOIN RLHistory.dbo.TRRawDetailValueDate AS trdvd (NOLOCK) ON od.OrderDetail_ID = trdvd.OrderDetail_ID
LEFT JOIN RLHistory.dbo.TRRawDetail odOrg (NOLOCK) ON odOrg.OrderDetail_ID=RueschLink.dbo.ReturnParentOrderDetailID (od.RelatedOrderDetail_ID) --Join Added for PBR-1192
AND od.RelatedOrderDetail_ID>0 AND od.RelatedOrderDetail_ID IS NOT NULL
LEFT JOIN RLHistory.dbo.TRRawHeader coOrg (NOLOCK) ON coOrg.ClientOrder_ID = odOrg.ClientOrder_ID --Join Added for PBR-1192
ORDER BY co.ConfirmationNo --order the confirmation number asc