Ejemplo n.º 1
0
        public bool isPickBatchComplete(Int64 ixPickBatch)
        {
            var outboundorderlinesinventoryallocation = _outboundorderlinesinventoryallocationService.IndexDbPost().Where(x => (x.nBaseUnitQuantityAllocated - x.nBaseUnitQuantityPicked) > 0).ToList()
                                                        .Join(getOrderLinesInBatch(ixPickBatch), ol => ol.ixOutboundOrderLine, olb => olb, (ol, olb) => new { Ol = ol, Olb = olb }).ToList();

            if (outboundorderlinesinventoryallocation.Any())
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
        private async Task <DialogTurnResult> PackToHandlingUnitPrompt(
            WaterfallStepContext step,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var nBaseUnitQuantityPicked = step.Result;

            ((PickBatchPickingPost)step.Values[DialogKey]).nBaseUnitQuantityPicked = Convert.ToDouble(nBaseUnitQuantityPicked);

            //Custom Code Start | Added Code Block
            step.Values[IsCompleteInventoryUnitPickKey] = _picking.isCompleteInventoryUnitPick((InventoryUnits)step.Values[PickSuggestionKey], Convert.ToDouble(nBaseUnitQuantityPicked));
            step.Values[IsHandlingUnitPickKey]          = _picking.isHandlingUnitPick((InventoryUnits)step.Values[PickSuggestionKey], Convert.ToDouble(nBaseUnitQuantityPicked));

            //We adjust the queued qty down
            var inventoryUnitPickedFrom = _inventoryunitsService.GetPost(((InventoryUnits)step.Values[PickSuggestionKey]).ixInventoryUnit);

            inventoryUnitPickedFrom.nBaseUnitQuantityQueued -= (double)step.Values[PickSuggestionQtyKey];
            inventoryUnitPickedFrom.UserName = step.Context.Activity.Conversation.Id;
            await _inventoryunitsService.Edit(inventoryUnitPickedFrom, _commonLookUps.getInventoryUnitTransactionContext().Where(x => x.sInventoryUnitTransactionContext == "Inventory Adjustment").Select(x => x.ixInventoryUnitTransactionContext).FirstOrDefault());

            var currentBotUserData = await _botSpielUserStateAccessors.BotUserDataAccessor.GetAsync(step.Context, () => _botUserData);

            //If we pick complete inventory unit we move it to the picker else we adjust and create/consolidate and then move to the picker
            if ((bool)step.Values[IsCompleteInventoryUnitPickKey])
            {
                //We now create and execute the move queue for the pickup
                step.Values[PickedInventoryUnitKey] = ((InventoryUnits)step.Values[PickSuggestionKey]).ixInventoryUnit;
                MoveQueuesPost moveQueuePost = new MoveQueuesPost();
                if (_picking.isHandlingUnitPick((InventoryUnits)step.Values[PickSuggestionKey], Convert.ToDouble(nBaseUnitQuantityPicked)))
                {
                    moveQueuePost.ixMoveQueueType = _commonLookUps.getMoveQueueTypes().Where(x => x.sMoveQueueType == "Unit Pickup - Consolidated Drop").Select(x => x.ixMoveQueueType).FirstOrDefault();
                }
                else
                {
                    moveQueuePost.ixMoveQueueType = _commonLookUps.getMoveQueueTypes().Where(x => x.sMoveQueueType == "Consolidated Pickup - Consolidated Drop").Select(x => x.ixMoveQueueType).FirstOrDefault();
                }
                moveQueuePost.ixMoveQueueContext        = _commonLookUps.getMoveQueueContexts().Where(x => x.sMoveQueueContext == "Picking").Select(x => x.ixMoveQueueContext).FirstOrDefault();
                moveQueuePost.ixSourceInventoryUnit     = ((InventoryUnits)step.Values[PickSuggestionKey]).ixInventoryUnit;
                moveQueuePost.ixTargetInventoryUnit     = ((InventoryUnits)step.Values[PickSuggestionKey]).ixInventoryUnit;
                moveQueuePost.ixSourceInventoryLocation = ((InventoryUnits)step.Values[PickSuggestionKey]).ixInventoryLocation;
                moveQueuePost.ixTargetInventoryLocation = currentBotUserData.ixInventoryLocation;
                moveQueuePost.ixSourceHandlingUnit      = ((InventoryUnits)step.Values[PickSuggestionKey]).ixHandlingUnit > 0 ? ((InventoryUnits)step.Values[PickSuggestionKey]).ixHandlingUnit : null;
                moveQueuePost.ixTargetHandlingUnit      = ((InventoryUnits)step.Values[PickSuggestionKey]).ixHandlingUnit > 0 ? ((InventoryUnits)step.Values[PickSuggestionKey]).ixHandlingUnit : null;
                moveQueuePost.sPreferredResource        = step.Context.Activity.Conversation.Id;
                moveQueuePost.nBaseUnitQuantity         = Convert.ToDouble(nBaseUnitQuantityPicked);
                moveQueuePost.dtStartedAt = DateTime.Now;
                moveQueuePost.ixPickBatch = ((PickBatchPickingPost)step.Values[DialogKey]).ixPickBatch;
                moveQueuePost.ixStatus    = _commonLookUps.getStatuses().Where(x => x.sStatus == "Active").Select(x => x.ixStatus).FirstOrDefault();
                moveQueuePost.UserName    = step.Context.Activity.Conversation.Id;
                var ixMoveQueue = await _movequeuesService.Create(moveQueuePost);

                //We now complete the move queue for the pickup
                var moveQueuePickUp = _movequeuesService.GetPost(ixMoveQueue);
                moveQueuePickUp.dtCompletedAt = DateTime.Now;
                moveQueuePickUp.ixStatus      = _commonLookUps.getStatuses().Where(x => x.sStatus == "Complete").Select(x => x.ixStatus).FirstOrDefault();
                moveQueuePickUp.UserName      = moveQueuePost.UserName;
                await _movequeuesService.Edit(moveQueuePickUp);
            }
            else
            {
                //Now we adjust the iu remaining down
                inventoryUnitPickedFrom.nBaseUnitQuantity -= Convert.ToDouble(nBaseUnitQuantityPicked);
                inventoryUnitPickedFrom.UserName           = step.Context.Activity.Conversation.Id;
                await _inventoryunitsService.Edit(inventoryUnitPickedFrom, _commonLookUps.getInventoryUnitTransactionContext().Where(x => x.sInventoryUnitTransactionContext == "Inventory Adjustment").Select(x => x.ixInventoryUnitTransactionContext).FirstOrDefault());

                //Now we either create or edit an iu for the qty picked
                inventoryUnitPickedFrom.nBaseUnitQuantity   = Convert.ToDouble(nBaseUnitQuantityPicked);
                inventoryUnitPickedFrom.UserName            = step.Context.Activity.Conversation.Id;
                inventoryUnitPickedFrom.ixInventoryLocation = currentBotUserData.ixInventoryLocation;

                if (_inventoryunitsService.IndexDb().Where(x =>
                                                           x.ixFacility == inventoryUnitPickedFrom.ixFacility &&
                                                           x.ixCompany == inventoryUnitPickedFrom.ixCompany &&
                                                           x.ixMaterial == inventoryUnitPickedFrom.ixMaterial &&
                                                           x.ixInventoryState == inventoryUnitPickedFrom.ixInventoryState &&
                                                           x.ixHandlingUnit == inventoryUnitPickedFrom.ixHandlingUnit &&
                                                           x.ixInventoryLocation == inventoryUnitPickedFrom.ixInventoryLocation &&
                                                           x.sBatchNumber == inventoryUnitPickedFrom.sBatchNumber &&
                                                           x.dtExpireAt == inventoryUnitPickedFrom.dtExpireAt && x.ixStatus == 5
                                                           ).Select(x => x.ixInventoryUnit).Any()
                    )
                {
                    //We edit the iu
                    inventoryUnitPickedFrom.ixInventoryUnit = _inventoryunitsService.IndexDb().Where(x =>
                                                                                                     x.ixFacility == inventoryUnitPickedFrom.ixFacility &&
                                                                                                     x.ixCompany == inventoryUnitPickedFrom.ixCompany &&
                                                                                                     x.ixMaterial == inventoryUnitPickedFrom.ixMaterial &&
                                                                                                     x.ixInventoryState == inventoryUnitPickedFrom.ixInventoryState &&
                                                                                                     x.ixHandlingUnit == inventoryUnitPickedFrom.ixHandlingUnit &&
                                                                                                     x.ixInventoryLocation == inventoryUnitPickedFrom.ixInventoryLocation &&
                                                                                                     x.sBatchNumber == inventoryUnitPickedFrom.sBatchNumber &&
                                                                                                     x.dtExpireAt == inventoryUnitPickedFrom.dtExpireAt && x.ixStatus == 5
                                                                                                     ).Select(x => x.ixInventoryUnit).FirstOrDefault();
                    inventoryUnitPickedFrom.nBaseUnitQuantity = _inventoryunitsService.GetPost(inventoryUnitPickedFrom.ixInventoryUnit).nBaseUnitQuantity + Convert.ToDouble(nBaseUnitQuantityPicked);
                    await _inventoryunitsService.Edit(inventoryUnitPickedFrom, _commonLookUps.getInventoryUnitTransactionContext().Where(x => x.sInventoryUnitTransactionContext == "Inventory Adjustment").Select(x => x.ixInventoryUnitTransactionContext).FirstOrDefault());

                    step.Values[PickedInventoryUnitKey] = inventoryUnitPickedFrom.ixInventoryUnit;
                }
                else
                {
                    //We create an iu
                    step.Values[PickedInventoryUnitKey] = await _inventoryunitsService.Create(inventoryUnitPickedFrom, _commonLookUps.getInventoryUnitTransactionContext().Where(x => x.sInventoryUnitTransactionContext == "Inventory Adjustment").Select(x => x.ixInventoryUnitTransactionContext).FirstOrDefault());
                }
            }

            //We now allocate the picked qty to open allocation lines

            step.Values[LineQtysPickedKey] = new List <Tuple <Int64, double> >();

            var pickedQtyNotAllocated = Convert.ToDouble(nBaseUnitQuantityPicked);

            _outboundorderlinesinventoryallocationService.IndexDbPost().Where(x => (x.nBaseUnitQuantityAllocated - x.nBaseUnitQuantityPicked) > 0).OrderBy(x => (x.nBaseUnitQuantityAllocated - x.nBaseUnitQuantityPicked) > 0).ToList()
            .Join(_picking.getOrderLinesInBatchForMaterial(((PickBatchPickingPost)step.Values[DialogKey]).ixPickBatch, ((InventoryUnits)step.Values[PickSuggestionKey]).ixMaterial), ol => ol.ixOutboundOrderLine, olb => olb, (ol, olb) => new { Ol = ol, Olb = olb }).ToList()
            .ForEach(y =>
            {
                if ((y.Ol.nBaseUnitQuantityAllocated - y.Ol.nBaseUnitQuantityPicked) <= pickedQtyNotAllocated && pickedQtyNotAllocated > 0)
                {
                    ((List <Tuple <Int64, double> >)step.Values[LineQtysPickedKey]).Add(new Tuple <long, double>(y.Ol.ixOutboundOrderLine, y.Ol.nBaseUnitQuantityAllocated - y.Ol.nBaseUnitQuantityPicked));
                    y.Ol.nBaseUnitQuantityPicked += y.Ol.nBaseUnitQuantityAllocated - y.Ol.nBaseUnitQuantityPicked;
                    pickedQtyNotAllocated        -= y.Ol.nBaseUnitQuantityAllocated - y.Ol.nBaseUnitQuantityPicked;
                    y.Ol.UserName = step.Context.Activity.Conversation.Id;
                    _outboundorderlinesinventoryallocationService.Edit(y.Ol);
                }
                else if ((y.Ol.nBaseUnitQuantityAllocated - y.Ol.nBaseUnitQuantityPicked) > pickedQtyNotAllocated && pickedQtyNotAllocated > 0)
                {
                    ((List <Tuple <Int64, double> >)step.Values[LineQtysPickedKey]).Add(new Tuple <long, double>(y.Ol.ixOutboundOrderLine, pickedQtyNotAllocated));
                    y.Ol.nBaseUnitQuantityPicked += pickedQtyNotAllocated;
                    pickedQtyNotAllocated        -= pickedQtyNotAllocated;
                    y.Ol.UserName = step.Context.Activity.Conversation.Id;
                    _outboundorderlinesinventoryallocationService.Edit(y.Ol);
                }
            }
                     );

            //Custom Code End

            //We have to figure out whether this is a pick of a complete Handling Unit.
            //Custom Code Start | Replaced Code Block
            //Replaced Code Block Start
            //return await step.PromptAsync(
            //    PackToHandlingUnitPromptId,
            //    new PromptOptions
            //    {
            //        Prompt = MessageFactory.Text($"Please enter a PackToHandlingUnit:"),
            //        RetryPrompt = MessageFactory.Text("I didn't understand. Please try again."),
            //    },
            //    cancellationToken);
            //Replaced Code Block End
            if ((bool)step.Values[IsHandlingUnitPickKey])
            {
                ((PickBatchPickingPost)step.Values[DialogKey]).sPackToHandlingUnit = ((InventoryUnits)step.Values[PickSuggestionKey]).HandlingUnits.sHandlingUnit;
                ((PickBatchPickingPost)step.Values[DialogKey]).ixHandlingUnit      = ((InventoryUnits)step.Values[PickSuggestionKey]).HandlingUnits.ixHandlingUnit;

                //We create the packing record
                var orderLinesToPack = ((List <Tuple <Int64, double> >)step.Values[LineQtysPickedKey]).GroupBy(x => x.Item1).Select(x => new { ixOutboundOrderLine = x.Key, Total = x.Sum(s => s.Item2) }).ToList();

                orderLinesToPack.ForEach(x =>
                {
                    //We check if there is an existing pack record we can use
                    if (
                        _outboundorderlinepackingService.IndexDb().Where(p =>
                                                                         p.ixHandlingUnit == ((PickBatchPickingPost)step.Values[DialogKey]).ixHandlingUnit &&
                                                                         p.ixOutboundOrderLine == x.ixOutboundOrderLine
                                                                         ).Any()
                        )
                    {
                        var outboundorderlinepack = _outboundorderlinepackingService.GetPost(_outboundorderlinepackingService.IndexDb().Where(p =>
                                                                                                                                              p.ixHandlingUnit == ((PickBatchPickingPost)step.Values[DialogKey]).ixHandlingUnit &&
                                                                                                                                              p.ixOutboundOrderLine == x.ixOutboundOrderLine
                                                                                                                                              ).Select(p => p.ixOutboundOrderLinePack).FirstOrDefault()
                                                                                             );
                        outboundorderlinepack.nBaseUnitQuantityPacked += x.Total;
                        outboundorderlinepack.UserName = step.Context.Activity.Conversation.Id;
                        _outboundorderlinepackingService.Edit(outboundorderlinepack);
                    }
                    else
                    {
                        var outboundorderlinepack = new OutboundOrderLinePackingPost();
                        outboundorderlinepack.ixOutboundOrderLine     = x.ixOutboundOrderLine;
                        outboundorderlinepack.ixHandlingUnit          = ((PickBatchPickingPost)step.Values[DialogKey]).ixHandlingUnit;
                        outboundorderlinepack.nBaseUnitQuantityPacked = x.Total;
                        outboundorderlinepack.ixStatus = _commonLookUps.getStatuses().Where(s => s.sStatus == "Active").Select(s => s.ixStatus).FirstOrDefault();
                        outboundorderlinepack.UserName = step.Context.Activity.Conversation.Id;
                        _outboundorderlinepackingService.Create(outboundorderlinepack);
                    }
                }
                                         );

                return(await step.EndDialogAsync(
                           (PickBatchPickingPost)step.Values[DialogKey],
                           cancellationToken));
            }
            else
            {
                return(await step.PromptAsync(
                           PackToHandlingUnitPromptId,
                           new PromptOptions
                {
                    //Custom Code Start | Replaced Code Block
                    //Replaced Code Block Start
                    //Prompt = MessageFactory.Text($"Please enter a PackToHandlingUnit:"),
                    //Replaced Code Block End
                    Prompt = MessageFactory.Text($"Please enter/scan the Pack To Handling Unit:"),
                    //Custom Code End
                    RetryPrompt = MessageFactory.Text("I didn't understand. Please try again."),
                },
                           cancellationToken));
            }
            //Custom Code End
        }