CREATE OR REPLACE VIEW V_SALESMONITORPERCUSTOMER_AUX AS
select A.INVOICEHEADER_ID, A.PARENTINVOICEHEADER_ID, A.INVOICENUMBER,
  (CASE WHEN a.invoiceheader_id = a.parentinvoiceheader_id THEN 1 else 0 end) isconsolidateinv,
  a.invoiceyear year, a.invoiceweek week, M.FREQUENCY,
  il.processingbusinessunit_id,
  procbu.code procbucode, procbu.description procbudesc,
  f.code plant, f.name plantname,e.businessunit_id invoicingBU_id,
  e.code businessunit, e.description businessunitdesc,
  a.invoicedate,
  k.code depot, k.description depotdesc,
  j.routenumber, j.description routedesc,
  g.state state,
  h.code marketsegment,
    (SELECT description FROM marketsegment_DESC m WHERE h.marketsegment_id = m.marketsegment_id
     AND m.LANGUAGE_ID = LANGUAGES.LANGUAGE_ID) marketsegmentdesc,
  d.siccode, d.masteraccount masteraccountcust,
  o.customernumber masteraccountnumber, o.masteraccount masteraccount,
  i.code chain, i.name chainname,
  d.customernumber, d.name customername, LANGUAGES.CODE LANGUAGECODE
from invoiceheader a
    left outer join (route j left outer join depot k on j.depot_id = k.depot_id)
      on a.route_id = j.route_id, MAX_DELIVERYFREQUENCY M,
  customer d
    left outer join chain i on (d.chain_id = i.chain_id),
  address g, businessunit e, plant f, marketsegment h, status n, customer o,invoiceline il,businessunit procbu ,languages
where a.documenttype = 1
  AND A.DELTOCUSTOMER_LINK_ID = d.CUSTOMER_ID
  and d.customer_id = m.customer_id
  and a.invoicebusinessunit_id = e.businessunit_id
  and procbu.plant_id = f.plant_id
  and d.marketsegment_id = h.marketsegment_id
  and a.status_id = n.status_id
  and n.statustype = 'IN'
  and n.code = '2'
  and d.masteraccount_id = o.customer_id
  and d.visitaddress_id = g.address_id
  and il.invoiceheader_id=a.invoiceheader_id
--and il.processingbusinessunit_id=procbu.businessunit_id;
  and NVL(il.processingbusinessunit_id, a.invoicebusinessunit_id) = procbu.businessunit_id; --ABS-65505
/