public PlasticSeqSchedule GetPlasticSeqScheduleBySequencingStationId(int id)
        {
            #region Get Plastic Seq Schedule By Sequencing Station Id
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            HP_MESDataContext dbMes = new HP_MESDataContext();
            SequencingFactory seqFactory = new SequencingFactory();
            var seqMessageProcessor = new SeqMessageProcessor();


            PlasticSeqSchedule schedule = null;

            var station = seqFactory.UpdateNextBoxBySequencingId(id);
            var scheduleStatus = dbSeq.SequencingStationLookups.Where(s => s.SequenceStationSettingsId == station.Id).FirstOrDefault();

            var tokens = MessageTokensCacheManager.MessageTokens.Where(s => s.LineId == station.Line.Id || s.LineId == 4).ToList();

            var lineBuffer = seqFactory.GetSequencingBuffer(station);

            schedule = new PlasticSeqSchedule
            {
                SequencingStation = station,
            };

            SequencingTrackingItem lastTrackItem = null;
            var tracking = seqFactory.GetSequencingHistory(station);

            if (tracking.Any())
            {
                lastTrackItem = tracking.OrderByDescending(i => i.OrderId).FirstOrDefault();
                if (lastTrackItem != null)
                {
                    if (lastTrackItem.Type == 2)
                    {
                        var lastRealJob = tracking.Where(o => o.OrderId < lastTrackItem.OrderId).OrderByDescending(f => f.OrderId).Where(x => x.ScheduleNum > 0).FirstOrDefault();

                        if (lastRealJob != null)
                        {
                            var unassignedBuffer = tracking.Where(o => o.OrderId <= lastTrackItem.OrderId && o.OrderId > lastRealJob.OrderId).OrderBy(f => f.OrderId).ToList();
                            if (unassignedBuffer.Count() > 0)
                            {
                                var unassignbox = new ScheduleBox(1, 1);
                                var items = unassignedBuffer.Select(item => seqMessageProcessor.GetSequencingMessage(station, item.ItemNumber, tokens).Trim());
                                int unassignPosition = 1;
                                foreach (var message in items)
                                {
                                    if (message.ToUpper().IndexOf("*SKIPPED*") < 0)
                                    {
                                        //string[] msg = message.Split(',');
                                        unassignbox.Items.Add(new ScheduleItem(unassignPosition, message));
                                        unassignPosition++;
                                    }

                                }
                                schedule.OffsetBoxes = unassignbox;
                            }
                        }


                    }
                }
            }


            if (lineBuffer != null)
            {
                //check for not assigned parts for schedule
                var itemsinbox = station.PartsPerBox;
                var boxperschedule = station.BoxPerSchedule;
                var itemsinschedule = itemsinbox * boxperschedule;

                var jobs = lineBuffer.Where(i => i.OrderId >= station.NextBox.OrderId).Take(itemsinschedule).ToList();
                var boxPosition = 1;
                var boxperpage = boxperschedule;

                do
                {
                    var currentbox = station.NextBox.BoxNum;
                    var box = new ScheduleBox(boxPosition, currentbox);

                    var items = jobs.Select(item => seqMessageProcessor.GetSequencingMessage(station, item.ItemNumber.Trim(), tokens).Trim());
                    if (!items.Any())
                        break;


                    foreach (var message in items)
                    {
                        if (message.ToUpper().IndexOf("*SKIPPED*") < 0)
                        {
                            box.Items.Add(new ScheduleItem((station.PartsPerBox - itemsinbox + 1), message));
                            --itemsinbox;
                        }

                        --itemsinschedule;
                        if (itemsinbox > 0 && itemsinschedule > 0) continue;
                        itemsinbox = station.PartsPerBox;
                        currentbox = currentbox >= station.BoxQty ? 1 : (++currentbox);
                        schedule.Boxes.Add(box);
                        --boxperschedule;
                        if (boxperschedule > 0 && itemsinschedule > 0)
                        {
                            --boxperpage;
                            boxPosition++;
                            box = new ScheduleBox(boxPosition, currentbox);
                        }
                        else
                        {
                            boxperpage = 0;
                            boxPosition = 1;
                            break;
                        }
                    }
                } while (boxperpage > 1);

            }

            return schedule;
            #endregion
        }
        public PlasticSeqSchedule GetPlasticSeqScheduleHistoryDetail(SequencingStation station, int scheduleNum)
        {
            #region Get Plastic Seq Schedule History Detail
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            SequencingFactory factory = new SequencingFactory();
            PlasticSeqSchedule plasticHistorySchedule = new PlasticSeqSchedule();
            var seqMessageProcessor = new SeqMessageProcessor();

            var tokens = MessageTokensCacheManager.MessageTokens.Where(t => t.LineId == station.Line.Id || t.LineId == 4).ToList();

            var boxHistory = (from h in dbSeq.IWS_GetSequencingDetailHistoryBySeqSettingsIdAndScheduleNum(station.Id, scheduleNum)
                              group h by new
                              {
                                  h.ScheduleNum,
                                  h.BoxNum
                              } into grp
                              select new ScheduleBox
                              {
                                  BoxNum = grp.Key.BoxNum,
                                  Items = (from s in grp.ToList()
                                           select new ScheduleItem
                                           {
                                               Message = seqMessageProcessor.GetSequencingMessage(station, s.ItemNumber.Trim(), tokens),
                                               Position = (int)s.Position
                                           }).ToList(),
                              }).ToList();

            plasticHistorySchedule = new PlasticSeqSchedule
            {
                SequencingStation = station,
                Boxes = boxHistory,
                Status = true,
            };

            return plasticHistorySchedule;
            #endregion
        }