public override void Synchronize(Func <LotKey> getInput)
        {
            if (getInput == null)
            {
                throw new ArgumentNullException("getInput");
            }
            var productionBatchKey = getInput();

            var productionBatch = UnitOfWork.ProductionBatchRepository.FindByKey(productionBatchKey,
                                                                                 b => b.Production.Results,
                                                                                 b => b.Production.ResultingChileLot.Lot,
                                                                                 b => b.PackSchedule.PackagingProduct.Product,
                                                                                 b => b.PackSchedule.ChileProduct.Product,
                                                                                 b => b.PackSchedule.ProductionLineLocation,
                                                                                 b => b.PackSchedule.ProductionBatches.Select(p => p.Production.PickedInventory));

            if (productionBatch == null)
            {
                throw new Exception(string.Format("ProductionBatch[{0}] not found.", productionBatchKey));
            }

            var lotNumber = LotNumberBuilder.BuildLotNumber(productionBatch);
            var lot       = OldContext.tblLots.FirstOrDefault(l => l.Lot == lotNumber);

            if (lot == null)
            {
                throw new Exception(string.Format("tblLot[{0}] not found.", lotNumber));
            }
            UpdateLot(lot, productionBatch);

            var oldPackSchedule = OldContext.tblPackSches.FirstOrDefault(p => p.PackSchID == productionBatch.PackSchedule.PackSchID);

            if (oldPackSchedule == null)
            {
                throw new Exception(string.Format("tblPackSch[{0}] could not be found.", productionBatch.PackSchedule.PackSchID));
            }
            oldPackSchedule.Serialized = SerializablePackSchedule.Serialize(productionBatch.PackSchedule);

            var serialized = new SerializedData(OldContext);

            serialized[SerializableType.ChileLotProduction, lotNumber] = new SerializableEmployeeIdentifiable(productionBatch.Production);
            if (productionBatch.Production.Results != null)
            {
                serialized[SerializableType.LotProductionResults, lotNumber] = new SerializableEmployeeIdentifiable(productionBatch.Production.Results);
            }

            OldContext.SaveChanges();

            Console.Write(ConsoleOutput.UpdatedLot, lot.Lot);
        }
        public static void SynchronizeOldContextPackSchedule(tblPackSch oldPackSchedule, PackSchedule newPackSchedule)
        {
            int chileProdId;

            if (!Int32.TryParse(newPackSchedule.ChileProduct.Product.ProductCode, out chileProdId))
            {
                throw new Exception(String.Format("Could not parse chile ProdID[{0}]", newPackSchedule.ChileProduct.Product.ProductCode));
            }

            oldPackSchedule.EmployeeID         = newPackSchedule.EmployeeId;
            oldPackSchedule.ProductionDeadline = newPackSchedule.ProductionDeadline.GetDate();
            oldPackSchedule.PackSchDate        = newPackSchedule.ScheduledProductionDate.Date;

            oldPackSchedule.ProdID         = chileProdId;
            oldPackSchedule.PackSchDesc    = newPackSchedule.SummaryOfWork;
            oldPackSchedule.BatchTypeID    = (int?)BatchTypeIDHelper.GetBatchTypeID(newPackSchedule.WorkType.Description);
            oldPackSchedule.ProductionLine = GetProductionLine(newPackSchedule.ProductionLineLocation.Description);

            oldPackSchedule.TargetWgt = (decimal?)newPackSchedule.DefaultBatchTargetParameters.BatchTargetWeight;
            oldPackSchedule.TgtAsta   = (decimal?)newPackSchedule.DefaultBatchTargetParameters.BatchTargetAsta;
            oldPackSchedule.TgtScan   = (decimal?)newPackSchedule.DefaultBatchTargetParameters.BatchTargetScan;
            oldPackSchedule.TgtScov   = (decimal?)newPackSchedule.DefaultBatchTargetParameters.BatchTargetScoville;

            oldPackSchedule.OrderNumber = newPackSchedule.OrderNumber;
            oldPackSchedule.Company_IA  = newPackSchedule.Customer == null ? null : newPackSchedule.Customer.Company.Name;

            if (oldPackSchedule.tblLots != null)
            {
                foreach (var lot in oldPackSchedule.tblLots)
                {
                    lot.ProdID         = oldPackSchedule.ProdID;
                    lot.ProductionDate = oldPackSchedule.PackSchDate;
                    lot.BatchTypeID    = oldPackSchedule.BatchTypeID;
                    lot.Company_IA     = oldPackSchedule.Company_IA;
                    lot.ProductionLine = oldPackSchedule.ProductionLine;
                }
            }

            oldPackSchedule.Serialized = SerializablePackSchedule.Serialize(newPackSchedule);
        }
