Esempio n. 1
0
        private tblMove Synchronize(IKey <InventoryShipmentOrder> orderKey, bool createdNew, out bool commitNewContext)
        {
            var order = UnitOfWork.InventoryShipmentOrderRepository.FindByKey(orderKey,
                                                                              o => o.DestinationFacility,
                                                                              o => o.SourceFacility,
                                                                              o => o.InventoryPickOrder.Items.Select(i => i.Product),
                                                                              o => o.InventoryPickOrder.Items.Select(i => i.Customer.Company),
                                                                              o => o.InventoryPickOrder.Items.Select(i => i.PackagingProduct.Product),
                                                                              o => o.PickedInventory.Items.Select(i => i.PackagingProduct.Product),
                                                                              o => o.PickedInventory.Items.Select(i => i.FromLocation),
                                                                              o => o.PickedInventory.Items.Select(i => i.CurrentLocation),
                                                                              o => o.ShipmentInformation);

            var oldOrder = GetOrCreateOldContextOrder(order, createdNew, out commitNewContext);

            if (order.OrderType == InventoryShipmentOrderTypeEnum.TreatmentOrder)
            {
                var treatmentOrder = UnitOfWork.TreatmentOrderRepository.FindBy(FindTreatmentOrder(orderKey));
                oldOrder.Serialized = SerializableMove.Serialize(treatmentOrder);
            }
            else
            {
                oldOrder.Serialized = SerializableMove.Serialize(order);
            }

            oldOrder.EmployeeID = order.EmployeeId;

            oldOrder.C2WHID = order.DestinationFacility.WHID ?? OldContextHelper.GetWarehouse(order.DestinationFacility.Name).WHID;
            oldOrder.WHID   = order.SourceFacility.WHID ?? OldContextHelper.GetWarehouse(order.SourceFacility.Name).WHID;

            SyncSetShipmentInformation.SetOrderShipment(order, oldOrder);
            SetPickOrder(order, oldOrder);
            if (!order.PickedInventory.Archived)
            {
                SetPickedInventory(order, oldOrder, ref commitNewContext);
            }

            return(oldOrder);
        }
        private IInventoryTreatmentKey DeterminedOrderTreatment(MovementOrderDTO order, SerializableMove deserialized)
        {
            if (deserialized != null && deserialized.TreatmentOrder != null && deserialized.TreatmentOrder.TreatmentKey != null)
            {
                return(deserialized.TreatmentOrder.TreatmentKey);
            }

            int treatmentId;

            if (order.ToWHID == null || !_treatmentIdsByFacility.TryGetValue(order.ToWHID.Value, out treatmentId))
            {
                treatmentId = order.tblOutgoings.Where(o => o.TTypeID == (int)TransType.FrmTrmt)
                              .Select(o => o.TrtmtID)
                              .Distinct().FirstOrDefault(i => i != 0);
                if (treatmentId == 0)
                {
                    return(null);
                }
            }

            return(NewContextHelper.GetInventoryTreatment(treatmentId));
        }
        protected override IEnumerable <TreatmentOrder> BirthRecords()
        {
            foreach (var order in SelectMovementOrdersToLoad())
            {
                var entityType = GetEntityType(order.TTypeID);
                LoadCount.AddRead(entityType);

                if (order.EntryDate == null)
                {
                    Log(new CallbackParameters(CallbackReason.NullEntryDate)
                    {
                        MovementOrder = order
                    });
                    continue;
                }
                var dateCreated = order.EntryDate.Value.ConvertLocalToUTC().Date;

                var sourceFacility = NewContextHelper.GetFacility(order.FromWHID);
                if (sourceFacility == null)
                {
                    Log(new CallbackParameters(CallbackReason.SourceWarehouseNotLoaded)
                    {
                        MovementOrder = order
                    });
                    continue;
                }

                var destinationFacility = NewContextHelper.GetFacility(order.ToWHID);
                if (destinationFacility == null)
                {
                    Log(new CallbackParameters(CallbackReason.TreatmentFaciltyNotLoaded)
                    {
                        MovementOrder = order
                    });
                    continue;
                }
                destinationFacility.FacilityType = FacilityType.Treatment;

                var deserialized = SerializableMove.Deserialize(order.Serialized);

                var treatment = DeterminedOrderTreatment(order, deserialized);
                if (treatment == null)
                {
                    Log(new CallbackParameters(CallbackReason.TreatmentNotDetermined)
                    {
                        MovementOrder = order
                    });
                    continue;
                }

                var shipmentDate = order.Date.GetDate() ?? order.EntryDate.GetDate();
                if (shipmentDate == null)
                {
                    Log(new CallbackParameters(CallbackReason.UndeterminedShipmentDate)
                    {
                        MovementOrder = order
                    });
                    continue;
                }

                var sequence      = PickedInventoryKeyHelper.Singleton.GetNextSequence(dateCreated);
                var shipmentOrder = SetOrderProperties(new InventoryShipmentOrder
                {
                    DateCreated             = dateCreated,
                    Sequence                = sequence,
                    ShipmentInfoDateCreated = dateCreated,
                    ShipmentInfoSequence    = NewContextHelper.ShipmentInformationKeys.GetNextSequence(dateCreated),
                    DestinationFacilityId   = destinationFacility.Id,
                    SourceFacilityId        = sourceFacility.Id
                }, order);
                if (shipmentOrder == null)
                {
                    continue;
                }

                var treatmentOrder = new TreatmentOrder
                {
                    DateCreated = dateCreated,
                    Sequence    = sequence,

                    InventoryTreatmentId   = treatment.InventoryTreatmentKey_Id,
                    InventoryShipmentOrder = shipmentOrder,
                    Returned = order.Returned.ConvertLocalToUTC()
                };
                treatmentOrder.InventoryShipmentOrder.ShipmentInformation = CreateShipmentInformation(treatmentOrder.InventoryShipmentOrder, order);
                treatmentOrder.InventoryShipmentOrder.InventoryPickOrder  = CreateInventoryPickOrder(treatmentOrder.InventoryShipmentOrder, order);
                treatmentOrder.InventoryShipmentOrder.PickedInventory     = CreatePickedInventory(treatmentOrder.InventoryShipmentOrder, order, treatmentOrder.InventoryShipmentOrder.ShipmentInformation.Status);
                if (treatmentOrder.InventoryShipmentOrder.PickedInventory == null)
                {
                    continue;
                }
                treatmentOrder.InventoryShipmentOrder.PickedInventory.Items.ForEach(i => i.TreatmentId = NewContextHelper.NoTreatment.Id);

                if (deserialized != null)
                {
                    deserialized.SetOrder(treatmentOrder);
                }

                LoadCount.AddLoaded(entityType);
                LoadCount.AddLoaded(EntityTypes.InventoryShipmentOrder);
                LoadCount.AddLoaded(EntityTypes.PickOrder);
                LoadCount.AddLoaded(EntityTypes.PickOrderItem, (uint)treatmentOrder.InventoryShipmentOrder.InventoryPickOrder.Items.Count);
                LoadCount.AddLoaded(EntityTypes.PickedInventory);
                LoadCount.AddLoaded(EntityTypes.PickedInventoryItem, (uint)treatmentOrder.InventoryShipmentOrder.PickedInventory.Items.Count);
                LoadCount.AddLoaded(EntityTypes.ShipmentInformation);

                yield return(treatmentOrder);
            }

            LoadCount.LogResults(l => Log(new CallbackParameters(l)));
        }
        protected override IEnumerable <InventoryShipmentOrder> BirthRecords()
        {
            LoadCount.Reset();

            foreach (var order in SelectMovementOrdersToLoad())
            {
                var entityType = GetEntityType(order.TTypeID);
                LoadCount.AddRead(entityType);

                if (order.EntryDate == null)
                {
                    Log(new CallbackParameters(CallbackReason.NullEntryDate)
                    {
                        MovementOrder = order
                    });
                    continue;
                }
                var dateCreated = order.EntryDate.Value.ConvertLocalToUTC().Date;
                var sequence    = PickedInventoryKeyHelper.Singleton.GetNextSequence(dateCreated);

                var sourceWarehouse = NewContextHelper.GetFacility(order.FromWHID);
                if (sourceWarehouse == null)
                {
                    Log(new CallbackParameters(CallbackReason.SourceWarehouseNotLoaded)
                    {
                        MovementOrder = order
                    });
                    continue;
                }

                var destinationWarehouse = NewContextHelper.GetFacility(order.ToWHID);
                if (destinationWarehouse == null)
                {
                    Log(new CallbackParameters(CallbackReason.DestinationWarehouseNotLoaded)
                    {
                        MovementOrder = order
                    });
                    continue;
                }

                var shipmentInformationSequence = NewContextHelper.ShipmentInformationKeys.GetNextSequence(dateCreated);

                var shipmentOrder = SetOrderProperties(new InventoryShipmentOrder
                {
                    DateCreated             = dateCreated,
                    Sequence                = sequence,
                    ShipmentInfoDateCreated = dateCreated,
                    ShipmentInfoSequence    = shipmentInformationSequence,
                    DestinationFacilityId   = destinationWarehouse.Id,
                    SourceFacilityId        = sourceWarehouse.Id
                }, order);
                if (shipmentOrder == null)
                {
                    continue;
                }

                shipmentOrder.ShipmentInformation = CreateShipmentInformation(shipmentOrder, order);
                shipmentOrder.InventoryPickOrder  = CreateInventoryPickOrder(shipmentOrder, order);
                shipmentOrder.PickedInventory     = CreatePickedInventory(shipmentOrder, order, shipmentOrder.ShipmentInformation.Status);
                if (shipmentOrder.PickedInventory == null)
                {
                    continue;
                }

                var deserialized = SerializableMove.Deserialize(order.Serialized);
                if (deserialized != null)
                {
                    deserialized.SetOrder(shipmentOrder);
                }

                LoadCount.AddLoaded(entityType);
                LoadCount.AddLoaded(EntityTypes.InventoryShipmentOrder);
                LoadCount.AddLoaded(EntityTypes.PickOrder);
                LoadCount.AddLoaded(EntityTypes.PickOrderItem, (uint)shipmentOrder.InventoryPickOrder.Items.Count);
                LoadCount.AddLoaded(EntityTypes.PickedInventory);
                LoadCount.AddLoaded(EntityTypes.PickedInventoryItem, (uint)shipmentOrder.PickedInventory.Items.Count);
                LoadCount.AddLoaded(EntityTypes.ShipmentInformation);

                yield return(shipmentOrder);
            }

            LoadCount.LogResults(l => Log(new CallbackParameters(l)));
        }