CREATE OR REPLACE VIEW public.vw_bi_itsa_fct_full_pnl AS
 WITH trade AS (
         SELECT pc."intPurchaseLineId" AS "intTradeId",
            (pc."intPurchaseLineId" || '_0'::text) AS strtradeid
           FROM vw_utility_dim_physical_purchase_contract pc
          WHERE ((1 = 1) AND (pc."intPurchaseLineId" > 0))
        UNION ALL
         SELECT sc."intSaleLineId" AS "intTradeId",
            ('0_'::text || sc."intSaleLineId") AS strtradeid
           FROM vw_utility_dim_physical_sale_contract sc
          WHERE ((1 = 1) AND (sc."intSaleLineId" > 0))
        )
 SELECT 2 AS "intPnlTypeId",
    trade.strtradeid AS "strConformedReferenceId",
    contracts."intPriceGroupId",
    contracts."Line Description",
    contracts."Open Quantity Signed",
    contracts."Price",
    contracts."Open Cost Amount" AS amount,
    contracts."Estimated BL Date" AS "Delivery Date",
    contracts."Line Group"
   FROM (vw_bi_itsa_physical_open_costs contracts
     JOIN trade trade ON ((contracts."intTradeLineId" = trade."intTradeId")))
UNION ALL
 SELECT 3 AS "intPnlTypeId",
    ((shipment."intPurchaseLineId" || '_'::text) || shipment."intSaleLineId") AS "strConformedReferenceId",
    1 AS "intPriceGroupId",
    'Purchase'::character varying AS "Line Description",
    shipment."Shipped Quantity" AS "Open Quantity Signed",
    shipment."Purchase Price" AS "Price",
    (shipment."Shipped Quantity" * (shipment."Purchase Price")::double precision) AS amount,
    shipment."BL Date" AS "Delivery Date",
    'Purchase'::text AS "Line Group"
   FROM vw_bi_itsa_fct_shipments shipment
UNION ALL
 SELECT 3 AS "intPnlTypeId",
    ((shipment."intPurchaseLineId" || '_'::text) || shipment."intSaleLineId") AS "strConformedReferenceId",
    1 AS "intPriceGroupId",
    'Sale'::character varying AS "Line Description",
    shipment."Shipped Quantity" AS "Open Quantity Signed",
    shipment."Sale Price" AS "Price",
    (shipment."Shipped Quantity" * (shipment."Sale Price")::double precision) AS amount,
    shipment."BL Date" AS "Delivery Date",
    'Sale'::text AS "Line Group"
   FROM vw_bi_itsa_fct_shipments shipment
UNION ALL
 SELECT 3 AS "intPnlTypeId",
    ((shipment."intPurchaseLineId" || '_'::text) || shipment."intSaleLineId") AS "strConformedReferenceId",
    2 AS "intPriceGroupId",
    fees."Fee" AS "Line Description",
    shipment."Shipped Quantity" AS "Open Quantity Signed",
    fees."Price",
    (shipment."Shipped Quantity" * (fees."Price")::double precision) AS amount,
    shipment."BL Date" AS "Delivery Date",
    fees."Cost Group" AS "Line Group"
   FROM (vw_bi_itsa_fct_shipment_fees fees
     JOIN vw_bi_itsa_fct_shipments shipment ON ((fees."intShipmentId" = shipment."intShipmentId")))
UNION ALL
 SELECT fees."intPnlTypeId",
    fees."strConformedReferenceId",
    fees."intPriceGroupId",
    fees."Line Description",
    fees."Open Quantity Signed",
    fees."Price",
    fees."Amount" AS amount,
    fees."Delivery Date",
    fees."Line Group"
   FROM vw_bi_itsa_fct_inherited_expenses_on_shipments fees
UNION ALL
 SELECT 1 AS "intPnlTypeId",
    trade."strTradeId" AS "strConformedReferenceId",
    1 AS "intPriceGroupId",
    'Expected Price'::character varying AS "Line Description",
    ctr."Contract Quantity" AS "Open Quantity Signed",
    ctr."Avg Price" AS "Price",
    (ctr."Contract Quantity" * ctr."Avg Price") AS amount,
    ctr."Delivery Date",
    'Expected'::text AS "Line Group"
   FROM (vw_bi_itsa_fct_contract ctr
     JOIN vw_bi_dim_trade trade ON ((ctr."TradeLineId" = trade."intTradeId")))
  WHERE ((trade."Book")::text ~~ '%Projection%'::text);;
