using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Text;
using System.Threading;

//namespace SE0085.CTdB
namespace FAS_LIB
{
    public class clsEngineMES
    {
        private LogWriter lcLogWriter;
        private scLevels _Plant;
        private scLevels _AssemblyLine;
        //private Dictionary<DateTime, scHolidays> _dtHoliday;
        //private Dictionary<string, string> _dtLaborContent;

        public clsEngineMES()
        {
            //this._dtHoliday = new Dictionary<DateTime, scHolidays>();
            //this._dtLaborContent = new Dictionary<string, string>();
            //this.GetdBConnString();
            //this.lcLogWriter = new LogWriter("C:\\MESdata\\MesImport.log");
            //this.LoadDictHolidays();
            //this.LoadDictLabor();
        }

        public clsEngineMES(scLevels Plant, scLevels AssemblyLine)
        {
            //this._dtHoliday = new Dictionary<DateTime, scHolidays>();
            //this._dtLaborContent = new Dictionary<string, string>();
            //this.GetdBConnString();
            //this.lcLogWriter = new LogWriter("C:\\MESdata\\MesImport.log");
            this._Plant = Plant;
            this._AssemblyLine = AssemblyLine;
            //this.LoadDictHolidays();
            //this.LoadDictLabor();
        }

        public void UpdateIfRouted(string FOLineSLSS)
        {
            // ISSUE: object of a compiler-generated type is created
            // ISSUE: variable of a compiler-generated type
            //clsEngineMES._Closure4__152 closure152 = new clsEngineMES._Closure4__152();
            string str = FOLineSLSS;
            string ldFO = str.Substring(0, 8);
            string ldLine = str.Substring(8, 3);
            string ldSL = str.Substring(11, 2);
            string ldSS = str.Substring(13, 2);
            using (SE0085CTEntities1 se0085CtEntities1 = new SE0085CTEntities1())
            {
                //se0085CtEntities1.Database.Connection.ConnectionString = this._CTConnection;
                // ISSUE: method reference
                //IQueryable<AnonymousType_63<string, int>> source = Queryable.Select(Queryable.Where((IQueryable<M0>)Queryable.Join((IQueryable<M0>)Queryable.Join((IQueryable<M0>)Queryable.Join((IQueryable<M0>)Queryable.Join((IQueryable<M0>)se0085CtEntities1.OrderLines, (IEnumerable<M1>)se0085CtEntities1.SEOrders, (Expression<Func<M0, M2>>)(ol => ol.SEOrder.OrderId), (Expression<Func<M1, M2>>)(fo => fo.OrderId), (Expression<Func<M0, M1, M3>>)((ol, fo) => new { ol = ol, fo = fo })), (IEnumerable<M1>)se0085CtEntities1.AssemblyLines, (Expression<Func<M0, M2>>)(data => data.ol.AssemblyLine.AssemLineId), (Expression<Func<M1, M2>>)(al => al.AssemLineId), (Expression<Func<M0, M1, M3>>)((data, al) => new
                //{
                //    It1 = data,
                //    al = al
                //})), (IEnumerable<M1>)se0085CtEntities1.OrdSections, (Expression<Func<M0, M2>>)(data => data.It1.ol.OrderLineId), (Expression<Func<M1, M2>>)(os => os.OrderLine.OrderLineId), (Expression<Func<M0, M1, M3>>)((data, os) => new
                //{
                //    It1 = data,
                //    os = os
                //})), (IEnumerable<M1>)se0085CtEntities1.SectionsRoutings, (Expression<Func<M0, M2>>)(data => data.os.OrdSectsId), (Expression<Func<M1, M2>>)(rt => rt.OrdSection.OrdSectsId), (Expression<Func<M0, M1, M3>>)((data, rt) => new
                //{
                //    ol = data.It1.It1.ol,
                //    fo = data.It1.It1.fo,
                //    al = data.It1.al,
                //    os = data.os,
                //    rt = rt
                //})), (Expression<Func<M0, bool>>)(data => Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), data.fo.OrderFO, closure152.ldFO, false) == 0 & Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), data.ol.OrderLineNum, closure152.ldLine, false) == 0 & Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), data.ol.OrderLineSL, closure152.ldSL, false) == 0 & Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), data.ol.OrderLineSS, closure152.ldSS, false) == 0)), (Expression<Func<M0, M1>>)(data => new
                //{
                //    AssemLineAbbrv = data.al.AssemLineAbbrv,
                //    RoutingId = data.rt.RoutingId
                //}));

                var source = from ol in se0085CtEntities1.OrderLines
                             join od in se0085CtEntities1.SEOrders
                             on ol.SEOrder.OrderId equals od.OrderId
                             join al in se0085CtEntities1.AssemblyLines
                             on ol.AssemblyLine.AssemLineId equals al.AssemLineId
                             join os in se0085CtEntities1.OrdSections
                             on ol.OrderLineId equals os.OrderLine.OrderLineId
                             join sr in se0085CtEntities1.SectionsRoutings
                             on os.OrdSectsId equals sr.OrdSection.OrdSectsId
                             where string.Equals(od.OrderFO,ldFO)
                             && string.Equals(ol.OrderLineNum,ldLine)
                             && string.Equals(ol.OrderLineSL, ldSL)
                             && string.Equals(ol.OrderLineSS, ldSS)
                             select new
                             {
                                 AssemLineAbbrv = al.AssemLineAbbrv,
                                 RoutingId = sr.RoutingId
                             };


                if (source != null && source.Count() > 0)
                {
                    string assemLineAbbrv = source.First().AssemLineAbbrv;
                    List<scMESUpdateFull> scMesUpdateFullList = new List<scMESUpdateFull>();
                    List<scMESUpdateFull> OrderList = this.LoadMESOrders(assemLineAbbrv, ldFO, ldLine, ldSL, ldSS);
                    if (OrderList != null && OrderList.Count > 0)
                    {
                        this._AssemblyLine = (scLevels)new clsAssemblyLine(this._Plant).rtnAssemblyLineByAbbrv(this._Plant.LevelId, assemLineAbbrv);
                        this.ScheduleOrder(ref OrderList);
                        this.MESLoad(this._Plant, this._AssemblyLine, OrderList, true, false);
                    }
                }
                se0085CtEntities1.Database.Connection.Close();
            }
       }


