select 12 as SOURCETYPE, SALESRETURN.ITSID as SOURCEID, SALESRETURN.ITSDATE,
'Inventory' as ACCDEBIT, 2001 as SUBACCDEBITTYPE, INVITEMCATEGORY.ITSID as SUBACCDEBITID, INVITEMCATEGORY.ITSNAME as SUBACCDEBIT, sum(SALESRETURNLINE.ITSCOST*SALESRETURNLINE.ITSQUANTITY) as DEBIT,
'COGS' as ACCCREDIT, 2001 as SUBACCCREDITTYPE, INVITEMCATEGORY.ITSID as SUBACCCREDITID, INVITEMCATEGORY.ITSNAME as SUBACCCREDIT, sum(SALESRETURNLINE.ITSCOST*SALESRETURNLINE.ITSQUANTITY) as CREDIT
from SALESRETURNLINE
join SALESRETURN on SALESRETURN.ITSID = SALESRETURNLINE.ITSOWNER
join INVITEM on INVITEM.ITSID = SALESRETURNLINE.INVITEM
join INVITEMCATEGORY on INVITEMCATEGORY.ITSID = INVITEM.ITSCATEGORY
join DEBTORCREDITOR on DEBTORCREDITOR.ITSID = SALESRETURN.CUSTOMER
where SALESRETURNLINE.REVERSEDID is null and SALESRETURN.REVERSEDID is null and HASMADEACCENTRIES = 0 :WHEREADD
group by SOURCETYPE, SOURCEID, ITSDATE, ACCDEBIT, SUBACCDEBITTYPE, SUBACCDEBITID, SUBACCDEBIT, ACCCREDIT, SUBACCCREDITTYPE, SUBACCCREDITID, SUBACCCREDIT
