private SaleProduct[] ConstructProducts(List<ProductImport> entities)
        {
           var temp = new List<SaleProduct>();
           var defaultSupplier = ObjectFactory.GetInstance<ISupplierRepository>().GetAll(true).FirstOrDefault(p => p.Name != null && p.Name.Equals("default"));
          if (defaultSupplier == null)
            {
                defaultSupplier = new Supplier(Guid.NewGuid()) { Name = "default", Code = "default", Description = "default" };
                try
                {
                    ObjectFactory.GetInstance<ISupplierRepository>().Save(defaultSupplier);

                }
                catch
                {
                }
            }

            foreach (var entity in entities)
            {
               var product = ObjectFactory.GetInstance<IProductRepository>().GetByCode(entity.ProductCode) as SaleProduct;
                    
                bool isNew = false;
                if(product==null)
                {
                    isNew = true;
                    product= new SaleProduct(Guid.NewGuid());
                }

                var brand = ObjectFactory.GetInstance<IProductBrandRepository>().GetAll(true)
                        .FirstOrDefault(
                            p => p.Code != null && (p.Code.Equals(entity.BrandCode, StringComparison.CurrentCultureIgnoreCase)) ||
                            p.Name != null && p.Name.Equals(entity.BrandCode, StringComparison.CurrentCultureIgnoreCase));

                if (string.IsNullOrEmpty(entity.BrandCode))
                    brand = ObjectFactory.GetInstance<IProductBrandRepository>().GetAll(true).FirstOrDefault(p => p.Code != null && (p.Code.Equals("default", StringComparison.CurrentCultureIgnoreCase) || p.Name != null && p.Name.Equals("default", StringComparison.CurrentCultureIgnoreCase)));


                if (brand == null)
                {
                    brand = ObjectFactory.GetInstance<IProductBrandRepository>().GetAll(true).FirstOrDefault(p => p.Code != null && p.Code == "default") ??
                            new ProductBrand(Guid.NewGuid())
                            {
                                Code = string.IsNullOrEmpty(entity.BrandCode) ? "default" : entity.BrandCode,
                                Name = string.IsNullOrEmpty(entity.BrandCode) ? "default" : entity.BrandCode,
                                Supplier = defaultSupplier
                            };
                    try
                    {
                        ObjectFactory.GetInstance<IProductBrandRepository>().Save(brand);
                    }
                    catch
                    {
                    }
                }

                product.Brand = brand;

                if (product.ProductType == null)
                {
                    var productType = ObjectFactory.GetInstance<IProductTypeRepository>().GetAll(true).FirstOrDefault(
                     p => p.Code != null && p.Code.Equals("default", StringComparison.CurrentCultureIgnoreCase) ||
                         p.Name != null &&
                     p.Name.Equals("default", StringComparison.CurrentCultureIgnoreCase));

                    if (productType == null)
                    {
                        productType = new ProductType(Guid.NewGuid())
                        {
                            Name = "default",
                            Code = "default",
                            Description = "default"
                        };
                        try
                        {
                            ObjectFactory.GetInstance<IProductTypeRepository>().Save(productType);
                        }
                        catch
                        {
                        }
                    }
                    product.ProductType = productType;
                }



                if (product.PackagingType == null)
                {
                    var packagingType = ObjectFactory.GetInstance<IProductPackagingTypeRepository>().GetAll(true).FirstOrDefault(
                        p => p.Code != null &&
                        p.Code.Equals("default", StringComparison.CurrentCultureIgnoreCase) ||
                        p.Name != null &&
                        p.Name.Equals("default", StringComparison.CurrentCultureIgnoreCase));
                    if (packagingType == null)
                    {
                        packagingType = new ProductPackagingType(Guid.NewGuid())
                        {
                            Name = "default",
                            Code =
                                "default",
                            Description = "default"
                        };
                        try
                        {
                            ObjectFactory.GetInstance<IProductPackagingTypeRepository>().Save(packagingType);
                        }
                        catch
                        {
                        }

                    }
                    product.PackagingType = packagingType;
                }


                if (product.Packaging == null)
                {
                    var packaging =
                        ObjectFactory.GetInstance<IProductPackagingRepository>().GetAll(true).FirstOrDefault(
                            p => p.Code != null && p.Code.Equals("default", StringComparison.CurrentCultureIgnoreCase) ||
                                p.Name != null &&
                                 p.Name.Equals("default", StringComparison.CurrentCultureIgnoreCase));
                    if (packaging == null)
                    {
                        packaging = new ProductPackaging(Guid.NewGuid())
                        {
                            Name = "default",
                            Code =
                                "default",
                            Description =
                                "default",
                        };
                        try
                        {
                            ObjectFactory.GetInstance<IProductPackagingRepository>().Save(packaging);
                        }
                        catch
                        {

                        }
                    }
                    product.Packaging = packaging;
                }

                if (product.VATClass == null)
                {
                    var productVat = ObjectFactory.GetInstance<IVATClassRepository>().GetAll(true).FirstOrDefault(
                            p => p.VatClass != null &&
                            p.VatClass == "defaultVAT" ||
                            p.Name != null &&
                            p.Name.Equals("defaultVAT", StringComparison.CurrentCultureIgnoreCase));

                    if (productVat == null)
                    {
                        var viatItem = new VATClass.VATClassItem(Guid.NewGuid())
                        {
                            EffectiveDate = DateTime.Now,
                            Rate = 0,

                        };
                        productVat = new VATClass(Guid.NewGuid())
                        {
                            Name = "defaultVAT",
                            VatClass = "defaultVAT",
                        };
                        productVat.VATClassItems.Add(viatItem);
                        try
                        {
                            ObjectFactory.GetInstance<IVATClassRepository>().Save(productVat);
                        }
                        catch
                        {
                        }

                    }
                    product.VATClass = productVat;
                }
                if (product.Flavour == null)
                {
                    var flavor = ObjectFactory.GetInstance<IProductFlavourRepository>().GetAll(true).FirstOrDefault(
                            p => p.Name != null &&
                            p.Name == "defaultflavor" || p.Code
                            != null &&
                            p.Code.Equals("defaultflavor", StringComparison.CurrentCultureIgnoreCase));

                    if (flavor == null)
                    {
                        flavor = new ProductFlavour(Guid.NewGuid())
                        {
                            Name = "defaultflavor",
                            Code = "defaultflavor",
                            Description = "defaultflavor",
                            ProductBrand = brand
                        };
                        try
                        {
                            ObjectFactory.GetInstance<IProductFlavourRepository>().Save(flavor);
                        }
                        catch
                        {
                        }

                    }
                    product.Flavour = flavor;
                }

                product.ProductCode = entity.ProductCode;
                product._Status = EntityStatus.Active; 
                product.ExFactoryPrice = entity.ExFactoryPrice;
                product.Description = entity.Description.Trim();

                if (isNew || HasProductChanged(product))
                    temp.Add(product);
            }
            return temp.ToArray();

        }
