Ejemplo n.º 1
0
        private FBAPickDetail CreateFBAPickDetailFromCartonLocation(FBACartonLocation fbaCartonLocationInDb, FBAShipOrder shipOrderInDb, int ctnQuantity, IList <FBAPickDetailCarton> pickDetailCartonList)
        {
            var pickDetail = new FBAPickDetail();

            pickDetail.AssembleUniqueIndex(fbaCartonLocationInDb.Container, fbaCartonLocationInDb.GrandNumber);
            pickDetail.AssembleFirstStringPart(fbaCartonLocationInDb.ShipmentId, fbaCartonLocationInDb.AmzRefId, fbaCartonLocationInDb.WarehouseCode);
            pickDetail.AssembleActualDetails(fbaCartonLocationInDb.GrossWeightPerCtn * ctnQuantity, fbaCartonLocationInDb.CBMPerCtn * ctnQuantity, ctnQuantity);

            pickDetail.Status       = FBAStatus.Picking;
            pickDetail.Size         = FBAStatus.Na;
            pickDetail.CtnsPerPlt   = 0;
            pickDetail.PickableCtns = ctnQuantity;
            pickDetail.Location     = fbaCartonLocationInDb.Location;
            pickDetail.InboundDate  = fbaCartonLocationInDb.FBAOrderDetail.FBAMasterOrder.InboundDate;

            fbaCartonLocationInDb.PickingCtns   += ctnQuantity;
            fbaCartonLocationInDb.AvailableCtns -= ctnQuantity;
            fbaCartonLocationInDb.Status         = FBAStatus.Picking;

            pickDetail.FBACartonLocation = fbaCartonLocationInDb;
            pickDetail.OrderType         = FBAOrderType.ECommerce;
            pickDetail.HowToDeliver      = fbaCartonLocationInDb.HowToDeliver;

            pickDetail.FBAShipOrder = shipOrderInDb;

            var pickDetailCarton = new FBAPickDetailCarton();

            pickDetailCarton.FBACartonLocation = fbaCartonLocationInDb;
            pickDetailCarton.FBAPickDetail     = pickDetail;
            pickDetailCarton.PickCtns          = ctnQuantity;

            pickDetailCartonList.Add(pickDetailCarton);
            return(pickDetail);
        }
        private FBAPickDetail CreateFBAPickDetail(FBACartonLocation cartonLocation, int ctns, IList <FBAPickDetailCarton> pickDetailCartonList)
        {
            //如果不是放在托盘上的货,则直接建立pickdetail和cartondetail的关系
            if (cartonLocation.Status != "InPallet")
            {
                var location = CombineLocation(cartonLocation.FBAPallet.FBAPalletLocations.Select(x => x.Location).ToList());

                return(new FBAPickDetail
                {
                    Location = location,
                    GrandNumber = cartonLocation.GrandNumber,
                    Container = cartonLocation.Container,
                    ShipmentId = cartonLocation.ShipmentId,
                    AmzRefId = cartonLocation.AmzRefId,
                    WarehouseCode = cartonLocation.WarehouseCode,
                    ActualCBM = cartonLocation.CBMPerCtn * ctns,
                    Size = " ",
                    ActualGrossWeight = cartonLocation.GrossWeightPerCtn * ctns,
                    ActualQuantity = ctns,
                    OrderType = FBAOrderType.Standard,
                    HowToDeliver = cartonLocation.HowToDeliver,
                    Status = FBAStatus.Picking,
                    PickableCtns = ctns,
                    FBACartonLocation = cartonLocation
                });
            }
            //如果是托盘里的carton,先找到其托盘对象,再在托盘对象中拣货
            else
            {
                var palletLocation = cartonLocation.FBAPallet.FBAPalletLocations.First();
                var pickDetail     = new FBAPickDetail
                {
                    Location          = palletLocation.Location,
                    GrandNumber       = palletLocation.GrandNumber,
                    Container         = palletLocation.Container,
                    ShipmentId        = palletLocation.ShipmentId,
                    AmzRefId          = palletLocation.AmzRefId,
                    WarehouseCode     = palletLocation.WarehouseCode,
                    ActualCBM         = cartonLocation.CBMPerCtn * ctns,
                    Size              = " ",
                    ActualGrossWeight = cartonLocation.GrossWeightPerCtn * ctns,
                    ActualQuantity    = ctns,
                    OrderType         = FBAOrderType.Standard,
                    HowToDeliver      = palletLocation.HowToDeliver,
                    Status            = FBAStatus.Picking,
                    PickableCtns      = ctns,
                    FBAPalletLocation = palletLocation
                };

                pickDetailCartonList.Add(new FBAPickDetailCarton {
                    PickCtns          = ctns,
                    FBAPickDetail     = pickDetail,
                    FBACartonLocation = cartonLocation
                });

                return(pickDetail);
            }
        }
        private void ShipPickDetailCartons(FBACartonLocation cartonLocationInDb, FBAPickDetail pickDetailInDb)
        {
            cartonLocationInDb.ShippedCtns += pickDetailInDb.ActualQuantity;
            cartonLocationInDb.PickingCtns -= pickDetailInDb.ActualQuantity;

            if (cartonLocationInDb.PickingCtns == 0 && cartonLocationInDb.AvailableCtns != 0 && cartonLocationInDb.Location != "Pallet")
            {
                cartonLocationInDb.Status = FBAStatus.InStock;
            }
            else if (cartonLocationInDb.PickingCtns == 0 && cartonLocationInDb.AvailableCtns != 0 && cartonLocationInDb.Location == "Pallet")
            {
                cartonLocationInDb.Status = FBAStatus.InPallet;
            }
            else if (cartonLocationInDb.PickingCtns == 0 && cartonLocationInDb.AvailableCtns == 0)
            {
                cartonLocationInDb.Status = FBAStatus.Shipped;
            }
        }
        // 散箱放回方法
        public void PutbackPickedCartonItemToNewLocation(FBAPickDetail pickDetailInDb, string newLocation, string user)
        {
            pickDetailInDb.FBACartonLocation.ActualQuantity    -= pickDetailInDb.ActualQuantity;
            pickDetailInDb.FBACartonLocation.PickingCtns       -= pickDetailInDb.ActualQuantity;
            pickDetailInDb.FBACartonLocation.ActualGrossWeight -= pickDetailInDb.ActualGrossWeight;
            pickDetailInDb.FBACartonLocation.ActualCBM         -= pickDetailInDb.ActualCBM;

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

            _context.OrderOperationLogs.Add(new OrderOperationLog
            {
                Type          = FBAStatus.PutBack,
                OperationDate = DateTime.Now,
                Operator      = user,
                FBAShipOrder  = pickDetailInDb.FBAShipOrder,
                Description   = "Put back " + pickDetailInDb.ActualQuantity + " loose carton(s) from Carton Location Id: " + pickDetailInDb.FBACartonLocation.Id + ", Container: " + pickDetailInDb.FBACartonLocation.Container + ", ShipmentId(SKU): " + pickDetailInDb.FBACartonLocation.ShipmentId + ", AmzRefId: " + pickDetailInDb.FBACartonLocation.AmzRefId + ", Warehouse code: " + pickDetailInDb.FBACartonLocation.WarehouseCode + " to new location: " + newLocation
            });

            _context.FBACartonLocations.Add(newFBACartonLocation);
            _context.FBAPickDetailCartons.RemoveRange(pickDetailInDb.FBAPickDetailCartons);
            _context.FBAPickDetails.Remove(pickDetailInDb);
            _context.SaveChanges();
        }
        // 托盘货放回方法
        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 CreatePallet([FromUri] string grandNumber, [FromUri] int pltQuantity, [FromUri] string pltSize, [FromUri] bool doesAppliedLabel, [FromUri] bool hasSortingMarking, [FromUri] bool isOverSizeOrOverwidth, [FromUri] string packType, [FromUri] bool isSelectedByCheckBox, [FromBody] IEnumerable <PalletInfoDto> objArray)
        {
            //进入库存的carton对象都按GW/ctn,CBM/ctn记录
            var cartonLocationList = new List <FBACartonLocation>();
            var orderDetailsInDb   = _context.FBAOrderDetails
                                     .Where(x => x.GrandNumber == grandNumber);
            var masterOrderInDb = _context.FBAMasterOrders.First(x => x.GrandNumber == grandNumber);

            if (masterOrderInDb.Container == "NULL" || masterOrderInDb.Container == "")
            {
                throw new Exception("Must assign container number befroe operation.");
            }

            if (objArray.Count() == 0)
            {
                throw new Exception("No items or cartons were selected.");
            }

            if (packType == FBAPackType.DetailPack)
            {
                if (isSelectedByCheckBox == true)
                {
                    throw new Exception("Detail pack does not support selecting items by check-boxes.");
                }

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

                    orderDetailInDb.ComsumedQuantity += obj.Quantity * pltQuantity;

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

                    var cartonLocation    = new FBACartonLocation();
                    var ctnsPerPlt        = obj.Quantity;
                    var grossWeightPerCtn = (float)Math.Round((orderDetailInDb.ActualGrossWeight / orderDetailInDb.ActualQuantity), 2);
                    var cbmPerCtn         = (float)Math.Round((orderDetailInDb.ActualCBM / orderDetailInDb.ActualQuantity), 2);

                    cartonLocation.AssembleFirstStringPart(orderDetailInDb.ShipmentId, orderDetailInDb.AmzRefId, orderDetailInDb.WarehouseCode);
                    cartonLocation.AssemblePltInfo(grossWeightPerCtn, cbmPerCtn, ctnsPerPlt);

                    cartonLocation.Container = orderDetailInDb.Container;
                    //cartonLocation.AvaliableCtns = cartonLocation.ActualQuantity;
                    cartonLocation.Location       = "Pallet";
                    cartonLocation.HowToDeliver   = orderDetailInDb.HowToDeliver;
                    cartonLocation.GrandNumber    = grandNumber;
                    cartonLocation.FBAOrderDetail = orderDetailInDb;
                    cartonLocation.ActualQuantity = ctnsPerPlt * pltQuantity;
                    cartonLocation.AvailableCtns  = ctnsPerPlt * pltQuantity;
                    cartonLocation.Status         = FBAStatus.InPallet;

                    cartonLocationList.Add(cartonLocation);
                }

                //建立FBAPallet对象
                var pallet           = new FBAPallet();
                var firstId          = objArray.First().Id;
                var firstOrderDetail = orderDetailsInDb
                                       .Include(x => x.FBAMasterOrder)
                                       .SingleOrDefault(x => x.Id == firstId);

                pallet.AssembleFirstStringPart(DistinctStringList(cartonLocationList.Select(x => x.ShipmentId)), DistinctStringList(cartonLocationList.Select(x => x.AmzRefId)), DistinctStringList(cartonLocationList.Select(x => x.WarehouseCode)));
                pallet.AssembleActualDetails(cartonLocationList.Sum(x => x.GrossWeightPerCtn * x.CtnsPerPlt * pltQuantity), cartonLocationList.Sum(x => x.CBMPerCtn * x.CtnsPerPlt * pltQuantity), cartonLocationList.Sum(x => x.CtnsPerPlt * pltQuantity));
                pallet.AssembleBoolValue(doesAppliedLabel, hasSortingMarking, isOverSizeOrOverwidth);

                pallet.Container      = firstOrderDetail.Container;
                pallet.HowToDeliver   = firstOrderDetail.HowToDeliver;
                pallet.PalletSize     = pltSize;
                pallet.GrandNumber    = grandNumber;
                pallet.FBAMasterOrder = firstOrderDetail.FBAMasterOrder;
                pallet.ActualPallets  = pltQuantity;

                _context.FBAPallets.Add(pallet);

                foreach (var cartonLocation in cartonLocationList)
                {
                    cartonLocation.FBAPallet = pallet;
                }

                _context.FBACartonLocations.AddRange(cartonLocationList);
            }
            else
            {
                foreach (var obj in objArray)
                {
                    var orderDetailInDb = orderDetailsInDb.SingleOrDefault(x => x.Id == obj.Id);
                    var packedCtn       = 0;

                    //如果是处理复选框对象,则默认分配所有剩下的箱数,否则按照数组中的数量分配
                    if (isSelectedByCheckBox == true)
                    {
                        packedCtn = orderDetailInDb.ActualQuantity - orderDetailInDb.ComsumedQuantity;
                        orderDetailInDb.ComsumedQuantity = orderDetailInDb.ActualQuantity;
                    }
                    else
                    {
                        packedCtn = obj.Quantity;
                        orderDetailInDb.ComsumedQuantity += obj.Quantity;
                    }

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

                    var cartonLocation = new FBACartonLocation();
                    cartonLocation.Status = FBAStatus.InPallet;
                    var grossWeightPerCtn = (float)Math.Round((orderDetailInDb.ActualGrossWeight / orderDetailInDb.ActualQuantity), 2);
                    var cbmPerCtn         = (float)Math.Round((orderDetailInDb.ActualCBM / orderDetailInDb.ActualQuantity), 2);

                    cartonLocation.AssembleFirstStringPart(orderDetailInDb.ShipmentId, orderDetailInDb.AmzRefId, orderDetailInDb.WarehouseCode);
                    cartonLocation.AssemblePltInfo(grossWeightPerCtn, cbmPerCtn, 0);

                    cartonLocation.Container      = orderDetailInDb.Container;
                    cartonLocation.Location       = "Pallet";
                    cartonLocation.HowToDeliver   = orderDetailInDb.HowToDeliver;
                    cartonLocation.GrandNumber    = grandNumber;
                    cartonLocation.FBAOrderDetail = orderDetailInDb;
                    cartonLocation.ActualQuantity = packedCtn;
                    cartonLocation.AvailableCtns  = packedCtn;

                    cartonLocationList.Add(cartonLocation);
                }

                //建立FBAPallet对象
                var pallet = new FBAPallet();

                var firstId          = objArray.First().Id;
                var firstOrderDetail = orderDetailsInDb
                                       .Include(x => x.FBAMasterOrder)
                                       .SingleOrDefault(x => x.Id == firstId);

                pallet.AssembleFirstStringPart(DistinctStringList(cartonLocationList.Select(x => x.ShipmentId)), DistinctStringList(cartonLocationList.Select(x => x.AmzRefId)), DistinctStringList(cartonLocationList.Select(x => x.WarehouseCode)));
                pallet.ActualQuantity = cartonLocationList.Sum(x => x.ActualQuantity);
                pallet.AssembleBoolValue(doesAppliedLabel, hasSortingMarking, isOverSizeOrOverwidth);

                pallet.Container      = firstOrderDetail.Container;
                pallet.HowToDeliver   = firstOrderDetail.HowToDeliver;
                pallet.PalletSize     = pltSize;
                pallet.FBAMasterOrder = firstOrderDetail.FBAMasterOrder;
                pallet.GrandNumber    = grandNumber;
                pallet.ActualPallets  = pltQuantity;

                _context.FBAPallets.Add(pallet);

                foreach (var cartonLocation in cartonLocationList)
                {
                    cartonLocation.FBAPallet = pallet;
                }

                _context.FBACartonLocations.AddRange(cartonLocationList);
            }

            _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();
        }