public IActionResult Add([FromBody] dynamic addressData)
        {
            ValidateSession();
            var result = "";

            try
            {
                if (addressData != null)
                {
                    Lms_AddressPoco addressPoco = JsonConvert.DeserializeObject <Lms_AddressPoco>(JsonConvert.SerializeObject(addressData));

                    if (addressPoco.Id < 1 && addressPoco.AddressLine.Trim() != string.Empty)
                    {
                        addressPoco.CreatedBy = sessionData.UserId;
                        var address = _addressLogic.Add(addressPoco);

                        result = "Success";
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(Json(result));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method is used to retrive existing address from Address table or Add new address if doesn't exist. Returns addressId. Address should not be updated from this method
        /// </summary>
        /// <param name="customerAddress"></param>
        /// <returns></returns>
        private int AddOrGetAddress(CustomerAddress customerAddress)
        {
            if (string.IsNullOrEmpty(customerAddress.AddressLine) || customerAddress.CityId < 1)
            {
                return(0);
            }

            int addressId = 0;

            _addressLogic = new Lms_AddressLogic(_cache, new EntityFrameworkGenericRepository <Lms_AddressPoco>(_dbContext));
            var addressList = _addressLogic.GetList();

            string unitNumber  = "";
            string addressLine = "";

            unitNumber  = !string.IsNullOrEmpty(customerAddress.AddressUnitNumber.Trim()) ? customerAddress.AddressUnitNumber.Trim().ToUpper() : null;
            addressLine = !string.IsNullOrEmpty(customerAddress.AddressLine.Trim()) ? customerAddress.AddressLine.Trim().ToUpper() : null;

            var existingBillingAddress = addressList.Where(c => c.UnitNumber == unitNumber && c.AddressLine == addressLine && c.CityId == customerAddress.CityId).FirstOrDefault();

            if (existingBillingAddress == null)
            {
                Lms_AddressPoco addressPoco = new Lms_AddressPoco();
                addressPoco.UnitNumber         = unitNumber;
                addressPoco.AddressLine        = 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.EmailAddress;
                addressPoco.EmailAddress2     = customerAddress.EmailAddress;
                addressPoco.ContactPersonName = customerAddress.ContactPersonName;

                addressId = _addressLogic.Add(addressPoco).Id;
            }
            else
            {
                existingBillingAddress.ProvinceId         = customerAddress.ProvinceId;
                existingBillingAddress.CountryId          = customerAddress.CountryId;
                existingBillingAddress.PostCode           = customerAddress.PostCode;
                existingBillingAddress.PrimaryPhoneNumber = customerAddress.PrimaryPhoneNumber;
                existingBillingAddress.Fax               = customerAddress.Fax;
                existingBillingAddress.EmailAddress1     = customerAddress.EmailAddress;
                existingBillingAddress.EmailAddress2     = customerAddress.EmailAddress;
                existingBillingAddress.ContactPersonName = customerAddress.ContactPersonName;

                addressId = _addressLogic.Update(existingBillingAddress).Id;
            }

            return(addressId);
        }
        public IActionResult Update([FromBody] dynamic addressData)
        {
            ValidateSession();
            var result = "";

            try
            {
                if (addressData != null)
                {
                    Lms_AddressPoco addressPoco = JsonConvert.DeserializeObject <Lms_AddressPoco>(JsonConvert.SerializeObject(addressData));

                    if (addressPoco.Id > 0 && addressPoco.AddressLine.Trim() != string.Empty)
                    {
                        var existingAddress = _addressLogic.GetSingleById(addressPoco.Id);
                        // it is required to pull existing data first,
                        // cause there are some data which do not come from UI

                        existingAddress.UnitNumber         = addressPoco.UnitNumber;
                        existingAddress.AddressLine        = addressPoco.AddressLine;
                        existingAddress.CityId             = addressPoco.CityId;
                        existingAddress.ProvinceId         = addressPoco.ProvinceId;
                        existingAddress.CountryId          = addressPoco.CountryId;
                        existingAddress.PostCode           = addressPoco.PostCode;
                        existingAddress.EmailAddress1      = addressPoco.EmailAddress1;
                        existingAddress.EmailAddress2      = addressPoco.EmailAddress2;
                        existingAddress.MobileNumber       = addressPoco.MobileNumber;
                        existingAddress.Fax                = addressPoco.Fax;
                        existingAddress.PrimaryPhoneNumber = addressPoco.PrimaryPhoneNumber;
                        existingAddress.ContactPersonName  = addressPoco.ContactPersonName;


                        var poco = _addressLogic.Update(existingAddress);
                        result = poco.Id.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(Json(result));
        }
        public string CreateNewCustomer(Lms_CustomerPoco customerPoco, Lms_AddressPoco billingAddressPoco, Lms_AddressPoco mailingAddressPoco, int branchId, bool isMailingBillingAddressSame = true)
        {
            if (JsonConvert.SerializeObject(billingAddressPoco) != JsonConvert.SerializeObject(mailingAddressPoco))
            {
                isMailingBillingAddressSame = false;
            }

            SqlParameter[] sqlParameters =
            {
                new SqlParameter("@CustomerName",                SqlDbType.VarChar, 50)
                {
                    Value = customerPoco.CustomerName
                },
                new SqlParameter("@BranchId",                    SqlDbType.Int)
                {
                    Value = branchId
                },
                new SqlParameter("@IsGstApplicable",             SqlDbType.Bit)
                {
                    Value = customerPoco.IsGstApplicable
                },
                new SqlParameter("@FuelSurChargePercentage",     SqlDbType.Decimal)
                {
                    Value = (object)customerPoco.FuelSurChargePercentage ?? DBNull.Value
                },
                new SqlParameter("@DiscountPercentage",          SqlDbType.Decimal)
                {
                    Value = (object)customerPoco.DiscountPercentage ?? DBNull.Value
                },
                new SqlParameter("@InvoiceDueDays",              SqlDbType.Int)
                {
                    Value = (object)customerPoco.InvoiceDueDays ?? DBNull.Value
                },
                new SqlParameter("@IsMailingBillingAddressSame", SqlDbType.Bit)
                {
                    Value = isMailingBillingAddressSame
                },
                new SqlParameter("@CreatedBy",                   SqlDbType.Int)
                {
                    Value = customerPoco.CreatedBy
                },

                new SqlParameter("@BillingUnitNo",               SqlDbType.VarChar, 50)
                {
                    Value = (object)billingAddressPoco.UnitNumber ?? DBNull.Value
                },
                new SqlParameter("@BillingAddressLine",          SqlDbType.VarChar, 150)
                {
                    Value = (object)billingAddressPoco.AddressLine ?? DBNull.Value
                },
                new SqlParameter("@BillingCityId",               SqlDbType.Int)
                {
                    Value = (object)billingAddressPoco.CityId ?? DBNull.Value
                },
                new SqlParameter("@BillingProvinceId",           SqlDbType.Int)
                {
                    Value = (object)billingAddressPoco.ProvinceId ?? DBNull.Value
                },
                new SqlParameter("@BillingCountryId",            SqlDbType.Int)
                {
                    Value = (object)billingAddressPoco.CountryId ?? DBNull.Value
                },
                new SqlParameter("@BillingPostCode",             SqlDbType.VarChar, 50)
                {
                    Value = (object)billingAddressPoco.PostCode ?? DBNull.Value
                },
                new SqlParameter("@BillingContactPersonName",    SqlDbType.VarChar, 200)
                {
                    Value = (object)billingAddressPoco.ContactPersonName ?? DBNull.Value
                },
                new SqlParameter("@BillingPhoneNumber",          SqlDbType.VarChar, 150)
                {
                    Value = (object)billingAddressPoco.PrimaryPhoneNumber ?? DBNull.Value
                },
                new SqlParameter("@BillingFaxNumber",            SqlDbType.VarChar, 150)
                {
                    Value = (object)billingAddressPoco.Fax ?? DBNull.Value
                },
                new SqlParameter("@BillingEmailAddress",         SqlDbType.VarChar, 200)
                {
                    Value = (object)billingAddressPoco.EmailAddress1 ?? DBNull.Value
                },

                new SqlParameter("@MailingUnitNo",               SqlDbType.VarChar, 50)
                {
                    Value = (object)mailingAddressPoco.UnitNumber ?? DBNull.Value
                },
                new SqlParameter("@MailingAddressLine",          SqlDbType.VarChar, 150)
                {
                    Value = (object)mailingAddressPoco.AddressLine ?? DBNull.Value
                },
                new SqlParameter("@MailingCityId",               SqlDbType.Int)
                {
                    Value = (object)mailingAddressPoco.CityId ?? DBNull.Value
                },
                new SqlParameter("@MailingProvinceId",           SqlDbType.Int)
                {
                    Value = (object)mailingAddressPoco.ProvinceId ?? DBNull.Value
                },
                new SqlParameter("@MailingCountryId",            SqlDbType.Int)
                {
                    Value = (object)mailingAddressPoco.CountryId ?? DBNull.Value
                },
                new SqlParameter("@MailingPostCode",             SqlDbType.VarChar, 50)
                {
                    Value = (object)mailingAddressPoco.PostCode ?? DBNull.Value
                },
                new SqlParameter("@MailingContactPersonName",    SqlDbType.VarChar, 200)
                {
                    Value = (object)mailingAddressPoco.ContactPersonName ?? DBNull.Value
                },
                new SqlParameter("@MailingPhoneNumber",          SqlDbType.VarChar, 150)
                {
                    Value = (object)mailingAddressPoco.PrimaryPhoneNumber ?? DBNull.Value
                },
                new SqlParameter("@MailingFaxNumber",            SqlDbType.VarChar, 150)
                {
                    Value = (object)mailingAddressPoco.Fax ?? DBNull.Value
                },
                new SqlParameter("@MailingEmailAddress",         SqlDbType.VarChar, 200)
                {
                    Value = (object)mailingAddressPoco.EmailAddress1 ?? DBNull.Value
                }
            };

            StringBuilder query = new StringBuilder();

            query.Append("EXEC CreateNewCustomer ");
            query.Append("@CustomerName, @BranchId, @IsGstApplicable, @FuelSurChargePercentage, @DiscountPercentage, @InvoiceDueDays, @MailingAddressId, ");
            query.Append("@BillingAddressId, @IsMailingBillingAddressSame, @CreatedBy, ");

            query.Append("@BillingUnitNo, @BillingAddressLine, @BillingCityId, @BillingProvinceId, @BillingCountryId, @BillingPostCode, ");
            query.Append("@BillingContactPersonName, @BillingPhoneNumber, @BillingFaxNumber, @BillingEmailAddress, ");

            query.Append("@MailingUnitNo, @MailingAddressLine, @MailingCityId, @MailingProvinceId, @MailingCountryId, @MailingPostCode, ");
            query.Append("@MailingContactPersonName, @MailingPhoneNumber, @MailingFaxNumber, @MailingEmailAddress ");

            var outPut = base.CallStoredProcedure(query.ToString(), sqlParameters);

            _cache.Remove(App_CacheKeys.Customers);
            _cache.Remove(App_CacheKeys.Addresses);
            _cache.Remove(App_CacheKeys.Accounts);

            return(outPut);
        }
Ejemplo n.º 5
0
        public IActionResult Add([FromBody] dynamic orderData)
        {
            ValidateSession();
            var result = "";

            try
            {
                if (orderData != null)
                {
                    using (var scope = new TransactionScope())
                    {
                        Lms_OrderPoco orderPoco = JsonConvert.DeserializeObject <Lms_OrderPoco>(JsonConvert.SerializeObject(orderData[0]));
                        _addressLogic = new Lms_AddressLogic(_cache, new EntityFrameworkGenericRepository <Lms_AddressPoco>(_dbContext));
                        var addressList = _addressLogic.GetList();

                        Lms_AddressPoco newAddress = new Lms_AddressPoco();

                        var orderAddressData    = (JObject)orderData[0];
                        var customerId          = orderAddressData.SelectToken("customerId").ToString();
                        var waybillNumber       = orderAddressData.SelectToken("wayBillNumber").ToString();
                        var customerAddressId   = orderAddressData.SelectToken("customerAddressId").ToString();
                        var customerAddressline = orderAddressData.SelectToken("customerAddressline").ToString();
                        var customerUnitNo      = orderAddressData.SelectToken("customerUnitNo").ToString();
                        var customerCityId      = orderAddressData.SelectToken("customerCityId").ToString();
                        var customerProvinceId  = orderAddressData.SelectToken("customerProvinceId").ToString();
                        var customerPostcode    = orderAddressData.SelectToken("customerPostcode").ToString();
                        var orderDate           = orderAddressData.SelectToken("orderDate").ToString();
                        orderPoco.ShipperAddressId  = customerAddressId == "" ? 0 : Convert.ToInt32(customerAddressId);
                        orderPoco.ShipperCustomerId = customerId == "" ? 0 : Convert.ToInt32(customerId);
                        orderPoco.IsInvoiced        = false;
                        orderPoco.CreateDate        = orderDate == "" ? DateTime.Today : Convert.ToDateTime(orderDate);
                        orderPoco.CreatedBy         = sessionData.UserId;
                        orderPoco.OrderTypeId       = 3; //3 for misc. order
                        var newWbNumber = _orderLogic.GetList().OrderByDescending(c => c.WayBillNumber).Take(1).FirstOrDefault().WayBillNumber;
                        if (!(newWbNumber.Length > 0))
                        {
                            newWbNumber = _configurationLogic.GetSingleById(1).DeliveryWBNoStartFrom;
                        }
                        else
                        {
                            newWbNumber = (Convert.ToInt16(newWbNumber) + 1).ToString();
                        }

                        orderPoco.WayBillNumber = newWbNumber;

                        var customerAddressInfo = addressList.Where(c => c.Id == orderPoco.ShipperAddressId).FirstOrDefault();
                        if (customerAddressInfo != null)
                        {
                            customerAddressInfo.UnitNumber = !string.IsNullOrEmpty(customerAddressInfo.UnitNumber) ? Convert.ToString(customerAddressInfo.UnitNumber).Trim().ToUpper() : "";
                            if (customerAddressline.Trim().ToUpper() == customerAddressInfo.AddressLine.Trim().ToUpper() && customerUnitNo.Trim().ToUpper() == customerAddressInfo.UnitNumber && Convert.ToInt16(customerCityId) == customerAddressInfo.CityId)
                            {
                                if (Convert.ToInt16(customerProvinceId) != customerAddressInfo.ProvinceId || customerPostcode != customerAddressInfo.PostCode)
                                {
                                    customerAddressInfo.ProvinceId = Convert.ToInt16(customerProvinceId);
                                    customerAddressInfo.PostCode   = customerPostcode;
                                    _addressLogic.Update(customerAddressInfo);
                                }
                            }
                            else
                            {
                                newAddress                 = new Lms_AddressPoco();
                                newAddress.AddressLine     = customerAddressline;
                                newAddress.UnitNumber      = customerUnitNo;
                                newAddress.CityId          = Convert.ToInt16(customerCityId);
                                newAddress.ProvinceId      = Convert.ToInt16(customerProvinceId);
                                newAddress.CountryId       = 41; // default Canada
                                newAddress.PostCode        = customerPostcode;
                                newAddress.CreatedBy       = sessionData.UserId;
                                orderPoco.ShipperAddressId = _addressLogic.Add(newAddress).Id;
                            }
                        }

                        List <Lms_OrderAdditionalServicePoco> orderAdditionalServices = JsonConvert.DeserializeObject <List <Lms_OrderAdditionalServicePoco> >(JsonConvert.SerializeObject(orderData[1]));
                        _orderAdditionalServiceLogic = new Lms_OrderAdditionalServiceLogic(_cache, new EntityFrameworkGenericRepository <Lms_OrderAdditionalServicePoco>(_dbContext));
                        if (orderPoco.Id < 1 && orderPoco.BillToCustomerId > 0)
                        {
                            var addedOrder = _orderLogic.Add(orderPoco);
                            foreach (var item in orderAdditionalServices)
                            {
                                item.OrderId = addedOrder.Id;
                                var existingRecord = _orderAdditionalServiceLogic.GetList().Where(c => c.OrderId == addedOrder.Id && c.AdditionalServiceId == item.AdditionalServiceId).FirstOrDefault();
                                if (existingRecord == null)
                                {
                                    _orderAdditionalServiceLogic.Add(item);
                                }
                                else
                                {
                                    _orderAdditionalServiceLogic.Remove(existingRecord);
                                    _orderAdditionalServiceLogic.Add(item);
                                }
                            }

                            result = addedOrder.WayBillNumber;
                        }

                        if (result != "")
                        {
                            scope.Complete();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(Json(result));
        }
Ejemplo n.º 6
0
        public IActionResult Update([FromBody] dynamic orderData)
        {
            ValidateSession();
            var result = "";

            try
            {
                if (orderData != null)
                {
                    using (var scope = new TransactionScope())
                    {
                        Lms_OrderPoco orderPoco = JsonConvert.DeserializeObject <Lms_OrderPoco>(JsonConvert.SerializeObject(orderData[0]));
                        _addressLogic = new Lms_AddressLogic(_cache, new EntityFrameworkGenericRepository <Lms_AddressPoco>(_dbContext));
                        var addressList = _addressLogic.GetList();

                        Lms_AddressPoco newAddress = new Lms_AddressPoco();

                        var orderAddressData    = (JObject)orderData[0];
                        var customerId          = orderAddressData.SelectToken("customerId").ToString();
                        var waybillNumber       = orderAddressData.SelectToken("wayBillNumber").ToString();
                        var customerAddressId   = orderAddressData.SelectToken("customerAddressId").ToString();
                        var customerAddressline = orderAddressData.SelectToken("customerAddressline").ToString();
                        var customerUnitNo      = orderAddressData.SelectToken("customerUnitNo").ToString();
                        var customerCityId      = orderAddressData.SelectToken("customerCityId").ToString();
                        var customerProvinceId  = orderAddressData.SelectToken("customerProvinceId").ToString();
                        var customerPostcode    = orderAddressData.SelectToken("customerPostcode").ToString();
                        var orderDate           = orderAddressData.SelectToken("orderDate").ToString();
                        orderPoco.ShipperAddressId  = customerAddressId == "" ? 0 : Convert.ToInt32(customerAddressId);
                        orderPoco.ShipperCustomerId = customerId == "" ? 0 : Convert.ToInt32(customerId);
                        orderPoco.CreateDate        = orderDate == "" ? DateTime.Today : Convert.ToDateTime(orderDate);

                        var customerAddressInfo = addressList.Where(c => c.Id == orderPoco.ShipperAddressId).FirstOrDefault();
                        if (customerAddressInfo != null)
                        {
                            customerAddressInfo.UnitNumber = !string.IsNullOrEmpty(customerAddressInfo.UnitNumber) ? Convert.ToString(customerAddressInfo.UnitNumber).Trim().ToUpper() : "";
                            if (customerAddressline.Trim().ToUpper() == customerAddressInfo.AddressLine.Trim().ToUpper() && customerUnitNo.Trim().ToUpper() == customerAddressInfo.UnitNumber && Convert.ToInt16(customerCityId) == customerAddressInfo.CityId)
                            {
                                if (Convert.ToInt16(customerProvinceId) != customerAddressInfo.ProvinceId || customerPostcode != customerAddressInfo.PostCode)
                                {
                                    customerAddressInfo.ProvinceId = Convert.ToInt16(customerProvinceId);
                                    customerAddressInfo.PostCode   = customerPostcode;
                                    _addressLogic.Update(customerAddressInfo);
                                }
                            }
                            else
                            {
                                newAddress                 = new Lms_AddressPoco();
                                newAddress.AddressLine     = customerAddressline;
                                newAddress.UnitNumber      = customerUnitNo;
                                newAddress.CityId          = Convert.ToInt16(customerCityId);
                                newAddress.ProvinceId      = Convert.ToInt16(customerProvinceId);
                                newAddress.CountryId       = 41; // default Canada, UI have to change to accept country
                                newAddress.PostCode        = customerPostcode;
                                newAddress.CreatedBy       = sessionData.UserId;
                                orderPoco.ShipperAddressId = _addressLogic.Add(newAddress).Id;
                            }
                        }

                        List <Lms_OrderAdditionalServicePoco> orderAdditionalServices = JsonConvert.DeserializeObject <List <Lms_OrderAdditionalServicePoco> >(JsonConvert.SerializeObject(orderData[1]));
                        _orderAdditionalServiceLogic = new Lms_OrderAdditionalServiceLogic(_cache, new EntityFrameworkGenericRepository <Lms_OrderAdditionalServicePoco>(_dbContext));
                        if (orderPoco.Id > 0 && orderPoco.BillToCustomerId > 0)
                        {
                            var existingOrder = _orderLogic.GetSingleById(orderPoco.Id);
                            if (existingOrder != null)
                            {
                                existingOrder.CommentsForInvoice = orderPoco.CommentsForInvoice;
                                existingOrder.IsPrintedOnInvoice = orderPoco.IsPrintedOnInvoice;
                                existingOrder.IsPrintedOnWayBill = orderPoco.IsPrintedOnWayBill;
                                existingOrder.CommentsForWayBill = orderPoco.CommentsForWayBill;

                                existingOrder.OrderBasicCost             = orderPoco.OrderBasicCost;
                                existingOrder.DiscountPercentOnOrderCost = orderPoco.DiscountPercentOnOrderCost;
                                existingOrder.ApplicableGstPercent       = orderPoco.ApplicableGstPercent;
                                existingOrder.TotalOrderCost             = orderPoco.TotalOrderCost;
                                existingOrder.TotalAdditionalServiceCost = orderPoco.TotalAdditionalServiceCost;

                                existingOrder.BillToCustomerId          = orderPoco.BillToCustomerId;
                                existingOrder.ReferenceNumber           = orderPoco.ReferenceNumber;
                                existingOrder.AwbCtnNumber              = orderPoco.AwbCtnNumber;
                                existingOrder.CargoCtlNumber            = orderPoco.CargoCtlNumber;
                                existingOrder.CreateDate                = orderPoco.CreateDate;
                                existingOrder.OrderedBy                 = orderPoco.OrderedBy;
                                existingOrder.ContactPhoneNumber        = orderPoco.ContactPhoneNumber;
                                existingOrder.DepartmentName            = orderPoco.DepartmentName;
                                existingOrder.ShipperAddressId          = orderPoco.ShipperAddressId;
                                existingOrder.ShipperCustomerId         = orderPoco.ShipperCustomerId;
                                existingOrder.ServiceProviderEmployeeId = orderPoco.ServiceProviderEmployeeId;

                                existingOrder.UnitTypeId    = orderPoco.UnitTypeId;
                                existingOrder.UnitQuantity  = orderPoco.UnitQuantity;
                                existingOrder.SkidQuantity  = orderPoco.SkidQuantity;
                                existingOrder.TotalPiece    = orderPoco.TotalPiece;
                                existingOrder.WeightScaleId = orderPoco.WeightScaleId;
                                existingOrder.WeightTotal   = orderPoco.WeightTotal;
                                existingOrder.CityId        = orderPoco.CityId;

                                var orderServices = _orderAdditionalServiceLogic.GetList().Where(c => c.OrderId == existingOrder.Id).ToList();
                                if (orderServices.Count > 0)
                                {
                                    foreach (var item in orderServices)
                                    {
                                        _orderAdditionalServiceLogic.Remove(item);
                                    }
                                }

                                if (orderAdditionalServices.Count > 0)
                                {
                                    foreach (var item in orderAdditionalServices)
                                    {
                                        if (item.AdditionalServiceId > 0)
                                        {
                                            item.OrderId = existingOrder.Id;
                                            _orderAdditionalServiceLogic.Add(item);
                                        }
                                    }
                                }

                                var updatedOrder = _orderLogic.Update(existingOrder);
                                result = updatedOrder.WayBillNumber;
                            }
                        }

                        if (result != "")
                        {
                            scope.Complete();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(Json(result));
        }
        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));
        }