Esempio n. 1
0
        public void UpdateSequencingStationSettingsById(SequencingStation newSetting)
        {
            PartSequencingDataContext db = new PartSequencingDataContext();
            var setting = (from s in db.SequencingStationSettings where s.Id == newSetting.Id select s).Single();
            setting.BoxPerSchedule = newSetting.BoxPerSchedule;
            setting.BoxQty = newSetting.BoxQty;
            setting.Description = newSetting.Description;
            setting.MessageFormat = newSetting.MessageFormat;
            setting.Obsolete = newSetting.Obsolete;
            setting.PartsPerBox = newSetting.PartsPerBox;
            setting.SeatOptions = newSetting.SeatOptions;
            setting.SeatOptions2Check = newSetting.SeatOptions2Check;
            setting.StationOrder = newSetting.StationOrder;
            setting.Offset = newSetting.Offset;
            db.SubmitChanges();

            SequencingSettingsCacheManager.RefreshSequencingStationSettings();
        }
        public void CompleteTrimSchedule(TrimScheduleInfo schedule, int id, int employeeId)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            EmployeeDatabase dbEmp = new EmployeeDatabase();
            SequencingFactory factory = new SequencingFactory();

            var employee = dbEmp.GetEmployeeInfo(employeeId);
            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 statusLookup = dbSeq.SequencingStationLookups.Where(i => i.SequenceStationSettingsId == seqStation.Id).FirstOrDefault();

                    int boxNum = schedule.ScheduleStatus.CurrentBoxNumber;
                    int scheduleNum = schedule.ScheduleStatus.CurrentScheduleNumber;

                    //check if job completed already
                    bool isComplete = false;
                    var isCompleteResult = dbSeq.IsSequencingScheduleComplete(seqStation.Id, boxNum, scheduleNum);
                    if (isCompleteResult.HasValue == true) // it's null
                        isComplete = (bool)isCompleteResult;

                    if (isComplete)
                    {
                        throw new HttpResponseException(
                            HttpErrorResponse.GetHttpErrorResponse(
                            HttpStatusCode.NotFound, "Trim Schedule Already Completed", string.Format("Trim Schedule {0} Already Completed", scheduleNum)));
                    }

                    if (schedule.ScheduleStatus.CurrentBoxNumber == statusLookup.NextBoxNum
                        && schedule.ScheduleStatus.CurrentScheduleNumber == statusLookup.NextScheduleNum)
                    {
                        var lastjob = schedule.SeatSchedule.Last();
                        var firstjob = schedule.SeatSchedule.First();

                        //get schedule parameters by station
                        var itemsinbox = station.PartsPerBox;
                        var boxperschedule = station.BoxPerSchedule;
                        var itemsinschedule = itemsinbox * boxperschedule;

                        //get sequencing buffer
                        var lineBuffer = factory.GetSequencingBuffer(station);
                        //filter sequencing buffer by last job and first job
                        var validBuffer = lineBuffer.Where(i => i.OrderId <= lastjob.OrderId && i.OrderId >= firstjob.OrderId).OrderBy(s => s.OrderId).ToList();

                        if (validBuffer.Count() == itemsinschedule) //check if there is enough seats to fulfill schedule
                        {
                            if (schedule.OffsetSeats != null)
                            {
                                if (schedule.OffsetSeats.Count() > 0)
                                {

                                }
                            }


                            //save history
                            SequencingTrackingHistoryDetail histDetail = new SequencingTrackingHistoryDetail
                            {
                                BoxNum = schedule.ScheduleStatus.CurrentBoxNumber,
                                DTM = DateTime.Now,
                                ScheduleNum = schedule.ScheduleStatus.CurrentScheduleNumber,
                                SequencingStationSettingsId = station.Id,
                                Type = 1
                            };
                            dbSeq.SequencingTrackingHistoryDetails.InsertOnSubmit(histDetail);

                            foreach (var seat in schedule.SeatSchedule)
                            {
                                SequencingTrackingHistory hist = new SequencingTrackingHistory
                                {
                                    Position = seat.Index + 1,
                                    OrderId = seat.OrderId,
                                    ItemNumber = seat.ItemNumber,
                                    OrderNumber = seat.OrderNumber,
                                    ItemType = seat.ItemType,
                                    JobId = seat.JobId,
                                    SequencingTrackingHistoryDetail = histDetail,
                                };

                                dbSeq.SequencingTrackingHistories.InsertOnSubmit(hist);
                            }

                            //queue station items
                            if ((bool)station.SequencingLocation.QueueItems)
                            {
                                var queueStations = dbSeq.SequencingLocations.Where(l => l.ParentId == station.SequencingLocation.Id).ToList();

                                if (queueStations.Count() > 0)
                                {
                                    foreach (var st in queueStations)
                                    {
                                        var queueItems = (from s in schedule.SeatSchedule
                                                          select new SequencingStationQueue
                                                          {
                                                              OrderNumber = s.OrderNumber == null ? "" : s.OrderNumber.Trim(),
                                                              BoxNum = schedule.ScheduleStatus.CurrentBoxNumber,
                                                              InsertedDTM = DateTime.Now,
                                                              UpdatedDTM = DateTime.Now,
                                                              ItemNumber = s.ItemNumber.Trim(),
                                                              ItemType = s.ItemType.Trim(),
                                                              JobId = s.JobId,
                                                              OrderId = s.OrderId,
                                                              Region = "",
                                                              ScheduleNum = schedule.ScheduleStatus.CurrentScheduleNumber,
                                                              SequencingLocationId = st.Id,
                                                              OrderStatus = (byte)s.OrderStatus,
                                                          }).ToList();

                                        dbSeq.SequencingStationQueues.InsertAllOnSubmit(queueItems);
                                        dbSeq.SubmitChanges();
                                    }
                                }
                            }

                            //no parent station delete from the queue table
                            if (station.SequencingLocation.ParentId != null)
                            {
                                var deleteSeats = dbSeq.SequencingStationQueues.Where(i => i.OrderId <= lastjob.OrderId && i.OrderId >= firstjob.OrderId && i.SequencingLocationId == station.SequencingLocation.Id).OrderBy(s => s.OrderId).ToList();
                                dbSeq.SequencingStationQueues.DeleteAllOnSubmit(deleteSeats);
                                dbSeq.SubmitChanges();
                            }

                            var scheduleHist = new SequencingStationScheduleHistory
                            {
                                EmployeeId = employeeId,
                                EmployeeName = employee == null ? "Unknown Employee" : employee.Name.Trim(),
                                ScheduleNum = schedule.ScheduleStatus.CurrentScheduleNumber,
                                SequencingLocationId = id,
                                DateCompleted = DateTime.Now,
                            };
                            dbSeq.SequencingStationScheduleHistories.InsertOnSubmit(scheduleHist);

                            var nextjob = lineBuffer.Where(i => i.OrderId > lastjob.OrderId).OrderBy(s => s.OrderId).FirstOrDefault();
                            statusLookup.NextBoxNum = schedule.ScheduleStatus.CurrentBoxNumber + 1;
                            statusLookup.NextOrderId = nextjob == null ? statusLookup.NextOrderId + 1 : nextjob.OrderId;
                            statusLookup.NextScheduleNum = schedule.ScheduleStatus.CurrentScheduleNumber + 1;
                            
                            dbSeq.SubmitChanges();

                            //if (id == 7) //first row complete schedule
                            //{
                            //    CompleteTrimSchedule(schedule, 8, employeeId);
                            //}
                        }
                    }
                }
            }
        }
