select 1 as SOURCETYPE, PURCHASEINVOICE.ITSID as SOURCEID, PURCHASEINVOICE.ITSDATE,
'Inventory' as ACCDEBIT, 2001 as SUBACCDEBITTYPE, INVITEMCATEGORY.ITSID as SUBACCDEBITID, INVITEMCATEGORY.ITSNAME as SUBACCDEBIT, sum(PURCHASEINVOICELINE.SUBTOTAL) as DEBIT,
'AccPayable' as ACCCREDIT, 2004 as SUBACCCREDITTYPE, DEBTORCREDITOR.ITSID as SUBACCCREDITID, DEBTORCREDITOR.ITSNAME as SUBACCCREDIT, sum(PURCHASEINVOICELINE.ITSTOTAL) as CREDIT
from PURCHASEINVOICELINE
join PURCHASEINVOICE on PURCHASEINVOICE.ITSID = PURCHASEINVOICELINE.ITSOWNER
join INVITEM on INVITEM.ITSID = PURCHASEINVOICELINE.INVITEM
join INVITEMCATEGORY on INVITEMCATEGORY.ITSID = INVITEM.ITSCATEGORY
join DEBTORCREDITOR on DEBTORCREDITOR.ITSID = PURCHASEINVOICE.VENDOR
where PURCHASEINVOICELINE.REVERSEDID is null and PURCHASEINVOICE.REVERSEDID is null and HASMADEACCENTRIES = 0 :WHEREADD
group by SOURCETYPE, SOURCEID, ITSDATE, ACCDEBIT, SUBACCDEBITTYPE, SUBACCDEBITID, SUBACCDEBIT, ACCCREDIT, SUBACCCREDITTYPE, SUBACCCREDITID, SUBACCCREDIT
