CREATE OR REPLACE VIEW public.vw_bi_itsa_physical_open_costs_v2 AS
 WITH valuation_info AS (
         SELECT pvl."intContractId",
            pvl."intContractLineId",
            pvl."strState",
            (sum(pvl."dblMtmPrice"))::numeric(18,2) AS "avgMtmPrice"
           FROM vw_utility_physical_valuation_line pvl
          GROUP BY pvl."intContractId", pvl."intContractLineId", pvl."strState"
        ), utility_open_quantity AS (
         SELECT
                CASE
                    WHEN (uop."strContractSide" = 'purchase'::text) THEN 'Purchase'::text
                    WHEN (uop."strContractSide" = 'sale'::text) THEN 'Sale'::text
                    ELSE NULL::text
                END AS "TradeType",
            uop."intContractLineId" AS "TradeLineId",
            (sum(uop."dblOpenQuantity"))::numeric(18,3) AS "OpenQuantitySigned",
            (sum(uop."dblOpenTargetedQuantity"))::numeric(18,3) AS "OpenExpectedQuantitySigned"
           FROM vw_utility_open_position uop
          GROUP BY
                CASE
                    WHEN (uop."strContractSide" = 'purchase'::text) THEN 'Purchase'::text
                    WHEN (uop."strContractSide" = 'sale'::text) THEN 'Sale'::text
                    ELSE NULL::text
                END, uop."intContractLineId"
        ), contract_open_quantity AS (
         SELECT 'Purchase'::text AS "TradeType",
            pc."intPurchaseLineId" AS "TradeLineId",
            (abs(COALESCE(uop."OpenQuantitySigned", (0)::numeric)))::numeric(18,3) AS "OpenQuantity",
            (COALESCE(uop."OpenQuantitySigned", (0)::numeric))::numeric(18,3) AS "OpenQuantitySigned",
            (COALESCE(uop."OpenExpectedQuantitySigned", (0)::numeric))::numeric(18,3) AS "OpenExpectedQuantitySigned",
            COALESCE(vi."avgMtmPrice", (0)::numeric(18,2)) AS "AvgMtmPrice",
            pc."dblTheoriticalQuantity" AS "TheoriticalQuantitySigned"
           FROM ((vw_utility_purchase_physical_contract pc
             LEFT JOIN utility_open_quantity uop ON (((uop."TradeType" = 'Purchase'::text) AND (uop."TradeLineId" = pc."intPurchaseLineId"))))
             LEFT JOIN valuation_info vi ON ((pc."intPurchaseLineId" = vi."intContractLineId")))
        UNION ALL
         SELECT 'Sale'::text AS "TradeType",
            pc."intSaleLineId" AS "TradeLineId",
            (abs(COALESCE(uop."OpenQuantitySigned", (0)::numeric)))::numeric(18,3) AS "OpenQuantity",
            (COALESCE(uop."OpenQuantitySigned", (0)::numeric))::numeric(18,3) AS "OpenQuantitySigned",
            (COALESCE(uop."OpenExpectedQuantitySigned", (0)::numeric))::numeric(18,3) AS "OpenExpectedQuantitySigned",
            COALESCE(vi."avgMtmPrice", (0)::numeric(18,2)) AS "AvgMtmPrice",
            (pc."dblTheoreticalQuantity" * ('-1'::integer)::numeric) AS "TheoriticalQuantitySigned"
           FROM ((vw_utility_sale_physical_contract pc
             LEFT JOIN utility_open_quantity uop ON (((uop."TradeType" = 'Sale'::text) AND (uop."TradeLineId" = pc."intSaleLineId"))))
             LEFT JOIN valuation_info vi ON ((pc."intSaleLineId" = vi."intContractLineId")))
        )
 SELECT 1 AS "Group Order",
    'Price'::text AS "Group Type",
    ctr."intPurchaseLineId" AS "intTradeLineId",
        CASE
            WHEN ((ctr."strPricingType")::text = 'basis'::text) THEN COALESCE(prc."strPriceDescription", 'Basis Price Curve Missing'::character varying)
            ELSE 'Fixed Price'::character varying
        END AS "Line Description",
    pur."dblUnitPrice" AS "Price",
    opq."OpenQuantitySigned" AS "Open Quantity Signed",
    ((opq."OpenQuantitySigned" * pur."dblUnitPrice") * ('-1'::integer)::numeric) AS "Open Cost Amount",
    1 AS "intPriceGroupId",
    ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
    opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
    'Price'::text AS "Line Group",
    opq."OpenExpectedQuantitySigned" AS "Open Expected Quantity Signed",
    ((opq."OpenExpectedQuantitySigned" * pur."dblUnitPrice") * ('-1'::integer)::numeric) AS "Open ExpectedCost Amount"
   FROM (((vw_utility_dim_physical_purchase_contract ctr
     JOIN vw_utility_purchase_physical_contract pur ON ((ctr."intPurchaseLineId" = pur."intPurchaseLineId")))
     JOIN contract_open_quantity opq ON (((ctr."intPurchaseLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Purchase'::text))))
     LEFT JOIN vw_utility_physical_pricing_definition prc ON (((prc."strContractType" = 'Purchase'::text) AND (prc."intContractLineId" = ctr."intPurchaseLineId"))))
  WHERE (((ctr."strPricingType")::text = ANY (ARRAY['priced'::text, 'basis'::text])) AND (opq."OpenQuantity" <> (0)::numeric))
UNION ALL
 SELECT 1 AS "Group Order",
    'Price'::text AS "Group Type",
    ctr."intPurchaseLineId" AS "intTradeLineId",
    prc."strPremiumType" AS "Line Description",
    prc."dblPremiumAmount" AS "Price",
    opq."OpenQuantitySigned" AS "Open Quantity Signed",
    ((opq."OpenQuantitySigned" * prc."dblPremiumAmount") * ('-1'::integer)::numeric) AS "Open Cost Amount",
    1 AS "intPriceGroupId",
    ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
    opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
    'Price'::text AS "Line Group",
    opq."OpenExpectedQuantitySigned" AS "Open Expected Quantity Signed",
    ((opq."OpenExpectedQuantitySigned" * prc."dblPremiumAmount") * ('-1'::integer)::numeric) AS "Open ExpectedCost Amount"
   FROM (((vw_utility_dim_physical_purchase_contract ctr
     JOIN vw_utility_purchase_physical_contract pur ON ((ctr."intPurchaseLineId" = pur."intPurchaseLineId")))
     JOIN contract_open_quantity opq ON (((ctr."intPurchaseLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Purchase'::text))))
     JOIN vw_utility_premium_composition prc ON (((prc."strContractType" = 'Purchase'::text) AND (prc."intContractLineId" = ctr."intPurchaseLineId"))))
  WHERE (((ctr."strPricingType")::text = ANY (ARRAY['priced'::text, 'basis'::text])) AND (opq."OpenQuantity" <> (0)::numeric))
UNION ALL
 SELECT DISTINCT 2 AS "Group Order",
    'MTM'::text AS "Group Type",
    ctr."intPurchaseLineId" AS "intTradeLineId",
    mtm."strStrategy" AS "Line Description",
    opq."AvgMtmPrice" AS "Price",
    opq."OpenQuantitySigned" AS "Open Quantity Signed",
    (opq."OpenQuantitySigned" * opq."AvgMtmPrice") AS "Open Cost Amount",
    3 AS "intPriceGroupId",
    ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
    opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
    'MTM'::text AS "Line Group",
    opq."OpenExpectedQuantitySigned" AS "Open Expected Quantity Signed",
    (opq."OpenExpectedQuantitySigned" * opq."AvgMtmPrice") AS "Open ExpectedCost Amount"
   FROM ((vw_utility_physical_mtm_definition mtm
     JOIN vw_utility_dim_physical_purchase_contract ctr ON ((mtm."intContractLineId" = ctr."intPurchaseLineId")))
     JOIN contract_open_quantity opq ON (((ctr."intPurchaseLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Purchase'::text))))
  WHERE ((mtm."strContractType" = 'Purchase'::text) AND (opq."OpenQuantity" <> (0)::numeric))
UNION ALL
 SELECT 3 AS "Group Order",
    'Fee'::text AS "Group Type",
    fees."intContractLineId" AS "intTradeLineId",
    fees."strProduct" AS "Line Description",
    fees."dblPrice" AS "Price",
    opq."OpenQuantitySigned" AS "Open Quantity Signed",
    ((((
        CASE
            WHEN ((fees."strPayOrRec")::text = 'pay'::text) THEN 1
            ELSE '-1'::integer
        END)::numeric * opq."OpenQuantity") * fees."dblPrice") * ('-1'::integer)::numeric) AS "Open Cost Amount",
    2 AS "intPriceGroupId",
    ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
    opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
    fees."strProductGroup" AS "Line Group",
    opq."OpenExpectedQuantitySigned" AS "Open Expected Quantity Signed",
    ((((
        CASE
            WHEN ((fees."strPayOrRec")::text = 'pay'::text) THEN 1
            ELSE '-1'::integer
        END)::numeric * opq."OpenExpectedQuantitySigned") * fees."dblPrice") * ('-1'::integer)::numeric) AS "Open ExpectedCost Amount"
   FROM ((vw_utility_contract_fees fees
     JOIN vw_utility_dim_physical_purchase_contract ctr ON ((fees."intContractLineId" = ctr."intPurchaseLineId")))
     JOIN contract_open_quantity opq ON (((ctr."intPurchaseLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Purchase'::text))))
  WHERE ((fees."strContractType" = 'Purchase'::text) AND (opq."OpenQuantity" <> (0)::numeric))
UNION ALL
 SELECT 1 AS "Group Order",
    'Price'::text AS "Group Type",
    ctr."intSaleLineId" AS "intTradeLineId",
        CASE
            WHEN ((ctr."strPricingType")::text = 'basis'::text) THEN COALESCE(prc."strPriceDescription", 'Basis Price Curve Missing'::character varying)
            ELSE 'Fixed Price'::character varying
        END AS "Line Description",
    sale."dblUnitPrice" AS "Price",
    opq."OpenQuantitySigned" AS "Open Quantity Signed",
    ((opq."OpenQuantitySigned" * sale."dblUnitPrice") * ('-1'::integer)::numeric) AS "Open Cost Amount",
    1 AS "intPriceGroupId",
    ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
    opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
    'Price'::text AS "Line Group",
    opq."OpenExpectedQuantitySigned" AS "Open Expected Quantity Signed",
    ((opq."OpenExpectedQuantitySigned" * sale."dblUnitPrice") * ('-1'::integer)::numeric) AS "Open ExpectedCost Amount"
   FROM (((vw_utility_dim_physical_sale_contract ctr
     JOIN vw_utility_sale_physical_contract sale ON ((ctr."intSaleLineId" = sale."intSaleLineId")))
     JOIN contract_open_quantity opq ON (((ctr."intSaleLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Sale'::text))))
     LEFT JOIN vw_utility_physical_pricing_definition prc ON (((prc."strContractType" = 'Sale'::text) AND (prc."intContractLineId" = ctr."intSaleLineId"))))
  WHERE (((ctr."strPricingType")::text = ANY (ARRAY['priced'::text, 'basis'::text])) AND (opq."OpenQuantity" <> (0)::numeric))
UNION ALL
 SELECT 1 AS "Group Order",
    'Price'::text AS "Group Type",
    ctr."intSaleLineId" AS "intTradeLineId",
    prc."strPremiumType" AS "Line Description",
    (prc."dblPremiumAmount" / abs(opq."OpenExpectedQuantitySigned")) AS "Price",
    opq."OpenQuantitySigned" AS "Open Quantity Signed",
    ((opq."OpenQuantitySigned" * prc."dblPremiumAmount") * ('-1'::integer)::numeric) AS "Open Cost Amount",
    1 AS "intPriceGroupId",
    ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
    opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
    'Price'::text AS "Line Group",
    opq."OpenExpectedQuantitySigned" AS "Open Expected Quantity Signed",
    ((opq."OpenExpectedQuantitySigned" * prc."dblPremiumAmount") * ('-1'::integer)::numeric) AS "Open ExpectedCost Amount"
   FROM (((vw_utility_dim_physical_sale_contract ctr
     JOIN vw_utility_sale_physical_contract pur ON ((ctr."intSaleLineId" = pur."intSaleLineId")))
     JOIN contract_open_quantity opq ON (((ctr."intSaleLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Sale'::text))))
     JOIN vw_utility_premium_composition prc ON (((prc."strContractType" = 'Sale'::text) AND (prc."intContractLineId" = ctr."intSaleLineId"))))
  WHERE (((ctr."strPricingType")::text = ANY (ARRAY['priced'::text, 'basis'::text])) AND (opq."OpenQuantity" <> (0)::numeric))
UNION ALL
 SELECT DISTINCT 2 AS "Group Order",
    'MTM'::text AS "Group Type",
    ctr."intSaleLineId" AS "intTradeLineId",
    mtm."strStrategy" AS "Line Description",
    opq."AvgMtmPrice" AS "Price",
    opq."OpenQuantitySigned" AS "Open Quantity Signed",
    (opq."OpenQuantitySigned" * opq."AvgMtmPrice") AS "Open Cost Amount",
    3 AS "intPriceGroupId",
    ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
    opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
    'MTM'::text AS "Line Group",
    opq."OpenExpectedQuantitySigned" AS "Open Expected Quantity Signed",
    (opq."OpenExpectedQuantitySigned" * opq."AvgMtmPrice") AS "Open ExpectedCost Amount"
   FROM ((vw_utility_physical_mtm_definition mtm
     JOIN vw_utility_dim_physical_sale_contract ctr ON ((mtm."intContractLineId" = ctr."intSaleLineId")))
     JOIN contract_open_quantity opq ON (((ctr."intSaleLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Sale'::text))))
  WHERE ((mtm."strContractType" = 'Sale'::text) AND (opq."OpenQuantity" <> (0)::numeric))
UNION ALL
 SELECT 3 AS "Group Order",
    'Fee'::text AS "Group Type",
    fees."intContractLineId" AS "intTradeLineId",
    fees."strProduct" AS "Line Description",
    fees."dblPrice" AS "Price",
    opq."OpenQuantitySigned" AS "Open Quantity Signed",
    ((((
        CASE
            WHEN ((fees."strPayOrRec")::text = 'pay'::text) THEN 1
            ELSE '-1'::integer
        END)::numeric * opq."OpenQuantity") * fees."dblPrice") * ('-1'::integer)::numeric) AS "Open Cost Amount",
    2 AS "intPriceGroupId",
    ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
    opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
    fees."strProductGroup" AS "Line Group",
    opq."OpenExpectedQuantitySigned" AS "Open Expected Quantity Signed",
    (((
        CASE
            WHEN ((fees."strPayOrRec")::text = 'pay'::text) THEN 1
            ELSE '-1'::integer
        END)::numeric * opq."OpenExpectedQuantitySigned") * fees."dblPrice") AS "Open ExpectedCost Amount"
   FROM ((vw_utility_contract_fees fees
     JOIN vw_utility_dim_physical_sale_contract ctr ON ((fees."intContractLineId" = ctr."intSaleLineId")))
     JOIN contract_open_quantity opq ON (((ctr."intSaleLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Sale'::text))))
  WHERE ((fees."strContractType" = 'Sale'::text) AND (opq."OpenQuantity" <> (0)::numeric));;
