public CallResult UpdateQuantity(ItemDTO item)
        {
            using (var db = _dbFactory.GetRWDb())
            {
                var itemView = db.Items.GetAllViewAsDto().FirstOrDefault(i => i.SKU == item.SKU);
                if (itemView == null)
                {
                    return(new CallResult()
                    {
                        Status = CallStatus.Fail,
                        Message = "Unable to find item: " + item.SKU
                    });
                }
                if (!itemView.StyleItemId.HasValue)
                {
                    return(new CallResult()
                    {
                        Status = CallStatus.Fail,
                        Message = "Item hasn't linked StyleItemId: " + item.SKU
                    });
                }

                var dbStyleItem = db.StyleItems.GetAll().FirstOrDefault(si => si.Id == itemView.StyleItemId);
                if (dbStyleItem == null)
                {
                    return(new CallResult()
                    {
                        Status = CallStatus.Fail,
                        Message = "Unable to find styleItem: " + dbStyleItem.Id
                    });
                }

                if (dbStyleItem.Quantity != item.RealQuantity)
                {
                    _log.Info("Quantity changed, SKU=" + item.SKU + ", " + dbStyleItem.Quantity + "=>" + item.RealQuantity);
                }
                dbStyleItem.Quantity        = item.RealQuantity;
                dbStyleItem.QuantitySetDate = _time.GetAppNowTime();
                dbStyleItem.QuantitySetBy   = null;

                db.Commit();

                SystemActionHelper.RequestQuantityDistribution(db, _actionService, dbStyleItem.StyleId, null);
            }

            return(new CallResult()
            {
                Status = CallStatus.Success
            });
        }
        public static void SetFinishStatus(IDbFactory dbFactory,
                                           ISystemActionService actionService,
                                           long pickListId,
                                           bool isFinished,
                                           DateTime when,
                                           long?by)
        {
            if (isFinished)
            {
                List <ScanItemDTO> pickListEntries;
                FBAPickList        dbPickList;
                using (var db = dbFactory.GetRWDb())
                {
                    pickListEntries = (from pi in db.FBAPickListEntries.GetAll()
                                       join l in db.Listings.GetAll() on pi.ListingId equals l.Id
                                       join i in db.Items.GetAll() on l.ItemId equals i.Id
                                       where pi.FBAPickListId == pickListId
                                       select new ScanItemDTO()
                    {
                        Quantity = pi.Quantity,
                        Barcode = i.Barcode,
                        StyleId = i.StyleId,
                    }).ToList();

                    dbPickList        = db.FBAPickLists.GetAll().FirstOrDefault(pi => pi.Id == pickListId);
                    dbPickList.Status = ShipmentStatuses.Finished;
                    db.Commit();
                }

                using (var invDb = dbFactory.GetInventoryRWDb())
                {
                    var scanOrder = new ScanOrderDTO()
                    {
                        Description = dbPickList.FBAPickListType + "-" + DateHelper.ToDateTimeString(dbPickList.CreateDate),
                        FileName    = dbPickList.Id.ToString(),
                        OrderDate   = when,
                        IsFBA       = true
                    };

                    invDb.ItemOrderMappings.AddNewOrder(scanOrder, pickListEntries);
                }

                var styleIds = pickListEntries.Where(i => i.StyleId.HasValue).Select(i => i.StyleId.Value).ToArray();
                using (var db = dbFactory.GetRWDb())
                {
                    SystemActionHelper.RequestQuantityDistribution(db, actionService, styleIds, by);
                }
            }
            else
            {
                FBAPickList dbPickList;
                using (var db = dbFactory.GetRWDb())
                {
                    dbPickList        = db.FBAPickLists.GetAll().FirstOrDefault(pi => pi.Id == pickListId);
                    dbPickList.Status = ShipmentStatuses.Default;
                    db.Commit();
                }

                var name = dbPickList.FBAPickListType + "-" + DateHelper.ToDateTimeString(dbPickList.CreateDate);
                using (var invDb = dbFactory.GetInventoryRWDb())
                {
                    var dbInvOrder = invDb.Orders.GetAll().FirstOrDefault(o => o.Description == name);
                    if (dbInvOrder != null)
                    {
                        invDb.Orders.Remove(dbInvOrder);
                        invDb.Commit();
                    }
                }
            }
        }
