private void FillGridWithCustomerDetails(int mandatoryDocId, string documentNo)
    {
        IList<CustomerDTO> lstCustomerDTO = new List<CustomerDTO>();

        if (Convert.ToInt32(mandatoryDocId) == 0)
        {
            CustomerDTO customerDetails = new CustomerDTO();
            customerDetails = ESalesUnityContainer.Container.Resolve<ICustomerService>().GetCustomerDetailsByCode(documentNo);

            if (customerDetails.Cust_Id > 0)
            {
                int agentId = Convert.ToInt32(customerDetails.Cust_AgentId);

                switch (agentId)
                {
                    case 20:
                        customerDetails.Cust_AgentName = ESalesUnityContainer.Container.Resolve<IAgentService>().GetAgentShortNameByAgentId(agentId);
                        break;
                    case 21:
                        customerDetails.Cust_AgentName = ESalesUnityContainer.Container.Resolve<IAgentService>().GetAgentShortNameByAgentId(agentId);
                        break;
                    case 22:
                        customerDetails.Cust_AgentName = ESalesUnityContainer.Container.Resolve<IAgentService>().GetAgentShortNameByAgentId(agentId);
                        break;
                    case 23:
                        customerDetails.Cust_AgentName = ESalesUnityContainer.Container.Resolve<IAgentService>().GetAgentShortNameByAgentId(agentId);
                        break;
                    case 24:
                        customerDetails.Cust_AgentName = ESalesUnityContainer.Container.Resolve<IAgentService>().GetAgentShortNameByAgentId(agentId);
                        break;
                }

                lstCustomerDTO.Add(customerDetails);
            }

        }
        else
        {
            CustomerDocDetailsDTO docDetails = new CustomerDocDetailsDTO();
            docDetails = ESalesUnityContainer.Container.Resolve<ICustomerDocService>().GetCustomerByDocumentId(mandatoryDocId, documentNo);

            if (docDetails.Cust_Doc_Customer != null)
            {
                lstCustomerDTO.Add(docDetails.Cust_Doc_Customer);
            }
        }

        if (lstCustomerDTO.Count > 0)
        {

            grdDCACustomersAssociation.DataSource = lstCustomerDTO;
            grdDCACustomersAssociation.DataBind();

        }
        else
        {
            ShowBlankGrid();
        }
    }
 /// <summary>
 /// Get Customer Document Details By DocId And CustId
 /// </summary>
 /// <param name="customerId">Int32:customerId</param>
 /// <param name="documentId">Int32:documentId</param>
 /// <returns></returns>
 public CustomerDocDetailsDTO GetCustomerDocumentDetailsByDocIdAndCustId(int customerId, int documentId)
 {
     CustomerDocDetailsDTO customerDocDetails = new CustomerDocDetailsDTO();
     AutoMapper.Mapper.Map(ESalesUnityContainer.Container.Resolve<IGenericRepository<customerdocdetail>>()
     .GetSingle(item => item.Cust_Doc_CustId == customerId && item.Cust_Doc_DocId == documentId
         && item.Cust_Doc_IsDeleted == false), customerDocDetails);
     return customerDocDetails;
 }
        /// <summary>
        /// Get Customer By Document Id
        /// </summary>
        /// <param name="documentType">Int32:documentType</param>
        /// <param name="documentNumber">string:documentNumber</param>
        /// <returns></returns>
        public CustomerDocDetailsDTO GetCustomerByDocumentId(int documentType, string documentNumber)
        {
            CustomerDocDetailsDTO customerDocDetails = new CustomerDocDetailsDTO();

            AutoMapper.Mapper.Map(ESalesUnityContainer.Container.Resolve<IGenericRepository<customerdocdetail>>()
            .GetSingle(item => item.Cust_Doc_DocId == documentType && item.Cust_Doc_No.Equals(documentNumber)
                && item.Cust_Doc_IsDeleted == false), customerDocDetails);

            return customerDocDetails;
        }
        /// <summary>
        /// Get Registered Customer By documentType and documentNumber
        /// </summary>
        /// <param name="documentType">Int32:documentType</param>
        /// <param name="documentNumber">string:documentNumber</param>
        /// <returns></returns>
        public CustomerDocDetailsDTO GetRegisteredCustomerByDocumentId(int documentType, string documentNumber)
        {
            CustomerDocDetailsDTO customerDocDetails = new CustomerDocDetailsDTO();

            AutoMapper.Mapper.Map(ESalesUnityContainer.Container.Resolve<IGenericRepository<customerdocdetail>>()
            .GetSingle(item => item.Cust_Doc_DocId == documentType
                && item.Cust_Doc_No == documentNumber
                && item.Cust_Doc_IsDeleted == false
                && item.customer.Cust_IsBlacklisted == false
                && item.customer.Cust_RegCustType == true
                && item.customer.Cust_Code != null
                ), customerDocDetails);

            return customerDocDetails;
        }
    private void FillGridWithAuthRepDetails(int mandatoryDocId, string documentNo)
    {
		CustomerDTO customer = new CustomerDTO();
		if (Convert.ToInt32(mandatoryDocId) == 0)
		{
			CustomerDTO customerDetails = new CustomerDTO();
			customerDetails = ESalesUnityContainer.Container.Resolve<ICustomerService>().GetCustomerDetailsByCode(documentNo);
			if (customerDetails.Cust_Id > 0)
			{
				customer = customerDetails;
			}
		}
		else
		{
			CustomerDocDetailsDTO doctype = new CustomerDocDetailsDTO();
			doctype = ESalesUnityContainer.Container.Resolve<ICustomerDocService>().GetCustomerByDocumentId(mandatoryDocId, documentNo);
			
			if (doctype.Cust_Doc_Customer != null)
			{
				customer = doctype.Cust_Doc_Customer;
			}
		}

		if (customer.Cust_Id>0)
        {
            IList<AuthRepDTO> lstAuthRepDetailsDTO = (ESalesUnityContainer.Container.Resolve<IAuthRepService>()
					.GetAuthRepDetailsForCustomer(customer.Cust_Id));

			ViewState[Globals.StateMgmtVariables.CUSTOMERID] = customer.Cust_Id;
			ViewState[Globals.StateMgmtVariables.CUSTFOLDERNAME] = customer.Cust_FolderName;

            if (lstAuthRepDetailsDTO.Count > 0)
            {
                grdManageAuthRep.DataSource = lstAuthRepDetailsDTO;
                grdManageAuthRep.DataBind();
            }
            else
            {
                FillBlankGrid();
            }
        }
        else
        {
            FillBlankGrid();
        }
    }
    private void FillDropdownWithCustomerDetails(int mandatoryDocId, string documentNo)
    {
        IList<CustomerDTO> lstCustomerDTO = new List<CustomerDTO>();

        if (Convert.ToInt32(mandatoryDocId) == 0)
        {
            CustomerDTO customerDetails = new CustomerDTO();
            customerDetails = ESalesUnityContainer.Container.Resolve<ICautionListService>().GetCustomerDetailsByCode(documentNo);
            if (customerDetails.Cust_Id > 0)
            {
                lstCustomerDTO.Add(customerDetails);
            }
        }
        else
        {
            CustomerDocDetailsDTO docDetails = new CustomerDocDetailsDTO();
            docDetails = ESalesUnityContainer.Container.Resolve<ICautionListService>().GetCustomerByDocumentId(mandatoryDocId, documentNo);

            if (docDetails.Cust_Doc_Customer != null)
            {
                lstCustomerDTO.Add(docDetails.Cust_Doc_Customer);
            }
        }

        if (lstCustomerDTO.Count > 0)
        {
            PopulateCustCautionList();
            DropDownList ddlCustomerName = (DropDownList)grdCustCautionLstMaster.FooterRow.FindControl("ddlCustomerName");
            ddlCustomerName.DataSource = lstCustomerDTO;
            ddlCustomerName.DataBind();
            ddlCustomerName.Items.Insert(0, new ListItem(Messages.SelectCustomer, "0"));
        }
        else
        {
            PopulateCustCautionList();
            // ShowBlankGrid();
        }
    }
	/// <summary>
	/// 
	/// </summary>
	private void FillGridWithCustomerDetails(int mandatoryDocId, string documentNo)
	{
		IList<CustomerDTO> lstCustomerDTO = new List<CustomerDTO>();

		if (Convert.ToInt32(mandatoryDocId) == 0)
		{
			CustomerDTO customerDetails = new CustomerDTO();
			customerDetails = ESalesUnityContainer.Container.Resolve<ICustomerService>().GetCustomerDetailsByCode(documentNo);
			if (customerDetails.Cust_Id > 0)
			{
				lstCustomerDTO.Add(customerDetails);
			}
			
		}
		else
		{
			CustomerDocDetailsDTO docDetails = new CustomerDocDetailsDTO();
			docDetails = ESalesUnityContainer.Container.Resolve<ICustomerDocService>().GetCustomerByDocumentId(mandatoryDocId, documentNo);

			if (docDetails.Cust_Doc_Customer != null)
			{
				lstCustomerDTO.Add(docDetails.Cust_Doc_Customer);
			}
		}

		if (lstCustomerDTO.Count > 0)
		{

			grdManageCustomers.DataSource = lstCustomerDTO;
			grdManageCustomers.DataBind();
			ViewState[Globals.StateMgmtVariables.TRADENAME] = lstCustomerDTO[0].Cust_TradeName;
		}
		else
		{
			FillBlankGrid();
		}
	}
 /// <summary>
 /// Verify Customer Document No Exists or not by custDocId,docId and docNo
 /// </summary>
 /// <param name="custDocId">Int32:custDocId</param>
 /// <param name="docId">Int32:docId</param>
 /// <param name="docNo">string:docNo</param>
 /// <returns></returns>
 public bool CustomerDocumentNoExists(int custDocId, int docId, string docNo)
 {
     CustomerDocDetailsDTO objCustomerDocDetailsDTO = new CustomerDocDetailsDTO();
     if (custDocId > 0)
     {
         AutoMapper.Mapper.Map(ESalesUnityContainer.Container.Resolve<IGenericRepository<customerdocdetail>>().GetSingle
             (item => item.Cust_Doc_Id != custDocId && item.Cust_Doc_DocId == docId && item.Cust_Doc_No == docNo &&
                 item.Cust_Doc_IsDeleted == false), objCustomerDocDetailsDTO);
     }
     else
     {
         AutoMapper.Mapper.Map(ESalesUnityContainer.Container.Resolve<IGenericRepository<customerdocdetail>>().GetSingle
             (item => item.Cust_Doc_DocId == docId && item.Cust_Doc_No == docNo && item.Cust_Doc_IsDeleted == false), objCustomerDocDetailsDTO);
     }
     return objCustomerDocDetailsDTO.Cust_Doc_Id > 0 ? true : false;
 }
 /// <summary>
 /// Delete Customer Document Details
 /// </summary>
 /// <param name="customerDocs"></param>
 private static void DeleteCustomerDocumentDetails(CustomerDocDetailsDTO customerDocs)
 {
     customerdocdetail custDocEntity = new customerdocdetail();
     AutoMapper.Mapper.Map(customerDocs, custDocEntity);
     ESalesUnityContainer.Container.Resolve<IGenericRepository<customerdocdetail>>().Update(custDocEntity);
 }
    private void FillDropdownWithTruckRegNo(int mandatoryDocId, string documentNo)
    {
        IList<CustomerDTO> lstCustomerDTO = new List<CustomerDTO>();
        IList<TruckDetailsDTO> lstTruckDTO = new List<TruckDetailsDTO>();

        if (Convert.ToInt32(mandatoryDocId) == 0)
        {
            CustomerDTO customerDetails = new CustomerDTO();
            customerDetails = ESalesUnityContainer.Container.Resolve<ICautionListService>().GetCustomerDetailsByCode(documentNo);
            if (customerDetails.Cust_Id > 0)
            {
                lstTruckDTO = ESalesUnityContainer.Container.Resolve<ICautionListService>().GetCautionListForTrucksByCustId(customerDetails.Cust_Id);
            }
        }
        else
        {
            CustomerDocDetailsDTO docDetails = new CustomerDocDetailsDTO();
            docDetails = ESalesUnityContainer.Container.Resolve<ICautionListService>().GetCustomerByDocumentId(mandatoryDocId, documentNo);

            if (docDetails.Cust_Doc_Customer != null)
            {
                lstTruckDTO = ESalesUnityContainer.Container.Resolve<ICautionListService>().GetCautionListForTrucksByCustId(docDetails.Cust_Doc_Id);
            }
        }

        if (lstTruckDTO.Count > 0)
        {
            PopulateTruckCautionList();
            DropDownList ddlTruckRegNo = (DropDownList)grdTruckCautionLst.FooterRow.FindControl("ddlTruckRegNo");
            ddlTruckRegNo.DataSource = lstTruckDTO;
            ddlTruckRegNo.DataBind();
            ddlTruckRegNo.Items.Insert(0, new ListItem(Messages.SelectTruckRegNo, "0"));
        }
        else
        {
            PopulateTruckCautionList();
            DropDownList ddlTruckRegNo = (DropDownList)grdTruckCautionLst.FooterRow.FindControl("ddlTruckRegNo");
            ddlTruckRegNo.Items.Insert(0, new ListItem(Messages.SelectTruckRegNo, "0"));
        }
    }
    private void SaveDocumentListForCustomer()
    {
        IList<CustomerDocDetailsDTO> listDocDetail = new List<CustomerDocDetailsDTO>();
        IList<CustomerDocumentsDTO> listCustDocument = new List<CustomerDocumentsDTO>();

        foreach (GridViewRow row in grdDocument.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                if (((TextBox)(row.Cells[3].Controls[1])).Text != string.Empty)
                {
                    CustomerDocDetailsDTO custDocDetails = new CustomerDocDetailsDTO();
                    custDocDetails.Cust_Doc_CustId = Convert.ToInt32(ViewState[Globals.StateMgmtVariables.CUSTOMERID]);
                    custDocDetails.Cust_Doc_DocId = Convert.ToInt32(grdDocument.DataKeys[row.RowIndex].Value);
                    custDocDetails.Cust_Doc_No = ((TextBox)(row.Cells[3].Controls[1])).Text;

                    DateTimeFormatInfo dateTimeFormatterProvider = DateTimeFormatInfo.CurrentInfo.Clone() as DateTimeFormatInfo;
                    dateTimeFormatterProvider.ShortDatePattern = "dd/MM/yyyy";
                    if (((TextBox)(row.Cells[4].Controls[1])).Text != string.Empty)
                    {
                        custDocDetails.Cust_Doc_ExDate = DateTime.Parse(((TextBox)(row.Cells[4].Controls[1])).Text, dateTimeFormatterProvider);
                    }

                    CustomerDocumentsDTO custDocument = new CustomerDocumentsDTO();
                    custDocument.CustDoc_CreatedBy = base.GetCurrentUserId();
                    custDocument.CustDoc_CreatedDate = DateTime.Now;

                    if (((Label)(row.Cells[6].Controls[1])).Text.Trim() != string.Empty)
                    {
                        string filePath = Path.Combine(Convert.ToString(ViewState[Globals.StateMgmtVariables.FILEPATH]),
                            ((Label)(row.Cells[6].Controls[1])).Text);
                        custDocument.CustDoc_File = ImageToBlob.ConvertImageToByteArray(filePath);
                    }
                    else
                    {
                        custDocument.CustDoc_File = null;
                    }

                    listCustDocument.Add(custDocument);

                    custDocDetails.Cust_Doc_FileName = ((Label)(row.Cells[6].Controls[1])).Text;
                    custDocDetails.Cust_Doc_CreatedBy = GetCurrentUserId();
                    custDocDetails.Cust_Doc_CreatedDate = DateTime.Now;
                    custDocDetails.Cust_Doc_LastUpdatedDate = DateTime.Now;

                    listDocDetail.Add(custDocDetails);
                }
            }
        }


        //Save Customer Document Details
        ESalesUnityContainer.Container.Resolve<ICustomerService>().SaveAndUpdateCustomerDocumentDetails(listDocDetail, listCustDocument);
        DeleteFiles();
    }
    private void FillGridWithTruckDetails(int mandatoryDocId, string documentNo)
    {
		CustomerDTO customer = new CustomerDTO();
		try
        {
            ExceptionHandler.AppExceptionManager.Process(() =>
            {
				if (Convert.ToInt32(mandatoryDocId) == 0)
				{
					CustomerDTO customerDetails = new CustomerDTO();
					customerDetails = ESalesUnityContainer.Container.Resolve<ICustomerService>().GetCustomerDetailsByCode(documentNo);
					if (customerDetails.Cust_Id > 0)
					{
						customer = customerDetails;
					}
				}
				else
				{
					CustomerDocDetailsDTO doctype = new CustomerDocDetailsDTO();
					doctype = ESalesUnityContainer.Container.Resolve<ICustomerDocService>().GetCustomerByDocumentId(mandatoryDocId, documentNo);
					if (doctype.Cust_Doc_Customer != null)
					{
						customer = doctype.Cust_Doc_Customer;
					}					
				}

				if (customer.Cust_Id >0)
                {	
                    IList<TruckDetailsDTO> lstTruckDetailsDTO = (ESalesUnityContainer.Container.Resolve<ITruckService>()
						.GetTruckDetailsForCustomer(customer.Cust_Id));

					ViewState[Globals.StateMgmtVariables.CUSTOMERID] = customer.Cust_Id;
					ViewState[Globals.StateMgmtVariables.CUSTFOLDERNAME] = customer.Cust_FolderName;

                    if (lstTruckDetailsDTO.Count > 0)
                    {
                        grdManageTrucks.DataSource = lstTruckDetailsDTO;
                        grdManageTrucks.DataBind();
                    }
                    else
                    {
                        FillBlankGrid();
                    }
                }
                else
                {
                    FillBlankGrid();
                }
            }, Globals.ExceptionTypes.ExceptionShielding.ToString());
        }
        catch (Exception ex)
        {
        }
    }
	private void FillGridWithCustomerDetails(int mandatoryDocId, string documentNo)
	{
		IList<CustomerDTO> lstCustomerDTO = new List<CustomerDTO>();
        CustomerDTO customerDetails = null;
		if (Convert.ToInt32(mandatoryDocId) == 0)
		{
			customerDetails = new CustomerDTO();
			customerDetails = ESalesUnityContainer.Container.Resolve<ICustomerService>().GetCustomerDetailsByCode(documentNo);
			if (customerDetails.Cust_Id > 0)
			{
				lstCustomerDTO.Add(customerDetails);
			}

		}
		else
		{
			CustomerDocDetailsDTO docDetails = new CustomerDocDetailsDTO();
			docDetails = ESalesUnityContainer.Container.Resolve<ICustomerDocService>().GetCustomerByDocumentId(mandatoryDocId, documentNo);

			if (docDetails.Cust_Doc_Customer != null)
			{
				lstCustomerDTO.Add(docDetails.Cust_Doc_Customer);
			}
		}

		if (lstCustomerDTO.Count > 0)
		{

			CustomerDocDetailsDTO custDocDetails = ESalesUnityContainer.Container.Resolve<ICustomerDocService>()
			 .GetCustomerDocumentDetailsByDocIdAndCustId(Convert.ToInt32(lstCustomerDTO[0].Cust_Id), Convert.ToInt32(ddlAllDocument.SelectedItem.Value));

			if (custDocDetails.Cust_Doc_Id > 0)
			{
				LoadDocumentGrid();
				ViewState[Globals.StateMgmtVariables.CUSTOMERID] = custDocDetails.Cust_Doc_CustId;
				foreach (GridViewRow row in grdDocument.Rows)
				{
					if (row.RowType == DataControlRowType.DataRow)
					{

						if (custDocDetails != null)
						{

							((TextBox)row.FindControl("txtDocNo")).Text = custDocDetails.Cust_Doc_No;
							((HiddenField)row.FindControl("hdnCustDocId")).Value = custDocDetails.Cust_Doc_Id.ToString();
							((TextBox)row.FindControl("txtDocExDate")).Text = string.IsNullOrEmpty(Convert.ToString(custDocDetails.Cust_Doc_ExDate)) ? string.Empty :
								Convert.ToDateTime(custDocDetails.Cust_Doc_ExDate).ToString("dd MMM yyyy");
						}
					}
				}
				grdManageCustomers.DataSource = lstCustomerDTO;
				grdManageCustomers.DataBind();
				//ViewState[Globals.StateMgmtVariables.TRADENAME] = lstCustomerDTO[0].Cust_TradeName;
			}
			else
			{
				grdManageCustomers.DataSource = lstCustomerDTO;
				grdManageCustomers.DataBind();
				CustomerDocDetailsDTO custDocDetailsNew = ESalesUnityContainer.Container.Resolve<ICustomerDocService>()
							 .GetCustomerDocumentDetailsByDocIdAndCustId(Convert.ToInt32(lstCustomerDTO[0].Cust_Id), Convert.ToInt32(ddlMandatoryDoc.SelectedItem.Value));
				LoadDocumentGrid();
                ViewState[Globals.StateMgmtVariables.CUSTOMERID] = customerDetails.Cust_Id;
			}
		}
		else
		{
			FillBlankGrid();
		}
	}
	/// <summary>
	/// 
	/// </summary>
	/// <returns></returns>
	private void SaveDocumentListForCustomer()
	{
		IList<CustomerDocDetailsDTO> listDocDetail = new List<CustomerDocDetailsDTO>();
		IList<CustomerDocumentsDTO> listCustDocument = new List<CustomerDocumentsDTO>();

		foreach (GridViewRow row in grdDocument.Rows)
		{
			if (row.RowType == DataControlRowType.DataRow)
			{
				if (((TextBox)(row.Cells[3].Controls[1])).Text != string.Empty)
				{
					CustomerDocDetailsDTO custDocDetails = new CustomerDocDetailsDTO();
					custDocDetails.Cust_Doc_CustId = Convert.ToInt32(ViewState[Globals.StateMgmtVariables.CUSTOMERID]);
					custDocDetails.Cust_Doc_DocId = Convert.ToInt32(grdDocument.DataKeys[row.RowIndex].Value);
					custDocDetails.Cust_Doc_No = ((TextBox)(row.Cells[3].Controls[1])).Text;

					DateTimeFormatInfo dateTimeFormatterProvider = DateTimeFormatInfo.CurrentInfo.Clone() as DateTimeFormatInfo;
					dateTimeFormatterProvider.ShortDatePattern = "dd/MM/yyyy";
					if (((TextBox)(row.Cells[4].Controls[1])).Text != string.Empty)
					{
						custDocDetails.Cust_Doc_ExDate = DateTime.Parse(((TextBox)(row.Cells[4].Controls[1])).Text, dateTimeFormatterProvider);
					}

					CustomerDocumentsDTO custDocument = new CustomerDocumentsDTO();
					custDocument.CustDoc_CreatedBy = base.GetCurrentUserId();
					custDocument.CustDoc_CreatedDate = DateTime.Now;

					//If fileupload control has file
					if (filAuthDoc.HasFile)
					{
						string uploadFilePath = Path.Combine(Server.MapPath("../CustomerAuthImages"), filAuthDoc.FileName);
						filAuthDoc.SaveAs(uploadFilePath);

						custDocument.CustDoc_File = ImageToBlob.ConvertImageToByteArray(uploadFilePath);
						custDocDetails.Cust_Doc_FileName = filAuthDoc.FileName;
						//Delete the file from application folder after converting into byte array
						File.Delete(uploadFilePath);
					}
					else
					{
						custDocument.CustDoc_File = null;
					}

					listCustDocument.Add(custDocument);

					custDocDetails.Cust_Doc_CreatedBy = GetCurrentUserId();
					custDocDetails.Cust_Doc_CreatedDate = DateTime.Now;
					custDocDetails.Cust_Doc_LastUpdatedDate = DateTime.Now;

					listDocDetail.Add(custDocDetails);
				}
			}
		}

		//Save Customer Document Details
		ESalesUnityContainer.Container.Resolve<ICustomerService>().EditCustomerDocumentDetails(listDocDetail, listCustDocument);
	}