public async Task <OperationResult> ImportCustomers(CustomerImportModel customersAsJson)
        {
            try
            {
                {
                    var rawJson        = JToken.Parse(customersAsJson.ImportList);
                    var rawHeadersJson = JToken.Parse(customersAsJson.Headers);

                    var headers         = rawHeadersJson;
                    var customerObjects = rawJson.Skip(1);

                    var core = await _coreHelper.GetCore();

                    var customerSettings = _options.Value;

                    var entityGroup = await core.EntityGroupRead(customerSettings.RelatedEntityGroupId.ToString());

                    foreach (var customerObj in customerObjects)
                    {
                        var customerNoExists    = int.TryParse(headers[5]["headerValue"].ToString(), out var customerNoColumn);
                        var companyNameExists   = int.TryParse(headers[3]["headerValue"].ToString(), out var companyNameColumn);
                        var contactPersonExists = int.TryParse(headers[4]["headerValue"].ToString(), out var contactPersonColumn);
                        if (customerNoExists ||
                            companyNameExists ||
                            contactPersonExists
                            )
                        {
                            var existingCustomer = FindCustomer(customerNoExists, customerNoColumn,
                                                                companyNameExists, companyNameColumn, contactPersonExists,
                                                                contactPersonColumn, headers, customerObj);
                            if (existingCustomer == null)
                            {
                                var customerModel =
                                    CustomersHelper.ComposeValues(new CustomerFullModel(), headers, customerObj);

                                var newCustomer = new Customer
                                {
                                    CityName              = customerModel.CityName,
                                    CompanyAddress        = customerModel.CompanyAddress,
                                    CompanyAddress2       = customerModel.CompanyAddress2,
                                    CompanyName           = customerModel.CompanyName,
                                    ContactPerson         = customerModel.ContactPerson,
                                    CreatedBy             = customerModel.CreatedBy,
                                    CustomerNo            = customerModel.CustomerNo,
                                    Description           = customerModel.Description,
                                    Email                 = customerModel.Email,
                                    Phone                 = customerModel.Phone,
                                    ZipCode               = customerModel.ZipCode,
                                    RelatedEntityId       = customerModel.RelatedEntityId,
                                    EanCode               = customerModel.EanCode,
                                    VatNumber             = customerModel.VatNumber,
                                    CountryCode           = customerModel.CountryCode,
                                    CreatedDate           = DateTime.Now,
                                    CrmId                 = customerModel.CrmId,
                                    CadastralNumber       = customerModel.CadastralNumber,
                                    PropertyNumber        = customerModel.PropertyNumber,
                                    ApartmentNumber       = customerModel.ApartmentNumber,
                                    CompletionYear        = customerModel.CompletionYear,
                                    FloorsWithLivingSpace = customerModel.FloorsWithLivingSpace
                                };

                                await newCustomer.Create(_dbContext);

                                if (customerSettings?.RelatedEntityGroupId != null)
                                {
                                    if (entityGroup == null)
                                    {
                                        return(new OperationResult(false, "Entity group not found"));
                                    }

                                    var nextItemUid = entityGroup.EntityGroupItemLst.Count;
                                    var label       = newCustomer.CompanyName;
                                    label += string.IsNullOrEmpty(newCustomer.CompanyAddress) ? "" : " - " + newCustomer.CompanyAddress;
                                    label += string.IsNullOrEmpty(newCustomer.ZipCode) ? "" : " - " + newCustomer.ZipCode;
                                    label += string.IsNullOrEmpty(newCustomer.CityName) ? "" : " - " + newCustomer.CityName;
                                    label += string.IsNullOrEmpty(newCustomer.Phone) ? "" : " - " + newCustomer.Phone;
                                    label += string.IsNullOrEmpty(newCustomer.ContactPerson) ? "" : " - " + newCustomer.ContactPerson;
                                    if (label.Count(f => f == '-') == 1 && label.Contains("..."))
                                    {
                                        label = label.Replace(" - ", "");
                                    }

                                    if (string.IsNullOrEmpty(label))
                                    {
                                        label = $"Empty company {nextItemUid}";
                                    }
                                    var item = await core.EntitySearchItemCreate(entityGroup.Id, $"{label}", $"{customerModel.Description}",
                                                                                 nextItemUid.ToString());

                                    if (item != null)
                                    {
                                        entityGroup = await core.EntityGroupRead(customerSettings.RelatedEntityGroupId.ToString());

                                        if (entityGroup != null)
                                        {
                                            foreach (var entityItem in entityGroup.EntityGroupItemLst)
                                            {
                                                if (entityItem.MicrotingUUID == item.MicrotingUUID)
                                                {
                                                    newCustomer.RelatedEntityId = entityItem.Id;
                                                    await newCustomer.Update(_dbContext);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                var customerModel =
                                    CustomersHelper.ComposeValues(new CustomerFullModel(), headers, customerObj);

                                existingCustomer.Description           = customerModel.Description;
                                existingCustomer.CustomerNo            = customerModel.CustomerNo;
                                existingCustomer.Email                 = customerModel.Email;
                                existingCustomer.Phone                 = customerModel.Phone;
                                existingCustomer.ZipCode               = customerModel.ZipCode;
                                existingCustomer.CityName              = customerModel.CityName;
                                existingCustomer.CompanyName           = customerModel.CompanyName;
                                existingCustomer.CompanyAddress        = customerModel.CompanyAddress;
                                existingCustomer.CompanyAddress2       = customerModel.CompanyAddress2;
                                existingCustomer.ContactPerson         = customerModel.ContactPerson;
                                existingCustomer.CountryCode           = customerModel.CountryCode;
                                existingCustomer.CrmId                 = customerModel.CrmId;
                                existingCustomer.CadastralNumber       = customerModel.CadastralNumber;
                                existingCustomer.PropertyNumber        = customerModel.PropertyNumber;
                                existingCustomer.ApartmentNumber       = customerModel.ApartmentNumber;
                                existingCustomer.CompletionYear        = customerModel.CompletionYear;
                                existingCustomer.FloorsWithLivingSpace = customerModel.FloorsWithLivingSpace;
//                                existingCustomer.Update(_dbContext);

                                if (existingCustomer.WorkflowState == Constants.WorkflowStates.Removed)
                                {
//                                    Customer customer = new Customer
//                                    {
//                                        Id = existingCustomer.Id,
//                                        Description = existingCustomer.Description,
//                                        Email = existingCustomer.Email,
//                                        Phone = existingCustomer.Phone,
//                                        ZipCode = existingCustomer.ZipCode,
//                                        CityName = existingCustomer.CityName,
//                                        CreatedBy = existingCustomer.CreatedBy,
//                                        CustomerNo = existingCustomer.CustomerNo,
//                                        CompanyName = existingCustomer.CompanyName,
//                                        CreatedDate = existingCustomer.CreatedDate,
//                                        ContactPerson = existingCustomer.ContactPerson,
//                                        CompanyAddress = existingCustomer.CompanyAddress,
//                                        CountryCode = existingCustomer.CountryCode
//                                    };
                                    existingCustomer.WorkflowState = Constants.WorkflowStates.Created;

                                    var nextItemUid = entityGroup.EntityGroupItemLst.Count;
                                    var label       = existingCustomer.CompanyName;
                                    label += string.IsNullOrEmpty(existingCustomer.CompanyAddress) ? "" : " - " + existingCustomer.CompanyAddress;
                                    label += string.IsNullOrEmpty(existingCustomer.ZipCode) ? "" : " - " + existingCustomer.ZipCode;
                                    label += string.IsNullOrEmpty(existingCustomer.CityName) ? "" : " - " + existingCustomer.CityName;
                                    label += string.IsNullOrEmpty(existingCustomer.Phone) ? "" : " - " + existingCustomer.Phone;
                                    label += string.IsNullOrEmpty(existingCustomer.ContactPerson) ? "" : " - " + existingCustomer.ContactPerson;
                                    if (label.Count(f => f == '-') == 1 && label.Contains("..."))
                                    {
                                        label = label.Replace(" - ", "");
                                    }

                                    if (string.IsNullOrEmpty(label))
                                    {
                                        label = $"Empty company {nextItemUid}";
                                    }
                                    var item = await core.EntitySearchItemCreate(entityGroup.Id, $"{label}", $"{existingCustomer.Description}",
                                                                                 nextItemUid.ToString());

                                    if (item != null)
                                    {
                                        entityGroup = await core.EntityGroupRead(customerSettings.RelatedEntityGroupId.ToString());

                                        if (entityGroup != null)
                                        {
                                            foreach (var entityItem in entityGroup.EntityGroupItemLst)
                                            {
                                                if (entityItem.MicrotingUUID == item.MicrotingUUID)
                                                {
                                                    existingCustomer.RelatedEntityId = entityItem.Id;
                                                }
                                            }
                                        }
                                    }
                                }
                                await existingCustomer.Update(_dbContext);
                            }
                        }
                    }
                }

                return(new OperationResult(true,
                                           _customersLocalizationService.GetString("CustomerCreated")));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationResult(false,
                                           _customersLocalizationService.GetString("ErrorWhileCreatingCustomer")));
            }
        }
 public async Task <OperationResult> ImportCustomer([FromBody] CustomerImportModel customerImportModel)
 {
     return(await _customersService.ImportCustomers(customerImportModel));
 }