Beispiel #3
0
        public long Apply(IUnitOfWork db,
                          ICacheService cache,
                          IQuantityManager quantityManager,
                          IStyleHistoryService styleHistory,
                          ISystemActionService actionService,
                          DateTime when,
                          long?by)
        {
            var style = db.Styles.Get(StyleId);

            style.UpdateDate = when;
            style.UpdatedBy  = by;

            style.ReSaveDate = when;
            style.ReSaveBy   = by;

            StyleViewModel.UpdateLocations(db, styleHistory, StyleId, Locations, when, by);

            var wasAnyChanges = false;

            if (Sizes != null && Sizes.Any())
            {
                var styleItems      = db.StyleItems.GetFiltered(si => si.StyleId == StyleId).ToList();
                var styleItemCaches = db.StyleItemCaches.GetFiltered(si => si.StyleId == StyleId).ToList();

                foreach (var size in Sizes)  //Update quantity (marking when/by)
                {
                    //int? oldQuantity = null;
                    //int? newQuantity = null;
                    string tag        = null;
                    bool   wasChanged = false;

                    var styleItem      = styleItems.FirstOrDefault(si => si.Id == size.StyleItemId);
                    var styleItemCache = styleItemCaches.FirstOrDefault(sic => sic.Id == size.StyleItemId);

                    if (styleItem != null)
                    {
                        if (size.UseBoxQuantity)
                        {
                            if (styleItem.Quantity != null)
                            {
                                var oldQuantity = styleItem.Quantity;
                                var newQuantity = size.BoxQuantity;
                                tag = size.BoxQuantitySetDate.ToString();

                                styleItem.Quantity        = null;
                                styleItem.QuantitySetDate = null;
                                styleItem.QuantitySetBy   = null;
                                //styleItem.RestockDate = null;
                                wasChanged = true;

                                quantityManager.LogStyleItemQuantity(db,
                                                                     styleItem.Id,
                                                                     newQuantity,
                                                                     oldQuantity,
                                                                     QuantityChangeSourceType.UseBoxQuantity,
                                                                     tag,
                                                                     null,
                                                                     null,
                                                                     when,
                                                                     by);
                            }
                        }

                        if (size.NewRestockDate.HasValue &&
                            styleItem.RestockDate != size.NewRestockDate)
                        {
                            styleHistory.AddRecord(styleItem.StyleId,
                                                   StyleHistoryHelper.RestockDateKey,
                                                   DateHelper.ToDateTimeString(styleItem.RestockDate),
                                                   StringHelper.JoinTwo("-", styleItem.Size, styleItem.Color),
                                                   DateHelper.ToDateTimeString(size.NewRestockDate),
                                                   styleItem.Id.ToString(),
                                                   by);

                            styleItem.RestockDate = size.NewRestockDate;
                            wasChanged            = true;
                        }

                        if (size.NewManuallyQuantity.HasValue)
                        {
                            var operationType = size.NewManuallyQuantity.Value < 0 ? QuantityOperationType.Lost : QuantityOperationType.AddManually;

                            var quantityOperation = new QuantityOperationDTO()
                            {
                                Type            = (int)operationType,
                                QuantityChanges = new List <QuantityChangeDTO>()
                                {
                                    new QuantityChangeDTO()
                                    {
                                        StyleId     = style.Id,
                                        StyleItemId = styleItem.Id,
                                        Quantity    = -1 * size.NewManuallyQuantity.Value,
                                        //NOTE: we need to change sign to opposite because we substract quantity operataions from inventory
                                    }
                                },
                                Comment = "From style quantity dialog",
                            };

                            quantityManager.AddQuantityOperation(db,
                                                                 quantityOperation,
                                                                 when,
                                                                 by);

                            //NOTE: Hot updating the cache (only for first few seconds to display the updates before recalculation)
                            if (styleItemCache != null)
                            {
                                styleItemCache.SpecialCaseQuantityFromDate += -1 * size.NewManuallyQuantity.Value;
                                styleItemCache.TotalSpecialCaseQuantity    += -1 * size.NewManuallyQuantity.Value;
                            }

                            wasChanged = true;
                        }

                        if (size.IsRemoveRestockDate == true)
                        {
                            styleItem.RestockDate = null;

                            wasChanged = true;
                        }

                        if (size.OnHold != styleItem.OnHold)
                        {
                            quantityManager.LogStyleItemQuantity(db,
                                                                 styleItem.Id,
                                                                 size.OnHold ? 0 : styleItem.Quantity,
                                                                 size.OnHold ? styleItem.Quantity : 0,
                                                                 QuantityChangeSourceType.OnHold,
                                                                 size.OnHold.ToString(),
                                                                 null,
                                                                 null,
                                                                 when,
                                                                 by);
                            styleItem.OnHold = size.OnHold;

                            wasChanged = true;
                        }
                    }

                    if (wasChanged)
                    {
                        db.Commit();
                        wasAnyChanges = true;
                    }
                }
            }

            //NOTE: always update cache
            cache.RequestStyleIdUpdates(db,
                                        new List <long>()
            {
                StyleId
            },
                                        UpdateCacheMode.IncludeChild,
                                        AccessManager.UserId);

            if (wasAnyChanges)
            {
                db.Commit();

                SystemActionHelper.RequestQuantityDistribution(db, actionService, StyleId, by);
            }

            return(StyleId);
        }
        public void Apply(IUnitOfWork db,
                          IQuantityManager quantityManager,
                          ILogService log,
                          ICacheService cache,
                          ISystemActionService actionService,
                          DateTime when,
                          long?by)
        {
            log.Info("AddSealedBoxWizardViewModel.Apply, StyleId=" + StyleId);

            if (IncomeType == (int)BoxIncomePackType.PPK)
            {
                var box = new SealedBoxViewModel();
                box.StyleId     = StyleId;
                box.BoxBarcode  = BoxBarcode;
                box.BoxQuantity = BoxQuantity;
                box.Price       = Price ?? 0;

                box.Owned    = Owned;
                box.PolyBags = false;
                box.Printed  = Printed;

                box.CreateDate = CreateDate.HasValue ? DateHelper.ConvertUtcToApp(CreateDate.Value.ToUniversalTime()) : when;

                box.StyleItems = new StyleItemCollection()
                {
                    Items = Sizes.Select(s => new StyleItemViewModel()
                    {
                        Id        = s.Id,
                        Breakdown = s.Breakdown,
                    }).ToList()
                };

                box.Apply(db, quantityManager, when, by);
            }

            if (IncomeType == (int)BoxIncomePackType.PolyBagged)
            {
                foreach (var size in Sizes.Where(si => si.Quantity > 0))
                {
                    var box = new SealedBoxViewModel();
                    box.StyleId     = StyleId;
                    box.BoxBarcode  = BoxBarcode;
                    box.BoxQuantity = size.Quantity ?? 0;
                    box.Price       = Price ?? 0;

                    box.Owned    = Owned;
                    box.PolyBags = true;
                    box.Printed  = Printed;

                    box.CreateDate = CreateDate.HasValue ? DateHelper.ConvertUtcToApp(CreateDate.Value.ToUniversalTime()) : when;

                    box.StyleItems = new StyleItemCollection()
                    {
                        Items = new List <StyleItemViewModel>()
                        {
                            new StyleItemViewModel()
                            {
                                Id        = size.Id,
                                Breakdown = UnitsPerBox
                            }
                        }
                    };

                    box.Apply(db, quantityManager, when, by);
                }
            }

            if (IncomeType == (int)BoxIncomePackType.Other)
            {
                var box = new OpenBoxViewModel();
                box.StyleId     = StyleId;
                box.BoxBarcode  = BoxBarcode;
                box.BoxQuantity = 1;
                box.Price       = Price ?? 0;

                box.Owned    = Owned;
                box.PolyBags = false;
                box.Printed  = Printed;

                box.CreateDate = CreateDate.HasValue ? DateHelper.ConvertUtcToApp(CreateDate.Value.ToUniversalTime()) : when;

                box.StyleItems = new StyleItemCollection()
                {
                    Items = Sizes.Select(s => new StyleItemViewModel()
                    {
                        Id       = s.Id,
                        Quantity = s.Quantity,
                    }).ToList()
                };

                box.Apply(db, quantityManager, when, by);
            }

            foreach (var size in Sizes)
            {
                var styleItem = db.StyleItems.Get(size.Id);
                if (size.UseBoxQuantity &&
                    styleItem.Quantity.HasValue)
                {
                    log.Info("Switch to box quantity, styleItemId=" + size.Id);

                    var oldQuantity = styleItem.Quantity;

                    styleItem.Quantity        = null;
                    styleItem.QuantitySetBy   = null;
                    styleItem.QuantitySetDate = null;
                    styleItem.RestockDate     = null;

                    db.Commit();

                    quantityManager.LogStyleItemQuantity(db,
                                                         styleItem.Id,
                                                         null,
                                                         oldQuantity,
                                                         QuantityChangeSourceType.UseBoxQuantity,
                                                         null,
                                                         null,
                                                         BoxBarcode,
                                                         when,
                                                         by);
                }
            }

            cache.RequestStyleIdUpdates(db,
                                        new List <long>()
            {
                StyleId
            },
                                        UpdateCacheMode.IncludeChild,
                                        by);

            SystemActionHelper.RequestQuantityDistribution(db, actionService, StyleId, by);
        }
        public void SyncQty(DropShipperApi api)
        {
            var styles = api.GetQuantities(_log, MarketType.DropShipper, MarketplaceKeeper.DsPAWMCom);

            styles = styles.Where(st => st.StyleID == "WS20052SS").ToList();

            using (var db = _dbFactory.GetRWDb())
            {
                foreach (var incomeStyle in styles)
                {
                    var findStyle = db.Styles.GetAll().FirstOrDefault(st => (st.StyleID == incomeStyle.StyleID ||
                                                                             st.OriginalStyleID.Contains(incomeStyle.StyleID + ";")) &&
                                                                      !st.Deleted);

                    if (findStyle != null)
                    {
                        var changedStyleItemIds = new List <long>();

                        foreach (var incomeStyleItem in incomeStyle.StyleItems)
                        {
                            var findStyleItem = db.StyleItems.GetAll().FirstOrDefault(si => si.StyleId == findStyle.Id &&
                                                                                      si.Size == incomeStyleItem.Size &&
                                                                                      (si.Color == incomeStyleItem.Color ||
                                                                                       (String.IsNullOrEmpty(si.Color) && String.IsNullOrEmpty(incomeStyleItem.Color))));
                            if (findStyleItem != null &&
                                (findStyleItem.Quantity != incomeStyleItem.Quantity ||
                                 findStyleItem.QuantitySetDate != incomeStyleItem.QuantitySetDate))
                            {
                                _quantityManager.LogStyleItemQuantity(db,
                                                                      findStyleItem.Id,
                                                                      incomeStyleItem.Quantity,
                                                                      findStyleItem.Quantity,
                                                                      QuantityChangeSourceType.SetByAutoQuantity,
                                                                      null,
                                                                      null,
                                                                      null,
                                                                      _time.GetAppNowTime(),
                                                                      null);

                                findStyleItem.Quantity        = incomeStyleItem.Quantity;
                                findStyleItem.QuantitySetDate = incomeStyleItem.QuantitySetDate;

                                changedStyleItemIds.Add(incomeStyleItem.StyleItemId);
                            }
                            else
                            {
                                _log.Info("Unable to find style size: " + findStyle.StyleID + " - " + incomeStyleItem.Size + " / " + incomeStyleItem.Color);
                            }
                        }
                        db.Commit();

                        if (changedStyleItemIds.Any())
                        {
                            _cacheService.RequestStyleIdUpdates(db,
                                                                new List <long>()
                            {
                                findStyle.Id
                            },
                                                                UpdateCacheMode.IncludeChild,
                                                                null);

                            SystemActionHelper.RequestQuantityDistribution(db, _actionService, findStyle.Id, null);
                        }
                    }
                    else
                    {
                        _log.Info("Unable to find style: " + incomeStyle.StyleID);
                    }
                }
            }
        }