public string Delete(int id = 0) { //variables crm_Customers customer = null; List <crm_Contacts> lstContact = new List <crm_Contacts>(); ActionResultModel model = new ActionResultModel(); int userRole = 0; try { //Check role has user role customer = _customerService.GetCustomerByID(id); if (customer == null) { model.IsSuccess = 0; model.Message = "The customer is not exist!"; return(JsonConvert.SerializeObject(model)); } lstContact = _contactService.GetListContactByCustomerID(id); using (TransactionScope scope = new TransactionScope()) { foreach (crm_Contacts item in lstContact) { _contactService.Delete(item); } _unitOfWork.SaveChanges(); //_tenantUnitOfWork.SaveChanges(); _customerService.Delete(customer); _unitOfWork.SaveChanges(); //_tenantUnitOfWork.SaveChanges(); scope.Complete(); } model.IsSuccess = 1; model.Message = "Delete customer successfully!"; _helper.InsertLogActive(_logService, _unitOfWork, "Customers", "Delete customer.", 3, true); return(JsonConvert.SerializeObject(model)); } catch { model.IsSuccess = 0; model.Message = "Delete fail!"; _helper.InsertLogActive(_logService, _unitOfWork, "Customers", "Delete customer.", 3, false); return(JsonConvert.SerializeObject(model)); } }
public static bool CheckOrgNumberExist(this IRepository <crm_Customers> repository, int id, string orgNumber) { //Variables bool isExist = false; crm_Customers customer = null; try { customer = repository.Queryable().Where(x => x.CustomerId != id && x.OrgNumber == orgNumber).FirstOrDefault(); } catch (Exception e) { } isExist = customer == null ? true : false; return(isExist); }
public string AddEditAction(CustomerModel model) { //Variables ActionResultModel resultModel = new ActionResultModel(); crm_Customers customer = null; crm_Contacts contact = null; UserInfo usInfo = System.Web.HttpContext.Current.Session["UserInfo"] as UserInfo; bool isSaveImageSuccess = true; string pathFiles = "/Tenants/" + _userInfo.TenantAlias; long orgNumber = 0; try { //Check case add or edit if (model.CustomerId > 0) { customer = _customerService.GetCustomerByID(model.CustomerId); if (model.ContactId > 0) { contact = _contactService.GetContactDefaultByCusID(model.CustomerId); } } else { customer = new crm_Customers(); } #region Validate data //Check email vaild if (!GlobalFunctions.IsValidEmail(model.Email.Trim())) { resultModel.IsSuccess = 0; resultModel.Message = "Email is invalid!"; return(JsonConvert.SerializeObject(resultModel)); } if (_countryService.GetCountryById(model.CountryId) == null) { resultModel.IsSuccess = 0; resultModel.Message = "Country is not exist!"; return(JsonConvert.SerializeObject(resultModel)); } if (customer == null && model.CustomerId > 0) { resultModel.IsSuccess = 0; resultModel.Message = "Customer is not exist!"; return(JsonConvert.SerializeObject(resultModel)); } if (!long.TryParse(model.OrgNumber, out orgNumber)) { resultModel.IsSuccess = 0; resultModel.Message = "Org number must be numeric!"; return(JsonConvert.SerializeObject(resultModel)); } if (!_customerService.CheckOrgNumberExist(model.CustomerId, model.OrgNumber.Trim())) { resultModel.IsSuccess = 0; resultModel.Message = "Org number is exist!"; return(JsonConvert.SerializeObject(resultModel)); } #endregion #region Set value for customer entity customer.CustomerName = WebUtility.HtmlEncode(model.CustomerName.Trim()); customer.Email = model.Email.Trim(); customer.PostedAddress = WebUtility.HtmlEncode(model.PostedAddress.Trim()); customer.VisitingAddress = WebUtility.HtmlEncode(model.VisitingAddress.Trim()); customer.CountryId = model.CountryId; customer.Fax = WebUtility.HtmlEncode(model.Fax.Trim()); customer.OrgNumber = model.OrgNumber; customer.CreatedDate = DateTime.Now; customer.CreatedBy = usInfo.ID; customer.Description = WebUtility.HtmlEncode(model.Description.Trim()); customer.Website = WebUtility.HtmlEncode(model.Website); customer.LinkedURL = WebUtility.HtmlEncode(model.LinkedURL); customer.FacebookURL = WebUtility.HtmlEncode(model.FacebookURL); customer.TwitterURL = WebUtility.HtmlEncode(model.TwitterURL); customer.GoogleplusURL = WebUtility.HtmlEncode(model.GoogleplusURL); customer.CustomerLogo = _logoModel.FileName != null ? _logoModel.FileName : model.CustomerId > 0?customer.CustomerLogo:""; #endregion #region Set value for contact entity if (model.FirstName.Trim() != string.Empty || model.LastName.Trim() != string.Empty || model.Address.Trim() != string.Empty || model.ContactPhone.Trim() != string.Empty || model.MobilePhone.Trim() != string.Empty) { if (contact != null) { contact.FirstName = model.FirstName.Trim() != string.Empty ? WebUtility.HtmlEncode(model.FirstName.Trim()) : contact.FirstName; contact.LastName = model.LastName.Trim() != string.Empty ? WebUtility.HtmlEncode(model.LastName.Trim()) : contact.LastName; contact.ContactPhone = model.ContactPhone.Trim() != string.Empty ? WebUtility.HtmlEncode(model.ContactPhone.Trim()) : contact.ContactPhone; contact.MobilePhone = model.MobilePhone.Trim() != string.Empty ? WebUtility.HtmlEncode(model.MobilePhone.Trim()) : contact.MobilePhone; contact.Address = model.Address.Trim() != string.Empty ? WebUtility.HtmlEncode(model.Address.Trim()) : contact.Address; } else { contact = new crm_Contacts(); contact.FirstName = WebUtility.HtmlEncode(model.FirstName.Trim()); contact.LastName = WebUtility.HtmlEncode(model.LastName.Trim()); contact.ContactPhone = WebUtility.HtmlEncode(model.ContactPhone.Trim()); contact.MobilePhone = WebUtility.HtmlEncode(model.MobilePhone.Trim()); contact.Address = WebUtility.HtmlEncode(model.Address.Trim()); contact.IsDefault = true; } } #endregion #region Perform save data //Save image try { if (_logoModel != null && !string.IsNullOrEmpty(_logoModel.FileName)) { //move a file from temps file to tenant folder var _sourceFile = Path.Combine(Server.MapPath(_tempFiles), _logoModel.FileName); var _destinationFile = Path.Combine(Server.MapPath(pathFiles), _logoModel.FileName); if (System.IO.File.Exists(_destinationFile)) { System.IO.File.Delete(_destinationFile); } System.IO.File.Move(_sourceFile, _destinationFile); _logoModel = null; } } catch { isSaveImageSuccess = false; } if (isSaveImageSuccess) { //Add if (model.CustomerId <= 0) { using (TransactionScope scope = new TransactionScope()) { _customerService.Insert(customer); _unitOfWork.SaveChanges(); //_tenantUnitOfWork.SaveChanges(); if (contact != null) { contact.CustomerId = customer.CustomerId; _contactService.Insert(contact); _unitOfWork.SaveChanges(); //_tenantUnitOfWork.SaveChanges(); } scope.Complete(); } } else//Edit { using (TransactionScope scope = new TransactionScope()) { _customerService.Update(customer); _unitOfWork.SaveChanges(); //_tenantUnitOfWork.SaveChanges(); if (contact != null) { contact.CustomerId = customer.CustomerId; if (contact.ContactId > 0) { _contactService.Update(contact); } else { _contactService.Insert(contact); } _unitOfWork.SaveChanges(); //_tenantUnitOfWork.SaveChanges(); } scope.Complete(); } } resultModel.IsSuccess = 1; resultModel.Message = "Data were saved successfully!"; _helper.InsertLogActive(_logService, _unitOfWork, "Customers", model.CustomerId <= 0 ? "Insert new customer" : "Update customer", model.CustomerId <= 0 ? 1 : 2, true); } else { resultModel.IsSuccess = 0; resultModel.Message = "Save image unsuccessfully!"; _helper.InsertLogActive(_logService, _unitOfWork, "Customers", "Save avatar image.", 1, false); } #endregion } catch (TransactionAbortedException te) { _helper.InsertLogActive(_logService, _unitOfWork, "Customers", model.CustomerId <= 0? "Insert new customer":"Update customer", model.CustomerId <= 0?1:2, false); } catch (ApplicationException ex) { _helper.InsertLogActive(_logService, _unitOfWork, "Customers", model.CustomerId <= 0 ? "Insert new customer" : "Update customer", model.CustomerId <= 0 ? 1 : 2, false); resultModel.IsSuccess = 0; resultModel.Message = "Data were saved unsuccessfully!"; } return(JsonConvert.SerializeObject(resultModel)); }
public ActionResult List([DataSourceRequest] DataSourceRequest request) { int total = 0; SortDescriptor sortDescriptor = (request.Sorts != null && request.Sorts.Count > 0) ? request.Sorts.FirstOrDefault() : new SortDescriptor("CreatedDate", ListSortDirection.Descending); sortDescriptor.Member = sortDescriptor.Member ?? "ContactName"; Func <IQueryable <crm_Contacts>, IOrderedQueryable <crm_Contacts> > order; var data = new List <crm_Contacts>(); switch (sortDescriptor.Member) { case "ContactName": if (sortDescriptor.SortDirection == ListSortDirection.Ascending) { order = x => x.OrderBy(y => (y.FirstName + " " + y.LastName)); } else { order = x => x.OrderByDescending(y => (y.FirstName + " " + y.LastName)); } break; case "ContactAddress": if (sortDescriptor.SortDirection == ListSortDirection.Ascending) { order = x => x.OrderBy(y => y.Address); } else { order = x => x.OrderByDescending(y => y.Address); } break; default: if (sortDescriptor.SortDirection == ListSortDirection.Ascending) { order = x => x.OrderBy(y => y.CreatedDate); } else { order = x => x.OrderByDescending(y => y.CreatedDate); } break; } data = _contactService.Select(null, order, null, request.Page, request.PageSize).ToList(); total = _contactService.Select(null, order, null, null, null).Count(); var _lstContactModel = new List <ContactModel>(); var _customer = new crm_Customers(); var _contactModel = new ContactModel(); foreach (var item in data) { _contactModel = item.ToModel(); if (_contactModel != null) { _contactModel.DisplayName = item.FirstName + " " + item.LastName; _contactModel.Address = item.Address; _contactModel.CreatedDate = item.CreatedDate; _contactModel.Email = item.Email; if (item.CustomerId == null) { _contactModel.Customer = ""; } else { _contactModel.Customer = _customerService.GetCustomerByID(item.CustomerId.Value).CustomerName; } } _lstContactModel.Add(_contactModel); } ViewBag.total = total; var result = new DataSourceResult() { Data = _lstContactModel, Total = total }; return(Json(result)); }