Code to get Sales and purchase Quantity and amount date and item wise in MSD Axapta

To  get Sales and purchase Quantity and amount date wise in Axapta you can try following code in Fetch method of customized Report to get the same.

 Query q;  
 QueryRun qr;  
 QueryBuildDatasource qbds;  
 QueryBuildRange qbr;  
 InventTable inventtab;  
 CustInvoiceTrans _custInvoiceTrans;  
 vendInvoiceTrans _vendInvoiceTrans  
 ;  
 q = this.Query();  
 qbds = q.addDataSource(tablenum(InventTable));  
 qr = new QueryRun(q);  
 while(qr.next())  
 {  
 inventtable = qr.get(tableNum(InventTable));  
 select sum(Qty),sum(LineAmount) from _vendInvoiceTrans where _vendInvoiceTrans.ItemId==_ItemId &&_vendInvoiceTrans.InvoiceDate >= fromDate &&_vendInvoiceTrans.InvoiceDate <= ToDate;  
 _PurchQty=_vendInvoiceTrans.Qty;  
 _PurchAmount=_vendInvoiceTrans.LineAmount;  
 select sum(Qty),sum(LineAmount) from _CustInvoiceTrans where _CustInvoiceTrans.ItemId==_ItemId &&_CustInvoiceTrans.InvoiceDate >= fromDate &&_CustInvoiceTrans.InvoiceDate <= ToDate;  
 _SalesQty=_CustInvoiceTrans.Qty;  
 _SalesAmount=_CustInvoiceTrans.LineAmount;  
 }  
If you are getting any error then you need to declare required variables for the same. This code can work in Ax 3,4 and 2009 version but you can get idea for Ax 2012 also to implement the same in any form or modules.