public with sharing class SampleInventorySectionController { public List SIInfo { get; set; } private String accId { get; set; } public String accName { get; set; } public SampleInventorySectionController(ApexPages.StandardController controller) { accId = controller.getId(); } public void init () { SIInfo = new List(); Map SIListInfoMap = new Map(); Decimal EffectiveInventoryInfo = 0; Decimal ExpiredInventoryInfo = 0; List samSheetList = [select Id, Batch__c, ValidTo__c, ProductCode__c, OTCode_Batch_ValidTo__c, Agency__c, Inventory__c, Pro_Model__c, Product__c, Product__r.Name, Product__r.Packing_list_manual__c, Product__r.Asset_Model_No__c from Sample_inventory_sheet__c where Agency__c =: accId and Checked_ExpiredInventory__c = false]; Account account = [select Id, Management_Code__c, Name from Account where Id =: accId]; accName = account.Name; if(samSheetList != null){ for(Sample_inventory_sheet__c samSheet : samSheetList){ if(!SIListInfoMap.containsKey(samSheet.ProductCode__c)){ if(samSheet.ValidTo__c >= Date.today() || samSheet.ValidTo__c == null){ EffectiveInventoryInfo = samSheet.Inventory__c; ExpiredInventoryInfo = 0; } else{ EffectiveInventoryInfo = 0; ExpiredInventoryInfo = samSheet.Inventory__c; } SIListInfo info = new SIListInfo(samSheet, EffectiveInventoryInfo, ExpiredInventoryInfo); SIInfo.add(info); SIListInfoMap.put(samSheet.ProductCode__c, info); } else{ SIListInfo oldinfo = SIListInfoMap.get(samSheet.ProductCode__c); if(samSheet.ValidTo__c >= Date.today() || samSheet.ValidTo__c == null){ EffectiveInventoryInfo = samSheet.Inventory__c; ExpiredInventoryInfo = 0; } else{ EffectiveInventoryInfo = 0; ExpiredInventoryInfo = samSheet.Inventory__c; } oldinfo.EffectiveInventory += EffectiveInventoryInfo; oldinfo.ExpiredInventory += ExpiredInventoryInfo; SIListInfoMap.put(samSheet.ProductCode__c, oldinfo); SIInfo.clear(); for(String ProModel : SIListInfoMap.keySet()){ SIListInfo info = SIListInfoMap.get(ProModel); SIInfo.add(info); } } } } } public class SIListInfo{ public Decimal EffectiveInventory { get; set; } public Decimal ExpiredInventory { get; set; } public String ProductName { get; set; } public Decimal ProductSpecs { get; set; } public String OTCode { get; set; } public SIListInfo(Sample_inventory_sheet__c samSheetInfo, Decimal EffectiveInventoryInfo, Decimal ExpiredInventoryInfo){ // Product2 product = [select Id, Name, ProductCode, Packing_list_manual__c, SFDA_Expiration_Date__c from Product2 where ProductCode =: ProductCode]; ProductName = samSheetInfo.Product__r.Name; ProductSpecs = samSheetInfo.Product__r.Packing_list_manual__c; EffectiveInventory = EffectiveInventoryInfo; ExpiredInventory = ExpiredInventoryInfo; OTCode = samSheetInfo.ProductCode__c; } } }