using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FAS_LIB
{
    public class TestDelete
    {
        public bool dBDeleteDataByRoutingId(int RoutingId, bool delRouting = false, bool delVouchers = false)
        {
            // ISSUE: object of a compiler-generated type is created
            // ISSUE: variable of a compiler-generated type
            //clsEngineMES._Closure4__155 closure155 = new clsEngineMES._Closure4__155();
            // ISSUE: reference to a compiler-generated field
            //closure155.RoutingId = RoutingId;
            int num = 0;
            bool flag1;
            if (delRouting)
                delVouchers = true;
            else if (!delVouchers)
            {
                flag1 = false;
                goto label_35;
            }
            using (SE0085CTEntities1 se0085CtEntities1 = new SE0085CTEntities1())
            {
                //se0085CtEntities1.Database.Connection.ConnectionString = this._CTConnection;
                //ParameterExpression parameterExpression;
                // ISSUE: reference to a compiler-generated field
                //IQueryable<AnonymousType_65<SectionsRouting, IEnumerable<LaborVoucher>>> source = Queryable.Select(Queryable.Where((IQueryable<M0>)Queryable.GroupJoin((IQueryable<M0>)se0085CtEntities1.SectionsRoutings, (IEnumerable<M1>)se0085CtEntities1.LaborVouchers, (Expression<Func<M0, M2>>)(r => r.RoutingId), (Expression<Func<M1, M2>>)(v => v.SectionsRouting.RoutingId), (Expression<Func<M0, IEnumerable<M1>, M3>>)((r, laborVouchers) => new
                //{
                //    r = r,
                //    vt = laborVouchers
                //})), (Expression<Func<M0, bool>>)Expression.Lambda<Func<AnonymousType_65<SectionsRouting, IEnumerable<LaborVoucher>>, bool>>((Expression)Expression.Equal(It.r.RoutingId, closure155.RoutingId, true, (MethodInfo)null), new ParameterExpression[1]
                //{
                //  parameterExpression
                //})), (Expression<Func<M0, M1>>)(data => new
                //{
                //    r = data.r,
                //    vt = data.vt
                //}));




                //IQueryable<scRoutingLabour> source = from r in se0085CtEntities1.SectionsRoutings
                //                                     join vt in se0085CtEntities1.LaborVouchers
                //                                     on r.RoutingId equals vt.RoutingId
                //                                     where r.RoutingId == RoutingId
                //                                     select new scRoutingLabour
                //                                     {
                //                                         sectionRouting = r,
                //                                         labourVoucher = vt
                //                                     };

                IQueryable<scRoutingLabour> source = from r in se0085CtEntities1.SectionsRoutings
                                                     join vt in se0085CtEntities1.LaborVouchers
                                                     on r.RoutingId equals vt.RoutingId
                                                     into LabourGroup
                                                     from s in LabourGroup.DefaultIfEmpty()
                                                     where r.RoutingId == RoutingId
                                                     select new scRoutingLabour
                                                     {
                                                         sectionRouting = r,
                                                         labourVoucher = s
                                                     };

                //var source1 = from r in se0085CtEntities1.SectionsRoutings
                //                                     join l in se0085CtEntities1.LaborVouchers
                //                                     on r.RoutingId equals l.RoutingId
                //                                     into laboursGroup
                //                                     where r.RoutingId == RoutingId
                //                                     select new {
                //                                         SectionsRouting = r,
                //                                         LaborVoucher = laboursGroup
                //                                     };

                bool flag2 = false;
                //IEnumerator<AnonymousType_65<SectionsRouting, IEnumerable<LaborVoucher>>> enumerator1;
                IEnumerator<scRoutingLabour> enumerator1;

                if (source.Count() != 0)
                {
                    enumerator1 = source.GetEnumerator();
                    try
                    {
                        while (enumerator1.MoveNext())
                        {
                            var current1 = enumerator1.Current;
                            if (delVouchers)
                            {
                                if (current1.labourVoucher != null)
                                {
                                    try
                                    {
                                        //IEnumerator<LaborVoucher> enumerator2;
                                        LaborVoucher enumerator2;
                                        try
                                        {
                                            enumerator2 = current1.labourVoucher;
                                            //while (enumerator2.MoveNext())
                                            //{
                                            LaborVoucher current2 = enumerator2;//.Current;
                                            se0085CtEntities1.LaborVouchers.Remove(current2);
                                            flag2 = true;
                                            //}
                                        }
                                        finally
                                        {
                                            //if (enumerator2 != null)
                                            //    enumerator2.Dispose();
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        ProjectData.SetProjectError(ex);
                                        flag1 = false;
                                        ProjectData.ClearProjectError();
                                        goto label_35;
                                    }
                                }
                            }
                            if (delRouting)
                            {
                                if (current1.sectionRouting != null)
                                {
                                    try
                                    {
                                        se0085CtEntities1.SectionsRoutings.Remove(current1.sectionRouting);
                                        flag2 = true;
                                    }
                                    catch (Exception ex)
                                    {
                                        ProjectData.SetProjectError(ex);
                                        flag1 = false;
                                        ProjectData.ClearProjectError();
                                        goto label_35;
                                    }
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator1 != null)
                            enumerator1.Dispose();
                    }
                }
                if (flag2)
                    num = se0085CtEntities1.SaveChanges();
                se0085CtEntities1.Database.Connection.Close();
            }
            flag1 = num > 0;
        label_35:
            return flag1;
        }

        public bool dBDeleteDataBySectionId(int SectionId, bool delSection = false, bool delRouting = false, bool delVouchers = false)
        {
            //return true;
            // ISSUE: object of a compiler-generated type is created
            // ISSUE: variable of a compiler-generated type
            //clsEngineMES._Closure4__154 closure154 = new clsEngineMES._Closure4__154();
            // ISSUE: reference to a compiler-generated field
            //closure154.SectionId = SectionId;
            int num = 0;
            bool flag1;
            if (delSection)
            {
                delRouting = true;
                delVouchers = true;
            }
            else if (delRouting)
                delVouchers = true;
            else if (!delVouchers)
            {
                flag1 = false;
                goto label_63;
            }
            //if (delSection & !delRouting & !delVouchers)
            //{
            //    this.lcLogWriter.logData("Requested Delete SectionId " + Conversions.ToString(SectionId));
            //}
            //else if (delSection & delRouting & !delVouchers)
            //{
            //    this.lcLogWriter.logData("Requested Delete SectionId plus routing " + Conversions.ToString(SectionId));
            //}
            //else if (delSection & delRouting & delVouchers)
            //{
            //    this.lcLogWriter.logData("Requested Delete SectionId plus routing and labor " + Conversions.ToString(SectionId));
            //}
            //else if (!delSection & !delRouting & !delVouchers)
            //{
            //    this.lcLogWriter.logData("Requested Delete SectionId " + Conversions.ToString(SectionId) + " but type was not specified");
            //}
            using (SE0085CTEntities1 se0085CtEntities1 = new SE0085CTEntities1())
            {
                //se0085CtEntities1.Database.Connection.ConnectionString = this._CTConnection;
                //ParameterExpression parameterExpression1;
                // ISSUE: reference to a compiler-generated field
                //IQueryable<AnonymousType_64<OrdSection, SectionsRouting, IEnumerable<LaborVoucher>>> source1 = Queryable.Select(Queryable.Where((IQueryable<M0>)Queryable.GroupJoin((IQueryable<M0>)Queryable.Join((IQueryable<M0>)se0085CtEntities1.OrdSections, (IEnumerable<M1>)se0085CtEntities1.SectionsRoutings, (Expression<Func<M0, M2>>)(s => s.OrdSectsId), (Expression<Func<M1, M2>>)(r => r.OrdSection.OrdSectsId), (Expression<Func<M0, M1, M3>>)((s, r) => new { s = s, r = r })), (IEnumerable<M1>)se0085CtEntities1.LaborVouchers, (Expression<Func<M0, M2>>)(data => data.r.RoutingId), (Expression<Func<M1, M2>>)(v => v.SectionsRouting.RoutingId), (Expression<Func<M0, IEnumerable<M1>, M3>>)((data, laborVouchers) => new
                //{
                //    s = data.s,
                //    r = data.r,
                //    vt = laborVouchers
                //})), (Expression<Func<M0, bool>>)Expression.Lambda<Func<AnonymousType_64<OrdSection, SectionsRouting, IEnumerable<LaborVoucher>>, bool>>((Expression)Expression.Equal(It.s.OrdSectsId, closure154.SectionId, true, (MethodInfo)null), new ParameterExpression[1]
                //{
                //  parameterExpression1
                //})), (Expression<Func<M0, M1>>)(data => new
                //{
                //    s = data.s,
                //    r = data.r,
                //    vt = data.vt
                //}));

                //GROUP JOIN
                //IQueryable<scRoutingLabour> source1 = from s in se0085CtEntities1.OrdSections
                //                                      join r in se0085CtEntities1.SectionsRoutings
                //                                      on s.OrdSectsId equals r.OrdSection.OrdSectsId
                //                                      join l in se0085CtEntities1.LaborVouchers
                //                                      on r.RoutingId equals l.SectionsRouting.RoutingId
                //                                      where s.OrdSectsId == SectionId
                //                                      select new scRoutingLabour
                //                                      {
                //                                          ordSection =s,
                //                                          sectionRouting =r,
                //                                          labourVoucher = l
                //                                      };

                IQueryable<scRoutingLabour> source1 = from s in se0085CtEntities1.OrdSections
                                                      join r in se0085CtEntities1.SectionsRoutings
                                                      on s.OrdSectsId equals r.OrdSection.OrdSectsId into RoutingGroup
                                                      from rg in RoutingGroup.DefaultIfEmpty()
                                                      join l in se0085CtEntities1.LaborVouchers
                                                      on rg.RoutingId equals l.SectionsRouting.RoutingId into LabourGroup
                                                      from lg in LabourGroup.DefaultIfEmpty()
                                                      where s.OrdSectsId == SectionId
                                                      select new scRoutingLabour
                                                      {
                                                          ordSection = s,
                                                          sectionRouting = rg,
                                                          labourVoucher = lg
                                                      };

                bool flag2 = false;
                //IEnumerator<AnonymousType_64<OrdSection, SectionsRouting, IEnumerable<LaborVoucher>>> enumerator1;
                IEnumerator<scRoutingLabour> enumerator1;

                if (source1.Count() == 0)
                {
                    //ParameterExpression parameterExpression2;
                    // ISSUE: reference to a compiler-generated field
                    //IQueryable<OrdSection> source2 = (IQueryable<OrdSection>)Queryable.Select<OrdSection, OrdSection>(Queryable.Where<OrdSection>((IQueryable<M0>)se0085CtEntities1.OrdSections, (Expression<Func<M0, bool>>)Expression.Lambda<Func<OrdSection, bool>>((Expression)Expression.Equal(s.OrdSectsId, closure154.SectionId, true, (MethodInfo)null), new ParameterExpression[1]
                    //{
                    //parameterExpression2
                    //})), (Expression<Func<M0, M1>>)(s => s));
                    IQueryable<OrdSection> source2 = (from s in se0085CtEntities1.OrdSections
                                                      where s.OrdSectsId == SectionId
                                                      select s).AsQueryable<OrdSection>();
                    IEnumerator<OrdSection> enumerator2;
                    if (delSection)
                    {
                        if (source2.Count<OrdSection>() != 0)
                        {
                                enumerator2 = source2.GetEnumerator();
                            try
                            {
                                while (enumerator2.MoveNext())
                                {
                                    OrdSection current = enumerator2.Current;
                                    try
                                    {
                                        se0085CtEntities1.OrdSections.Remove(current);
                                        flag2 = true;
                                    }
                                    catch (Exception ex)
                                    {
                                        ProjectData.SetProjectError(ex);
                                        flag1 = false;
                                        ProjectData.ClearProjectError();
                                        goto label_63;
                                    }
                                }
                            }
                            finally
                            {
                                if (enumerator2 != null)
                                    enumerator2.Dispose();
                            }
                        }
                    }
                }
                else
                {
                   enumerator1 = source1.GetEnumerator();
                    try
                    {
                        while (enumerator1.MoveNext())
                        {
                            var current1 = enumerator1.Current;
                            if (delVouchers)
                            {
                                if (current1.labourVoucher != null)
                                {
                                    try
                                    {
                                       // IEnumerator<LaborVoucher> enumerator2;
                                        LaborVoucher  enumerator2;
                                        try
                                        {
                                            enumerator2 = current1.labourVoucher;//.vt.GetEnumerator();
                                            //while (enumerator2.MoveNext())
                                            //{
                                            LaborVoucher current2 = enumerator2;//.Current;
                                                se0085CtEntities1.LaborVouchers.Remove(current2);
                                                flag2 = true;
                                            //}
                                        }
                                        finally
                                        {
                                            //if (enumerator2 != null)
                                            //    enumerator2.Dispose();
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        ProjectData.SetProjectError(ex);
                                        flag1 = false;
                                        ProjectData.ClearProjectError();
                                        goto label_63;
                                    }
                                }
                            }
                            if (delRouting)
                            {
                                if (current1.sectionRouting != null)
                                {
                                    try
                                    {
                                        se0085CtEntities1.SectionsRoutings.Remove(current1.sectionRouting);
                                        flag2 = true;
                                    }
                                    catch (Exception ex)
                                    {
                                        ProjectData.SetProjectError(ex);
                                        flag1 = false;
                                        ProjectData.ClearProjectError();
                                        goto label_63;
                                    }
                                }
                            }
                            if (delSection)
                            {
                                if (current1.ordSection != null)
                                {
                                    try
                                    {
                                        se0085CtEntities1.OrdSections.Remove(current1.ordSection);
                                        flag2 = true;
                                    }
                                    catch (Exception ex)
                                    {
                                        ProjectData.SetProjectError(ex);
                                        flag1 = false;
                                        ProjectData.ClearProjectError();
                                        goto label_63;
                                    }
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator1 != null)
                            enumerator1.Dispose();
                    }
                }
                if (flag2)
                {
                    try
                    {
                        num = se0085CtEntities1.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        ProjectData.SetProjectError(ex);
                        Exception exception = ex;
                        // ISSUE: reference to a compiler-generated field
                        //this.lcLogWriter.logData("Section Delete Failed! " + Conversions.ToString(SectionId) + " Error; " + exception.Message);
                        ProjectData.ClearProjectError();
                    }
                }
                se0085CtEntities1.Database.Connection.Close();
            }
            flag1 = num > 0;
        label_63:
            return flag1;
        }

    }
}
