select
    inv.ad_client_id,
    inv.ad_org_id,
    bp.name as toko,
    inv.documentdate as tanggal,
    dt.name as doctype,
    row_number() over() as no,
    case
        when invl.c_charge_id is null then prod.name
        else ch.name
    end as product,
    inv.description as keterangan,
    inv.documentno,
    inv.dateinvoiced,
    pt.name as payterm,
    case
        when inv.paymentrule = 'P' then 'On Credit'
        else inv.paymentrule
    end as payrule,
    co.documentno as pono,
    co.dateordered as podate,
    io.documentno as shipno,
    asi.lot,
    inv.totallines,
    uom.uomsymbol as uom,
    cur.iso_code as cur,
    case
        when inv.docstatus = 'CO' then 'Completed'
        when inv.docstatus = 'VO' then 'Void'
        when inv.docstatus = 'DR' then 'Draft'
        when inv.docstatus = 'RE' then 'Reversed'
        when inv.docstatus = 'IN' then 'Invalid'
        when inv.docstatus = 'CL' then 'Closed'
    end as docstatus,
    co.grandtotal,
    (
        select
            sum(co.grandtotal) as grandtotalorder
        from
            (
                select
                    co.c_order_id,
                    co.grandtotal
                from
                    c_order co
                    left join c_orderline col on col.c_order_id = co.c_order_id
                    left join c_invoiceline cin on cin.c_orderline_id = col.c_orderline_id
                where
                    cin.c_invoice_id = 1000231
                group by
                    co.c_order_id,
                    co.grandtotal
            ) co
    ) as grandtotalorder,
    (
        select
            address1 || ' ' || COALESCE(address2, '')
        from
            c_location
        where
            c_location_id = bloc.c_location_id
    ) as alamat,
    -- (
    --     select
    --         sum(taxamt)
    --     from
    --         c_invoiceline
    --     where
    --         c_invoice_id = 1000231
    -- ) as pajak,
    coalesce(org.description, ' ') as orgdesc,
    coalesce(lf.address1, ' ') as o1,
    coalesce(lf.address2, ' ') as o2,
    coalesce(lf.address3, ' ') as o3,
    coalesce(lf.address4, ' ') as o4,
    coalesce(lf.city, ' ') as city,
    coalesce(f.phone, ' ') as phone,
    coalesce(f.fax, ' ') as fax
from
    c_invoice inv
    left join c_invoiceline invl on invl.c_invoice_id = inv.c_invoice_id
    left join c_orderline col on col.c_orderline_id = invl.c_orderline_id
    left join c_order co on co.c_order_id = col.c_order_id
    left join c_bpartner bp on bp.c_bpartner_id = inv.c_bpartner_id
    left join c_paymentterm pt on pt.c_paymentterm_id = inv.c_paymentterm_id
    left join M_AttributeSetInstance asi on asi.M_AttributeSetInstance_id = invl.M_AttributeSetInstance_id
    left join m_product prod on prod.m_product_id = invl.m_product_id
    left join c_doctype dt on dt.c_doctype_id = inv.c_doctypetarget_id
    left join c_bpartner_location bloc on bloc.c_bpartner_location_id = inv.c_bpartner_location_id
    left join c_uom uom on uom.c_uom_id = invl.c_uom_id
    left join c_currency cur on cur.c_currency_id = inv.c_currency_id
    left join c_charge ch on ch.c_charge_id = invl.c_charge_id
    left join c_chargetype cht on cht.c_chargetype_id = ch.c_chargetype_id
    left join ad_org org on org.ad_org_id = inv.ad_org_id
    left join ad_orginfo f on f.ad_org_id = org.ad_org_id
    left join c_location lf on lf.c_location_id = f.c_location_id
    left join m_inout io on io.m_inout_id = inv.m_inout_id
where
    inv.c_invoice_id = 1000231