public TrimSchedule GetTrimSchedule(int id)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            SequencingFactory factory = new SequencingFactory();
            HP_MESDataContext dbMes = new HP_MESDataContext();
            ItemDetailsFactory itemDetailsModel = new ItemDetailsFactory();
            TrimSchedule trimSchedule = null;
            List<Seat> offsetSeats = null;
            List<Seat> currentjobs = null;
            var seqMessageProcessor = new SeqMessageProcessor();

            var seqStations = dbSeq.SequencingStationSettings.Where(i => i.SequencingLocationId == id).ToList();
            if (seqStations.Count() > 0)
            {
                var seqStation = seqStations.FirstOrDefault();
                if (seqStation != null)
                {
                    var station = factory.UpdateNextBoxBySequencingId(seqStation.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 = factory.GetSequencingBuffer(station);

                    if (lineBuffer != null)
                    {
                        var itemsinbox = station.PartsPerBox;
                        var boxperschedule = station.BoxPerSchedule;
                        var itemsinschedule = itemsinbox * boxperschedule;
                        var boxqty = station.BoxQty;

                        trimSchedule = new TrimSchedule();

                        var tracking = factory.GetSequencingHistory(station);
                        if (tracking.Any()) 
                        {
                            var lastTrackItem = tracking.LastOrDefault();
                            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 offsetjobs = (from j in unassignedBuffer
                                                              select new Seat
                                                              {
                                                                  OrderStatus = (int)j.OrderStatus,
                                                                  Complete = false,
                                                                  TrimStyle = GetOptionValue(j.ItemNumber.Trim(), 100),
                                                                  Colour = GetOptionValue(j.ItemNumber.Trim(), 101),
                                                                  OrderNumber = j.OrderNumber == null ? "" : j.OrderNumber.Trim(),
                                                                  ItemNumber = j.ItemNumber.Trim(),
                                                                  ItemType = j.ItemType.Trim(),
                                                                  DescLine1 = j.ItemNumber.Trim(),
                                                                  DescLine2 = seqMessageProcessor.GetSequencingMessage(station, j.ItemNumber.Trim(), tokens),
                                                                  JobId = (int)j.JobId,
                                                                  OrderId = j.OrderId,
                                                                  Item = itemDetailsModel.GetItemInfoByLineId(j.ItemNumber.Trim(), station.Line.Id)
                                                              }).ToList();

                                            offsetSeats = offsetjobs;
                                        }
                                    }

                                }
                            }


                        }


                        if (station.SequencingLocation.ParentId == null)
                        {
                            currentjobs = (from j in lineBuffer.Where(i => i.OrderId >= station.NextBox.OrderId).Take(itemsinschedule).ToList()
                                           select new Seat
                                           {
                                               OrderStatus = (int)j.OrderStatus,
                                               Complete = false,
                                               TrimStyle = GetOptionValue(j.ItemNumber.Trim(), 100),
                                               Colour = GetOptionValue(j.ItemNumber.Trim(), 101),
                                               OrderNumber = j.OrderNumber == null ? "" : j.OrderNumber.Trim(),
                                               ItemNumber = j.ItemNumber.Trim(),
                                               ItemType = j.ItemType.Trim(),
                                               DescLine1 = j.ItemNumber.Trim(),
                                               DescLine2 = seqMessageProcessor.GetSequencingMessage(station, j.ItemNumber.Trim(), tokens),
                                               JobId = (int)j.JobId,
                                               OrderId = j.OrderId,
                                               Item = itemDetailsModel.GetItemInfoByLineId(j.ItemNumber.Trim(), station.Line.Id),
                                           }).OrderBy(s => s.OrderId).ToList();
                        }
                        else //from queue
                        {
                            currentjobs = (from j in lineBuffer.Where(i => i.OrderId >= station.NextBox.OrderId).Take(itemsinschedule).ToList()
                                            select new Seat
                                            {
                                                ScheduleNum = j.ScheduleNum,
                                                BoxNum = j.BoxNum,
                                                OrderStatus = (int)j.OrderStatus,
                                                Complete = false,
                                                TrimStyle = GetOptionValue(j.ItemNumber.Trim(), 100),
                                                Colour = GetOptionValue(j.ItemNumber.Trim(), 101),
                                                OrderNumber = j.OrderNumber == null ? "" : j.OrderNumber.Trim(),
                                                ItemNumber = j.ItemNumber.Trim(),
                                                ItemType = j.ItemType.Trim(),
                                                DescLine1 = j.ItemNumber.Trim(),
                                                DescLine2 = seqMessageProcessor.GetSequencingMessage(station, j.ItemNumber.Trim(), tokens),
                                                JobId = (int)j.JobId,
                                                OrderId = j.OrderId,
                                                Item = itemDetailsModel.GetItemInfoByLineId(j.ItemNumber.Trim(), station.Line.Id),
                                            }).OrderBy(s => s.OrderId).ToList();
                        }


                        if (currentjobs.Count() != itemsinschedule)
                            return null;

                        for(int i=0; i<currentjobs.Count(); i++)
                            currentjobs[i].Index = i;

                        trimSchedule.CurrentSchedule = new TrimScheduleInfo
                        {
                            Complete = false,
                            SeatSchedule = currentjobs,
                            SummaryList = GetSummaryHeaderItems(currentjobs, station.Line.Id),
                            ScheduleStatus = GetTrimScheduleStatus(station, scheduleStatus, currentjobs),
                            OffsetSeats = offsetSeats
                        };

                        return trimSchedule;

                    }
                }
            }
            return null;
        }
        public TrimScheduleInfo GetTrimScheduleHistoryDetail(int locationId, int scheduleNum)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            ItemDetailsFactory itemDetailsModel = new ItemDetailsFactory();
            SequencingFactory factory = new SequencingFactory();
            var seqMessageProcessor = new SeqMessageProcessor();

            var seqStation = dbSeq.SequencingStationSettings.Where(i => i.SequencingLocationId == locationId).FirstOrDefault();
            var tokens = MessageTokensCacheManager.MessageTokens.Where(s => s.LineId == seqStation.LineId || s.LineId == 4).ToList();
            var station = factory.UpdateNextBoxBySequencingId(seqStation.Id);

            TrimScheduleInfo trimHistorySchedule = null;

            if (station != null)
            {
                SequencingTrackingItem lastItem = null;
                var tracking = factory.GetSequencingHistory(station);

                if (tracking.Any())
                {
                    lastItem = tracking.OrderByDescending(i => i.OrderId).First();
                    if (lastItem != null)
                    {
                        var historyjobs = (from t in dbSeq.IWS_GetSequencingDetailHistoryBySeqSettingsIdAndScheduleNum(station.Id, scheduleNum)
                                          select new Seat
                                          {
                                            ScheduleNum = t.ScheduleNum,
                                            BoxNum = t.BoxNum,
                                            Complete = false,
                                            TrimStyle = GetOptionValue(t.ItemNumber.Trim(), 100),
                                            Colour = GetOptionValue(t.ItemNumber.Trim(), 101),
                                            ItemNumber = t.ItemNumber.Trim(),
                                            ItemType = t.ItemType.Trim(),
                                            DescLine1 = t.ItemNumber.Trim(),
                                            DescLine2 = seqMessageProcessor.GetSequencingMessage(station, t.ItemNumber.Trim(), tokens),
                                            OrderId = t.OrderId,
                                            Item = itemDetailsModel.GetItemInfoByLineId(t.ItemNumber.Trim(), station.Line.Id)
                                          }).ToList();

                        var firstItem = historyjobs.FirstOrDefault();

                        trimHistorySchedule = new TrimScheduleInfo
                        {
                            Complete = true,
                            ScheduleStatus = new TrimScheduleStatus
                            {
                                CurrentBoxNumber = firstItem == null ? 0 : firstItem.BoxNum,
                                CurrentScheduleNumber = firstItem == null ? 0 : firstItem.ScheduleNum
                            },
                            SeatSchedule = historyjobs,
                            SummaryList = GetSummaryHeaderItems(historyjobs, station.Line.Id),
                        };
                        return trimHistorySchedule;
                    }

                }

            }
            return trimHistorySchedule;
        }
        public TrimScheduleInfo GetNextTrimSchedule(int locationId)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            SequencingFactory factory = new SequencingFactory();
            HP_MESDataContext dbMes = new HP_MESDataContext();
            ItemDetailsFactory itemDetailsModel = new ItemDetailsFactory();
            TrimScheduleInfo nextTrimScheduleInfo = null;

            var seqMessageProcessor = new SeqMessageProcessor();

            var seqStations = dbSeq.SequencingStationSettings.Where(i => i.SequencingLocationId == locationId).ToList();
            if (seqStations.Count() > 0)
            {
                var seqStation = seqStations.FirstOrDefault();
                if (seqStation != null)
                {
                    var station = factory.UpdateNextBoxBySequencingId(seqStation.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 = factory.GetSequencingBuffer(station);

                    if (lineBuffer != null)
                    {
                        var itemsinbox = station.PartsPerBox;
                        var boxperschedule = station.BoxPerSchedule;
                        var itemsinschedule = itemsinbox * boxperschedule;
                        var boxqty = station.BoxQty;

                        var currentjobs = (from j in lineBuffer.Where(i => i.OrderId >= station.NextBox.OrderId).Take(itemsinschedule).ToList()
                                           select new Seat
                                           {
                                               OrderStatus = (int)j.OrderStatus,
                                               Complete = false,
                                               TrimStyle = GetOptionValue(j.ItemNumber.Trim(), 100),
                                               Colour = GetOptionValue(j.ItemNumber.Trim(), 101),
                                               OrderNumber = j.OrderNumber == null ? "" : j.OrderNumber.Trim(),
                                               ItemNumber = j.ItemNumber.Trim(),
                                               ItemType = j.ItemType.Trim(),
                                               DescLine1 = j.ItemNumber.Trim(),
                                               DescLine2 = seqMessageProcessor.GetSequencingMessage(station, j.ItemNumber.Trim(), tokens),
                                               JobId = (int)j.JobId,
                                               OrderId = j.OrderId,
                                               Item = itemDetailsModel.GetItemInfoByLineId(j.ItemNumber.Trim(), station.Line.Id),
                                           }).OrderBy(s => s.OrderId).ToList();

                        if (currentjobs.Count() != itemsinschedule)
                            return null;

                        var lastcurrentjob = currentjobs.Last();

                        var nextjobs = (from j in lineBuffer.Where(i => i.OrderId > lastcurrentjob.OrderId).Take(itemsinschedule).ToList()
                                        select new Seat
                                        {
                                            OrderStatus = (int)j.OrderStatus,
                                            Complete = false,
                                            TrimStyle = GetOptionValue(j.ItemNumber.Trim(), 100),
                                            Colour = GetOptionValue(j.ItemNumber.Trim(), 101),
                                            OrderNumber = j.OrderNumber == null ? "" : j.OrderNumber.Trim(),
                                            ItemNumber = j.ItemNumber.Trim(),
                                            ItemType = j.ItemType.Trim(),
                                            DescLine1 = j.ItemNumber.Trim(),
                                            DescLine2 = seqMessageProcessor.GetSequencingMessage(station, j.ItemNumber.Trim(), tokens),
                                            JobId = (int)j.JobId,
                                            OrderId = j.OrderId,
                                            Item = itemDetailsModel.GetItemInfoByLineId(j.ItemNumber.Trim(), station.Line.Id)
                                        }).OrderBy(s => s.OrderId).ToList();

                        if (nextjobs.Count() == itemsinschedule)
                        {
                            nextTrimScheduleInfo = new TrimScheduleInfo
                            {
                                Complete = false,
                                SeatSchedule = nextjobs,
                                SummaryList = GetSummaryHeaderItems(nextjobs, station.Line.Id),
                                ScheduleStatus = new TrimScheduleStatus
                                {
                                    CurrentBoxNumber = (scheduleStatus.NextBoxNum + 1) > boxqty ? 1 : scheduleStatus.NextBoxNum + 1,
                                    CurrentScheduleNumber = scheduleStatus.NextScheduleNum + 1
                                }
                            };
                        }
                        else
                        {
                            return null;
                        }

                        for (int i = 0; i < nextjobs.Count(); i++)
                            nextjobs[i].Index = i;

                        return nextTrimScheduleInfo;

                    }
                }
            }
            return null;
        }