        public void ScheduleOrder(ref List<scMESUpdateFull> OrderList)
        {
        //    // ISSUE: object of a compiler-generated type is created
        //    // ISSUE: variable of a compiler-generated type
        //    clsEngineMES._Closure4__156 closure156 = new clsEngineMES._Closure4__156();
        //    DateTime[] TargetDate = new DateTime[26];
        //    int index1 = 0;
        //    do
        //    {
        //        TargetDate[index1] = Conversions.ToDate("01/01/2020");
        //        checked { ++index1; }
        //    }
        //    while (index1 <= 24);
        //    string OrdSER = " ";
        //    string ElecClpx = "1";
        //    string MechClpx = "1";
        //    int SectCnt = 1;
        //    // ISSUE: reference to a compiler-generated field
        //    closure156.ldCurrentWC = "";
        //    // ISSUE: reference to a compiler-generated field
        //    closure156.ldWCList = new List<string>();
        //    // ISSUE: reference to a compiler-generated field
        //    closure156.ldWCList.Add("SHIPPING");
        //    // ISSUE: reference to a compiler-generated field
        //    closure156.ldWCList.Add("SHIP");
        //    Conversions.ToDate("01/01/1901");
        //    // ISSUE: reference to a compiler-generated field
        //    closure156.ldOrderFO = OrderList[0].hd_q2c_nbr;
        //    // ISSUE: reference to a compiler-generated field
        //    closure156.ldOrderLineNum = OrderList[0].od_ln_dsply_seq_nbr;
        //    // ISSUE: reference to a compiler-generated field
        //    closure156.ldOrderLineSL = OrderList[0].hd_sub_ln_itm_id;
        //    // ISSUE: reference to a compiler-generated field
        //    closure156.ldOrderLineSS = OrderList[0].hd_orig_shpschd_id;
        //    using (SE0085CTEntities1 se0085CtEntities1 = new SE0085CTEntities1())
        //    {
        //        se0085CtEntities1.Database.Connection.ConnectionString = this._CTConnection;
        //        // ISSUE: method reference
        //        IQueryable<AnonymousType_67<DateTime?, string, string, string>> source1 = Queryable.Select(Queryable.Where((IQueryable<M0>)Queryable.Join((IQueryable<M0>)Queryable.Join((IQueryable<M0>)se0085CtEntities1.SEOrders, (IEnumerable<M1>)se0085CtEntities1.OrderLines, (Expression<Func<M0, M2>>)(od => od.OrderId), (Expression<Func<M1, M2>>)(ol => ol.SEOrder.OrderId), (Expression<Func<M0, M1, M3>>)((od, ol) => new { od = od, ol = ol })), (IEnumerable<M1>)se0085CtEntities1.OrderLinesExts, (Expression<Func<M0, M2>>)(data => data.ol.OrderLineId), (Expression<Func<M1, M2>>)(oe => oe.OrderLine.OrderLineId), (Expression<Func<M0, M1, M3>>)((data, oe) => new
        //        {
        //            od = data.od,
        //            ol = data.ol,
        //            oe = oe
        //        })), (Expression<Func<M0, bool>>)(data => Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), data.od.OrderFO, closure156.ldOrderFO, false) == 0 & Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), data.ol.OrderLineNum, closure156.ldOrderLineNum, false) == 0 & Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), data.ol.OrderLineSL, closure156.ldOrderLineSL, false) == 0 & Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), data.ol.OrderLineSS, closure156.ldOrderLineSS, false) == 0)), (Expression<Func<M0, M1>>)(data => new
        //        {
        //            OrderLineCurTargetDt = data.oe.OrderLineCurTargetDt,
        //            OrderLineSER = data.ol.OrderLineSER,
        //            OrderLineElecClpx = data.oe.OrderLineElecClpx,
        //            OrderLineMechClpx = data.oe.OrderLineMechClpx
        //        }));


        //        if (source1 != null && source1.Count() > 0)
        //        {
        //            int index2 = 0;
        //            do
        //            {
        //                TargetDate[index2] = (DateTime)source1.FirstOrDefault().OrderLineCurTargetDt;
        //                checked { ++index2; }
        //            }
        //            while (index2 <= 24);
        //            OrdSER = source1.FirstOrDefault().OrderLineSER;
        //            ElecClpx = source1.FirstOrDefault().OrderLineElecClpx;
        //            MechClpx = source1.FirstOrDefault().OrderLineMechClpx;
        //        }
        //        ParameterExpression parameterExpression;
        //        IQueryable<RoutingSeq> source2 = (IQueryable<RoutingSeq>)Queryable.Select<RoutingSeq, RoutingSeq>(Queryable.Where<RoutingSeq>((IQueryable<M0>)se0085CtEntities1.RoutingSeqs, (Expression<Func<M0, bool>>)Expression.Lambda<Func<RoutingSeq, bool>>((Expression)Expression.Equal(rs.AssemblyLine.AssemLineId, this._AssemblyLine.LevelId, true, (MethodInfo)null), new ParameterExpression[1]
        //        {
        //          parameterExpression
        //        })), (Expression<Func<M0, M1>>)(rs => rs));

        //        if (source2 != null && source2.Count<RoutingSeq>() > 0)
        //        {
        //            // ISSUE: method pointer
        //            Dictionary<string, RoutingSeq> dictionary = (Dictionary<string, RoutingSeq>)Enumerable.ToDictionary<RoutingSeq, string>((IEnumerable<M0>)source2, (Func<M0, M1>)new Func<RoutingSeq, string>((object)null, __methodptr(_Lambda4__87)));
        //            // ISSUE: reference to a compiler-generated field
        //            do
        //            {
        //                IQueryable<string> source3;
        //                do
        //                {
        //                    // ISSUE: reference to a compiler-generated field
        //                    // ISSUE: reference to a compiler-generated field
        //                    closure156.ldCurrentWC = closure156.ldWCList[0];
        //                    // ISSUE: reference to a compiler-generated field
        //                    if (dictionary.ContainsKey(closure156.ldCurrentWC))
        //                    {
        //                        List<scMESUpdateFull> scMesUpdateFullList = OrderList;
        //                        // ISSUE: virtual method pointer
        //                        IntPtr num = __vmethodptr(closure156, _Lambda4__88);
        //                        Func<scMESUpdateFull, bool> func = new Func<scMESUpdateFull, bool>((object)closure156, num);
        //                        // ISSUE: method pointer
        //                        // ISSUE: method pointer
        //                        IEnumerable<scMESUpdateFull> source4 = (IEnumerable<scMESUpdateFull>)Enumerable.Select<scMESUpdateFull, scMESUpdateFull>((IEnumerable<M0>)Enumerable.OrderBy<scMESUpdateFull, int>(Enumerable.Where<scMESUpdateFull>((IEnumerable<M0>)scMesUpdateFullList, (Func<M0, bool>)func), (Func<M0, M1>)new Func<scMESUpdateFull, int>((object)null, __methodptr(_Lambda4__89))), (Func<M0, M1>)new Func<scMESUpdateFull, scMESUpdateFull>((object)null, __methodptr(_Lambda4__90)));
        //                        if (source4 != null && source4.Count<scMESUpdateFull>() > 0)
        //                        {
        //                            SectCnt = 0;
        //                            IEnumerator<scMESUpdateFull> enumerator;
        //                            try
        //                            {
        //                                enumerator = source4.GetEnumerator();
        //                                while (enumerator.MoveNext())
        //                                {
        //                                    scMESUpdateFull current = enumerator.Current;
        //                                    checked { ++SectCnt; }
        //                                    current.od_trgt_dt = TargetDate[current.hd_sect_id];
        //                                    this.ScheduleWC(ref dictionary, ref current, OrdSER, ElecClpx, MechClpx, SectCnt);
        //                                }
        //                            }
        //                            finally
        //                            {
        //                                if (enumerator != null)
        //                                    enumerator.Dispose();
        //                            }
        //                            // ISSUE: reference to a compiler-generated field
        //                            this.SyncShipSplt(ref OrderList, closure156.ldCurrentWC);
        //                        }
        //                        // ISSUE: reference to a compiler-generated field
        //                        // ISSUE: reference to a compiler-generated field
        //                        if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(dictionary[closure156.ldCurrentWC].RtSeqSeries, "PRIMARY", false) == 0 | Microsoft.VisualBasic.CompilerServices.Operators.CompareString(dictionary[closure156.ldCurrentWC].RtSeqSeries, "PRIMRY", false) == 0)
        //                        {
        //                            bool flag = true;
        //                            // ISSUE: reference to a compiler-generated field
        //                            bool? rtSeqSync2Wc = dictionary[closure156.ldCurrentWC].RtSeqSync2WC;
        //                            IEnumerator<scMESUpdateFull> enumerator1;
        //                            if ((rtSeqSync2Wc.HasValue ? new bool?(rtSeqSync2Wc.GetValueOrDefault() == flag) : new bool?()).GetValueOrDefault())
        //                            {
        //                                DateTime t2 = Conversions.ToDate("12/31/2039");
        //                                IEnumerator<scMESUpdateFull> enumerator2;
        //                                try
        //                                {
        //                                    enumerator2 = source4.GetEnumerator();
        //                                    while (enumerator2.MoveNext())
        //                                    {
        //                                        scMESUpdateFull current = enumerator2.Current;
        //                                        if (DateTime.Compare(current.due_st, t2) < 0)
        //                                            t2 = current.due_st;
        //                                    }
        //                                }
        //                                finally
        //                                {
        //                                    if (enumerator2 != null)
        //                                        enumerator2.Dispose();
        //                                }
        //                                IEnumerator<scMESUpdateFull> enumerator3;
        //                                try
        //                                {
        //                                    enumerator3 = source4.GetEnumerator();
        //                                    while (enumerator3.MoveNext())
        //                                    {
        //                                        scMESUpdateFull current = enumerator3.Current;
        //                                        TargetDate[current.hd_sect_id] = t2;
        //                                    }
        //                                }
        //                                finally
        //                                {
        //                                    if (enumerator3 != null)
        //                                        enumerator3.Dispose();
        //                                }
        //                            }
        //                            else
        //                            {
        //                                try
        //                                {
        //                                    enumerator1 = source4.GetEnumerator();
        //                                    while (enumerator1.MoveNext())
        //                                    {
        //                                        scMESUpdateFull current = enumerator1.Current;
        //                                        TargetDate[current.hd_sect_id] = current.due_st;
        //                                    }
        //                                }
        //                                finally
        //                                {
        //                                    if (enumerator1 != null)
        //                                        enumerator1.Dispose();
        //                                }
        //                            }
        //                        }
        //                    }
        //                    // ISSUE: reference to a compiler-generated field
        //                    // ISSUE: reference to a compiler-generated field
        //                    closure156.ldWCList.Remove(closure156.ldCurrentWC);
        //                    // ISSUE: method reference
        //                    IQueryable<string> source5 = (IQueryable<string>)Queryable.Select<RoutingSeq, string>(Queryable.Where<RoutingSeq>((IQueryable<M0>)source2, (Expression<Func<M0, bool>>)(rt => !(Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), rt.RtSeqSeries, "PRIMARY", false) == 0 | Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), rt.RtSeqSeries, "PRIMRY", false) == 0) & Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), rt.RtSeqNextWorkCtr, closure156.ldCurrentWC, false) == 0)), (Expression<Func<M0, M1>>)(rt => rt.RtSeqWorkCenter));
        //                    List<string>.Enumerator enumerator4;
        //                    if (source5 != null)
        //                    {
        //                        if (source5.Count<string>() > 0)
        //                        {
        //                            try
        //                            {
        //                                enumerator4 = source5.ToList<string>().GetEnumerator();
        //                                while (enumerator4.MoveNext())
        //                                {
        //                                    string current = enumerator4.Current;
        //                                    this.ScheduleSubProc(ref OrderList, dictionary[current].RtSeqSeries, current, OrdSER, ElecClpx, MechClpx, SectCnt, TargetDate);
        //                                }
        //                            }
        //                            finally
        //                            {
        //                                enumerator4.Dispose();
        //                            }
        //                        }
        //                    }
        //                    // ISSUE: method reference
        //                    source3 = (IQueryable<string>)Queryable.Select<RoutingSeq, string>(Queryable.Where<RoutingSeq>((IQueryable<M0>)source2, (Expression<Func<M0, bool>>)(rt => (Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), rt.RtSeqSeries, "PRIMARY", false) == 0 | Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), rt.RtSeqSeries, "PRIMRY", false) == 0) & Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), rt.RtSeqNextWorkCtr, closure156.ldCurrentWC, false) == 0)), (Expression<Func<M0, M1>>)(rt => rt.RtSeqWorkCenter));
        //                }
        //                while (source3 == null);
        //                List<string>.Enumerator enumerator5;
        //                if (source3.Count<string>() > 0)
        //                {
        //                    try
        //                    {
        //                        enumerator5 = source3.ToList<string>().GetEnumerator();
        //                        while (enumerator5.MoveNext())
        //                        {
        //                            string current = enumerator5.Current;
        //                            // ISSUE: reference to a compiler-generated field
        //                            if (!closure156.ldWCList.Contains(current))
        //                            {
        //                                // ISSUE: reference to a compiler-generated field
        //                                closure156.ldWCList.Add(current);
        //                            }
        //                        }
        //                    }
        //                    finally
        //                    {
        //                        enumerator5.Dispose();
        //                    }
        //                }
        //                else
        //                {
        //                    // ISSUE: reference to a compiler-generated field
        //                    if (closure156.ldWCList.Count == 0)
        //                        break;
        //                }
        //            }
        //            while (closure156.ldWCList.Count != 0);
        //        }
        //        se0085CtEntities1.Database.Connection.Close();
        //    }
        }