Beispiel #2
0
        public static VATClass.VATClassItem MapItem(this tblVATClassItem vci)
        {
            var item = new VATClass.VATClassItem(vci.id)
            {
                Rate = vci.Rate,
                EffectiveDate = vci.EffectiveDate,

            };
            item._SetDateCreated(vci.IM_DateCreated);
            item._SetDateLastUpdated(vci.IM_DateLastUpdated);
            item._SetStatus((EntityStatus)vci.IM_Status);
            return item;
        }
        private VATClass  Map(VATClassImport vatClassImport)
        {
            var exists = Queryable.FirstOrDefault(_context.tblVATClass, p => p.Name == vatClassImport.Name);


            Guid id = exists != null ? exists.id : Guid.NewGuid();

            var vatClass = new VATClass(id);
            var vatClassItemsList = new List<VATClass.VATClassItem>();


            vatClass.Name = vatClassImport.Code;
            vatClass.VatClass = vatClassImport.Name;


            var classItemExists = Queryable.FirstOrDefault(_context.tblVATClassItem, p => p.VATClassID == id && p.Rate==vatClassImport.CurrentRate);


            Guid classItemId = classItemExists != null ? classItemExists.id : Guid.NewGuid();

            var vatClassItem = new VATClass.VATClassItem(classItemId)
            {
                
                EffectiveDate = vatClassImport.CurrentEffectiveDate,
                Rate = vatClassImport.CurrentRate,
               //Math.Round(Convert.ToDecimal((vatClassImport.CurrentRate / 100)), 4)
            };

            vatClassItemsList.Add(vatClassItem);
            vatClass.AddVatClassItems(vatClassItemsList);

            return vatClass;
        }
       private Outlet[] ContsructEntities(IEnumerable<OutletImport> entities)
        {
           
                var temp = new List<Outlet>();
                var defaultDistributr =ObjectFactory.GetInstance<ICostCentreRepository>()
                    .GetByCode("PZ Cussons EA", CostCentreType.Distributor);
                   
                var allRoutes = ObjectFactory.GetInstance<IRouteRepository>().GetAll().ToList();
                var outletCategories = ObjectFactory.GetInstance<IOutletCategoryRepository>().GetAll().ToList();
                foreach (var importentity in entities)
                {
                    var domainEntity =
                        ObjectFactory.GetInstance<ICostCentreRepository>().GetByCode(importentity.OutletCode,
                                                                                     CostCentreType.Outlet, true) as
                        Outlet;

                    bool IsNew = false;
                    if(domainEntity==null)
                    {
                        domainEntity = new Outlet(Guid.NewGuid());
                        IsNew = true;
                    }
                    #region Routes

                    if (domainEntity != null && domainEntity.Route == null)
                    {
                        domainEntity.Route = allRoutes.FirstOrDefault(
                           p =>
                           p.Name != null &&
                           p.Name.ToLower().Trim() == importentity.RouteName.Trim().ToLower());

                    }
                   
                    if (domainEntity.Route != null && !string.IsNullOrEmpty(importentity.RouteName))
                    {
                       var newRoute = allRoutes.FirstOrDefault(p => p.Name.Trim().ToLower() == importentity.RouteName.Trim().ToLower());
                        if (newRoute != null)
                            domainEntity.Route = newRoute;
                    }
                   
                    if (domainEntity.Route == null && importentity.RouteName.StartsWith("SALES VA"))
                        domainEntity.Route =
                            allRoutes.FirstOrDefault(p => p.Name.Contains("SALES VA"));

                    if (domainEntity.Route == null && !string.IsNullOrEmpty(importentity.RouteName))
                    {
                        var region = ObjectFactory.GetInstance<IRegionRepository>().GetAll(true).FirstOrDefault(p => p.Name == "Region A");
                        if (region == null)
                        {
                            region = new Region(Guid.NewGuid())
                            {
                                Country = ObjectFactory.GetInstance<ICountryRepository>().GetAll(true).FirstOrDefault(p => p.Name == "Kenya"),
                                Name = "",
                                Description = ""
                            };
                            try
                            {
                                ObjectFactory.GetInstance<IRegionRepository>().Save(region);
                            }
                            catch
                            {

                            }

                        }
                        var route = new Route(Guid.NewGuid())
                        {
                            Name = importentity.RouteName,
                            Code = importentity.RouteName,
                            Region = region
                        };
                        try
                        {
                            ObjectFactory.GetInstance<IRouteRepository>().Save(route);
                        }
                        catch
                        {

                        }
                        domainEntity.Route = route;
                    }
                   
                    try
                    {
                        ObjectFactory.GetInstance<IRouteRepository>().Save(domainEntity.Route);
                    }
                    catch
                    {
                    } 
                    
                    #endregion

                    if (defaultDistributr == null) throw new ArgumentNullException("distributor");
                    domainEntity.ParentCostCentre = new CostCentreRef() { Id = defaultDistributr.Id };
                   

                    if (domainEntity.OutletCategory == null)
                    {

                        OutletCategory category = outletCategories
                            .FirstOrDefault(
                                s => s.Name != null &&
                                     s.Name == "defaultoutletcategory");
                        if (category == null)
                        {
                            category = new OutletCategory(Guid.NewGuid())
                            {
                                Name =
                                    string.IsNullOrEmpty(importentity.Name)
                                        ? "defaultoutletcategory"
                                        : importentity.Name,
                                Code =
                                    string.IsNullOrEmpty(importentity.Name)
                                        ? "defaultoutletcategory"
                                        : importentity.Name
                            };
                            try
                            {
                                ObjectFactory.GetInstance<IOutletCategoryRepository>().Save(category,true);
                            }
                            catch
                            {
                            }
                        }
                        domainEntity.OutletCategory = category;
                    }
                    
                    if (domainEntity.OutletType == null)
                    {
                        OutletType type =ObjectFactory.GetInstance<IOutletTypeRepository>().GetAll(true).FirstOrDefault(
                                p =>
                                p.Name != null &&
                                p.Name == "defaultoutlettype");

                        if (type == null)
                        {
                            type = new OutletType(Guid.NewGuid())
                            {
                                Name =
                                    string.IsNullOrEmpty(importentity.Name)
                                        ? "defaultoutlettype"
                                        : importentity.Name,
                                Code =
                                    string.IsNullOrEmpty(importentity.Name)
                                        ? "defaultoutlettype"
                                        : importentity.Name
                            };
                            try
                            {
                                ObjectFactory.GetInstance<IOutletTypeRepository>().Save(type);
                            }
                            catch
                            {
                            }
                        }
                        domainEntity.OutletType = type;
                    }
                   
                    if (domainEntity.OutletProductPricingTier == null)
                    {
                        ProductPricingTier tire =ObjectFactory.GetInstance<IProductPricingTierRepository>()
                            .GetAll(true).FirstOrDefault(
                                p => p.Code != null && p.Code.Trim() == importentity.Currency.Trim());

                        if (tire == null)
                        {
                            tire = new ProductPricingTier(Guid.NewGuid())
                            {
                                Name = importentity.Currency.Trim(),
                                Code = importentity.Currency.Trim(),
                                Description = importentity.Currency.Trim(),
                            };
                            try
                            {
                                ObjectFactory.GetInstance<IProductPricingTierRepository>().Save(tire);
                            }
                            catch
                            {
                            }
                        }

                        domainEntity.OutletProductPricingTier = tire;
                    }
                    if (domainEntity.VatClass == null)
                    {
                        VATClass productVat = ObjectFactory.GetInstance<IVATClassRepository>().GetAll(true).
                            FirstOrDefault(
                                p => p.Name != null &&
                                     p.Name == "defaultVAT");
                        if (productVat == null)
                        {
                            var viatItem = new VATClass.VATClassItem(Guid.NewGuid())
                            {
                                EffectiveDate = DateTime.Now,
                                Rate = 0,
                            };
                            productVat = new VATClass(Guid.NewGuid())
                            {
                                Name = "defaultVAT",
                                VatClass = "defaultVAT",
                            };
                            productVat.VATClassItems.Add(viatItem);
                            try
                            {
                                ObjectFactory.GetInstance<IVATClassRepository>().Save(productVat);
                            }
                            catch
                            {
                            }
                        }
                        domainEntity.VatClass = productVat;
                    }
                    domainEntity._Status =EntityStatus.Active; //GetStatus(importentity.Status);
                    domainEntity.Name = importentity.Name.Trim();
                    domainEntity.CostCentreCode = importentity.OutletCode.Trim();
                        temp.Add(domainEntity);

                }
                return temp.ToArray();
           
        }
       private SaleProduct[] ConstructProducts(IEnumerable<ProductImport> entities)
       {
           var temp = new List<SaleProduct>();

           var defaultSupplier = ObjectFactory.GetInstance<ISupplierRepository>().GetAll(true).FirstOrDefault(p => p.Name != null && p.Name.Equals("default"));
           if (defaultSupplier == null)
           {
               defaultSupplier = new Supplier(Guid.NewGuid()) { Name = "default", Code = "default", Description = "default" };
               try
               {
                   ObjectFactory.GetInstance<ISupplierRepository>().Save(defaultSupplier);

               }
               catch
               {
               }
           }
          var validEntities = entities.Where(p => p.Description != null && !p.Description.ToLower().Contains("discontinued"));
          foreach (var entity in validEntities)
          {
              var product =
                  ObjectFactory.GetInstance<IProductRepository>().GetByCode(entity.ProductCode, true) as SaleProduct;
              bool isNew = false;
              if(product==null)
              {
                  product = new SaleProduct(Guid.NewGuid());
                  isNew = true;
              }
                             
               #region brand
               var allBrands = ObjectFactory.GetInstance<IProductBrandRepository>().GetAll(true).ToList();
               var brand = allBrands
                       .FirstOrDefault(
                           p =>
                           p.Code != null && p.Code.Equals(entity.BrandCode, StringComparison.CurrentCultureIgnoreCase) ||
                           p.Name != null && p.Name.Equals(entity.BrandCode, StringComparison.CurrentCultureIgnoreCase)
                           );

               if (string.IsNullOrEmpty(entity.BrandCode))
                   brand = allBrands.FirstOrDefault(p => p.Code != null && p.Code == "default");

                   if (brand == null)
                   {
                       brand = allBrands.FirstOrDefault(p => p.Code != null && p.Code == "default") ??
                               new ProductBrand(Guid.NewGuid())
                                   {
                                       Code = string.IsNullOrEmpty(entity.BrandCode) ? "default" : entity.BrandCode,
                                       Name = string.IsNullOrEmpty(entity.BrandCode) ? "default" : entity.BrandCode,
                                       Supplier = defaultSupplier
                                   };
                       try
                       {
                           ObjectFactory.GetInstance<IProductBrandRepository>().Save(brand);
                       }
                       catch
                       {
                       }
                   }
               
               product.Brand = brand;
               #endregion

               #region Product Type

               var productType = ObjectFactory.GetInstance<IProductTypeRepository>().GetAll(true).FirstOrDefault(
                       p => p.Code != null &&
                       p.Code.Equals(entity.ProductTypeCode, StringComparison.CurrentCultureIgnoreCase) ||
                       p.Code!= null &&p.Code.Equals("default", StringComparison.CurrentCultureIgnoreCase) ||
                      p.Name != null &&
                       p.Name.Equals("default", StringComparison.CurrentCultureIgnoreCase));
               if (productType == null)
               {
                   productType = new ProductType(Guid.NewGuid())
                                     {
                                         Name = string.IsNullOrEmpty(entity.ProductTypeCode)
                                                    ? "default"
                                                    : entity.ProductTypeCode,
                                         Code = string.IsNullOrEmpty(entity.ProductTypeCode)
                                                    ? "default"
                                                    : entity.ProductTypeCode,
                                         Description = string.IsNullOrEmpty(entity.ProductTypeCode)
                                                           ? "default"
                                                           : entity.ProductTypeCode
                                     };
                   try
                   {
                      ObjectFactory.GetInstance<IProductTypeRepository>().Save(productType);
                   }catch
                   {
                   }
               }
               product.ProductType = productType;
               #endregion

               #region Packaging type
               var allPackagingTypes = ObjectFactory.GetInstance<IProductPackagingTypeRepository>().GetAll(true).ToList();
               ProductPackagingType packagingType = null;

               if (string.IsNullOrEmpty(entity.PackagingTypeCode))
                   packagingType = allPackagingTypes.FirstOrDefault(p => p.Name != null && p.Name == "default");
               else
               {
                   packagingType = allPackagingTypes.FirstOrDefault(
                   p =>
                   p.Code != null && p.Code.Equals(entity.PackagingTypeCode, StringComparison.CurrentCultureIgnoreCase));
                   
               }
               if(packagingType==null)
               {
                   packagingType =allPackagingTypes.FirstOrDefault(p => p.Name != null && p.Name == "default")?? new ProductPackagingType(Guid.NewGuid())
                                       {
                                           Name =
                                               string.IsNullOrEmpty(entity.PackagingTypeCode)
                                                   ? "default"
                                                   : entity.PackagingTypeCode,
                                           Code =
                                               string.IsNullOrEmpty(entity.PackagingTypeCode)
                                                   ? "default"
                                                   : entity.PackagingTypeCode,
                                           Description =
                                               string.IsNullOrEmpty(entity.PackagingTypeCode)
                                                   ? "default"
                                                   : entity.PackagingTypeCode
                                       };
                   try
                   {
                       ObjectFactory.GetInstance<IProductPackagingTypeRepository>().Save(packagingType);
                   }
                   catch
                   {
                   }
               }
               product.PackagingType = packagingType;

               #endregion

               #region Packaging
               var packagings = ObjectFactory.GetInstance<IProductPackagingRepository>().GetAll(true).ToList();

               ProductPackaging packaging;
               if(string.IsNullOrEmpty(entity.PackagingCode))
                   packaging =
                       packagings.FirstOrDefault(
                           p => p.Code != null && p.Code.Equals("default", StringComparison.CurrentCultureIgnoreCase));
               else
               {
                   packaging =
                       packagings.FirstOrDefault(
                           p =>
                           p.Code != null &&
                           p.Code.Equals(entity.PackagingCode, StringComparison.CurrentCultureIgnoreCase));
               }
               if(packaging==null)
               {
                   packaging = packagings.FirstOrDefault(
                       p => p.Code != null && p.Code.Equals("default", StringComparison.CurrentCultureIgnoreCase)) ??
                               new ProductPackaging(Guid.NewGuid())
                                   {
                                       Name =
                                           string.IsNullOrEmpty(entity.PackagingCode) ? "default" : entity.PackagingCode,
                                       Code =
                                           string.IsNullOrEmpty(entity.PackagingCode) ? "default" : entity.PackagingCode,
                                       Description =
                                           string.IsNullOrEmpty(entity.PackagingCode) ? "default" : entity.PackagingCode
                                   };
                   try
                   {
                       ObjectFactory.GetInstance<IProductPackagingRepository>().Save(packaging);
                   }catch
                   {
                       
                   }
               }
               product.Packaging = packaging;
               #endregion

               #region VAT
               VATClass productVat;

               var productVats = ObjectFactory.GetInstance<IVATClassRepository>().GetAll(true).ToList();
               if(string.IsNullOrEmpty(entity.VATClass))
                  productVat= productVats.FirstOrDefault(p => p.VatClass != null  && p.VatClass.Equals("defaultVAT", StringComparison.CurrentCultureIgnoreCase));
               else
               {
                   productVat =
                       productVats.FirstOrDefault(
                           p =>
                           p.VatClass != null &&
                           p.VatClass.Equals(entity.VATClass, StringComparison.CurrentCultureIgnoreCase) ||
                           p.Name != null && p.Name.Equals(entity.VATClass, StringComparison.CurrentCultureIgnoreCase));
               }

               if(productVat==null)
               {
                   var viatItem = new VATClass.VATClassItem(Guid.NewGuid())
                                      {
                                          EffectiveDate = DateTime.Now,
                                          Rate = 0,

                                      };

                   productVat =
                       productVats.FirstOrDefault(
                           p =>
                           p.VatClass != null &&
                           p.VatClass.Equals("defaultVAT", StringComparison.CurrentCultureIgnoreCase)) ??
                       new VATClass(Guid.NewGuid())
                           {
                               Name = string.IsNullOrEmpty(entity.VATClass) ? "defaultVAT" : entity.VATClass,
                               VatClass = string.IsNullOrEmpty(entity.VATClass) ? "defaultVAT" : entity.VATClass,
                           };
                   productVat.VATClassItems.Add(viatItem);
                   try
                   {
                       ObjectFactory.GetInstance<IVATClassRepository>().Save(productVat);
                   }catch
                   {
                   }
               }
               product.VATClass = productVat;
               #endregion
              
               #region Flavour
               var allFlavors = ObjectFactory.GetInstance<IProductFlavourRepository>().GetAll(true).ToList();
               ProductFlavour flavor;
               if (string.IsNullOrEmpty(entity.ProductFlavourCode))
                   flavor =
                       allFlavors.FirstOrDefault(
                           p =>
                           p.Code != null && p.Code.Equals("defaultflavor", StringComparison.CurrentCultureIgnoreCase));
               else
               {
                   flavor=allFlavors.FirstOrDefault(p => p.Code != null && p.Code == entity.ProductFlavourCode);
               }
               if(flavor==null)
               {
                   flavor =
                       allFlavors.FirstOrDefault(
                           p =>
                           p.Code != null && p.Code.Equals("defaultflavor", StringComparison.CurrentCultureIgnoreCase)) ??
                       new ProductFlavour(Guid.NewGuid())
                           {
                               Name =
                                   string.IsNullOrEmpty(entity.ProductFlavourCode)
                                       ? "defaultflavor"
                                       : entity.ProductFlavourCode,
                               Code =
                                   string.IsNullOrEmpty(entity.ProductFlavourCode)
                                       ? "defaultflavor"
                                       : entity.ProductFlavourCode,
                               Description =
                                   string.IsNullOrEmpty(entity.ProductFlavourCode)
                                       ? "defaultflavor"
                                       : entity.ProductFlavourCode,
                               ProductBrand = product.Brand
                           };
                   try
                   {
                       ObjectFactory.GetInstance<IProductFlavourRepository>().Save(flavor);
                   }
                   catch (Exception ex)
                   {
                       FileUtility.LogError(ex.Message);
                   }
               }
               product.Flavour = flavor;
               #endregion

               #region Discount Group
               if(!string.IsNullOrEmpty(entity.DiscountGroup))
               {
                   DiscountGroup discountGroup = ObjectFactory.GetInstance<IGroupDiscountMapper>().FindByCode(entity.DiscountGroup);
                      
                   if(discountGroup==null)
                   {
                       discountGroup = new DiscountGroup(Guid.NewGuid())
                       {
                           Code = entity.DiscountGroup.Trim(),
                           Name = entity.DiscountGroup.Trim(),
                           _Status = EntityStatus.Active
                       };
                       try
                       {
                           ObjectFactory.GetInstance<IGroupDiscountMapper>().Insert(discountGroup);
                       }catch(Exception ex)
                       {
                           FileUtility.LogError(ex.Message);
                       }
                   }

               }
              
               #endregion
              
               product.ProductCode = entity.ProductCode;
               product.ExFactoryPrice = entity.ExFactoryPrice;
               product.Description = entity.Description;
              UpdateDefaultTierPricing(product.Id, product.ExFactoryPrice, product.ExFactoryPrice);

               if (isNew || HasProductChanged(product))
                   temp.Add(product);
           }
           return temp.ToArray();
       }
       private async Task<IEnumerable<Product>> ConstructEntities(IEnumerable<ProductImport> entities)
       {
           return await Task.Run(() =>
                                     {
                                        
               var temp = new List<SaleProduct>();
               var exisitingsproducts = _productRepository.GetAll(true).OfType<SaleProduct>().ToList();
               var defaultSupplier =
                   _supplierRepository.GetAll(true).FirstOrDefault(p => p.Name != null && p.Name.Equals("default"));
               var allBrands = _productBrandRepository.GetAll(true).ToList();
               if (defaultSupplier == null)
               {
                   defaultSupplier = new Supplier(Guid.NewGuid())
                                         {Name = "default", Code = "default", Description = "default"};
                   try
                   {
                       _supplierRepository.Save(defaultSupplier);

                   }
                   catch
                   {
                   }
               }
               
               foreach (var entity in entities)
               {
                   
                   var product = exisitingsproducts.FirstOrDefault(p =>  p.ProductCode !=null && p.ProductCode.Equals(entity.ProductCode,StringComparison.CurrentCultureIgnoreCase
                       )) ?? new SaleProduct(Guid.NewGuid());

                   
                   var brand = allBrands
                           .FirstOrDefault(
                               p =>p.Code != null && (p.Code.Equals(entity.BrandCode, StringComparison.CurrentCultureIgnoreCase)) ||
                               p.Name != null && p.Name.Equals(entity.BrandCode, StringComparison.CurrentCultureIgnoreCase));

                   if (string.IsNullOrEmpty(entity.BrandCode))
                       brand = allBrands.FirstOrDefault(p => p.Code != null && (p.Code.Equals("default", StringComparison.CurrentCultureIgnoreCase) || p.Name != null && p.Name.Equals("default", StringComparison.CurrentCultureIgnoreCase)));
                               
                              
                   if (brand == null)
                   {
                       brand = allBrands.FirstOrDefault(p => p.Code != null && p.Code == "default") ??
                               new ProductBrand(Guid.NewGuid())
                                   {
                                       Code = string.IsNullOrEmpty(entity.BrandCode) ? "default" : entity.BrandCode,
                                       Name = string.IsNullOrEmpty(entity.BrandCode) ? "default" : entity.BrandCode,
                                       Supplier = defaultSupplier
                                   };
                       try
                       {
                           _productBrandRepository.Save(brand);
                       }
                       catch
                       {
                       }
                   }

                   product.Brand = brand;

                   if(product.ProductType==null)
                   {
                      var productType= _productTypeRepository.GetAll(true).FirstOrDefault(
                       p => p.Code != null && p.Code.Equals("default", StringComparison.CurrentCultureIgnoreCase) ||
                           p.Name != null &&
                       p.Name.Equals("default", StringComparison.CurrentCultureIgnoreCase));

                       if (productType == null)
                       {
                           productType = new ProductType(Guid.NewGuid())
                           {
                               Name = "default",
                               Code = "default",
                               Description = "default"
                           };
                           try
                           {
                               _productTypeRepository.Save(productType);
                           }
                           catch
                           {
                           }
                       }
                       product.ProductType = productType;
                   }
                   
                  

                   if (product.PackagingType == null)
                   {
                       var packagingType = _productPackagingTypeRepository.GetAll(true).FirstOrDefault(
                           p => p.Code != null &&
                           p.Code.Equals("default", StringComparison.CurrentCultureIgnoreCase) ||
                           p.Name != null &&
                           p.Name.Equals("default", StringComparison.CurrentCultureIgnoreCase));
                       if (packagingType == null)
                       {
                          packagingType= new ProductPackagingType(Guid.NewGuid())
                           {
                               Name = "default",
                               Code =
                                   "default",
                               Description = "default"
                           };
                           try
                           {
                               _productPackagingTypeRepository.Save(packagingType);
                           }
                           catch
                           {
                           }
                           
                       }
                       product.PackagingType = packagingType;
                   }


                   if (product.Packaging == null)
                   {
                       var packaging =
                           _productPackagingRepository.GetAll(true).FirstOrDefault(
                               p => p.Code != null && p.Code.Equals("default", StringComparison.CurrentCultureIgnoreCase) ||
                                   p.Name != null &&
                                    p.Name.Equals("default", StringComparison.CurrentCultureIgnoreCase));
                       if (packaging == null)
                       {
                           packaging = new ProductPackaging(Guid.NewGuid())
                                           {
                                               Name = "default",
                                               Code =
                                                   "default",
                                               Description =
                                                   "default",
                                           };
                           try
                           {
                               _productPackagingRepository.Save(packaging);
                           }
                           catch
                           {

                           }
                       }
                       product.Packaging = packaging;
                   }

                   if (product.VATClass == null)
                   {
                       var productVat =
                           _vatClassRepository.GetAll(true).FirstOrDefault(
                               p => p.VatClass != null &&
                               p.VatClass == "defaultVAT" ||
                               p.Name != null &&
                               p.Name.Equals("defaultVAT", StringComparison.CurrentCultureIgnoreCase));

                       if (productVat == null)
                       {
                           var viatItem = new VATClass.VATClassItem(Guid.NewGuid())
                                              {
                                                  EffectiveDate = DateTime.Now,
                                                  Rate = 0,

                                              };
                           productVat = new VATClass(Guid.NewGuid())
                                            {
                                                Name = "defaultVAT",
                                                VatClass = "defaultVAT",
                                            };
                           productVat.VATClassItems.Add(viatItem);
                           try
                           {
                               _vatClassRepository.Save(productVat);
                           }
                           catch
                           {
                           }

                       }
                       product.VATClass = productVat;
                   }
                   if(product.Flavour==null)
                   {
                       var flavor =
                           _flavourRepository.GetAll(true).FirstOrDefault(
                               p =>p.Name !=null &&
                               p.Name == "defaultflavor" ||p.Code
                               !=null &&
                               p.Code.Equals("defaultflavor", StringComparison.CurrentCultureIgnoreCase));

                       if (flavor == null)
                       {
                           flavor = new ProductFlavour(Guid.NewGuid())
                                        {
                                            Name = "defaultflavor",
                                            Code = "defaultflavor",
                                            Description = "defaultflavor",
                                            ProductBrand = brand
                                        };
                           try
                           {
                               _flavourRepository.Save(flavor);
                           }
                           catch
                           {
                           }

                       }
                       product.Flavour = flavor;
                   }
                   
                   product.ProductCode = entity.ProductCode;
                   product._Status =EntityStatus.New; //GeneralHelper.GetStatus(entity.Status);
                   product.ExFactoryPrice = entity.ExFactoryPrice;
                   product.Description = entity.Description;

                   

                   temp.Add(product);
               }
               return temp.OfType<Product>().ToList();
           });

       }