-- View: public.vw_utility_dim_physical_sale_contract

-- DROP VIEW public.vw_utility_dim_physical_sale_contract;

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"
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"
   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
  WHERE 1 = 1 AND sl.type::text = 'line'::text;

ALTER TABLE public.vw_utility_dim_physical_sale_contract
    OWNER TO postgres;