        public void MESLoad(scLevels SEPlant, scLevels AssemblyLine, List<scMESUpdateFull> MESOrderData, bool blnIncludeShipped, bool exCloseAfter = false)
        {
            this.lcLogWriter.logData("Plant " + SEPlant.LevelName + " - " + AssemblyLine.LevelAbbrv);
            scMESOrderInfo scMesOrderInfo1 = new scMESOrderInfo();
            ArrayList MESExtractData = new ArrayList();
            bool flag1 = false;
            bool flag2 = false;
            bool flag3 = false;
            int num = 0;
            List<scMESUpdateFull>.Enumerator enumerator1;
            try
            {
                enumerator1 = MESOrderData.GetEnumerator();
                while (enumerator1.MoveNext())
                {
                    scMESUpdateFull current = enumerator1.Current;
                    Thread.CurrentThread.Join(0);
                    checked { ++num; }
                    if (MESOrderData.Count == num)
                        flag3 = true;
                    scMESOrderInfo scMesOrderInfo2 = new scMESOrderInfo();
                    scMesOrderInfo2.AssemblyLineId = current.AssemblyLineId;
                    scMesOrderInfo2.OrderFO = current.hd_q2c_nbr;
                    scMesOrderInfo2.OrderLineNum = current.od_ln_dsply_seq_nbr;
                    scMesOrderInfo2.OrderLineSL = current.hd_sub_ln_itm_id;
                    scMesOrderInfo2.OrderLineSS = current.hd_orig_shpschd_id;
                    scMesOrderInfo2.OrdShipSplt = current.hd_shp_splt;
                    scMesOrderInfo2.OrdSectsNum = current.hd_sect_id;
                    scMesOrderInfo2.OrdSectsProdNum = current.hd_prdn_ord_nbr;
                    scMesOrderInfo2.OrderLineTargetDt = current.od_trgt_dt;
                    scMesOrderInfo2.OrderLineShipDt = current.od_curr_prom_dt;
                    scMesOrderInfo2.RoutingWorkCenter = current.dt_eto_workctr;
                    scMesOrderInfo2.RoutingNextWorkCtr = current.RoutingNextWorkCtr;
                    scMesOrderInfo2.RoutingSchStartDt = current.due_st;
                    scMesOrderInfo2.RoutingSchFinishDt = current.due_fn;
                    scMesOrderInfo2.RoutingActStartDt = current.act_st;
                    scMesOrderInfo2.RoutingActFinishDt = current.act_fn;
                    scMesOrderInfo2.RoutingRunTimeMin = current.run_tm_tot;
                    scMesOrderInfo2.RoutingStatusCD = current.dt_prdn_dtl_status_cd;
                    if (!flag1)
                    {
                        scMesOrderInfo1 = scMesOrderInfo2;
                        flag1 = true;
                    }
                    if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(scMesOrderInfo1.OrderFO, scMesOrderInfo2.OrderFO, false) == 0 & Microsoft.VisualBasic.CompilerServices.Operators.CompareString(scMesOrderInfo1.OrderLineNum, scMesOrderInfo2.OrderLineNum, false) == 0 & Microsoft.VisualBasic.CompilerServices.Operators.CompareString(scMesOrderInfo1.OrderLineSL, scMesOrderInfo2.OrderLineSL, false) == 0 & Microsoft.VisualBasic.CompilerServices.Operators.CompareString(scMesOrderInfo1.OrderLineSS, scMesOrderInfo2.OrderLineSS, false) == 0 & scMesOrderInfo1.OrdSectsNum == scMesOrderInfo2.OrdSectsNum && Microsoft.VisualBasic.CompilerServices.Operators.CompareString(scMesOrderInfo1.OrdSectsProdNum, scMesOrderInfo2.OrdSectsProdNum, false) != 0)
                    {
                        this.lcLogWriter.logData("Quantity " + scMesOrderInfo1.OrdSectsProdNum + " and " + scMesOrderInfo2.OrdSectsProdNum);
                        scMesOrderInfo2.OrderLineQty = checked(scMesOrderInfo1.OrderLineQty + 1);
                        scMesOrderInfo1 = scMesOrderInfo2;
                    }
                    if (!(Microsoft.VisualBasic.CompilerServices.Operators.CompareString(scMesOrderInfo1.OrderFO, scMesOrderInfo2.OrderFO, false) == 0 & Microsoft.VisualBasic.CompilerServices.Operators.CompareString(scMesOrderInfo1.OrderLineNum, scMesOrderInfo2.OrderLineNum, false) == 0 & Microsoft.VisualBasic.CompilerServices.Operators.CompareString(scMesOrderInfo1.OrderLineSL, scMesOrderInfo2.OrderLineSL, false) == 0 & Microsoft.VisualBasic.CompilerServices.Operators.CompareString(scMesOrderInfo1.OrderLineSS, scMesOrderInfo2.OrderLineSS, false) == 0) | flag3)
                    {
                        if (flag3)
                            MESExtractData.Add((object)scMesOrderInfo2);
                        if (scMesOrderInfo1.OrderLineQty > 1)
                        {
                            try
                            {
                                foreach (scMESOrderInfo scMesOrderInfo3 in MESExtractData)
                                    scMesOrderInfo3.OrderLineQty = scMesOrderInfo1.OrderLineQty;
                            }
                            finally
                            {
                                //IEnumerator enumerator2;
                                //if (enumerator2 is IDisposable)
                                //    (enumerator2 as IDisposable).Dispose();
                            }
                        }
                        this.updateMESData(MESExtractData);
                        scMesOrderInfo1 = scMesOrderInfo2;
                        MESExtractData.Clear();
                        MESExtractData.Add((object)scMesOrderInfo2);
                    }
                    else
                        MESExtractData.Add((object)scMesOrderInfo2);
                    if (!flag2)
                        ;
                    if (flag3)
                        break;
                }
            }
            finally
            {
                //enumerator1.Dispose();
            }
        }

