Exemple #1
0
        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));
        }
Exemple #2
0
        private int CreateNewAccount(Lms_CustomerPoco customerPoco)
        {
            int newAccountId = 0;

            _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();

            newAccountId = accounts.Max(c => c.Id) + 1;

            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 newAcc = _chartOfAccountLogic.Add(accountPoco);

            if (newAcc != null)
            {
                return(newAccountId);
            }
            else
            {
                return(0);
            }
        }
Exemple #3
0
        public IActionResult Add([FromBody] dynamic customerData)
        {
            ValidateSession();
            int customerId = 0;
            int billingAddressId, 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]));

                    customerId = CreateNewCustomer(customerPoco);
                    customerBillingAddress.CustomerId  = customerId;
                    customerShippingAddress.CustomerId = customerId;

                    if (customerId < 1)
                    {
                        DeleteCustomerById(customerId);
                        return(Json(""));
                    }

                    billingAddressId  = AddOrGetAddress(customerBillingAddress);
                    shippingAddressId = AddOrGetAddress(customerShippingAddress);
                    if (billingAddressId < 1 && shippingAddressId < 1)
                    {
                        DeleteCustomerById(customerId);
                        return(Json(""));
                    }

                    if (billingAddressId > 0)
                    {
                        SetBillingShippingAddress(customerId, billingAddressId, (byte)Enum_AddressType.Billing, true);
                    }

                    if (shippingAddressId > 0)
                    {
                        SetBillingShippingAddress(customerId, shippingAddressId, (byte)Enum_AddressType.Shipping, true);
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(Json(customerId.ToString()));
        }
Exemple #4
0
        private int CreateNewCustomer(Lms_CustomerPoco customerPoco)
        {
            int newCustomerId = _customerLogic.GetMaxId() + 1;

            var newAccountNo = CreateNewAccount(customerPoco);

            if (newAccountNo > 0)
            {
                customerPoco.Id         = newCustomerId;
                customerPoco.AccountId  = newAccountNo;
                customerPoco.CreateDate = DateTime.Now;
                customerPoco.CreatedBy  = sessionData.UserId;
                var customerId = _customerLogic.Add(customerPoco).Id;
                if (customerId > 0)
                {
                    return(customerId);
                }
            }
            return(0);
        }
        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 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));
        }
Exemple #7
0
        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()));
        }