select TAX as TAXID,  sum(PURCHASERETURNLINE.SUBTOTAL * ITSPERCENTAGE / 100) as TOTALTAX
from PURCHASERETURNLINE 
join PURCHASEINVOICELINE on PURCHASEINVOICELINE.ITSID = PURCHASERETURNLINE.PURCHASEINVOICELINE
join INVITEM on INVITEM.ITSID = PURCHASEINVOICELINE.INVITEM
join INVITEMTAXCATEGORY on INVITEMTAXCATEGORY.ITSID = INVITEM.TAXCATEGORY
join INVITEMTAXCATEGORYLINE on INVITEMTAXCATEGORYLINE.ITSOWNER = INVITEMTAXCATEGORY.ITSID
where PURCHASEINVOICELINE.REVERSEDID is null and PURCHASERETURNLINE.REVERSEDID is null and PURCHASERETURNLINE.ITSOWNER = :ITSOWNER
group by TAX;