        public List<scMESUpdateFull> LoadMESOrders(string AssyLine, string Q2CNbr, string Q2CLine, string Q2CSL, string Q2CSS)
        {
            //Commented out bcz not in use
            List<scMESUpdateFull> scMesUpdateFullList = new List<scMESUpdateFull>();
            //DataSet dataSet = new DataSet();
            //OdbcConnection odbcConnection = new OdbcConnection();
            //odbcConnection.ConnectionString = "Driver={Sybase ASE ODBC Driver};NA=mcldb01.m087.us.schneider-electric.com,5000;Database=dbmes;Uid=misuser;Pwd=misuser;";
            //OdbcCommand selectCommand = new OdbcCommand();
            //selectCommand.Connection = odbcConnection;
            //odbcConnection.Open();
            //OdbcDataAdapter odbcDataAdapter = new OdbcDataAdapter(selectCommand);
            //odbcDataAdapter.SelectCommand.CommandTimeout = 2000;
            //string str1 = "select distinct  hd.eto_ln  ,hd.q2c_nbr  ,od.ln_dsply_seq_nbr  ,hd.sub_ln_itm_id  ,hd.orig_shpschd_id  ,hd.shp_splt  ,hd.sect_id  ,hd.prdn_ord_nbr  ,dt.eto_workctr  ,dt.prdn_dtl_status_cd  ,SUM(dt.run_tm_mins) as run_tm_tot  ,MIN(dt.due_to_strt_dt) as due_st  ,MAX(dt.due_to_fnsh_dt) as due_fn  ,MIN(dt.act_strt_dt) as act_st  ,MAX(dt.act_fnsh_dt) as act_fn from dbmes.dbo.mespohdr as hd  join dbmes.dbo.mespodtl as dt  on hd.prdn_ord_nbr = dt.prdn_ord_nbr  join dbmes.dbo.messordr as od  on od.q2c_nbr = hd.q2c_nbr and od.ln_nbr = hd.ln_nbr and od.bom_nbr = hd.bom_nbr  group by  hd.eto_ln  ,hd.q2c_nbr  ,od.ln_dsply_seq_nbr  ,hd.sub_ln_itm_id  ,hd.orig_shpschd_id  ,hd.shp_splt  ,hd.sect_id  ,hd.prdn_ord_nbr  ,dt.eto_workctr  ,dt.prdn_dtl_status_cd  having hd.eto_ln = ? AND hd.q2c_nbr = ? AND od.ln_dsply_seq_nbr = ? AND hd.sub_ln_itm_id = ? AND hd.orig_shpschd_id = ?  order by hd.q2c_nbr, od.ln_dsply_seq_nbr, hd.sect_id, due_st ";
            //selectCommand.CommandText = str1;
            //selectCommand.Parameters.Clear();
            //string str2 = AssyLine;
            //selectCommand.Parameters.AddWithValue("eto_ln", (object)str2);
            //string str3 = Q2CNbr;
            //selectCommand.Parameters.AddWithValue("q2c_nbr", (object)str3);
            //string str4 = Q2CLine;
            //selectCommand.Parameters.AddWithValue("ln_dsply_seq_nbr", (object)str4);
            //string str5 = Q2CSL;
            //selectCommand.Parameters.AddWithValue("sub_ln_itm_id", (object)str5);
            //string str6 = Q2CSS;
            //selectCommand.Parameters.AddWithValue("orig_shpschd_id", (object)str6);
            //odbcDataAdapter.SelectCommand = selectCommand;
            //dataSet.Tables.Clear();
            //odbcDataAdapter.Fill(dataSet);
            //List<string> stringList = new List<string>();
            //try
            //{
            //    foreach (DataColumn column in (InternalDataCollectionBase)dataSet.Tables[0].Columns)
            //    {
            //        stringList.Add(column.ColumnName);
            //        //short num;
            //        //checked { ++num; }
            //    }
            //}
            //finally
            //{
            //    //IEnumerator enumerator;
            //    //if (enumerator is IDisposable)
            //    //    (enumerator as IDisposable).Dispose();
            //}
            //try
            //{
            //    foreach (DataRow row in dataSet.Tables[0].Rows)
            //    {
            //        short num = 0;
            //        scMESUpdateFull scMesUpdateFull = new scMESUpdateFull();
            //        List<string>.Enumerator enumerator;
            //            enumerator = stringList.GetEnumerator();
            //        try
            //        {
            //            while (enumerator.MoveNext())
            //            {
            //                string current = enumerator.Current;
            //                if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "eto_ln", false) == 0)
            //                    scMesUpdateFull.hd_eto_ln = Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num]));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "q2c_nbr", false) == 0)
            //                    scMesUpdateFull.hd_q2c_nbr = Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num]));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "ln_dsply_seq_nbr", false) == 0)
            //                    scMesUpdateFull.od_ln_dsply_seq_nbr = Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num]));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "sub_ln_itm_id", false) == 0)
            //                    scMesUpdateFull.hd_sub_ln_itm_id = Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num]));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "orig_shpschd_id", false) == 0)
            //                    scMesUpdateFull.hd_orig_shpschd_id = Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num]));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "shp_splt", false) == 0)
            //                    scMesUpdateFull.hd_shp_splt = Conversions.ToInteger(Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num])));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "sect_id", false) == 0)
            //                    scMesUpdateFull.hd_sect_id = Conversions.ToInteger(Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num])));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "prdn_ord_nbr", false) == 0)
            //                    scMesUpdateFull.hd_prdn_ord_nbr = Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num]));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "disp_dt", false) == 0)
            //                    scMesUpdateFull.hd_disp_dt = Conversions.ToDate(Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num])));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "eto_workctr", false) == 0)
            //                    scMesUpdateFull.dt_eto_workctr = Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num]));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "run_tm_tot", false) == 0)
            //                    scMesUpdateFull.run_tm_tot = Conversions.ToInteger(row[(int)num]);
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "due_st", false) == 0)
            //                    scMesUpdateFull.due_st = Conversions.ToDate(row[(int)num]);
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "due_fn", false) == 0)
            //                    scMesUpdateFull.due_fn = Conversions.ToDate(row[(int)num]);
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "act_st", false) == 0)
            //                    scMesUpdateFull.act_st = Conversions.ToDate(row[(int)num]);
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "act_fn", false) == 0)
            //                    scMesUpdateFull.act_fn = Conversions.ToDate(row[(int)num]);
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "curr_prom_dt", false) == 0)
            //                    scMesUpdateFull.od_curr_prom_dt = Conversions.ToDate(Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num])));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "trgt_dt", false) == 0)
            //                    scMesUpdateFull.od_trgt_dt = Conversions.ToDate(Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num])));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "prdn_dtl_status_cd", false) == 0)
            //                    scMesUpdateFull.dt_prdn_dtl_status_cd = Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num]));
            //                else if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(current, "update_dt", false) == 0)
            //                    scMesUpdateFull.dt_update_dt = Conversions.ToDate(Microsoft.VisualBasic.Strings.Trim(Conversions.ToString(row[(int)num])));
            //                checked { ++num; }
            //            }
            //        }
            //        finally
            //        {
            //            enumerator.Dispose();
            //        }
            //        scMesUpdateFullList.Add(scMesUpdateFull);
            //    }
            //}
            //finally
            //{
            //    //IEnumerator enumerator;
            //    //if (enumerator is IDisposable)
            //    //    (enumerator as IDisposable).Dispose();
            //}
            return scMesUpdateFullList;
        }

        public void updateMESData(ArrayList MESExtractData)
        {
            //ArrayList arrayList = new ArrayList();
            //IEnumerable<scMESOrderInfo> source1 = (IEnumerable<scMESOrderInfo>)Enumerable.Select<scMESOrderInfo, scMESOrderInfo>((IEnumerable<M0>)Enumerable.ThenBy<scMESOrderInfo, string>(Enumerable.ThenBy<scMESOrderInfo, int>(Enumerable.ThenBy<scMESOrderInfo, string>(Enumerable.ThenBy<scMESOrderInfo, string>(Enumerable.ThenBy<scMESOrderInfo, string>(Enumerable.OrderBy<scMESOrderInfo, string>((IEnumerable<M0>)Enumerable.Select<object, scMESOrderInfo>((IEnumerable<M0>)MESExtractData.Cast<object>(), (Func<M0, M1>)new Func<object, scMESOrderInfo>((object)null, __methodptr(_Lambda4__67))), (Func<M0, M1>)new Func<scMESOrderInfo, string>((object)null, __methodptr(_Lambda4__68))), (Func<M0, M1>)new Func<scMESOrderInfo, string>((object)null, __methodptr(_Lambda4__69))), (Func<M0, M1>)new Func<scMESOrderInfo, string>((object)null, __methodptr(_Lambda4__70))), (Func<M0, M1>)new Func<scMESOrderInfo, string>((object)null, __methodptr(_Lambda4__71))), (Func<M0, M1>)new Func<scMESOrderInfo, int>((object)null, __methodptr(_Lambda4__72))), (Func<M0, M1>)new Func<scMESOrderInfo, string>((object)null, __methodptr(_Lambda4__73))), (Func<M0, M1>)new Func<scMESOrderInfo, scMESOrderInfo>((object)null, __methodptr(_Lambda4__74)));
            //if (source1.Count<scMESOrderInfo>() <= 0)
            //    return;
            //arrayList.AddRange((ICollection)source1.ToArray<scMESOrderInfo>());
            //int num1 = 0;
            //string Left = "";
            //ArrayList MESSectionList1 = new ArrayList();
            //try
            //{
            //    foreach (scMESOrderInfo scMesOrderInfo in arrayList)
            //    {
            //        if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(Left, scMesOrderInfo.OrdSectsProdNum, false) != 0)
            //        {
            //            Left = scMesOrderInfo.OrdSectsProdNum;
            //            MESSectionList1.Add((object)scMesOrderInfo.OrdSection);
            //            checked { ++num1; }
            //        }
            //    }
            //}
            //finally
            //{
            //    IEnumerator enumerator;
            //    if (enumerator is IDisposable)
            //        (enumerator as IDisposable).Dispose();
            //}
            //scMESOrderInfo scMesOrderInfo1 = new scMESOrderInfo();
            //scMESOrderInfo scMesOrderInfo2 = (scMESOrderInfo)arrayList[0];
            //scMESTableStatus[] scMesTableStatusArray1 = this.StatusMES_o_l_rLOJ(scMesOrderInfo2.OrderFO, scMesOrderInfo2.OrderLineNum, scMesOrderInfo2.OrderLineSL, scMesOrderInfo2.OrderLineSS);
            //bool flag;
            //if (scMesTableStatusArray1 == null)
            //{
            //    this.AddMESSections(arrayList, MESSectionList1);
            //    flag = false;
            //}
            //else if (scMesTableStatusArray1.Length > num1)
            //{
            //    int num2 = checked(num1 + 1);
            //    int length = scMesTableStatusArray1.Length;
            //    int num3 = num2;
            //    while (num3 <= length)
            //    {
            //        this.dBDeleteDataBySectionId(((IEnumerable<scMESTableStatus>)scMesTableStatusArray1).ElementAt<scMESTableStatus>(checked(num3 - 1)).OrdSectsId, true, false, false);
            //        checked { ++num3; }
            //    }
            //    flag = true;
            //}
            //else if (scMesTableStatusArray1.Length < num1)
            //{
            //    ArrayList MESSectionList2 = new ArrayList();
            //    int length = scMesTableStatusArray1.Length;
            //    int num2 = checked(num1 - 1);
            //    int index = length;
            //    while (index <= num2)
            //    {
            //        MESSectionList2.Add(RuntimeHelpers.GetObjectValue(MESSectionList1[index]));
            //        checked { ++index; }
            //    }
            //    this.AddMESSections(arrayList, MESSectionList2);
            //    flag = true;
            //}
            //else
            //    flag = true;
            //if (flag)
            //{
            //    scMESTableStatus[] scMesTableStatusArray2 = this.StatusMES_o_l_rLOJ(scMesOrderInfo2.OrderFO, scMesOrderInfo2.OrderLineNum, scMesOrderInfo2.OrderLineSL, scMesOrderInfo2.OrderLineSS);
            //    if (scMesTableStatusArray2.Length == num1)
            //    {
            //        // ISSUE: object of a compiler-generated type is created
            //        // ISSUE: variable of a compiler-generated type
            //        clsEngineMES._Closure4__142 closure142 = new clsEngineMES._Closure4__142();
            //        // ISSUE: reference to a compiler-generated field
            //        closure142.ldProcSection = new scOrdSectionsInfo();
            //        int index = 0;
            //        try
            //        {
            //            foreach (scOrdSectionsInfo scOrdSectionsInfo in MESSectionList1)
            //            {
            //                // ISSUE: reference to a compiler-generated field
            //                closure142.ldProcSection = scOrdSectionsInfo;
            //                ArrayList MESExtractData1 = new ArrayList();
            //                // ISSUE: method pointer
            //                IEnumerable<M1> m1s = Enumerable.Select<object, scMESOrderInfo>((IEnumerable<M0>)arrayList.Cast<object>(), (Func<M0, M1>)new Func<object, scMESOrderInfo>((object)null, __methodptr(_Lambda4__75)));
            //                // ISSUE: virtual method pointer
            //                IntPtr num2 = __vmethodptr(closure142, _Lambda4__76);
            //                Func<scMESOrderInfo, bool> func = new Func<scMESOrderInfo, bool>((object)closure142, num2);
            //                // ISSUE: method pointer
            //                IEnumerable<scMESOrderInfo> source2 = (IEnumerable<scMESOrderInfo>)Enumerable.Select<scMESOrderInfo, scMESOrderInfo>(Enumerable.Where<scMESOrderInfo>((IEnumerable<M0>)m1s, (Func<M0, bool>)func), (Func<M0, M1>)new Func<scMESOrderInfo, scMESOrderInfo>((object)null, __methodptr(_Lambda4__77)));
            //                if (source2.Count<scMESOrderInfo>() > 0)
            //                {
            //                    MESExtractData1.AddRange((ICollection)source2.ToArray<scMESOrderInfo>());
            //                    try
            //                    {
            //                        foreach (scMESOrderInfo scMesOrderInfo3 in MESExtractData1)
            //                            scMesOrderInfo3.OrdSectsId = scMesTableStatusArray2[index].OrdSectsId;
            //                    }
            //                    finally
            //                    {
            //                        IEnumerator enumerator;
            //                        if (enumerator is IDisposable)
            //                            (enumerator as IDisposable).Dispose();
            //                    }
            //                    this.UpdateMES_o_l_s_r(MESExtractData1);
            //                    checked { ++index; }
            //                }
            //                else
            //                    break;
            //            }
            //        }
            //        finally
            //        {
            //            IEnumerator enumerator;
            //            if (enumerator is IDisposable)
            //                (enumerator as IDisposable).Dispose();
            //        }
            //    }
            //    else
            //        this.lcLogWriter.logData("Error importing data, aborting MES import for Order; " + scMesOrderInfo2.OrderFO + "-" + scMesOrderInfo2.OrderLineNum + "-" + scMesOrderInfo2.OrderLineSS + "-" + scMesOrderInfo2.OrderLineSL);
            //}
        }
    }
}