Пример #3
0
        private static Dictionary <int, Tuple <DateTime, int> > GetEmployeeIdentifiableByLot(PackScheduleDTO packSchedule)
        {
            if (packSchedule == null || string.IsNullOrWhiteSpace(packSchedule.Serialized))
            {
                return(new Dictionary <int, Tuple <DateTime, int> >());
            }

            try
            {
                var lotKeyParser = new LotKey();
                var employeeKey  = new EmployeeKey();
                return(SerializablePackSchedule.Deserialize(packSchedule.Serialized).Batches.ToDictionary(b =>
                {
                    var lotKey = lotKeyParser.Parse(b.LotKey);
                    return LotNumberBuilder.BuildLotNumber(lotKey).LotNumber;
                }, b => new Tuple <DateTime, int>(b.TimeStamp, employeeKey.Parse(b.EmployeeKey).EmployeeKey_Id)));
            }
            catch (Exception ex)
            {
                throw new Exception("Error parsing PackSchedule deserialized data.", ex);
            }
        }
        private bool ProcessSerializedPackSchedule(PackSchedule packSchedule, PackScheduleDTO oldPackSchedule, out bool isSerialized)
        {
            isSerialized = true;
            int?pkgID;

            if (!SerializablePackSchedule.DeserializeIntoPackSchedule(packSchedule, oldPackSchedule.Serialized, out pkgID))
            {
                isSerialized = false;
                return(false);
            }

            int?batchTypeId;

            if (oldPackSchedule.BatchLots != null && oldPackSchedule.BatchLots.Any())
            {
                batchTypeId = GetSingleBatchTypeID(oldPackSchedule);
                if (batchTypeId == null)
                {
                    Log(new CallbackParameters(CallbackReason.NoSingleBatchType)
                    {
                        PackSchedule = oldPackSchedule
                    });
                    return(false);
                }
            }
            else
            {
                batchTypeId = oldPackSchedule.BatchTypeID.Value;
            }
            packSchedule.WorkTypeId = WorkTypeFactory.BuildWorkTypeFromBatchTypeID(batchTypeId.Value).Id;

            var packagingProduct = _newContextHelper.GetPackagingProduct(pkgID);

            if (packagingProduct != null)
            {
                packSchedule.PackagingProductId = packagingProduct.Id;
            }
            else if (!SetDeterminedPackaging(packSchedule, oldPackSchedule))
            {
                return(false);
            }


            var productionBatches = packSchedule.ProductionBatches.ToList();

            productionBatches.RemoveAll(batch =>
            {
                var lotKey    = new LotKey(batch);
                var lotNumber = LotNumberParser.BuildLotNumber(lotKey);

                List <DateTime?> ids;
                if (_batchItemPackSchIds.TryGetValue(lotNumber, out ids))
                {
                    if (ids.Any())
                    {
                        if (ids.Any(i => i != oldPackSchedule.PackSchID))
                        {
                            Log(new CallbackParameters(CallbackReason.MismatchedBatchItemPackSchID)
                            {
                                PackSchedule = oldPackSchedule,
                                BatchNumber  = lotNumber
                            });
                        }
                        return(true);
                    }
                }

                _loadCount.AddRead(EntityTypes.ProductionBatch);
                _loadCount.AddRead(EntityTypes.Notebook);
                _loadCount.AddRead(EntityTypes.PickedInventory);
                _loadCount.AddRead(EntityTypes.ChileLot);
                _loadCount.AddRead(EntityTypes.Lot);

                if (!_newContextHelper.LotLoaded(lotKey))
                {
                    Log(new CallbackParameters(CallbackReason.BatchLotNotLoaded)
                    {
                        PackSchedule = oldPackSchedule,
                        LotKey       = lotKey
                    });
                    return(true);
                }

                var dateCreated = batch.LotDateCreated.Date;
                var sequence    = PickedInventoryKeyHelper.Singleton.GetNextSequence(dateCreated);

                batch.Production.PickedInventoryDateCreated = batch.Production.PickedInventory.DateCreated = dateCreated;
                batch.Production.PickedInventorySequence    = batch.Production.PickedInventory.Sequence = sequence;

                var notebook = _notebookFactory.BirthNext(batch.TimeStamp);
                batch.InstructionNotebook            = notebook;
                batch.InstructionNotebookDateCreated = notebook.Date;
                batch.InstructionNotebookSequence    = notebook.Sequence;

                var deserializedProduction = _serializedData.GetDeserialized <SerializableEmployeeIdentifiable>(SerializableType.ChileLotProduction, lotNumber.ToString());
                if (deserializedProduction != null)
                {
                    batch.Production.EmployeeId = deserializedProduction.EmployeeKey.EmployeeKeyId;
                    batch.Production.TimeStamp  = deserializedProduction.TimeStamp;
                }

                return(false);
            });
            packSchedule.ProductionBatches = productionBatches;

            return(true);
        }