Esempio n. 3
0
 public void CreateMessageToken(MessageToken token)
 {
     PartSequencingDataContext db = new PartSequencingDataContext();
     db.MessageTokens.InsertOnSubmit(token);
     db.SubmitChanges();
     MessageTokensCacheManager.RefreshMessageTokens();
 }
Esempio n. 4
0
 public void DeleteMessageTokenById(int id)
 {
     PartSequencingDataContext db = new PartSequencingDataContext();
     var token = db.MessageTokens.Where(s => s.Id == id).FirstOrDefault();
     if (token != null)
     {
         db.MessageTokens.DeleteOnSubmit(token);
         db.SubmitChanges();
     }
     MessageTokensCacheManager.RefreshMessageTokens();
 }
        public void CompleteSequencingStation(int id)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            MesDataContext dbMes = new MesDataContext();

            var station = UpdateNextBoxBySequencingId(id);
            var completeDTM = DateTime.Now;

            var lookup = dbSeq.SequencingStationLookups.Where(s => s.SequenceStationSettingsId == station.Id).Single();

            var lineBuffer = GetSequencingBuffer(station);

            if (station.ScheduleAvailable)
            {
                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();

                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();

                int position = 1;
                foreach (var job in jobs)
                {
                    var message = 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);
                    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,
                    };

                }

                lookup.NextOrderId = jobs.Last().OrderId + 1;
                dbSeq.SubmitChanges();

            }
        }
