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