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;
        }
        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;
        }
Esempio n. 4
0
        private void UpdateNextSeqScheduleSideBySide(SequencingStation station, List<SideBySide> buffer, int side)
        {
            SequencingFactory factory = new SequencingFactory();
            var seqMessageProcessor = new SeqMessageProcessor();

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

            var itemsinbox = station.PartsPerBox;
            var boxperschedule = station.BoxPerSchedule;
            var itemsinschedule = itemsinbox * boxperschedule;

            SideBySide firstItem = null;
            if (side ==1)
                firstItem = buffer.Where(s => s.Complete1 == false).OrderBy(s => s.OrderId).FirstOrDefault();
            else
                firstItem = buffer.Where(s => s.Complete2 == false).OrderBy(s => s.OrderId).FirstOrDefault();

            if (firstItem != null)
            {
                if(side == 1)
                    firstItem.StartDrawSchedule1 = true;
                else
                    firstItem.StartDrawSchedule2 = true;

                var currentBox = station.NextBox.BoxNum;
                var jobs = buffer.Where(s => s.OrderId >= firstItem.OrderId).OrderBy(s => s.OrderId).Take(itemsinschedule).ToList();

                var itemsinscheduleTmp = itemsinschedule;
                var itemsinboxTmp = itemsinbox;
                var boxesperscheduleTmp = boxperschedule;

                foreach (var j in jobs)
                {
                    if (side == 1)
                    {
                        j.ScheduleNum1 = station.NextBox.ScheduleNum;
                        j.ScheduleCount1 = itemsinschedule;
                        j.StartDrawBox1 = true;
                        j.BoxNum1 = currentBox;
                    }
                    else
                    {
                        j.ScheduleNum2 = station.NextBox.ScheduleNum;
                        j.ScheduleCount2 = itemsinschedule;
                        j.StartDrawBox2 = true;
                        j.BoxNum2 = currentBox;
                    }

                    var message = seqMessageProcessor.GetSequencingMessage(station, j.ItemNumber, tokens).Trim();

                    if (message.ToUpper().IndexOf("*SKIPPED*") < 0)
                        --itemsinboxTmp;

                    --itemsinscheduleTmp;

                    if (itemsinboxTmp > 0 && itemsinscheduleTmp > 0) continue;
                    if (itemsinschedule <= 0)
                        break;

                    itemsinboxTmp = station.PartsPerBox;
                    --boxesperscheduleTmp;

                    if (side == 1)
                        j.StartDrawBox1 = false;
                    else
                        j.StartDrawBox2 = false;

                    if (currentBox + 1 > station.BoxQty)
                        currentBox = 1;
                    else
                    {
                        currentBox++;
                    }

                }

                

            }
        }
