/// <summary>
        /// Save and update agent details in database
        /// </summary>
        /// <param name="agentDetails">object containing agent details</param>
        public int SaveAndUpdateAgent(AgentDTO agentDetails)
        {            
            agentdetail agentEntity = new agentdetail();
            AutoMapper.Mapper.Map(agentDetails, agentEntity);

            if (agentEntity.Agent_Id > 0)
            {
                ESalesUnityContainer.Container.Resolve<IGenericRepository<agentdetail>>().Update(agentEntity);
            }
            else
            {
                //Save agent details
                ESalesUnityContainer.Container.Resolve<IGenericRepository<agentdetail>>().Save(agentEntity);

                //Gets list of materials from database
                IList<MaterialTypeDTO> listMaterial = ESalesUnityContainer.Container.Resolve<IMaterialTypeService>()
                    .GetMaterialTypeList(false);

                //Loops through list of materials and create mapping between agent and material
                foreach (var material in listMaterial)
                {                    
                    AgentMaterialPercentageDTO agentMaterialPercentageDetails = new AgentMaterialPercentageDTO();
                    agentMaterialPercentageDetails.AMP_Agent_Id = agentEntity.Agent_Id;
                    agentMaterialPercentageDetails.AMP_MaterialType_Id = material.MaterialType_Id;
                    agentMaterialPercentageDetails.AMP_IsActive = true;
                    agentMaterialPercentageDetails.AMP_CreatedBy = agentEntity.Agent_CreatedBy;
                    AgentMaterialPercentageService agentMatPercentageService = new AgentMaterialPercentageService();

                    //Save agent material mapping
                    agentMatPercentageService.SaveAgentMaterialPercentage(agentMaterialPercentageDetails);
                }
            }
            return agentEntity.Agent_Id;
        }
 /// <summary>
 /// get material percentage by Id
 /// </summary>
 /// <param name="agentId"></param>
 /// <param name="materialTypeId"></param>
 /// <returns></returns>
 public AgentMaterialPercentageDTO GetAgentMaterialPercentageByIds(int agentId, int materialTypeId)
 {
     AgentMaterialPercentageDTO agentMaterialPercentageDetails = new AgentMaterialPercentageDTO();
     AutoMapper.Mapper.Map(ESalesUnityContainer.Container.Resolve<IGenericRepository<agentmaterialpercentage>>()
         .GetSingle(item => item.AMP_Agent_Id == agentId && item.AMP_MaterialType_Id == materialTypeId
             && item.AMP_IsDeleted == false), agentMaterialPercentageDetails);
     return agentMaterialPercentageDetails;
 }
 /// <summary>
 /// To update the agent material percentage
 /// </summary>
 /// <param name="agentMaterialPercentageDetails"></param>
 /// <returns></returns>
 public int UpdateAgentPercentage(AgentMaterialPercentageDTO agentMaterialPercentageDetails)
 {
     agentmaterialpercentage agentMaterialPercentageEntity = new agentmaterialpercentage();
     AutoMapper.Mapper.Map(agentMaterialPercentageDetails, agentMaterialPercentageEntity);
     ESalesUnityContainer.Container.Resolve<IGenericRepository<agentmaterialpercentage>>()
         .Update(agentMaterialPercentageEntity);
     return agentMaterialPercentageEntity.AMP_Id;
 }
        /// <summary>
        /// To save the agent material percentage
        /// </summary>
        /// <param name="agentMaterialPercentageDetails"></param>
        public void SaveAgentMaterialPercentage(AgentMaterialPercentageDTO agentMaterialPercentageDetails)
        {
            agentmaterialpercentage agentMaterialPercentageEntity = new agentmaterialpercentage();
            AutoMapper.Mapper.Map(agentMaterialPercentageDetails, agentMaterialPercentageEntity);

            ESalesUnityContainer.Container.Resolve<IGenericRepository<agentmaterialpercentage>>()
                .Save(agentMaterialPercentageEntity);
        }
        /// <summary>
        /// Save Material Type
        /// </summary>
        /// <param name="materialTypeDetails"></param>
        /// <returns></returns>
        public int SaveMaterialType(MaterialTypeDTO materialTypeDetails)
        {
            materialtype materialtypeEntity = new materialtype();
            AutoMapper.Mapper.Map(materialTypeDetails, materialtypeEntity);
            ESalesUnityContainer.Container.Resolve<IGenericRepository<materialtype>>().Save(materialtypeEntity);

            IList<AgentDTO> listAgentDTO = ESalesUnityContainer.Container.Resolve<IAgentService>().GetAgentList();
            AgentMaterialPercentageDTO objAgentMaterialPercentageDTO = new AgentMaterialPercentageDTO();

            foreach (AgentDTO agent in listAgentDTO)
            {

                objAgentMaterialPercentageDTO.AMP_Agent_Id = agent.Agent_Id;
                objAgentMaterialPercentageDTO.AMP_MaterialType_Id = materialtypeEntity.MaterialType_Id;
                objAgentMaterialPercentageDTO.AMP_IsActive = true;
                objAgentMaterialPercentageDTO.AMP_CreatedBy = materialtypeEntity.MaterialType_CreatedBy;
                ESalesUnityContainer.Container.Resolve<IAgentMaterialPercentageService>()
                    .SaveAgentMaterialPercentage(objAgentMaterialPercentageDTO);
            }
            return materialtypeEntity.MaterialType_Id;
        }
    protected void grdMaterialPercentage_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Page.IsValid)
        {
            List<AgentMaterialPercentageDTO> lstAgentMaterialPercentageDTO = new List<AgentMaterialPercentageDTO>();

            for (int i = 0; i < grdMaterialPercentage.Rows.Count; i++)
            {
                AgentMaterialPercentageDTO objAgentMaterialPercentageDTO = new AgentMaterialPercentageDTO();
                GridViewRow r = grdMaterialPercentage.Rows[i];
                objAgentMaterialPercentageDTO.AMP_Percentage = Math.Round(Convert.ToDecimal(((TextBox)grdMaterialPercentage.Rows[i].FindControl("txtPercentage")).Text), 2);
                objAgentMaterialPercentageDTO.AMP_Id = Convert.ToInt32(grdMaterialPercentage.DataKeys[i].Value);
                lstAgentMaterialPercentageDTO.Add(objAgentMaterialPercentageDTO);
            }

            ESalesUnityContainer.Container.Resolve<IAgentMaterialPercentageService>()
                .UpdateAgentpercentage(lstAgentMaterialPercentageDTO);
            
            PopulateMaterialPercentage();
            ucMessageBox.ShowMessage(Resources.Messages.MaterialPercentageAllocatedSuccessfully);
        }
    }