CREATE OR REPLACE VIEW public.vw_bi_dim_sale_physical_contract AS
 WITH pricingstatus AS (
         SELECT pvl_1."intContractLineId",
            pvl_1."strState"
           FROM vw_utility_physical_valuation_line pvl_1
          GROUP BY pvl_1."intContractLineId", pvl_1."strState"
        )
 SELECT dpc."intSaleLineId",
    dpc."strContractNb" AS "Contract Nb",
    dpc."strCounterparty" AS "Customer",
    dpc."strReference" AS "Contract Ref",
    dpc."dtmContractDate" AS "Contract Date",
        CASE
            WHEN ((dpc."strContractStatus")::text = 'Draft'::text) THEN (((dpc."strContractStatus")::text || ' 🧾'::text))::character varying
            WHEN ((dpc."strContractStatus")::text = 'Confirmed'::text) THEN (((dpc."strContractStatus")::text || ' ✅'::text))::character varying
            WHEN ((dpc."strContractStatus")::text = 'Validated'::text) THEN (((dpc."strContractStatus")::text || ' ✔️'::text))::character varying
            WHEN ((dpc."strContractStatus")::text = 'Processing'::text) THEN (((dpc."strContractStatus")::text || ' ⏳'::text))::character varying
            ELSE dpc."strContractStatus"
        END AS "Contract Status",
    dpc."strPaymentTerm" AS "Payment Terms",
    dpc."strCurrency" AS "Currency",
    dpc."strWeightBasis" AS "Weight Basis",
    dpc."strBroker" AS "Broker",
    dpc."strCertif" AS "Certification",
    dpc."strAssociation" AS "Association",
    dpc."strCrop" AS "Crop",
    dpc."dblTolMinPct" AS "Tolerance Min %",
    dpc."dblTolMaxPct" AS "Tolerance Max %",
    dpc."strIncoterm" AS "Inco Terms",
    dpc."strLoadingPlace" AS "Loading Place",
    dpc."strDestinationPlace" AS "Destination Place",
    dpc."strProduct" AS "Product",
    dpc."strDeliveryPeriod" AS "Delivery Period",
    dpc."strQuantityUom" AS "Quantity UOM",
    dpc."dtmDeliveryDateFrom" AS "Delivery Date From",
    dpc."dtmDeliveryDateTo" AS "Delivery Date To",
        CASE
            WHEN (dpc."dtmDeliveryDateTo" < CURRENT_DATE) THEN 'Overdue'::text
            WHEN ((dpc."dtmDeliveryDateFrom" <= CURRENT_DATE) AND (dpc."dtmDeliveryDateTo" >= CURRENT_DATE)) THEN 'Current'::text
            WHEN (dpc."dtmDeliveryDateFrom" > CURRENT_DATE) THEN 'Future'::text
            ELSE 'Unknown'::text
        END AS "Delivery Status",
    dpc."strRegion" AS "Bassin",
    dpc."strBookName" AS "Book",
    dpc."strStrategyName" AS "Strategy",
        CASE
            WHEN ((dpc."strBookName")::text ~~ '%Projection%'::text) THEN 'Yes'::text
            ELSE 'No'::text
        END AS "Is Projection",
    dpc."strIncotermCode" AS "Incoterm Code",
    dpc."dtmEstimatedBLDate" AS "Estimated BL Date",
    dpc."strPricingType" AS "Pricing Type",
    COALESCE(mtm."strStrategy", 'Undefined'::character varying) AS "MtM Strategy",
    COALESCE(mtm."strScenario", 'Undefined'::character varying) AS "MtM Scenario",
    COALESCE(mtm."strComponentCurve", 'Undefined'::character varying) AS "MtM Component Curve",
    dpc."strCounterpartyCountry" AS "Customer Country",
    COALESCE(pvl."strState", ('Undefined'::character varying)::text) AS "Price Fixing Status",
    ('0_'::text || dpc."intSaleLineId") AS "strTradeLineId",
    COALESCE(mtm."strStrategyAltName", ('Undefined'::character varying)::text) AS "MtM Strategy Aternative Name"
   FROM ((vw_utility_dim_physical_sale_contract dpc
     LEFT JOIN vw_utility_physical_mtm_definition mtm ON (((dpc."intSaleLineId" = mtm."intContractLineId") AND (mtm."strContractType" = 'Sale'::text) AND ((mtm."strComponentFixType")::text = 'Market price'::text))))
     LEFT JOIN pricingstatus pvl ON ((dpc."intSaleLineId" = pvl."intContractLineId")));;