Esempio n. 5
0
        public JobBuffer ScheduleAdminView(int id)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            HP_MESDataContext dbMes = new HP_MESDataContext();
            ItemDetailsFactory itemDetailsModel = new ItemDetailsFactory();
            var seqMessageProcessor = new SeqMessageProcessor();

            var station = UpdateNextBoxBySequencingId(id);

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

            var lineBuffer = GetAdminSequencingBuffer(station);

            var boxBuffer = new JobBuffer
            {
                CurrentBoxNum = station.NextBox.BoxNum,
                CurrentScheduleNum = station.NextBox.ScheduleNum,
                SequencingStation = station,
                Buffer = new List<SequenceItem>()
            };

            var tracking = GetSequencingHistory(station);

            boxBuffer.Buffer = (lineBuffer.GroupJoin(tracking, jb => jb.OrderId, st => st.OrderId, (jb, g) => new { _jb = jb, g }).
            SelectMany(t => t.g.DefaultIfEmpty(), (t, st) => new SequenceItem
            {
                BoxNum = st == null ? 0 : st.BoxNum,
                OrderId = t._jb.OrderId,
                Complete = true,
                OrderNumber = t._jb.OrderNumber == null ? "" : t._jb.OrderNumber,
                ScheduleNum = st == null ? 0 : st.ScheduleNum,
                ItemNumber = t._jb.ItemNumber,
                JobId = (int)t._jb.JobId,
                Region = t._jb.Region.Trim(),
                Type = st == null ? 0 : (int)st.Type,
                Message = seqMessageProcessor.GetSequencingMessage(station, t._jb.ItemNumber, tokens),
                Item = itemDetailsModel.GetItemInfo(t._jb.ItemNumber)
            })).ToList();

            boxBuffer.Buffer = boxBuffer.Buffer.OrderBy(s => s.OrderId).ToList();

            var itemsinbox = station.PartsPerBox;
            var boxperschedule = station.BoxPerSchedule;
            var itemsintheschedule = itemsinbox * boxperschedule;
            if (station.NextBox != null)
            {

                var currentbox = station.NextBox.BoxNum;

                var jobs = boxBuffer.Buffer.Where(i => i.OrderId >= station.NextBox.OrderId).Take(itemsintheschedule).OrderBy(s => s.OrderId).ToList();

                foreach (var item in jobs)
                {
                    item.Complete = false;
                    item.BoxNum = currentbox;
                    item.ScheduleNum = station.NextBox.ScheduleNum;

                    if (item.Message.ToUpper().IndexOf("*SKIPPED*") < 0)
                    {
                        item.Skipped = false;
                        --itemsinbox;
                    }
                    else
                        item.Skipped = true;


                    if (itemsinbox > 0 && itemsintheschedule > 0) continue;
                    if (itemsintheschedule <= 1)
                    {
                        break;
                    }

                    itemsinbox = station.PartsPerBox;
                    --boxperschedule;
                    currentbox = currentbox >= station.BoxQty ? 1 : ++currentbox;

                }

            }

            var scheduleNum = -1;
            var boxNum = -1;
            var initialJobId = Int32.MinValue;

            for(var i=0;i<boxBuffer.Buffer.Count(); i++) {
                if (i == 0) {
                    initialJobId = boxBuffer.Buffer[i].JobId;
                }

                if (boxBuffer.Buffer[i].ScheduleNum != scheduleNum)
                {
                    
                    boxBuffer.Buffer[i].StartDrawSchedule = true;
                    boxBuffer.Buffer[i].InitialJobId = initialJobId;

                    var countSchedule = 0;
                    for(var j=i; j<boxBuffer.Buffer.Count();j++) {
                        if (boxBuffer.Buffer[j].ScheduleNum == boxBuffer.Buffer[i].ScheduleNum) {
                            countSchedule++;
                        } else {
                            break;
                        }
                    }

                    if (boxBuffer.Buffer[i].Complete)
                        boxBuffer.Buffer[i].ScheduleCount = countSchedule;
                    else
                    {
                        if (countSchedule > itemsintheschedule)
                            boxBuffer.Buffer[i].ScheduleCount = itemsintheschedule;
                        else
                            boxBuffer.Buffer[i].ScheduleCount = countSchedule;
                    }
                    initialJobId = boxBuffer.Buffer[i].JobId;
                    scheduleNum = boxBuffer.Buffer[i].ScheduleNum;
                }
                else
                {
                    boxBuffer.Buffer[i].StartDrawSchedule = false;

                }
            }

            scheduleNum = -1;
            for (var i = 0; i < boxBuffer.Buffer.Count(); i++)
            {
                if (boxBuffer.Buffer[i].BoxNum != boxNum || boxBuffer.Buffer[i].ScheduleNum != scheduleNum)
                {
                    boxBuffer.Buffer[i].StartDrawBox = true;

                    var countBox = 0;
                    for (var j = i; j < boxBuffer.Buffer.Count(); j++)
                    {
                        if (boxBuffer.Buffer[j].BoxNum == boxBuffer.Buffer[i].BoxNum && boxBuffer.Buffer[j].ScheduleNum == boxBuffer.Buffer[i].ScheduleNum)
                        {
                            countBox++;
                        }
                        else
                            break;
                    }
                    boxBuffer.Buffer[i].BoxCount = countBox;
                    boxNum = boxBuffer.Buffer[i].BoxNum;
                    scheduleNum = boxBuffer.Buffer[i].ScheduleNum;
                }
                else
                {
                    boxBuffer.Buffer[i].StartDrawBox = false;
                }
            }
            
            return boxBuffer;

        }
        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 void CompletePlasticSeqByStation(SequencingStation station)
        {
            #region Complete Plastic Seq By Station
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            HP_MESDataContext dbMes = new HP_MESDataContext();
            SequencingFactory seqFactory = new SequencingFactory();
            var seqMessageProcessor = new SeqMessageProcessor();

            var completeDTM = DateTime.Now;

            var lineBuffer = seqFactory.GetSequencingBuffer(station);

            if (station.ScheduleAvailable)
            {
                Log.Logging.Info("station.ScheduleAvailable = true|Seq Id: {0}", station.Id);

                var itemsinbox = station.PartsPerBox;
                var boxperschedule = station.BoxPerSchedule;
                var itemsinschedule = itemsinbox * boxperschedule;

                var currentBox = station.NextBox.BoxNum;

                var jobs = lineBuffer.Where(i => i.OrderId >= station.NextBox.OrderId).Take(itemsinschedule).ToList();
                Log.Logging.Info("Job Count: {0}|Station: {1} Seq Id: {2}", jobs.Count(), station.SequencingLocation.Description, station.Id);
                var tokens = MessageTokensCacheManager.MessageTokens.Where(t => t.LineId == station.Line.Id).ToList();

                SequencingTrackingHistoryDetail histDetail = new SequencingTrackingHistoryDetail
                {
                    BoxNum = currentBox,
                    DTM = completeDTM,
                    ScheduleNum = station.NextBox.ScheduleNum,
                    SequencingStationSettingsId = station.Id,
                    Type = 1
                };

                dbSeq.SequencingTrackingHistoryDetails.InsertOnSubmit(histDetail);
                dbSeq.SubmitChanges();

                Log.Logging.Info("History Header|BoxNum={0}, DTM={1}, ScheduleNum={2}, SeqId={3}, Type={4}", histDetail.BoxNum, histDetail.DTM, histDetail.ScheduleNum, histDetail.SequencingStationSettingsId, histDetail.Type);

                int position = 1;
                foreach (var job in jobs)
                {
                    var message = seqMessageProcessor.GetSequencingMessage(station, job.ItemNumber, tokens).Trim();

                    SequencingTrackingHistory hist = new SequencingTrackingHistory
                    {
                        OrderNumber = job.OrderNumber,
                        ItemNumber = job.ItemNumber,
                        ItemType = job.ItemType,
                        JobId = job.JobId,
                        Position = position,
                        OrderId = job.OrderId,
                        SequencingTrackingHistoryDetail = histDetail,
                    };
                    position++;

                    dbSeq.SequencingTrackingHistories.InsertOnSubmit(hist);

                    Log.Logging.Info("History Detail Item|Job Hist = {0}|Station: {1} Seq Id: {2}", hist, station.SequencingLocation.Description, station.Id);
                    Log.Logging.Info("History Detail Item|OrderNumber={0}, ItemNumber={1}, ItemType={2}, JobId={3}, Position={4}, OrderId={5}", hist.OrderNumber, hist.ItemNumber, hist.ItemType, hist.JobId, hist.Position, hist.OrderId);

                    dbSeq.SubmitChanges();

                    if (message.ToUpper().IndexOf("*SKIPPED*") < 0)
                        --itemsinbox;

                    --itemsinschedule;

                    if (itemsinbox > 0 && itemsinschedule > 0) continue;
                    if (itemsinschedule <= 0)
                        break;

                    itemsinbox = station.PartsPerBox;
                    --boxperschedule;

                    if (currentBox + 1 > station.BoxQty)
                        currentBox = 1;
                    else
                    {
                        currentBox++;
                        position = 1;
                    }

                    histDetail = new SequencingTrackingHistoryDetail
                    {
                        Type = 1,
                        BoxNum = currentBox,
                        DTM = completeDTM,
                        ScheduleNum = station.NextBox.ScheduleNum,
                        SequencingStationSettingsId = station.Id,
                    };

                    Log.Logging.Info("History Detail|BoxNum={0}, DTM={1}, ScheduleNum={2}, SeqId={3}, Type={4}", histDetail.BoxNum, histDetail.DTM, histDetail.ScheduleNum, histDetail.SequencingStationSettingsId, histDetail.Type);

                }


                var lookup = dbSeq.SequencingStationLookups.Where(s => s.SequenceStationSettingsId == station.Id).FirstOrDefault();
                if (lookup != null)
                {
                    if (jobs.Count() > 0)
                    {
                        Log.Logging.Info("Count Jobs={0} SeqId={1}", jobs.Count(), station.Id);
                        var lastJob = jobs.Last();
                        if (lastJob != null)
                        {
                            Log.Logging.Info("Last Jobs: OrderId={0} SeqId={1}", lastJob.OrderId, station.Id);
                            lookup.NextOrderId = lastJob.OrderId + 1;
                            dbSeq.SubmitChanges();
                            Log.Logging.Info("Success Update Lookup: NextOrderId={0} NextScheduleNum={1} NextNextBoxNum={2}", lookup.NextOrderId, lookup.NextScheduleNum, lookup.NextBoxNum);
                        }
                    }
                }

            }
            else
            {
                Log.Logging.Info("station.ScheduleAvailable = false|Seq Id: {0}", station.Id);
            }
            #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
        }