Esempio n. 6
0
 public void UpdateMessageToken(MessageToken token)
 {
     PartSequencingDataContext db = new PartSequencingDataContext();
     db.MessageTokens.Attach(token);
     db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, token);
     db.SubmitChanges();
     MessageTokensCacheManager.RefreshMessageTokens();
 }
        public void CompleteSequencingByAisleId(int id, int employeeId)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            EmployeeDatabase dbEmp = new EmployeeDatabase();
            PlasticSeqDatabase db = new PlasticSeqDatabase();
            var seqStations = db.GetSequencingStationsBySequencingLocationId(id);

            int scheduleNum = 0;
            if (seqStations.Count() > 0)
            {
                var first = seqStations.FirstOrDefault();
                if (first != null)
                {
                    scheduleNum = first.NextBox.ScheduleNum;
                }
            }

            foreach (var station in seqStations)
                CompleteSequencingStation(station.Id);

            var employee = dbEmp.GetEmployeeInfo(employeeId);

            var scheduleHist = new SequencingStationScheduleHistory
            {
                EmployeeId = employeeId,
                EmployeeName = employee == null ? "Unknown Employee" : employee.Name.Trim(),
                ScheduleNum = scheduleNum,
                SequencingLocationId = id,
                DateCompleted = DateTime.Now,
            };

            dbSeq.SequencingStationScheduleHistories.InsertOnSubmit(scheduleHist);
            dbSeq.SubmitChanges();
        }
        public SequencingStation UpdateNextBoxBySequencingId(int id)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            MesDataContext dbMes = new MesDataContext();
            PlasticSeqDatabase dbPlasticSeq = new PlasticSeqDatabase();

            SequencingTrackingItem lastItem = null;
            SequencingTrackingItem firstItem = null;

            var station = (from s in SequencingSettingsCacheManager.SequencingStationSettings
                           where s.Obsolete == false && s.Id == id
                           select new SequencingStation
                           {
                               Description = s.Description,
                               ScheduleAvailable = false,
                               ItemsWaiting = 0,
                               ItemsWaitingPercentage = 0,
                               Id = s.Id,
                               SequencingLocation = dbSeq.SequencingLocations.Where(x => x.Id == s.SequencingLocationId).Single(),
                               Line = dbSeq.Lines.Where(x => x.Id == s.LineId).Single(),
                               Station = dbPlasticSeq.GetStation((int)s.StationId),
                               StationOrder = (int)s.StationOrder,
                               BoxPerSchedule = (int)s.BoxPerSchedule,
                               BoxQty = (int)s.BoxQty,
                               MessageFormat = s.MessageFormat,
                               Obsolete = (bool)s.Obsolete,
                               PartsPerBox = (int)s.PartsPerBox,
                               SeatOptions = (int)s.SeatOptions,
                               SeatOptions2Check = (int)s.SeatOptions2Check,
                               BaseStation = SequencingSettingsCacheManager.SequencingStationSettings.Where(i=>i.Id == SequencingSettingsCacheManager.BaseSequencingStationOffsets.Where(x=>x.LineId == s.LineId).Single().SequencingStationSettingsId).Single(),
                               Offset = (int)s.Offset,
                           }).Single();

            var tracking = GetSequencingHistory(station);
            var lineBuffer = GetSequencingBuffer(station);

            var minBuffer = lineBuffer.OrderBy(j => j.OrderId).FirstOrDefault();

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

            if (tracking.Any())
            {
                lastItem = tracking.OrderByDescending(j => j.OrderId).First();
                firstItem = tracking.OrderBy(j => j.OrderId).First();
            }

            station.NextBox = new SequenceBox();

            if (scheduleStatus == null)
            {
                int nextOrderId = Int16.MinValue;

                if (lineBuffer.Count() > 0)
                    nextOrderId = lineBuffer.First().OrderId;


                if (lastItem != null)
                {
                    if (lastItem.OrderId > nextOrderId) {
                        nextOrderId = lastItem.OrderId + 1;
                    }
                }

                scheduleStatus = new SequencingStationLookup
                {
                    SequenceStationSettingsId = station.Id,
                    NextBoxNum = 1,
                    NextScheduleNum = 1,
                    NextOrderId = nextOrderId
                };

                station.NextBox.BoxNum = scheduleStatus.NextBoxNum;
                station.NextBox.OrderId = scheduleStatus.NextOrderId;
                station.NextBox.ScheduleNum = scheduleStatus.NextScheduleNum;
                SetScheduleAvailable(station, lineBuffer.Count());

                dbSeq.SequencingStationLookups.InsertOnSubmit(scheduleStatus);
                dbSeq.SubmitChanges();
            }
            else
            {
                if (lastItem != null)
                {
                    if (lastItem.Type == 2)
                    {
                        var lastRealJob = tracking.Where(o => o.OrderId < lastItem.OrderId).OrderByDescending(f => f.OrderId).Where(x => x.ScheduleNum > 0).FirstOrDefault();
                        if (lastRealJob != null)
                        {
                            station.NextBox.ScheduleNum = lastRealJob.ScheduleNum + 1;
                            station.NextBox.BoxNum = lastRealJob.BoxNum + 1;
                            station.NextBox.OrderId = lastItem.OrderId + 1;
                        }
                        else
                        {
                            station.NextBox.ScheduleNum = scheduleStatus.NextScheduleNum;
                            station.NextBox.BoxNum = scheduleStatus.NextBoxNum;
                            station.NextBox.OrderId = lastItem.OrderId + 1;
                        }
                    }
                    else
                    {
                        if ((lastItem.BoxNum + station.BoxPerSchedule) > station.BoxQty)
                            station.NextBox.BoxNum = 1;
                        else
                            station.NextBox.BoxNum = (lastItem.BoxNum + 1);

                        station.NextBox.ScheduleNum = lastItem.ScheduleNum + 1;
                        station.NextBox.OrderId = lastItem.OrderId + 1;
                    }
                    

                }
                else
                {
                    if (minBuffer != null)
                    {
                        station.NextBox.OrderId = minBuffer.OrderId;
                        station.NextBox.BoxNum = 1;
                        station.NextBox.ScheduleNum = 1;
                    }
                    else
                    {
                        station.NextBox.OrderId = scheduleStatus.NextOrderId;
                        station.NextBox.BoxNum = scheduleStatus.NextBoxNum;
                        station.NextBox.ScheduleNum = scheduleStatus.NextScheduleNum;
                    }

                }

                scheduleStatus.NextBoxNum = station.NextBox.BoxNum;
                scheduleStatus.NextScheduleNum = station.NextBox.ScheduleNum;
                scheduleStatus.NextOrderId = station.NextBox.OrderId;

                dbSeq.SubmitChanges();

                var validBuffer = (from l in lineBuffer
                                   where l.OrderId >= station.NextBox.OrderId
                                   select l).ToList();

                SetScheduleAvailable(station, validBuffer.Count());

            }


            return station;

        }
        public void UpdateOffsetStationById(int id, int direction, int offset)
        {
            PartSequencingDataContext dbSeq = new PartSequencingDataContext();
            MesDataContext dbMes = new MesDataContext();

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

            SequencingTrackingItem lastItem = null;
            var tracking = GetSequencingHistory(station);
            var lineBuffer = GetSequencingBuffer(station);

            if (tracking.Any())
            {
                lastItem = tracking.OrderByDescending(i => i.OrderId).First();

                if (direction == 0) //down
                {
                    var buffer = lineBuffer.Where(item => item.OrderId > lastItem.OrderId).ToList();
                    buffer = buffer.OrderBy(item => item.OrderId).Take(offset).ToList();

                    if (buffer.Count() == offset)   //enough schedule to move it
                    {
                        var lastBuffer = buffer.Last();
                        SequencingTrackingHistoryDetail histDetail = new SequencingTrackingHistoryDetail
                        {
                            BoxNum = -1, //scheduleStatus.NextBoxNum - 1 == 0 ? 4 : scheduleStatus.NextBoxNum - 1,
                            DTM = DateTime.Now,
                            ScheduleNum = -1, //scheduleStatus.NextScheduleNum - 1,
                            SequencingStationSettingsId = station.Id,
                            Type = 2
                        };
                        dbSeq.SequencingTrackingHistoryDetails.InsertOnSubmit(histDetail);

                        int position = 1;
                        foreach (var seat in buffer)
                        {
                            SequencingTrackingHistory hist = new SequencingTrackingHistory
                            {
                                OrderNumber = seat.OrderNumber,
                                ItemNumber = seat.ItemNumber,
                                ItemType = seat.ItemType,
                                Position = position,
                                JobId = seat.JobId,
                                OrderId = seat.OrderId,
                                OrderStatus = (int)seat.OrderStatus,
                                SequencingTrackingHistoryDetail = histDetail,
                            };

                            dbSeq.SequencingTrackingHistories.InsertOnSubmit(hist);
                            position++;
                        }

                        scheduleStatus.NextOrderId = lastBuffer.OrderId + 1;
                        dbSeq.SubmitChanges();
                    }
                }
                else if (direction == 1) //up
                {

                    var buffer = tracking.OrderByDescending(i => i.OrderId).Take(offset).ToList();
                    if (buffer.Count() == offset)   //enough schedule to move it up
                    {
                        var lastBuffer = buffer.Last();

                        foreach (var seat in buffer)
                        {
                            var deleteSeat = dbSeq.SequencingTrackingHistories.Where(s=>s.Id == seat.SequencingTrackingHistoryId).FirstOrDefault();
                            if (deleteSeat != null)
                                dbSeq.SequencingTrackingHistories.DeleteOnSubmit(deleteSeat);
                        }

                        if (lastBuffer.Type == 2)
                        {
                            scheduleStatus.NextOrderId = lastBuffer.OrderId;
                        }

                        dbSeq.SubmitChanges();

                        var histDetailId = buffer.First().SequencingTrackingHistoryDetailsId;
                        var root = dbSeq.SequencingTrackingHistoryDetails.Where(s => s.Id == histDetailId).FirstOrDefault();
                        if (root != null)
                        {
                            if (root.SequencingTrackingHistories.Count() == 0)
                            {
                                dbSeq.SequencingTrackingHistoryDetails.DeleteOnSubmit(root);
                                dbSeq.SubmitChanges();
                            }
                        }

                    }
                }


            }
            else
            {
                if (lineBuffer.Any())
                {
                    if (direction == 0) //down
                    {
                        var buffer = lineBuffer.Take(offset).ToList();
                        if (buffer.Count() == offset)
                        {
                            var lastBuffer = buffer.Last();
                            SequencingTrackingHistoryDetail histDetail = new SequencingTrackingHistoryDetail
                            {
                                BoxNum = -1, //scheduleStatus.NextBoxNum - 1 == 0 ? 4 : scheduleStatus.NextBoxNum - 1,
                                DTM = DateTime.Now,
                                ScheduleNum = -1, //scheduleStatus.NextScheduleNum - 1,
                                SequencingStationSettingsId = station.Id,
                                Type = 2
                            };
                            dbSeq.SequencingTrackingHistoryDetails.InsertOnSubmit(histDetail);

                            int position = 1;
                            foreach (var seat in buffer)
                            {
                                SequencingTrackingHistory hist = new SequencingTrackingHistory
                                {
                                    OrderNumber = seat.OrderNumber,
                                    ItemNumber = seat.ItemNumber,
                                    ItemType = seat.ItemType,
                                    Position = position,
                                    JobId = seat.JobId,
                                    OrderId = seat.OrderId,
                                    OrderStatus = (int)seat.OrderStatus,
                                    SequencingTrackingHistoryDetail = histDetail,
                                };

                                dbSeq.SequencingTrackingHistories.InsertOnSubmit(hist);
                                position++;
                            }

                            scheduleStatus.NextOrderId = lastBuffer.OrderId + 1;
                            dbSeq.SubmitChanges();
                        }
                    }
                    else if (direction == 1)
                    {
                        //cannot go move up when there is no tracking
                    }
                }
            }

            //send broadcast to clients to update/refresh screen
            var context = GlobalHost.ConnectionManager.GetHubContext<FrontTrimSeqHub>();
            context.Clients.All.frontTrimReceiveBroadcast(999, station.SequencingLocation.Id, "Refresh Command");
            //context.Clients.All.frontTrimReceiveBroadcast(998, 1, "Maintenance");

        }
        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 async Task<int> CompletePlasticSeqByLocationId(int id, int employeeId)
        {
            #region Complete Plastic Seq By Location Id
            try
            {
                PartSequencingDataContext dbSeq = new PartSequencingDataContext();
                EmployeeDatabase dbEmp = new EmployeeDatabase();
                PlasticSeqDatabase db = new PlasticSeqDatabase();

                int scheduleNum = 0;

                var seqStations = db.GetSequencingStationsBySequencingLocationId(id);

                if (seqStations.Count() > 0)
                {
                    var first = seqStations.FirstOrDefault();
                    if (first != null)
                        scheduleNum = first.NextBox.ScheduleNum;
                }

                Log.Logging.Info("Global Complete|Schedule #{0}|Location Id: {1}", scheduleNum, id);

                await Task.Run(() => Parallel.ForEach(seqStations, station =>
                {
                    Log.Logging.Info("Attempt Complete Schedule|Station: {0} Seq Id: {1}", station.SequencingLocation.Description, station.Id);
                    CompletePlasticSeqByStation(station);
                    Log.Logging.Info("Complete Schedule Done|Seq Id: {1}", id);
                }));

                var employee = dbEmp.GetEmployeeInfo(employeeId);

                var scheduleHist = new SequencingStationScheduleHistory
                {
                    EmployeeId = employeeId,
                    EmployeeName = employee == null ? "Unknown Employee" : employee.Name.Trim(),
                    ScheduleNum = scheduleNum,
                    SequencingLocationId = id,
                    DateCompleted = DateTime.Now,
                };

                dbSeq.SequencingStationScheduleHistories.InsertOnSubmit(scheduleHist);
                dbSeq.SubmitChanges();
                Log.Logging.Info("Insert|SequencingStationScheduleHistory|Schedule #{0}, {1}, {2}, {3}", scheduleNum, scheduleHist.SequencingLocationId, scheduleHist.EmployeeName, scheduleHist.DateCompleted);
                return 1;
            }
            catch (Exception ex)
            {
                Log.Logging.Fatal("FATAL|CompleteSequencingByAisleId(SeqId={0}, EmployeeId={1})", id, employeeId);
                Log.Logging.Fatal("FATAL|Error: {0}", ex.Message);
            }
            return 0;
            #endregion
        }