select 12 as SOURCETYPE, SALESRETURN.ITSID as SOURCEID, SALESRETURN.ITSDATE,
'SalesReturns' as ACCDEBIT, 2001 as SUBACCDEBITTYPE, INVITEMCATEGORY.ITSID as SUBACCDEBITID, INVITEMCATEGORY.ITSNAME as SUBACCDEBIT, sum(SALESRETURNLINE.SUBTOTAL) as DEBIT,
'ReturnsPayable' as ACCCREDIT, 2004 as SUBACCCREDITTYPE, DEBTORCREDITOR.ITSID as SUBACCCREDITID, DEBTORCREDITOR.ITSNAME as SUBACCCREDIT, sum(SALESRETURNLINE.ITSTOTAL) 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
