using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FAS_LIB
{
    public class clsEngineCTdB
    {
        public scItemMaster rtnByAssyPartNum(string AssyPart)
        {
            
            scItemMaster scItemMaster = new scItemMaster();
            using (SE0085CTEntities1 se0085CtEntities1 = new SE0085CTEntities1())
            {
        //        se0085CtEntities1.Database.Connection.ConnectionString = this._CTConnection;
        //        ParameterExpression parameterExpression;

        //        // ISSUE: reference to a compiler-generated field
        //        IQueryable<scItemMaster> source = (IQueryable<scItemMaster>)Queryable.Select<AssyPart, scItemMaster>(Queryable.Where<AssyPart>((IQueryable<M0>)se0085CtEntities1.AssyParts, (Expression<Func<M0, bool>>)(p => Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), p.AssyPartNum, closure32.AssyPart, false) == 0)), (Expression<Func<M0, M1>>)Expression.Lambda<Func<AssyPart, scItemMaster>>((Expression)Expression.MemberInit(Expression.New((ConstructorInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.Ector)), (Expression[])null), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_AssyPartId)), (Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_AssyPartId)))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_AssyPartNumber)), (Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_AssyPartNum)))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_Description1)), (Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_AssyDesc1)))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_Description2)), (Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_AssyDesc2)))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_BuyMake)), (Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_BuyMake)))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_OFOKanban)), (Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_OFOKanban)))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_AssyPartActive)), (Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_AssyPartActive)))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_CostCode)), (Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_CostCode)))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_UOM)), (Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_UOM)))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_UnitCost)), (Expression)Expression.Convert((Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_UnitCost))), typeof(Decimal))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_MaterialCost)), (Expression)Expression.Convert((Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_MaterialCost))), typeof(Decimal))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_LaborCost)), (Expression)Expression.Convert((Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_LaborCost))), typeof(Decimal))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_OverHeadCost)), (Expression)Expression.Convert((Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_OveheadCost))), typeof(Decimal))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_O08FBUR)), (Expression)Expression.Convert((Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_O08FBUR))), typeof(Decimal))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_O09PBUR)), (Expression)Expression.Convert((Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_O09PBUR))), typeof(Decimal))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_O10AMU)), (Expression)Expression.Convert((Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_O10AMU))), typeof(Decimal))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_O11VBUR)), (Expression)Expression.Convert((Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_O11VBUR))), typeof(Decimal))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_Vendor)), (Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_Vendor)))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_Routing)), (Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_Routing)))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_ItemType)), (Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_ItemType)))), (MemberBinding)Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(__methodref(scItemMaster.set_DateAdded)), (Expression)Expression.Convert((Expression)Expression.Property((Expression)parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(AssyPart.get_DateAdded))), typeof(DateTime)))), new ParameterExpression[1]
        //{
        //  parameterExpression
        //}));
                IQueryable<scItemMaster> source = (from s in se0085CtEntities1.AssyParts
                                                   where string.Equals(s.AssyPartNum, AssyPart)
                                                   select new scItemMaster
                                                   {
                                                       AssyPartId = s.AssyPartId,
                                                       AssyPartNumber = s.AssyPartNum,
                                                       BuyMake = s.BuyMake,
                                                       OFOKanban = s.OFOKanban,
                                                       Description1 = s.AssyDesc1,
                                                       Description2 = s.AssyDesc2,
                                                       AssyPartActive = s.AssyPartActive,
                                                       DateAdded = s.DateAdded.HasValue ? s.DateAdded.Value : DateTime.Now,
                                                       CostCode = s.CostCode,
                                                       UOM =s.UOM,
                                                       UnitCost = s.UnitCost.HasValue ? s.UnitCost.Value : 0,
                                                       LaborCost = s.LaborCost.HasValue ? s.LaborCost.Value : 0,
                                                       MaterialCost = s.MaterialCost.HasValue ? s.MaterialCost.Value : 0,
                                                       OverHeadCost =s.OveheadCost.HasValue ? s.OveheadCost.Value : 0 ,
                                                       O08FBUR = s.O08FBUR.HasValue ? s.O08FBUR.Value : 0,
                                                       O09PBUR = s.O09PBUR.HasValue ? s.O09PBUR.Value : 0,
                                                       O10AMU = s.O10AMU.HasValue ? s.O10AMU.Value : 0,
                                                       O11VBUR = s.O11VBUR.HasValue ? s.O11VBUR.Value : 0,
                                                       Vendor = s.Vendor,
                                                       Routing = s.Routing,
                                                       ItemType = s.ItemType
                                                   }).AsQueryable<scItemMaster>();
                if (source.Count<scItemMaster>() == 1)
                    scItemMaster = source.First<scItemMaster>();
                se0085CtEntities1.Database.Connection.Close();
            }
            return scItemMaster;
        }

        public int rtnOrderLineId(string FOLineSLSS)
        {
            // ISSUE: object of a compiler-generated type is created
            // ISSUE: variable of a compiler-generated type
            //clsEngineCTdB closure2 = new clsEngineCTdB();
            //// ISSUE: reference to a compiler-generated field
            //closure2.ldOrder = new scOrderData();
            //// ISSUE: reference to a compiler-generated field
            //closure2.ldOrder.FactoryOrder = FOLineSLSS;
            int num = 0;
            using (SE0085CTEntities1 se0085CtEntities1 = new SE0085CTEntities1())
            {
                //se0085CtEntities1.Database.Connection.ConnectionString = this._CTConnection;                
                //IQueryable<int> source = (IQueryable<int>)Queryable.Select(Queryable.Where((IQueryable<M0>)Queryable.Join((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.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.SEPlants, (Expression<Func<M0, M2>>)(data => data.al.SEPlant.SEPlantId), (Expression<Func<M1, M2>>)(pl => pl.SEPlantId), (Expression<Func<M0, M1, M3>>)((data, pl) => new
                //{
                //    od = data.It1.od,
                //    ol = data.It1.ol,
                //    al = data.al,
                //    pl = pl
                //})), (Expression<Func<M0, bool>>)(data => Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), data.od.OrderFO, closure2.ldOrder.OrderFO, false) == 0 & Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), data.ol.OrderLineNum, closure2.ldOrder.OrderLineNum, false) == 0 & Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), data.ol.OrderLineSL, closure2.ldOrder.OrderLineSL, false) == 0 & Expression.Call((Expression)null, (MethodInfo)MethodBase.GetMethodFromHandle(__methodref(Microsoft.VisualBasic.CompilerServices.Operators.CompareString)), data.ol.OrderLineSS, closure2.ldOrder.OrderLineSS, false) == 0)), (Expression<Func<M0, M1>>)(data => data.ol.OrderLineId));

                //EXTRA CODE .
                //38090312 001 0001
                //38575365 004 0001
                //38671194 001 0001
                //38777183 006 0001
                //29868638 224 0001
                //DOUBT
                IQueryable<int> source = from od in se0085CtEntities1.SEOrders
                            join ol in se0085CtEntities1.OrderLines
                            on od.OrderId equals ol.OrderId
                            join al in se0085CtEntities1.AssemblyLines
                            on ol.AssemLineId equals al.AssemLineId
                            join pl in se0085CtEntities1.SEPlants
                            on al.SEPlantId equals pl.SEPlantId
                            select ol.OrderLineId;

                if (source != null && source.Count<int>() > 0)
                    num = source.First<int>();
                se0085CtEntities1.Database.Connection.Close();
            }
            return num;
        }
    }
}
