public PlasticSeqSchedule GetPlasticSeqScheduleBySequencingStationId(int id)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            MesDataContext dbMes = new MesDataContext();
            PlasticSeqSchedule schedule = null;

            var station = 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 = GetSequencingBuffer(station);

            schedule = new PlasticSeqSchedule
            {
                SequencingStation = station,
            };

            SequencingTrackingItem lastTrackItem = null;
            var tracking = 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 => 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;
                            }
                        }


                        //var unassignedBuffer = tracking.Where(o => o.OrderId <= lastTrackItem.OrderId).OrderBy(f => f.OrderId).Where(x => x.ScheduleNum == -1).ToList();
                        //if (unassignedBuffer.Count() > 0)
                        //{
                        //    var unassignbox = new ScheduleBox(1, 1);
                        //    var items = unassignedBuffer.Select(item => 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;
                        //}

                    }
                }
            }

            //var lastJobId = Int32.MinValue;

            //if (lastItem != null)
            //    lastJobId = lastItem.OrderId;

            //var unassignedBuffer = lineBuffer.Where(i => i.OrderId > lastJobId && i.OrderId < station.NextBox.OrderId).ToList();
            
            //if (unassignedBuffer.Count() > 0)
            //{
            //    var unassignbox = new ScheduleBox(1, 1);

            //    var items = unassignedBuffer.Select(item => 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, msg[0]));
            //        }
            //        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 => 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;

        }
        public PlasticSeqSchedule GetPlasticScheduleHistoryDetail(SequencingStation station, int scheduleNum)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            SequencingScheduleFactory factory = new SequencingScheduleFactory();
            PlasticSeqSchedule plasticHistorySchedule = new PlasticSeqSchedule();

            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 = factory.GetSequencingMessage(station, s.ItemNumber.Trim(), tokens),
                                            Position = (int)s.Position
                                        }).ToList(),
                           }).ToList();

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

            return plasticHistorySchedule;
        }