public ActionResult AddAddress(AddressModel model) { var customer = workContext.GetAuthenticatedCustomer(); if (customer == null) { return(RedirectToAction("SingIn", "Login")); } Address address = new Address(); address.FirsName = model.FirsName; address.LastName = model.LastName; address.ProvinceId = model.ProvinceId; address.CountiesId = model.CountiesId; address.Email = model.Email; address.Phone = model.Phone; address.Address1 = model.Address; address.CreatedOnUtc = DateTime.Now; db.Addresses.Add(address); if (db.SaveChanges() > 0) { var addressId = db.Addresses.Max(x => x.Id); CustomerAddressMapping cam = new CustomerAddressMapping(); cam.AddressId = addressId; cam.CustomerId = customer.Id; db.CustomerAddressMappings.Add(cam); db.SaveChanges(); } return(RedirectToAction("AddressList", "Customer")); }
public ActionResult AddressDelete(int id) { Address deleted = db.Addresses.SingleOrDefault(x => x.Id == id); CustomerAddressMapping cam = db.CustomerAddressMappings.SingleOrDefault(x => x.AddressId == id); db.CustomerAddressMappings.Remove(cam); db.Addresses.Remove(deleted); db.SaveChanges(); return(RedirectToAction("AddressList", "Customer")); }
/// <summary> /// Inserts a customer-address mapping record /// </summary> /// <param name="customer">Customer</param> /// <param name="address">Address</param> public virtual void InsertCustomerAddress(Core.Domain.Customers.Customer customer, Address address) { if (customer is null) { throw new ArgumentNullException(nameof(customer)); } if (address is null) { throw new ArgumentNullException(nameof(address)); } if (_customerAddressMappingRepository.Table.FirstOrDefault(m => m.AddressId == address.Id && m.CustomerId == customer.Id) is null) { var mapping = new CustomerAddressMapping { AddressId = address.Id, CustomerId = customer.Id }; _customerAddressMappingRepository.Insert(mapping); } }
public IActionResult Add([FromBody] dynamic customerData) { ValidateSession(); var result = ""; try { if (customerData != null) { Lms_CustomerPoco customerPoco = JsonConvert.DeserializeObject <Lms_CustomerPoco>(JsonConvert.SerializeObject(customerData[0])); CustomerAddressMapping customerAddress = JsonConvert.DeserializeObject <CustomerAddressMapping>(JsonConvert.SerializeObject(customerData[1])); _configurationLogic = new Lms_ConfigurationLogic(_cache, new EntityFrameworkGenericRepository <Lms_ConfigurationPoco>(_dbContext)); _chartOfAccountLogic = new Lms_ChartOfAccountLogic(_cache, new EntityFrameworkGenericRepository <Lms_ChartOfAccountPoco>(_dbContext)); var parentGLForCustomerAccount = _configurationLogic.GetSingleById(1).ParentGLForCustomerAccount; var accounts = _chartOfAccountLogic.GetList().Where(c => c.ParentGLCode == parentGLForCustomerAccount).ToList(); var newAccountId = accounts.Max(c => c.Id) + 1; var newCustomerId = _customerLogic.GetMaxId() + 1; using (var scope = new TransactionScope()) { Lms_ChartOfAccountPoco accountPoco = new Lms_ChartOfAccountPoco(); accountPoco.Id = newAccountId; accountPoco.ParentGLCode = parentGLForCustomerAccount; accountPoco.AccountName = customerPoco.CustomerName; accountPoco.BranchId = sessionData.BranchId == null ? 1 : (int)sessionData.BranchId; accountPoco.CurrentBalance = 0; accountPoco.IsActive = true; accountPoco.Remarks = "Customer Account Receivable"; accountPoco.CreateDate = DateTime.Now; accountPoco.CreatedBy = sessionData.UserId; var addedAcc = _chartOfAccountLogic.Add(accountPoco); if (addedAcc.Id > 0) { customerPoco.Id = newCustomerId; customerPoco.AccountId = addedAcc.Id; customerPoco.CreateDate = DateTime.Now; customerPoco.CreatedBy = sessionData.UserId; var customerId = _customerLogic.Add(customerPoco).Id; result = customerId.ToString(); } if (customerAddress != null && !string.IsNullOrEmpty(customerAddress.AddressLine)) { customerAddress.CustomerId = newCustomerId; customerAddress.IsDefault = true; _addressLogic = new Lms_AddressLogic(_cache, new EntityFrameworkGenericRepository <Lms_AddressPoco>(_dbContext)); var addressList = _addressLogic.GetList(); _customerAddressMappingLogic = new Lms_CustomerAddressMappingLogic(_cache, new EntityFrameworkGenericRepository <Lms_CustomerAddressMappingPoco>(_dbContext)); var customerAddressList = _customerAddressMappingLogic.GetList().Where(c => c.CustomerId == customerAddress.CustomerId); int addressId = 0; var existingAddress = addressList.Where(c => c.UnitNumber == customerAddress.UnitNumber && c.AddressLine == customerAddress.AddressLine && c.CityId == customerAddress.CityId).FirstOrDefault(); if (existingAddress != null) { existingAddress.ProvinceId = customerAddress.ProvinceId; existingAddress.CountryId = customerAddress.CountryId; existingAddress.PostCode = customerAddress.PostCode; existingAddress.PrimaryPhoneNumber = customerAddress.PrimaryPhoneNumber; existingAddress.Fax = customerAddress.Fax; existingAddress.EmailAddress1 = customerAddress.EmailAddress1; existingAddress.EmailAddress2 = customerAddress.EmailAddress1; existingAddress.ContactPersonName = customerAddress.ContactPersonName; addressId = _addressLogic.Update(existingAddress).Id; } else { Lms_AddressPoco addressPoco = new Lms_AddressPoco(); addressPoco.UnitNumber = customerAddress.UnitNumber; addressPoco.AddressLine = customerAddress.AddressLine; addressPoco.CityId = customerAddress.CityId; addressPoco.ProvinceId = customerAddress.ProvinceId; addressPoco.CountryId = customerAddress.CountryId; addressPoco.PostCode = customerAddress.PostCode; addressPoco.PrimaryPhoneNumber = customerAddress.PrimaryPhoneNumber; addressPoco.Fax = customerAddress.Fax; addressPoco.EmailAddress1 = customerAddress.EmailAddress1; addressPoco.EmailAddress2 = customerAddress.EmailAddress1; addressPoco.ContactPersonName = customerAddress.ContactPersonName; addressId = _addressLogic.Add(addressPoco).Id; } if (customerAddress.AddressTypeId == 0) { Lms_CustomerAddressMappingPoco customerAddressMappingPoco = new Lms_CustomerAddressMappingPoco(); customerAddressMappingPoco.CustomerId = customerAddress.CustomerId; customerAddressMappingPoco.AddressId = addressId; customerAddressMappingPoco.AddressTypeId = (byte)Enum_AddressType.Billing; customerAddressMappingPoco.IsDefault = customerAddress.IsDefault; _customerAddressMappingLogic.Add(customerAddressMappingPoco); customerAddressMappingPoco = new Lms_CustomerAddressMappingPoco(); customerAddressMappingPoco.CustomerId = customerAddress.CustomerId; customerAddressMappingPoco.AddressId = addressId; customerAddressMappingPoco.AddressTypeId = (byte)Enum_AddressType.Shipping; customerAddressMappingPoco.IsDefault = customerAddress.IsDefault; _customerAddressMappingLogic.Add(customerAddressMappingPoco); } else { Lms_CustomerAddressMappingPoco customerAddressMappingPoco = new Lms_CustomerAddressMappingPoco(); customerAddressMappingPoco.CustomerId = customerAddress.CustomerId; customerAddressMappingPoco.AddressId = addressId; customerAddressMappingPoco.AddressTypeId = customerAddress.AddressTypeId; customerAddressMappingPoco.IsDefault = customerAddress.IsDefault; _customerAddressMappingLogic.Add(customerAddressMappingPoco); } } scope.Complete(); } } } catch (Exception ex) { } return(Json(result)); }
public IActionResult AddAddress([FromBody] dynamic addressData) { ValidateSession(); var result = ""; try { if (addressData != null) { CustomerAddressMapping customerAddress = JsonConvert.DeserializeObject <CustomerAddressMapping>(JsonConvert.SerializeObject(addressData[0])); //var jAddressObject = (JObject)addressData[0]; //var shippingAddressMappingId = Convert.ToString(jAddressObject.SelectToken("shippingAddressMappingId")); //var billingAddressMappingId = Convert.ToString(jAddressObject.SelectToken("billingAddressMappingId")); if (customerAddress != null) { _addressLogic = new Lms_AddressLogic(_cache, new EntityFrameworkGenericRepository <Lms_AddressPoco>(_dbContext)); var addressList = _addressLogic.GetList(); _customerAddressMappingLogic = new Lms_CustomerAddressMappingLogic(_cache, new EntityFrameworkGenericRepository <Lms_CustomerAddressMappingPoco>(_dbContext)); var customerAddressList = _customerAddressMappingLogic.GetList().Where(c => c.CustomerId == customerAddress.CustomerId); int newAddressId = 0; //using (var scope = new TransactionScope()) //{ var existingAddress = addressList.Where(c => c.UnitNumber == customerAddress.UnitNumber && c.AddressLine == customerAddress.AddressLine && c.CityId == customerAddress.CityId).FirstOrDefault(); if (existingAddress != null) { existingAddress.ProvinceId = customerAddress.ProvinceId; existingAddress.CountryId = customerAddress.CountryId; existingAddress.PostCode = customerAddress.PostCode; existingAddress.PrimaryPhoneNumber = customerAddress.PrimaryPhoneNumber; existingAddress.Fax = customerAddress.Fax; existingAddress.EmailAddress1 = customerAddress.EmailAddress1; existingAddress.EmailAddress2 = customerAddress.EmailAddress1; existingAddress.ContactPersonName = customerAddress.ContactPersonName; newAddressId = _addressLogic.Update(existingAddress).Id; } else { Lms_AddressPoco addressPoco = new Lms_AddressPoco(); addressPoco.UnitNumber = customerAddress.UnitNumber; addressPoco.AddressLine = customerAddress.AddressLine; addressPoco.CityId = customerAddress.CityId; addressPoco.ProvinceId = customerAddress.ProvinceId; addressPoco.CountryId = customerAddress.CountryId; addressPoco.PostCode = customerAddress.PostCode; addressPoco.PrimaryPhoneNumber = customerAddress.PrimaryPhoneNumber; addressPoco.Fax = customerAddress.Fax; addressPoco.EmailAddress1 = customerAddress.EmailAddress1; addressPoco.EmailAddress2 = customerAddress.EmailAddress1; addressPoco.ContactPersonName = customerAddress.ContactPersonName; newAddressId = _addressLogic.Add(addressPoco).Id; } // This will ensure only one address is set as default address for the same type if (customerAddress.IsDefault == true) { var typeWiseAddresses = customerAddressList.Where(c => c.CustomerId == customerAddress.CustomerId && c.AddressTypeId == customerAddress.AddressTypeId).ToList(); if (typeWiseAddresses.Count > 0) { foreach (var item in typeWiseAddresses) { item.IsDefault = false; _customerAddressMappingLogic.Update(item); } } } var existingCustomerAddressMapping = customerAddressList.Where(c => c.AddressId == customerAddress.AddressId && c.AddressTypeId == customerAddress.AddressTypeId).FirstOrDefault(); if (existingCustomerAddressMapping != null) { existingCustomerAddressMapping.AddressId = newAddressId; existingCustomerAddressMapping.IsDefault = customerAddress.IsDefault; _customerAddressMappingLogic.Update(existingCustomerAddressMapping); } else { Lms_CustomerAddressMappingPoco customerAddressMappingPoco = new Lms_CustomerAddressMappingPoco(); customerAddressMappingPoco.CustomerId = customerAddress.CustomerId; customerAddressMappingPoco.AddressId = customerAddress.AddressId; customerAddressMappingPoco.AddressTypeId = customerAddress.AddressTypeId; customerAddressMappingPoco.IsDefault = customerAddress.IsDefault; _customerAddressMappingLogic.Add(customerAddressMappingPoco); } //scope.Complete(); result = newAddressId.ToString(); //} } } } catch (Exception ex) { } return(Json(result)); }
public IActionResult Update([FromBody] dynamic customerData) { ValidateSession(); var result = ""; try { if (customerData != null) { Lms_CustomerPoco customerPoco = JsonConvert.DeserializeObject <Lms_CustomerPoco>(JsonConvert.SerializeObject(customerData[0])); CustomerAddressMapping customerAddress = JsonConvert.DeserializeObject <CustomerAddressMapping>(JsonConvert.SerializeObject(customerData[1])); _configurationLogic = new Lms_ConfigurationLogic(_cache, new EntityFrameworkGenericRepository <Lms_ConfigurationPoco>(_dbContext)); _chartOfAccountLogic = new Lms_ChartOfAccountLogic(_cache, new EntityFrameworkGenericRepository <Lms_ChartOfAccountPoco>(_dbContext)); var jAddressObject = (JObject)customerData[1]; var shippingAddressMappingId = Convert.ToString(jAddressObject.SelectToken("shippingAddressMappingId")); var billingAddressMappingId = Convert.ToString(jAddressObject.SelectToken("billingAddressMappingId")); //using (var scope = new TransactionScope()) //{ if (customerPoco.Id > 0) { var customer = _customerLogic.GetSingleById(customerPoco.Id); customer.CustomerName = customerPoco.CustomerName; customer.DiscountPercentage = customerPoco.DiscountPercentage; customer.FuelSurChargePercentage = customerPoco.FuelSurChargePercentage > 0 ? customerPoco.FuelSurChargePercentage : null; customer.InvoiceDueDays = customerPoco.InvoiceDueDays; customer.IsGstApplicable = customerPoco.IsGstApplicable; customer.IsActive = customerPoco.IsActive; var accountInfo = _chartOfAccountLogic.GetSingleById(customer.AccountId); accountInfo.AccountName = customerPoco.CustomerName; _chartOfAccountLogic.Update(accountInfo); result = _customerLogic.Update(customer).Id.ToString(); if (customerAddress != null) { customerAddress.CustomerId = customerPoco.Id; customerAddress.IsDefault = true; _addressLogic = new Lms_AddressLogic(_cache, new EntityFrameworkGenericRepository <Lms_AddressPoco>(_dbContext)); var addressList = _addressLogic.GetList(); _customerAddressMappingLogic = new Lms_CustomerAddressMappingLogic(_cache, new EntityFrameworkGenericRepository <Lms_CustomerAddressMappingPoco>(_dbContext)); var customerAddressList = _customerAddressMappingLogic.GetList().Where(c => c.CustomerId == customerAddress.CustomerId); int addressId = 0; var existingAddress = addressList.Where(c => c.UnitNumber == customerAddress.UnitNumber && c.AddressLine == customerAddress.AddressLine && c.CityId == customerAddress.CityId).ToList().FirstOrDefault(); if (existingAddress != null) { existingAddress.ProvinceId = customerAddress.ProvinceId; existingAddress.CountryId = customerAddress.CountryId; existingAddress.PostCode = customerAddress.PostCode; existingAddress.PrimaryPhoneNumber = customerAddress.PrimaryPhoneNumber; existingAddress.Fax = customerAddress.Fax; existingAddress.EmailAddress1 = customerAddress.EmailAddress1; existingAddress.EmailAddress2 = customerAddress.EmailAddress1; existingAddress.ContactPersonName = customerAddress.ContactPersonName; addressId = _addressLogic.Update(existingAddress).Id; } else { Lms_AddressPoco addressPoco = new Lms_AddressPoco(); addressPoco.UnitNumber = customerAddress.UnitNumber; addressPoco.AddressLine = customerAddress.AddressLine; addressPoco.CityId = customerAddress.CityId; addressPoco.ProvinceId = customerAddress.ProvinceId; addressPoco.CountryId = customerAddress.CountryId; addressPoco.PostCode = customerAddress.PostCode; addressPoco.PrimaryPhoneNumber = customerAddress.PrimaryPhoneNumber; addressPoco.Fax = customerAddress.Fax; addressPoco.EmailAddress1 = customerAddress.EmailAddress1; addressPoco.EmailAddress2 = customerAddress.EmailAddress1; addressPoco.ContactPersonName = customerAddress.ContactPersonName; addressId = _addressLogic.Add(addressPoco).Id; } // This will ensure only one address is set as default address for the same type var typeWiseAddresses = new List <Lms_CustomerAddressMappingPoco>(); if (customerAddress.IsDefault) { typeWiseAddresses = customerAddressList.Where(c => c.CustomerId == customerAddress.CustomerId && c.AddressTypeId == customerAddress.AddressTypeId).ToList(); if (typeWiseAddresses.Count > 0) { foreach (var item in typeWiseAddresses) { item.IsDefault = false; _customerAddressMappingLogic.Update(item); } } } var addressMappingList = _customerAddressMappingLogic.GetList(); if (customerAddress.AddressTypeId == 0) { Lms_CustomerAddressMappingPoco customerAddressMappingPoco = new Lms_CustomerAddressMappingPoco(); if (billingAddressMappingId != "") { customerAddressMappingPoco = addressMappingList.Where(c => c.Id == Convert.ToInt32(billingAddressMappingId)).FirstOrDefault(); customerAddressMappingPoco.AddressTypeId = (byte)Enum_AddressType.Billing; customerAddressMappingPoco.AddressId = addressId; customerAddressMappingPoco.IsDefault = true; _customerAddressMappingLogic.Update(customerAddressMappingPoco); } else { customerAddressMappingPoco = new Lms_CustomerAddressMappingPoco(); customerAddressMappingPoco.CustomerId = customerAddress.CustomerId; customerAddressMappingPoco.AddressId = addressId; customerAddressMappingPoco.AddressTypeId = (byte)Enum_AddressType.Billing; customerAddressMappingPoco.IsDefault = true; _customerAddressMappingLogic.Add(customerAddressMappingPoco); } if (shippingAddressMappingId != "") { customerAddressMappingPoco = addressMappingList.Where(c => c.Id == Convert.ToInt32(shippingAddressMappingId)).FirstOrDefault(); customerAddressMappingPoco.AddressTypeId = (byte)Enum_AddressType.Shipping; customerAddressMappingPoco.AddressId = addressId; customerAddressMappingPoco.IsDefault = true; _customerAddressMappingLogic.Update(customerAddressMappingPoco); } else { customerAddressMappingPoco = new Lms_CustomerAddressMappingPoco(); customerAddressMappingPoco.CustomerId = customerAddress.CustomerId; customerAddressMappingPoco.AddressId = addressId; customerAddressMappingPoco.AddressTypeId = (byte)Enum_AddressType.Shipping; customerAddressMappingPoco.IsDefault = true; _customerAddressMappingLogic.Add(customerAddressMappingPoco); } } else { Lms_CustomerAddressMappingPoco customerAddressMappingPoco = new Lms_CustomerAddressMappingPoco(); if (customerAddress.AddressTypeId == 1) { if (billingAddressMappingId != "") { customerAddressMappingPoco = addressMappingList.Where(c => c.Id == Convert.ToInt32(billingAddressMappingId)).FirstOrDefault(); customerAddressMappingPoco.AddressId = addressId; customerAddressMappingPoco.IsDefault = true; _customerAddressMappingLogic.Update(customerAddressMappingPoco); } else { customerAddressMappingPoco = new Lms_CustomerAddressMappingPoco(); customerAddressMappingPoco.CustomerId = customerAddress.CustomerId; customerAddressMappingPoco.AddressId = addressId; customerAddressMappingPoco.AddressTypeId = customerAddress.AddressTypeId; customerAddressMappingPoco.IsDefault = true; _customerAddressMappingLogic.Add(customerAddressMappingPoco); } } if (customerAddress.AddressTypeId == 2) { if (shippingAddressMappingId != "") { customerAddressMappingPoco = addressMappingList.Where(c => c.Id == Convert.ToInt32(shippingAddressMappingId)).FirstOrDefault(); customerAddressMappingPoco.AddressId = addressId; customerAddressMappingPoco.IsDefault = true; _customerAddressMappingLogic.Update(customerAddressMappingPoco); } else { customerAddressMappingPoco = new Lms_CustomerAddressMappingPoco(); customerAddressMappingPoco.CustomerId = customerAddress.CustomerId; customerAddressMappingPoco.AddressId = addressId; customerAddressMappingPoco.AddressTypeId = customerAddress.AddressTypeId; customerAddressMappingPoco.IsDefault = true; _customerAddressMappingLogic.Add(customerAddressMappingPoco); } } } } //scope.Complete(); } //} } } catch (Exception ex) { } return(Json(result)); }