CREATE OR REPLACE VIEW public.vw_pnl AS
 SELECT pp.name AS "Supplier",
    sp.name AS "Client",
        CASE
            WHEN (pt.name IS NOT NULL) THEN pt.name
            ELSE pt2.name
        END AS "Product",
    v.amount AS "Amount"
   FROM ((((((((((valuation_valuation v
     LEFT JOIN product_product pr ON ((pr.id = v.product)))
     LEFT JOIN product_template pt ON ((pt.id = v.product)))
     LEFT JOIN product_template pt2 ON ((pt2.id = pr.template)))
     LEFT JOIN product_product pr2 ON ((pr2.template = pt2.id)))
     LEFT JOIN purchase_purchase p ON ((p.id = v.purchase)))
     LEFT JOIN purchase_line pl ON ((pl.id = v.line)))
     LEFT JOIN sale_sale s ON ((s.id = v.sale)))
     LEFT JOIN sale_line sl ON ((sl.id = v.sale_line)))
     LEFT JOIN party_party pp ON ((pp.id = p.party)))
     LEFT JOIN party_party sp ON ((sp.id = s.party)));;
