private tblPackSch CreateNewPackSchedule(PackSchedule packSchedule, bool useSuppliedPSNum)
        {
            var packSchId  = packSchedule.TimeStamp.ConvertLocalToUTC().RoundMillisecondsForSQL();
            var tblPackSch = new tblPackSch
            {
                SerializedKey = packSchedule.ToPackScheduleKey(),
                PackSchID     = packSchId,
                PSNum         = useSuppliedPSNum && packSchedule.PSNum != null ? packSchedule.PSNum.Value :
                                OldContext.tblPackSches.Select(p => p.PSNum).Where(p => p != null).DefaultIfEmpty(0).Max().Value + 1,
                PSStatus   = 1,
                SetTrtmtID = 0
            };

            SynchronizePackScheduleHelper.SynchronizeOldContextPackSchedule(tblPackSch, packSchedule);
            return(tblPackSch);
        }
        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);
        }