CREATE OR REPLACE VIEW public.vw_utility_dim_physical_sale_contract AS
 SELECT 0 AS "intSaleLineId",
    ''::character varying AS "strContractNb",
    ''::character varying AS "strCounterparty",
    ''::character varying AS "strReference",
    NULL::date AS "dtmContractDate",
    ''::character varying AS "strContractStatus",
    ''::character varying AS "strPaymentTerm",
    ''::character varying AS "strCurrency",
    ''::character varying AS "strCertification",
    ''::character varying AS "strWeightBasis",
    ''::character varying AS "strBroker",
    ''::character varying AS "strCertif",
    ''::character varying AS "strAssociation",
    ''::character varying AS "strCrop",
    0 AS "dblTolMinPct",
    0 AS "dblTolMaxPct",
    ''::character varying AS "strIncoterm",
    ''::character varying AS "strLoadingPlace",
    ''::character varying AS "strDestinationPlace",
    ''::text AS "strProduct",
    ''::character varying AS "strDeliveryPeriod",
    NULL::date AS "dtmDeliveryDateFrom",
    NULL::date AS "dtmDeliveryDateTo",
    ''::character varying AS "strQuantityUom",
    ''::character varying AS "strRegion",
    ''::character varying AS "strStrategyName",
    ''::character varying AS "strBookName",
    ''::character varying AS "strIncotermCode",
    NULL::date AS "dtmEstimatedBLDate",
    ''::character varying AS "strPricingType",
    ''::character varying AS "strCounterpartyCountry"
UNION ALL
 SELECT sl.id AS "intSaleLineId",
    sc.number AS "strContractNb",
    pty.name AS "strCounterparty",
    sc.reference AS "strReference",
    sc.sale_date AS "dtmContractDate",
    sc.state AS "strContractStatus",
    COALESCE(pt.name, ''::character varying) AS "strPaymentTerm",
    cur.name AS "strCurrency",
    COALESCE(sc.certification, ''::character varying) AS "strCertification",
    COALESCE(wbs.name, ''::character varying) AS "strWeightBasis",
    COALESCE(brk.name, ''::character varying) AS "strBroker",
    COALESCE(cer.name, ''::character varying) AS "strCertif",
    COALESCE(ass.name, ''::character varying) AS "strAssociation",
    COALESCE(crp.name, ''::character varying) AS "strCrop",
    sc.tol_min AS "dblTolMinPct",
    sc.tol_max AS "dblTolMaxPct",
    COALESCE(inc.name, ''::character varying) AS "strIncoterm",
    COALESCE(src.name, ''::character varying) AS "strLoadingPlace",
    COALESCE(dst.name, ''::character varying) AS "strDestinationPlace",
    concat('[', pr."strProductCode", '] ', pr."strTemplate") AS "strProduct",
    COALESCE(pm.month_name) AS "strDeliveryPeriod",
    sl.from_del AS "dtmDeliveryDateFrom",
    sl.to_del AS "dtmDeliveryDateTo",
    pu1.name AS "strQuantityUom",
    ad."strRegion",
    strategy."strValueName" AS "strStrategyName",
    book."strValueName" AS "strBookName",
    COALESCE(inc.code, ''::character varying) AS "strIncotermCode",
    COALESCE(bl.estimated_date, sl.to_del) AS "dtmEstimatedBLDate",
    sl.price_type AS "strPricingType",
    COALESCE(ad."strCountry", 'Undefined'::character varying) AS "strCounterpartyCountry"
   FROM (((((((((((((((((((sale_line sl
     JOIN sale_sale sc ON ((sl.sale = sc.id)))
     JOIN party_party pty ON ((sc.party = pty.id)))
     JOIN currency_currency cur ON ((sc.currency = cur.id)))
     JOIN product_uom pu1 ON ((sl.unit = pu1.id)))
     LEFT JOIN party_party brk ON ((sc.broker = brk.id)))
     LEFT JOIN account_invoice_payment_term pt ON ((sc.payment_term = pt.id)))
     LEFT JOIN purchase_crop crp ON ((sc.crop = crp.id)))
     LEFT JOIN purchase_certification cer ON ((sc.certif = cer.id)))
     LEFT JOIN purchase_association ass ON ((sc.association = ass.id)))
     LEFT JOIN purchase_weight_basis wbs ON ((sc.wb = wbs.id)))
     LEFT JOIN stock_location src ON ((sc.from_location = src.id)))
     LEFT JOIN stock_location dst ON ((sc.to_location = dst.id)))
     LEFT JOIN incoterm_incoterm inc ON ((sc.incoterm = inc.id)))
     JOIN vw_utility_product pr ON ((sl.product = pr."intProductId")))
     LEFT JOIN product_month pm ON ((sl.del_period = pm.id)))
     LEFT JOIN vw_utility_last_counterparty_address ad ON ((pty.id = ad."intPartyId")))
     LEFT JOIN vw_utility_strategy_dimension strategy ON (((sl.id = strategy."intTradeLineId") AND (strategy."strTradeCategory" = 'Physical'::text) AND (strategy."strTradeType" = 'Sale'::text))))
     LEFT JOIN vw_utility_book_dimension book ON (((sl.id = book."intTradeLineId") AND (book."strTradeCategory" = 'Physical'::text) AND (book."strTradeType" = 'Sale'::text))))
     LEFT JOIN pricing_estimated bl ON (((sl.id = bl.sale_line) AND ((bl.trigger)::text = 'bldate'::text))))
  WHERE ((1 = 1) AND ((sl.type)::text = 'line'::text));;
