private static CustomerSite getEntityByModel(CustomerSiteModel model)
        {
            if (model == null) return null;

            CustomerSite entity = new CustomerSite();

            if (model.Id == 0)
            {
                entity.CreateBy = AuthenticationHelper.UserId;
                entity.CreateDate = DateTime.Now;
            }
            else
            {
                entity.CreateBy = model.CreateBy;
                entity.CreateDate = model.CreateDate;
            }
            entity.CodeCombinationId = model.CodeCombinationId;
            entity.CustomerId = model.CustomerId;
            entity.EndDate = model.EndDate;
            entity.Id = model.Id;
            entity.SiteAddress = model.SiteAddress;
            entity.SiteContact = model.SiteContact;
            entity.SiteName = model.SiteName;
            entity.StartDate = model.StartDate;
            entity.TaxCodeId = model.TaxId;
            entity.UpdateBy = AuthenticationHelper.UserId;
            entity.UpdateDate = DateTime.Now;
            return entity;
        }
        public ActionResult Edit(long customerId, long? id)
        {
            ViewBag.CustomerName = CustomerHelper.GetCustomer(customerId.ToString()).CustomerName;

            CustomerSiteModel model;
            if (id != null)
            {
                model = CustomerHelper.GetCustomerSite(id.Value.ToString());
                CodeCombinitionCreateViewModel codeCombination = CodeCombinationHelper.GetCodeCombination(model.CodeCombinationId.ToString());

                model.CodeCombinationString = Utility.Stringize(".", codeCombination.Segment1, codeCombination.Segment2, codeCombination.Segment3,
                    codeCombination.Segment4, codeCombination.Segment5, codeCombination.Segment6, codeCombination.Segment7, codeCombination.Segment8);
            }

            else
            {
                model = new CustomerSiteModel();
                model.CustomerId = customerId;
            }

            model.TaxCode = taxService.GetAll(AuthenticationHelper.CompanyId.Value)
                .Select(x => new SelectListItem
                {
                    Text = x.TaxName,
                    Value = x.Id.ToString()
                }).ToList();
            model.TaxId = model.TaxCode.Any() ? Convert.ToInt64(model.TaxCode.First().Value) : 0;

            //model.CodeCombination = codeCombinationService.GetAllCodeCombinitionView(AuthenticationHelper.CompanyId.Value)
            //        .Select(x => new SelectListItem
            //        {
            //            Text = x.CodeCombinitionCode,
            //            Value = x.Id.ToString()
            //        }).ToList();
            //model.CodeCombinationId = model.CodeCombination.Any() ? Convert.ToInt64(model.CodeCombination.First().Value) : 0;

            return View(model);
        }
        public ActionResult Edit(CustomerSiteModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string result = "";
                    CustomerModel customer = CustomerHelper.GetCustomer(model.CustomerId.ToString());
                    if ((model.StartDate >= customer.StartDate && model.EndDate <= customer.EndDate) ||
                        (model.StartDate == null && customer.StartDate == null ||
                        model.EndDate == null && customer.EndDate == null))
                    {
                        result = CustomerHelper.SaveCustomerSite(model);
                        return RedirectToAction("Index", new { Id = model.CustomerId });
                    }
                    else
                    {
                        ModelState.AddModelError("Error", "Site Dates should be within the range of Customer Dates.");
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Error", ex.Message);
                }
            }

            model.TaxCode = taxService.GetAll(AuthenticationHelper.CompanyId.Value)
                .Select(x => new SelectListItem
                {
                    Text = x.TaxName,
                    Value = x.Id.ToString()
                }).ToList();
            model.TaxId = model.TaxCode.Any() ? Convert.ToInt64(model.TaxCode.First().Value) : 0;

            return View(model);
        }
 public static string SaveCustomerSite(CustomerSiteModel model)
 {
     if (model.Id > 0)
     {
         return siteService.Update(getEntityByModel(model));
     }
     else
     {
         return siteService.Insert(getEntityByModel(model));
     }
 }
 public static CustomerSiteModel GetCustomerSite(string customerSiteId)
 {
     CustomerSiteModel customerSite = new CustomerSiteModel(siteService.GetSingle(customerSiteId, AuthenticationHelper.CompanyId.Value));
     return customerSite;
 }