private bool AddUpdateCategorySubCategoryMapRepo(SubCategoryViewModel viewModel)
        {
            using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
            {
                using (DbContextTransaction transaction = dbContext.Database.BeginTransaction())
                {

                    DbSet<SubCategory> subCategorySet = dbContext.SubCategorySet;
                    SubCategory subCategory = new SubCategory();
                    ObjectMapper.PropertyMap(viewModel, subCategory);

                    FindResult<CategoryViewModel> categoryResult = categoryRepository.GetAll(dbContext);

                    FindResult<CategorySubCategoryMapViewModel> mapResult = categorySubCategoryMapRepo.FindBy(x => x.SubCategoryId == viewModel.SubCategoryId);

                    List<CategorySubCategoryMapViewModel> existingMappings = new List<CategorySubCategoryMapViewModel>();

                    existingMappings = mapResult.Entities;

                    List<Guid> deletionList = new List<Guid>();

                    List<Guid> newAdded = new List<Guid>();

                    foreach (var emapvm in existingMappings)
                    {
                        if (!viewModel.CategoryIds.Contains(emapvm.CategoryId))
                        {
                            deletionList.Add(emapvm.CategorySubCategoryMapId);
                        }
                    }

                    if (viewModel.CategoryIds != null && viewModel.CategoryIds.Count > 0)
                    {

                        foreach (var cId in viewModel.CategoryIds)
                        {
                            CategorySubCategoryMapViewModel existingMap = existingMappings.FirstOrDefault(x => x.SubCategoryId == viewModel.SubCategoryId && x.CategoryId == cId);

                            if (existingMap == null)
                            {
                                newAdded.Add(cId);
                            }
                        }

                    }

                    if (deletionList.Count > 0)
                    {
                        foreach (Guid mapid in deletionList)
                        {
                            categorySubCategoryMapRepo.Delete(mapid);
                        }
                    }

                    if (newAdded.Count > 0)
                    {
                        foreach (Guid categoryId in newAdded)
                        {
                            CategorySubCategoryMapViewModel catSubCatMapVM = new CategorySubCategoryMapViewModel
                            {

                                CategorySubCategoryMapId = Guid.Empty,
                                CategoryId = categoryId,
                                SubCategoryId = viewModel.SubCategoryId,

                            };

                            categorySubCategoryMapRepo.Update(dbContext, catSubCatMapVM);
                        }
                    }

                    subCategorySet.Attach(subCategory);

                    dbContext.Entry<SubCategory>(subCategory).State = EntityState.Modified;

                    dbContext.SaveChanges();

                    transaction.Commit();
                }
            }

            return true;
        }
        public UpdateResult<PurchaseOrderViewModel> SavePurchaseOrder(PurchaseOrderViewModel purchaseOrder)
        {
            UpdateResult<PurchaseOrderViewModel> updateResult = new UpdateResult<PurchaseOrderViewModel>();

            bool isNewOrder = purchaseOrder.PurchaseOrderId == Guid.Empty ? true : false;

            using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
            {

                using (DbContextTransaction tran = dbContext.Database.BeginTransaction())
                {

                    try
                    {
                        updateResult = purchaseOrderRepository.Update(dbContext, purchaseOrder);

                        if (!updateResult.Success || updateResult.Entity == null)
                        {
                            return updateResult;
                        }

                        Dictionary<Guid, PurchaseOrderItemViewModel> existingPurchaseOrderItems = new Dictionary<Guid, PurchaseOrderItemViewModel>();

                        List<Guid> soItemIds = new List<Guid>();

                        if (!isNewOrder)
                        {
                            FindResult<PurchaseOrderItemViewModel> existingSOItemsResult = purchaseOrderItemRepository.FindBy(dbContext, x => x.PurchaseOrderId == purchaseOrder.PurchaseOrderId);

                            if (existingSOItemsResult.Success)
                            {
                                existingPurchaseOrderItems = existingSOItemsResult.Entities.ToDictionary(x => x.PurchaseOrderItemId, y => y);

                                soItemIds = existingPurchaseOrderItems.Keys.ToList();
                            }

                        }

                        purchaseOrder.PurchaseOrderId = updateResult.Entity.PurchaseOrderId;

                        foreach (PurchaseOrderItemViewModel sItem in purchaseOrder.PurchaseOrderItems)
                        {

                            sItem.PurchaseOrderId = purchaseOrder.PurchaseOrderId;

                            decimal quantityToUpdate = sItem.Quantity;

                            if (!soItemIds.Contains(sItem.PurchaseOrderItemId))
                            {

                                sItem.PurchaseOrderItemId = Guid.Empty;

                            }
                            else
                            {

                                quantityToUpdate -= existingPurchaseOrderItems[sItem.PurchaseOrderItemId].Quantity;
                                existingPurchaseOrderItems.Remove(sItem.PurchaseOrderItemId);

                            }

                            UpdateResult<PurchaseOrderItemViewModel> soItemUpdResult = purchaseOrderItemRepository.Update(dbContext, sItem);

                            if (soItemUpdResult.Success)
                            {
                                sItem.PurchaseOrderItemId = soItemUpdResult.Entity.PurchaseOrderId;

                                ProductVariant pv = dbContext.ProductVariantSet.Where(x => x.ProductVariantId == sItem.ProductVariantId).FirstOrDefault();

                                if (pv != null)
                                {
                                    pv.TransactionQuantity += sItem.Quantity;
                                    pv.LastPurchasePrice = sItem.Price;

                                    dbContext.ProductVariantSet.Attach(pv);
                                    dbContext.Entry(pv).State = EntityState.Modified;
                                    dbContext.SaveChanges();
                                }

                            }

                        }//For Each sItem in purchaseOrder.PurchaseOrderItems

                        foreach (Guid key in existingPurchaseOrderItems.Keys)
                        {
                            purchaseOrderItemRepository.Delete(dbContext, key);

                            PurchaseOrderItemViewModel pvItem = existingPurchaseOrderItems[key];

                            ProductVariant pv = dbContext.ProductVariantSet.Where(x => x.ProductVariantId == pvItem.ProductVariantId).FirstOrDefault();

                            if(pv != null)
                            {
                                pv.TransactionQuantity -= pvItem.Quantity;

                                dbContext.ProductVariantSet.Attach(pv);
                                dbContext.Entry(pv).State = EntityState.Modified;
                                dbContext.SaveChanges();
                            }

                        }

                        tran.Commit();
                        updateResult.Success = true;

                    }//Try
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        updateResult.Success = false;
                        updateResult.ErrorMessage = ex.Message;

                    }

                }//Using Transaction

            }// Using DBContext

            return updateResult;
        }
        public UpdateResult<ExternalActivity> UpdateExternalActivityDetails(ExternalActivityViewModel externalActivity)
        {
            UpdateResult<ExternalActivity> updateResult = new UpdateResult<ExternalActivity>() { Success = false };

            using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
            {
                DbSet<ExternalActivity> externalActivitySet = dbContext.ExternalActivitySet;

                ExternalActivity existingActivity = externalActivitySet.Where(x => x.StoreProviderType == (int)externalActivity.StoreProviderType
                                                                && x.ExternalActivityType == (int)externalActivity.ExternalActivityType).FirstOrDefault();

                if (existingActivity != null)
                {
                    Guid existingId = existingActivity.ActivityId;

                    existingActivity.ActivityStatus = (int)ExternalActivityStatus.Completed;
                    existingActivity.ExternalActivityType = (int)externalActivity.ExternalActivityType;
                    existingActivity.LastPerformedOn = externalActivity.LastPerformedOn;
                    existingActivity.StoreProviderType = (int)externalActivity.StoreProviderType;
                    externalActivity.UnshippedOrdersCount = externalActivity.UnshippedOrdersCount;

                    externalActivitySet.Attach(existingActivity);

                    dbContext.Entry(existingActivity).State = EntityState.Modified;

                }
                else
                {

                    existingActivity = existingActivity = new ExternalActivity()
                    {
                        ActivityId = Guid.NewGuid(),
                        ActivityStatus = (int)externalActivity.ActivityStatus,
                        ExternalActivityType = (int)externalActivity.ExternalActivityType,
                        LastPerformedOn = externalActivity.LastPerformedOn,
                        StoreProviderType = (int)externalActivity.StoreProviderType,
                    };

                    externalActivitySet.Add(existingActivity);
                }

                dbContext.SaveChanges();
                updateResult.Entity = existingActivity;

            }

            return updateResult;
        }
        public ExternalOrderShippedResult SetExternalOrderShipped(ApiOrderShipmentRequest orderShipmentRequest)
        {
            ExternalOrderShippedResult shippedResult = new ExternalOrderShippedResult();

            shippedResult = new ExternalOrderClient().SetOrdersShipped(orderShipmentRequest);

            try
            {
                if (shippedResult != null && shippedResult.ShippingResults.Count > 0)
                {
                    using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
                    {
                        DbSet<ExternalOrder> externalOrderSet = dbContext.ExternalOrderSet;

                        foreach (ShippingResult shippingResult in shippedResult.ShippingResults)
                        {

                            ExternalOrder shippedOrder = externalOrderSet.Where(x => x.StoreOrderHeaderId == shippingResult.ExternalOrderHeaderId).FirstOrDefault();

                            if (shippedOrder != null)
                            {

                                if (shippingResult.Success)
                                {
                                    try
                                    {
                                        shippedOrder.ShippedDate = shippingResult.ShippedDate.Value;
                                        shippedOrder.OrderStatus = (int)StoreOrderStatusEnum.Shipped;

                                        shippedOrder.StoreOrderStatus = StoreOrderStatusEnum.Shipped.ToString();

                                        externalOrderSet.Attach(shippedOrder);

                                        dbContext.Entry(shippedOrder).State = EntityState.Modified;

                                        dbContext.SaveChanges();
                                    }
                                    catch(DbEntityValidationException dbEx)
                                    {
                                        string errorMessage = string.Join("; ", dbEx.EntityValidationErrors);
                                        shippedResult.ErrorMessage = shippedResult.ErrorMessage + "; " + errorMessage;
                                    }
                                }

                            }

                        }

                    }

                    shippedResult.Success = true;
                }
            }

            catch (Exception ex)
            {

                shippedResult.ErrorMessage = shippedResult.ErrorMessage + "; " +ex.Message;
                shippedResult.Success = false;
            }

            return shippedResult;
        }
        public EntityOperationResultBase SaveExternalOrder(List<ExternalOrder> orders)
        {
            EntityOperationResultBase updateResult = new EntityOperationResultBase() { Success = false };
            try
            {
                using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
                {
                    DbSet<ExternalOrder> externalOrderSet = dbContext.ExternalOrderSet;

                    foreach (var order in orders)
                    {

                        ExternalOrder existingOrder = externalOrderSet.Where(x => x.StoreProviderOrderId == order.StoreProviderOrderId).FirstOrDefault();

                        if (existingOrder == null)
                        {
                            order.OrderStatus = (int)StoreOrderStatusEnum.Unshipped;
                            order.StoreOrderHeaderId = Guid.NewGuid();
                            order.ShippedDate = null;
                            order.JsonShippingAddress = JsonConvert.SerializeObject(order.ShippingAddress);
                            dbContext.ExternalOrderSet.Add(order);

                            foreach (var lineItem in order.ExternalOrderLineItems)
                            {
                                lineItem.ExternalOrderLineItemId = Guid.NewGuid();
                                lineItem.StoreOrderId = order.StoreOrderHeaderId;
                                dbContext.ExternalOrderLineItemSet.Add(lineItem);

                            }

                            dbContext.SaveChanges();
                        }
                    }

                    updateResult.Success = true;
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                var errorMessages = dbEx.EntityValidationErrors
                        .SelectMany(x => x.ValidationErrors)
                        .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(" The validation errors are: ", fullErrorMessage);
                updateResult.Success = false;
                updateResult.ErrorMessage = exceptionMessage;
            }
            catch (Exception ex)
            {
                string ErrorMessage = string.Format("An error {0} occurred while saving changes to database",
                                   ex.Message);
                updateResult.Success = false;
                updateResult.ErrorMessage = ErrorMessage;
            }

            return updateResult;
        }
Esempio n. 6
0
        public UpdateResult<ProductViewModel> SaveProduct(ProductViewModel productViewModel)
        {
            UpdateResult<ProductViewModel> result = new UpdateResult<ProductViewModel>() { Success = false };

            Guid categoryId = productViewModel.CategorySubCategoryMap.CategoryId;
            Guid subCategoryId = productViewModel.CategorySubCategoryMap.SubCategoryId;

            using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
            {
                try
                {

                    UpdateResult<ProductViewModel> updateResult = productRepository.Update(dbContext, productViewModel);

                    if (!updateResult.Success)
                    {
                        return updateResult;
                    }

                    foreach (ProductVariantViewModel prvm in productViewModel.ProductVariants)
                    {

                        prvm.ProductId = updateResult.Entity.ProductId;

                        ProductVariant pv = dbContext.ProductVariantSet.Where(x => x.ProductVariantId == prvm.ProductVariantId).FirstOrDefault();

                        if (pv == null)
                        {
                            prvm.ProductVariantId = Guid.Empty;
                        }

                        UpdateResult<ProductVariantViewModel> pvUpdateResult = productVariantRepository.Update(prvm);

                        if (!pvUpdateResult.Success)
                        {
                            result.Success = false;
                            result.ErrorMessage = pvUpdateResult.ErrorMessage;
                            return result;
                        }

                        List<ProductVariantAttributeValue> productVariantAttributeValueMaps = dbContext.ProductVariantAttributeValueSet
                                            .Where(x => x.ProductVariantId == prvm.ProductVariantId).ToList();

                        List<Guid> attributeSelectValueIds = new List<Guid>();

                        foreach (var item in prvm.ProductVariantAttributeValues)
                        {
                            if (item.ProductAttributeId != Guid.Empty)
                            {

                                if ((!item.AttributeValueListId.HasValue
                                     || item.AttributeValueListId.Value == Guid.Empty)
                                     && !string.IsNullOrEmpty(item.ProductAttributeValue))
                                {
                                    SubCategoryAttributeMap scAttrMap = dbContext.SubCategoryAttributeMapSet
                                        .Where(x => x.ProductAttributeId == item.ProductAttributeId && x.SubCategoryId == subCategoryId).FirstOrDefault();

                                    AttributeValueList avl = new AttributeValueList()
                                    {

                                        AttributeValueListId = Guid.NewGuid(),
                                        AttributeValue = item.ProductAttributeValue,
                                        CategoryId = categoryId,
                                        SubCategoryAttributeMapID = scAttrMap.SubCategoryAttributeMapId,
                                    };

                                    dbContext.AttributeValueListSet.Add(avl);

                                    item.AttributeValueListId = avl.AttributeValueListId;

                                    ProductVariantAttributeValue newPVAttrValue = new ProductVariantAttributeValue()
                                    {
                                        ProductVariantId = prvm.ProductVariantId,
                                        AttributeValueListId = item.AttributeValueListId.Value,
                                    };

                                    dbContext.ProductVariantAttributeValueSet.Add(newPVAttrValue);
                                    dbContext.SaveChanges();

                                }
                                else if (item.AttributeValueListId.HasValue && item.AttributeValueListId != Guid.Empty && !string.IsNullOrEmpty(item.ProductAttributeValue))
                                {
                                    ProductVariantAttributeValue pvAttrValue = productVariantAttributeValueMaps
                                                        .Where(x => x.AttributeValueListId == item.AttributeValueListId.Value)
                                                        .FirstOrDefault();

                                    if (pvAttrValue != null)
                                    {
                                        productVariantAttributeValueMaps.Remove(pvAttrValue);
                                    }
                                    else
                                    {
                                        ProductVariantAttributeValue newPVAttrValue = new ProductVariantAttributeValue()
                                        {
                                            ProductVariantId = prvm.ProductVariantId,
                                            AttributeValueListId = item.AttributeValueListId.Value,
                                        };

                                        dbContext.ProductVariantAttributeValueSet.Add(newPVAttrValue);
                                        dbContext.SaveChanges();

                                    }
                                }
                            }

                        }

                        if (productVariantAttributeValueMaps != null && productVariantAttributeValueMaps.Count > 0)
                        {
                            foreach (var pvavm in productVariantAttributeValueMaps)
                            {
                                dbContext.ProductVariantAttributeValueSet.Remove(pvavm);

                            }

                            dbContext.SaveChanges();
                        }
                    }

                    result.Success = true;
                }
                catch (Exception ex)
                {
                    result.Success = false;
                    result.ErrorMessage = ex.Message;
                }
            }

            return result;
        }
        public UpdateResult<ProductViewModel> SaveProduct(ProductViewModel productViewModel)
        {
            UpdateResult<ProductViewModel> result = new UpdateResult<ProductViewModel>() { Success = false };

            Guid categoryId = productViewModel.CategorySubCategoryMap.CategoryId;
            Guid subCategoryId = productViewModel.CategorySubCategoryMap.SubCategoryId;

            using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
            {
                try
                {
                    string categoryCode = categoryRepository.GetCategoryCode(categoryId);

                    string subCategoryCode = subCategoryRepository.GetSubCategoryCode(subCategoryId);

                    string productName = Utility.GetNormalizedString(productViewModel.ProductName);

                    string firstTwo = productName.Substring(0, 2).ToUpper();

                    //string strSeed = DateTime.Now.ToString("MMddyyHHmm");

                    //int numberSeed = 0;

                    //if(!int.TryParse(strSeed, out numberSeed))
                    //{
                    //    numberSeed = int.MaxValue;
                    //}

                    //int seed = int.Parse(strSeed);

                    UpdateResult<ProductViewModel> updateResult = productRepository.Update(dbContext, productViewModel);

                    if (!updateResult.Success)
                    {
                        return updateResult;
                    }

                    foreach (ProductVariantViewModel prvm in productViewModel.ProductVariants)
                    {

                        prvm.ProductId = updateResult.Entity.ProductId;

                        ProductVariant pv = dbContext.ProductVariantSet.Where(x => x.ProductVariantId == prvm.ProductVariantId).FirstOrDefault();

                        if (pv == null)
                        {
                            prvm.ProductVariantId = Guid.Empty;
                            Random random = new Random();

                            string randomNumber = random.Next(999999).ToString().PadLeft(10, "0"[0]);

                            string skuCode = string.Format("ST-{0}{1}{2}{3}", categoryCode, subCategoryCode, firstTwo, randomNumber);

                            prvm.SKUCode = skuCode;
                        }

                        UpdateResult<ProductVariantViewModel> pvUpdateResult = productVariantRepository.Update(prvm);

                        if (!pvUpdateResult.Success)
                        {
                            result.Success = false;
                            result.ErrorMessage = pvUpdateResult.ErrorMessage;
                            return result;
                        }

                        List<ProductVariantAttributeValue> productVariantAttributeValueMaps = dbContext.ProductVariantAttributeValueSet
                                            .Where(x => x.ProductVariantId == prvm.ProductVariantId).ToList();

                        List<Guid> attributeSelectValueIds = new List<Guid>();

                        foreach (var item in prvm.ProductVariantAttributeValues)
                        {
                            if (item.ProductAttributeId != Guid.Empty)
                            {

                                if ((!item.AttributeValueListId.HasValue
                                     || item.AttributeValueListId.Value == Guid.Empty)
                                     && !string.IsNullOrEmpty(item.ProductAttributeValue))
                                {
                                    SubCategoryAttributeMap scAttrMap = dbContext.SubCategoryAttributeMapSet
                                        .Where(x => x.ProductAttributeId == item.ProductAttributeId && x.SubCategoryId == subCategoryId).FirstOrDefault();

                                    AttributeValueList avl = new AttributeValueList()
                                    {

                                        AttributeValueListId = Guid.NewGuid(),
                                        AttributeValue = item.ProductAttributeValue,
                                        CategoryId = categoryId,
                                        SubCategoryAttributeMapID = scAttrMap.SubCategoryAttributeMapId,
                                    };

                                    dbContext.AttributeValueListSet.Add(avl);

                                    item.AttributeValueListId = avl.AttributeValueListId;

                                    ProductVariantAttributeValue newPVAttrValue = new ProductVariantAttributeValue()
                                    {
                                        ProductVariantId = prvm.ProductVariantId,
                                        AttributeValueListId = item.AttributeValueListId.Value,
                                    };

                                    dbContext.ProductVariantAttributeValueSet.Add(newPVAttrValue);
                                    dbContext.SaveChanges();

                                }
                                else if (item.AttributeValueListId.HasValue && item.AttributeValueListId != Guid.Empty && !string.IsNullOrEmpty(item.ProductAttributeValue))
                                {
                                    ProductVariantAttributeValue pvAttrValue = productVariantAttributeValueMaps
                                                        .Where(x => x.AttributeValueListId == item.AttributeValueListId.Value)
                                                        .FirstOrDefault();

                                    if (pvAttrValue != null)
                                    {
                                        productVariantAttributeValueMaps.Remove(pvAttrValue);
                                    }
                                    else
                                    {
                                        ProductVariantAttributeValue newPVAttrValue = new ProductVariantAttributeValue()
                                        {
                                            ProductVariantId = prvm.ProductVariantId,
                                            AttributeValueListId = item.AttributeValueListId.Value,
                                        };

                                        dbContext.ProductVariantAttributeValueSet.Add(newPVAttrValue);
                                        dbContext.SaveChanges();

                                    }
                                }
                            }

                        }

                        if (productVariantAttributeValueMaps != null && productVariantAttributeValueMaps.Count > 0)
                        {
                            foreach (var pvavm in productVariantAttributeValueMaps)
                            {
                                dbContext.ProductVariantAttributeValueSet.Remove(pvavm);

                            }

                            dbContext.SaveChanges();
                        }
                    }

                    result.Success = true;
                }
                catch (Exception ex)
                {
                    result.Success = false;
                    result.ErrorMessage = ex.Message;
                }
            }

            return result;
        }