Пример #1
0
        private FBAPickDetail CreateFBAPickDetailFromPalletLocation(FBAPalletLocation fbaPalletLocationInDb, FBAShipOrder shipOrderInDb, int pltQuantity, int newPltQuantity, IList <FBAPickDetailCarton> pickDetailCartonList, IEnumerable <PickCartonDto> objArray)
        {
            var pickDetail = new FBAPickDetail();

            pickDetail.AssembleUniqueIndex(fbaPalletLocationInDb.Container, fbaPalletLocationInDb.GrandNumber);
            pickDetail.AssembleFirstStringPart(fbaPalletLocationInDb.ShipmentId, fbaPalletLocationInDb.AmzRefId, fbaPalletLocationInDb.WarehouseCode);
            pickDetail.AssembleActualDetails(0, 0, objArray.Sum(x => x.PickQuantity));

            pickDetail.Status            = FBAStatus.Picking;
            pickDetail.Size              = fbaPalletLocationInDb.PalletSize;
            pickDetail.PickableCtns      = objArray.Sum(x => x.PickQuantity);
            pickDetail.NewPlts           = newPltQuantity;
            pickDetail.PltsFromInventory = pltQuantity;
            //pickDetail.ActualPlts = pltQuantity + newPltQuantity;
            //现在强行规定,实际出库托盘数量为0,防止仓库偷懒不调整
            pickDetail.ActualPlts = 0;
            pickDetail.CtnsPerPlt = fbaPalletLocationInDb.CtnsPerPlt;
            pickDetail.Location   = fbaPalletLocationInDb.Location;

            fbaPalletLocationInDb.PickingPlts += pltQuantity;
            //如果需要在库存中体现新打的托盘数量,禁用上面一行,启用下面一行
            //fbaPalletLocationInDb.PickingPlts += pltQuantity + newPltQuantity;

            fbaPalletLocationInDb.AvailablePlts -= pltQuantity;
            fbaPalletLocationInDb.Status         = FBAStatus.Picking;

            pickDetail.HowToDeliver      = fbaPalletLocationInDb.HowToDeliver;
            pickDetail.FBAPalletLocation = fbaPalletLocationInDb;
            pickDetail.OrderType         = FBAOrderType.Standard;
            pickDetail.HowToDeliver      = fbaPalletLocationInDb.HowToDeliver;
            pickDetail.InboundDate       = fbaPalletLocationInDb.FBAMasterOrder.InboundDate;

            pickDetail.FBAShipOrder = shipOrderInDb;

            var cartonLocationInPalletsInDb = fbaPalletLocationInDb.FBAPallet.FBACartonLocations;

            foreach (var obj in objArray)
            {
                if (obj.PickQuantity == 0)
                {
                    continue;
                }

                var cartonInPalletInDb = cartonLocationInPalletsInDb.SingleOrDefault(x => x.Id == obj.Id);

                cartonInPalletInDb.PickingCtns += obj.PickQuantity;

                var pickDetailCarton = new FBAPickDetailCarton
                {
                    PickCtns          = obj.PickQuantity,
                    FBAPickDetail     = pickDetail,
                    FBACartonLocation = cartonInPalletInDb
                };

                cartonInPalletInDb.AvailableCtns -= obj.PickQuantity;
                pickDetail.ActualCBM             += obj.PickQuantity * cartonInPalletInDb.CBMPerCtn;
                pickDetail.ActualGrossWeight     += obj.PickQuantity * cartonInPalletInDb.GrossWeightPerCtn;

                pickDetailCartonList.Add(pickDetailCarton);
            }

            // 如果捡完了托盘数量但是箱子还有剩余,则报错
            var availablePlts = fbaPalletLocationInDb.FBAPallet.FBAPalletLocations.Sum(x => x.AvailablePlts);
            var availableCtns = fbaPalletLocationInDb.FBAPallet.FBACartonLocations.Sum(x => x.AvailableCtns);

            if (availablePlts == 0 && availableCtns != 0)
            {
                throw new Exception("Pick failed. The pallets number of SKU " + fbaPalletLocationInDb.ShipmentId + " will be 0 after this pick but there are still many cartons inside. Please make sure there is no thing left before picking the last pallte.");
            }

            // 如果托盘中的箱子捡完了但是托盘数没捡完,则自动把所有剩下的托盘数带上

            if (availableCtns == 0 && availablePlts != 0)
            {
                pickDetail.PltsFromInventory       += availablePlts;
                fbaPalletLocationInDb.PickingPlts  += availablePlts;
                fbaPalletLocationInDb.AvailablePlts = 0;
            }

            return(pickDetail);
        }
        // 托盘货放回方法
        public void PutbackPickedPalletItemsToNewLocation(FBAPickDetail pickDetailInDb, string newLocation, string user)
        {
            var pickedCtns = 0;

            pickDetailInDb.FBAPalletLocation.ActualPlts        -= pickDetailInDb.PltsFromInventory;
            pickDetailInDb.FBAPalletLocation.PickingPlts       -= pickDetailInDb.PltsFromInventory;
            pickDetailInDb.FBAPalletLocation.ActualGrossWeight -= pickDetailInDb.ActualGrossWeight;
            pickDetailInDb.FBAPalletLocation.ActualCBM         -= pickDetailInDb.ActualCBM;

            pickDetailInDb.FBAPalletLocation.FBAPallet.ActualPallets     -= pickDetailInDb.PltsFromInventory;
            pickDetailInDb.FBAPalletLocation.FBAPallet.ActualQuantity    -= pickDetailInDb.ActualQuantity;
            pickDetailInDb.FBAPalletLocation.FBAPallet.ActualGrossWeight -= pickDetailInDb.ActualGrossWeight;
            pickDetailInDb.FBAPalletLocation.FBAPallet.ActualCBM         -= pickDetailInDb.ActualCBM;
            pickDetailInDb.FBAPalletLocation.FBAPallet.ComsumedPallets   -= pickDetailInDb.PltsFromInventory;

            var newFBAPallet = new FBAPallet {
                ActualCBM             = pickDetailInDb.ActualCBM,
                ActualGrossWeight     = pickDetailInDb.ActualGrossWeight,
                ActualPallets         = pickDetailInDb.PltsFromInventory,
                ActualQuantity        = pickDetailInDb.ActualQuantity,
                AmzRefId              = pickDetailInDb.FBAPalletLocation.FBAPallet.AmzRefId,
                Container             = pickDetailInDb.FBAPalletLocation.FBAPallet.Container,
                ShipmentId            = pickDetailInDb.FBAPalletLocation.FBAPallet.ShipmentId,
                ComsumedPallets       = pickDetailInDb.PltsFromInventory,
                GrandNumber           = pickDetailInDb.FBAPalletLocation.FBAPallet.GrandNumber,
                HowToDeliver          = pickDetailInDb.FBAPalletLocation.FBAPallet.HowToDeliver,
                LocationStatus        = FBAStatus.PutBack,
                IsOverSizeOrOverwidth = pickDetailInDb.FBAPalletLocation.FBAPallet.IsOverSizeOrOverwidth,
                HasSortingMarking     = pickDetailInDb.FBAPalletLocation.FBAPallet.HasSortingMarking,
                PalletSize            = pickDetailInDb.FBAPalletLocation.FBAPallet.PalletSize,
                WarehouseCode         = pickDetailInDb.FBAPalletLocation.FBAPallet.WarehouseCode,
                DoesAppliedLabel      = pickDetailInDb.FBAPalletLocation.FBAPallet.DoesAppliedLabel,
                FBAMasterOrder        = pickDetailInDb.FBAPalletLocation.FBAPallet.FBAMasterOrder,
                Memo = "Put back from shipping order: " + pickDetailInDb.FBAShipOrder.ShipOrderNumber + " on " + DateTime.Now.ToString("yyyy-MM-dd")
            };

            var newCartonLocationsList = new List <FBACartonLocation>();
            var ctnLogList             = new List <OrderOperationLog>();

            foreach (var p in pickDetailInDb.FBAPickDetailCartons)
            {
                p.FBACartonLocation.ActualQuantity    -= p.PickCtns;
                p.FBACartonLocation.PickingCtns       -= p.PickCtns;
                p.FBACartonLocation.ActualGrossWeight -= p.FBACartonLocation.GrossWeightPerCtn * p.PickCtns;
                p.FBACartonLocation.ActualCBM         -= p.FBACartonLocation.CBMPerCtn * p.PickCtns;

                if (p.FBACartonLocation.ActualGrossWeight < 0)
                {
                    p.FBACartonLocation.ActualGrossWeight = 0;
                }

                if (p.FBACartonLocation.ActualCBM < 0)
                {
                    p.FBACartonLocation.ActualCBM = 0;
                }

                var newFBACartonLocation = new FBACartonLocation {
                    Status            = FBAStatus.InPallet,
                    ActualCBM         = p.FBACartonLocation.CBMPerCtn * p.PickCtns,
                    ActualGrossWeight = p.FBACartonLocation.GrossWeightPerCtn * p.PickCtns,
                    ActualQuantity    = p.PickCtns,
                    LocationStatus    = FBAStatus.PutBack,
                    AmzRefId          = p.FBACartonLocation.AmzRefId,
                    AvailableCtns     = p.PickCtns,
                    CBMPerCtn         = p.FBACartonLocation.CBMPerCtn,
                    Container         = p.FBACartonLocation.Container,
                    GrandNumber       = p.FBACartonLocation.GrandNumber,
                    ShipmentId        = p.FBACartonLocation.ShipmentId,
                    GrossWeightPerCtn = p.FBACartonLocation.GrossWeightPerCtn,
                    Location          = "Pallet",
                    HowToDeliver      = p.FBACartonLocation.HowToDeliver,
                    WarehouseCode     = p.FBACartonLocation.WarehouseCode,
                    FBAPallet         = newFBAPallet,
                    FBAOrderDetail    = p.FBACartonLocation.FBAOrderDetail,
                    FBAMasterOrder    = p.FBACartonLocation.FBAMasterOrder,
                    Memo = "Put back from shipping order: " + pickDetailInDb.FBAShipOrder.ShipOrderNumber + " on " + DateTime.Now.ToString("yyyy-MM-dd")
                };

                pickedCtns += p.PickCtns;
                newCartonLocationsList.Add(newFBACartonLocation);
                ctnLogList.Add(new OrderOperationLog {
                    Type          = FBAStatus.PutBack,
                    OperationDate = DateTime.Now,
                    Operator      = user,
                    FBAShipOrder  = pickDetailInDb.FBAShipOrder,
                    Description   = "Put back " + p.PickCtns + " in-pallet carton(s) from Carton Location Id: " + p.FBACartonLocation.Id + ", Container: " + p.FBACartonLocation.Container + ", ShipmentId(SKU): " + p.FBACartonLocation.ShipmentId + ", AmzRefId: " + p.FBACartonLocation.AmzRefId + ", Warehouse code: " + p.FBACartonLocation.WarehouseCode + " to new location: " + newLocation
                });
            }

            pickDetailInDb.FBAPalletLocation.ActualQuantity -= pickedCtns;

            var newFBAPalletLocation = new FBAPalletLocation {
                Status            = FBAStatus.InStock,
                ActualCBM         = pickDetailInDb.ActualCBM,
                ActualGrossWeight = pickDetailInDb.ActualGrossWeight,
                ActualPlts        = pickDetailInDb.PltsFromInventory,
                ActualQuantity    = pickedCtns,
                AvailablePlts     = pickDetailInDb.PltsFromInventory,
                AmzRefId          = pickDetailInDb.AmzRefId,
                Container         = pickDetailInDb.Container,
                CBMPerPlt         = pickDetailInDb.FBAPalletLocation.CBMPerPlt,
                CtnsPerPlt        = pickDetailInDb.FBAPalletLocation.CtnsPerPlt,
                GrandNumber       = pickDetailInDb.FBAPalletLocation.GrandNumber,
                GrossWeightPerPlt = pickDetailInDb.FBAPalletLocation.GrossWeightPerPlt,
                HowToDeliver      = pickDetailInDb.FBAPalletLocation.HowToDeliver,
                Location          = newLocation,
                PalletSize        = pickDetailInDb.FBAPalletLocation.PalletSize,
                ShipmentId        = pickDetailInDb.FBAPalletLocation.ShipmentId,
                LocationStatus    = FBAStatus.PutBack,
                WarehouseCode     = pickDetailInDb.FBAPalletLocation.WarehouseCode,
                FBAMasterOrder    = pickDetailInDb.FBAPalletLocation.FBAMasterOrder,
                FBAPallet         = newFBAPallet,
                Memo = "Put back from shipping order: " + pickDetailInDb.FBAShipOrder.ShipOrderNumber + " on " + DateTime.Now.ToString("yyyy-MM-dd")
            };

            var pltLog = new OrderOperationLog
            {
                Type          = FBAStatus.PutBack,
                FBAShipOrder  = pickDetailInDb.FBAShipOrder,
                OperationDate = DateTime.Now,
                Operator      = user
            };

            if (pickDetailInDb.FBAPickDetailCartons == null)
            {
                pltLog.Description = "Put back " + pickDetailInDb.PltsFromInventory + " empty pallet(s) from Pallet Location Id: " + pickDetailInDb.FBAPalletLocation.Id + ", Container: " + pickDetailInDb.Container + ", ShipmentId(SKU): " + pickDetailInDb.ShipmentId + ", AmzRefId: " + pickDetailInDb.AmzRefId + ", Warehouse code: " + pickDetailInDb.WarehouseCode + " to new location: " + newLocation;
            }
            else
            {
                pltLog.Description = "Put back " + pickDetailInDb.PltsFromInventory + " pallet(s) from Pallet Location Id: " + pickDetailInDb.FBAPalletLocation.Id + " Container: " + pickDetailInDb.Container + ", ShipmentId(SKU): " + pickDetailInDb.ShipmentId + ", AmzRefId: " + pickDetailInDb.AmzRefId + ", Warehouse code: " + pickDetailInDb.WarehouseCode + " to new location: " + newLocation;
            }

            _context.OrderOperationLogs.Add(pltLog);
            _context.OrderOperationLogs.AddRange(ctnLogList);

            _context.FBACartonLocations.AddRange(newCartonLocationsList);
            _context.FBAPallets.Add(newFBAPallet);
            _context.FBAPalletLocations.Add(newFBAPalletLocation);

            _context.FBAPickDetailCartons.RemoveRange(pickDetailInDb.FBAPickDetailCartons);
            _context.FBAPickDetails.Remove(pickDetailInDb);

            _context.SaveChanges();
        }
        public void CreateLocationObjects([FromUri] string grandNumber, [FromUri] string inventoryType, [FromBody] IEnumerable <FBALocationDto> objArray)
        {
            var masterOrderInDb = _context.FBAMasterOrders.SingleOrDefault(x => x.GrandNumber == grandNumber);

            if (inventoryType == FBAInventoryType.Pallet)
            {
                var palletLocationList = new List <FBAPalletLocation>();
                var palletsInDb        = _context.FBAPallets
                                         .Include(x => x.FBACartonLocations)
                                         .Where(x => x.GrandNumber == grandNumber &&
                                                x.ActualPallets - x.ComsumedPallets > 0);

                if (palletsInDb.Count() == 0)
                {
                    throw new Exception("No quantity for allocating.");
                }

                foreach (var obj in objArray)
                {
                    var palletInDb = palletsInDb
                                     .Include(x => x.FBACartonLocations)
                                     .SingleOrDefault(x => x.Id == obj.Id);

                    //如果这是一个rough packed pallets,那么默认分配所有货物
                    //if (palletInDb.FBACartonLocations.Sum(x => x.CtnsPerPlt) == 0)
                    //{
                    //    obj.Quantity = palletInDb.ActualPallets;
                    //}

                    //所有类型的pallets现在不允许分开入库
                    obj.Quantity = palletInDb.ActualPallets - palletInDb.ComsumedPallets;

                    palletInDb.ComsumedPallets += obj.Quantity;

                    if (palletInDb.ComsumedPallets > palletInDb.ActualPallets)
                    {
                        throw new Exception("Not enough quantity for comsuming. Check Id:" + obj.Id);
                    }

                    var palletLocation = new FBAPalletLocation();

                    palletLocation.Status       = FBAStatus.InStock;
                    palletLocation.HowToDeliver = palletInDb.HowToDeliver;
                    //palletLocation.GrossWeightPerPlt = palletInDb.ActualGrossWeight / palletInDb.ActualPallets;
                    palletLocation.GrossWeightPerPlt = palletInDb.FBACartonLocations.Sum(x => x.GrossWeightPerCtn * x.ActualQuantity) / obj.Quantity;
                    //palletLocation.CBMPerPlt = palletInDb.ActualCBM / palletInDb.ActualPallets;
                    palletLocation.CBMPerPlt     = palletInDb.FBACartonLocations.Sum(x => x.CBMPerCtn * x.ActualQuantity) / obj.Quantity;
                    palletLocation.CtnsPerPlt    = palletInDb.FBACartonLocations.Sum(x => x.CtnsPerPlt) == 0 ? 0 : palletInDb.ActualQuantity / palletInDb.ActualPallets;
                    palletLocation.AvailablePlts = obj.Quantity;
                    palletLocation.Location      = obj.Location;
                    palletLocation.PalletSize    = palletInDb.PalletSize;

                    palletLocation.AssembleFirstStringPart(palletInDb.ShipmentId, palletInDb.AmzRefId, palletInDb.WarehouseCode);
                    //PalletLocation的Actualquantity指内含cartons的总数量
                    palletLocation.AssembleActualDetails(palletLocation.GrossWeightPerPlt * obj.Quantity, palletLocation.CBMPerPlt * obj.Quantity, palletInDb.FBACartonLocations.Sum(x => x.ActualQuantity));
                    palletLocation.ActualPlts = obj.Quantity;
                    palletLocation.AssembleUniqueIndex(palletInDb.Container, palletInDb.GrandNumber);

                    palletLocation.FBAMasterOrder = masterOrderInDb;
                    palletLocation.FBAPallet      = palletInDb;

                    palletLocationList.Add(palletLocation);
                }
                _context.FBAPalletLocations.AddRange(palletLocationList);
            }
            else
            {
                var cartonLocationList = new List <FBACartonLocation>();
                var orderDetailsInDb   = _context.FBAOrderDetails
                                         .Where(x => x.GrandNumber == grandNumber &&
                                                x.ActualQuantity - x.ComsumedQuantity > 0);

                if (orderDetailsInDb.Count() == 0)
                {
                    throw new Exception("No quantity for allocating.");
                }

                foreach (var obj in objArray)
                {
                    var orderDetailInDb = orderDetailsInDb.SingleOrDefault(x => x.Id == obj.Id);
                    orderDetailInDb.ComsumedQuantity += obj.Quantity;

                    if (orderDetailInDb.ComsumedQuantity > orderDetailInDb.ActualQuantity)
                    {
                        throw new Exception("Not enough quantity for comsuming. Check Id:" + obj.Id);
                    }

                    if (orderDetailInDb.Container == "NULL" || orderDetailInDb.Container == "")
                    {
                        throw new Exception("Please assign container number first.");
                    }

                    var cartonLocation = new FBACartonLocation();

                    cartonLocation.Status            = FBAStatus.InStock;
                    cartonLocation.HowToDeliver      = orderDetailInDb.HowToDeliver;
                    cartonLocation.GrossWeightPerCtn = (float)Math.Round((orderDetailInDb.ActualGrossWeight / orderDetailInDb.ActualQuantity), 2);
                    cartonLocation.CBMPerCtn         = (float)Math.Round((orderDetailInDb.ActualCBM / orderDetailInDb.ActualQuantity), 2);
                    cartonLocation.AvailableCtns     = obj.Quantity;
                    cartonLocation.Location          = obj.Location;

                    cartonLocation.AssembleFirstStringPart(orderDetailInDb.ShipmentId, orderDetailInDb.AmzRefId, orderDetailInDb.WarehouseCode);
                    cartonLocation.AssembleActualDetails(cartonLocation.GrossWeightPerCtn * obj.Quantity, cartonLocation.CBMPerCtn * obj.Quantity, obj.Quantity);
                    cartonLocation.AssembleUniqueIndex(orderDetailInDb.Container, orderDetailInDb.GrandNumber);

                    cartonLocation.FBAOrderDetail = orderDetailInDb;

                    cartonLocationList.Add(cartonLocation);
                }

                _context.FBACartonLocations.AddRange(cartonLocationList);
            }
            _context.SaveChanges();
        }