public IActionResult Update([FromBody] dynamic customerData) { ValidateSession(); var result = ""; try { if (customerData != null) { Lms_CustomerPoco customerPoco = JsonConvert.DeserializeObject <Lms_CustomerPoco>(JsonConvert.SerializeObject(customerData[0])); if (customerPoco.Id > 0) { var customer = _customerLogic.GetSingleById(customerPoco.Id); customer.Id = customerPoco.Id; customer.CustomerName = customerPoco.CustomerName; customer.DiscountPercentage = customerPoco.DiscountPercentage; customer.InvoiceDueDays = customerPoco.InvoiceDueDays; customer.IsGstApplicable = customerPoco.IsGstApplicable; customer.IsActive = customerPoco.IsActive; _customerLogic.Update(customer); } } } 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)); }
public IActionResult Update([FromBody] dynamic customerData) { ValidateSession(); int customerId = 0; int billingAddressId = 0; int shippingAddressId = 0; try { if (customerData != null) { Lms_CustomerPoco customerPoco = JsonConvert.DeserializeObject <Lms_CustomerPoco>(JsonConvert.SerializeObject(customerData[0])); CustomerAddress customerBillingAddress = JsonConvert.DeserializeObject <CustomerAddress>(JsonConvert.SerializeObject(customerData[1])); CustomerAddress customerShippingAddress = JsonConvert.DeserializeObject <CustomerAddress>(JsonConvert.SerializeObject(customerData[2])); if (customerPoco.Id < 1) { return(Json("")); } var existingCustomer = _customerLogic.GetSingleById(customerPoco.Id); if (existingCustomer != null) { existingCustomer.CustomerName = customerPoco.CustomerName; existingCustomer.DiscountPercentage = customerPoco.DiscountPercentage; existingCustomer.FuelSurChargePercentage = customerPoco.FuelSurChargePercentage > 0 ? customerPoco.FuelSurChargePercentage : null; existingCustomer.InvoiceDueDays = customerPoco.InvoiceDueDays; existingCustomer.IsGstApplicable = customerPoco.IsGstApplicable; existingCustomer.IsActive = customerPoco.IsActive; var existingAccountInfo = _chartOfAccountLogic.GetSingleById(existingCustomer.AccountId); existingAccountInfo.AccountName = customerPoco.CustomerName; _chartOfAccountLogic.Update(existingAccountInfo); customerId = _customerLogic.Update(existingCustomer).Id; } customerBillingAddress.CustomerId = customerId; customerShippingAddress.CustomerId = customerId; billingAddressId = AddOrGetAddress(customerBillingAddress); shippingAddressId = AddOrGetAddress(customerShippingAddress); DeleteExistingMappedBillingAddress(customerId); if (billingAddressId > 0) { SetBillingShippingAddress(customerId, billingAddressId, (byte)Enum_AddressType.Billing, true); } DeleteExistingMappedShippingAddress(customerId, shippingAddressId); MakeAllShippingAddressSecondary(customerId); if (shippingAddressId > 0) { SetBillingShippingAddress(customerId, shippingAddressId, (byte)Enum_AddressType.Shipping, true); } } } catch (Exception ex) { } return(Json(customerId.ToString())); }