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;
        }
        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 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;
        }