public IQueryable <ComboBoxResult> GetPendingProductsForJobOrderInspectionRequestCancel(string term, int JobOrderInspectionRequestCancelHeaderId)//DocTypeId
        {
            JobOrderInspectionRequestCancelHeader header = new JobOrderInspectionRequestCancelHeaderService(db).Find(JobOrderInspectionRequestCancelHeaderId);

            var settings = new JobOrderInspectionRequestSettingsService(db).GetJobOrderInspectionRequestSettingsForDocument(header.DocTypeId, header.DivisionId, header.SiteId);

            string[] contraDocTypes = null;
            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                contraDocTypes = settings.filterContraDocTypes.Split(",".ToCharArray());
            }
            else
            {
                contraDocTypes = new string[] { "NA" };
            }

            string[] contraSites = null;
            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                contraSites = settings.filterContraSites.Split(",".ToCharArray());
            }
            else
            {
                contraSites = new string[] { "NA" };
            }

            string[] contraDivisions = null;
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                contraDivisions = settings.filterContraDivisions.Split(",".ToCharArray());
            }
            else
            {
                contraDivisions = new string[] { "NA" };
            }

            var Query = (from p in db.ViewJobOrderInspectionRequestBalance
                         join jir in db.JobOrderInspectionRequestHeader on p.JobOrderInspectionRequestHeaderId equals jir.JobOrderInspectionRequestHeaderId
                         join t in db.Product on p.ProductId equals t.ProductId into ProdTable
                         from ProTab in ProdTable.DefaultIfEmpty()
                         where p.BalanceQty > 0 &&
                         p.SiteId == header.SiteId && p.DivisionId == header.DivisionId && p.JobWorkerId == header.JobWorkerId && jir.ProcessId == header.ProcessId
                         select new
            {
                ProductId = p.ProductId,
                ProductName = ProTab.ProductName,
                DocTypeId = jir.DocTypeId,
                SiteId = jir.SiteId,
                DivisionId = jir.DivisionId,
            }
                         );

            if (!string.IsNullOrEmpty(term))
            {
                Query = Query.Where(m => m.ProductName.ToLower().Contains(term.ToLower()));
            }
            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                Query = Query.Where(m => contraDocTypes.Contains(m.DocTypeId.ToString()));
            }
            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                Query = Query.Where(m => contraSites.Contains(m.SiteId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.SiteId == header.SiteId);
            }
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                Query = Query.Where(m => contraDivisions.Contains(m.DivisionId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.DivisionId == header.DivisionId);
            }

            return(from p in Query
                   group p by p.ProductId into g
                   orderby g.Max(m => m.ProductName)
                   select new ComboBoxResult
            {
                text = g.Max(m => m.ProductName),
                id = g.Key.ToString(),
            });
        }
        public IEnumerable <JobOrderInspectionRequestLineViewModel> GetPendingJobRequestsForAC(int HeaderId, string term, int Limiter)//Product Id
        {
            var DivisionId = (int)System.Web.HttpContext.Current.Session["DivisionId"];
            var SiteId     = (int)System.Web.HttpContext.Current.Session["SiteId"];

            var InspectionReqCanHeader = db.JobOrderInspectionRequestCancelHeader.Find(HeaderId);

            var settings = new JobOrderInspectionRequestSettingsService(db).GetJobOrderInspectionRequestSettingsForDocument(InspectionReqCanHeader.DocTypeId, InspectionReqCanHeader.DivisionId, InspectionReqCanHeader.SiteId);

            string[] contraDocTypes = null;
            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                contraDocTypes = settings.filterContraDocTypes.Split(",".ToCharArray());
            }
            else
            {
                contraDocTypes = new string[] { "NA" };
            }

            string[] contraSites = null;
            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                contraSites = settings.filterContraSites.Split(",".ToCharArray());
            }
            else
            {
                contraSites = new string[] { "NA" };
            }

            string[] contraDivisions = null;
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                contraDivisions = settings.filterContraDivisions.Split(",".ToCharArray());
            }
            else
            {
                contraDivisions = new string[] { "NA" };
            }

            var Query = (from p in db.ViewJobOrderInspectionRequestBalance
                         join jir in db.JobOrderInspectionRequestHeader on p.JobOrderInspectionRequestHeaderId equals jir.JobOrderInspectionRequestHeaderId
                         where p.BalanceQty > 0 && p.JobWorkerId == InspectionReqCanHeader.JobWorkerId && jir.ProcessId == jir.ProcessId
                         orderby p.JobOrderInspectionRequestNo
                         select new
            {
                JobOrderInspectionRequestHeaderDocNo = p.JobOrderInspectionRequestNo,
                JobOrderInspectionRequestLineId = p.JobOrderInspectionRequestLineId,
                Dimension1Name = p.Dimension1.Dimension1Name,
                Dimension2Name = p.Dimension2.Dimension2Name,
                Dimension3Name = p.Dimension3.Dimension3Name,
                Dimension4Name = p.Dimension4.Dimension4Name,
                ProductName = p.Product.ProductName,
                BalanceQty = p.BalanceQty,
                DocTypeId = jir.DocTypeId,
                SiteId = jir.SiteId,
                DivisionId = jir.DivisionId,
            });

            if (!string.IsNullOrEmpty(term))
            {
                Query = Query.Where(m => m.JobOrderInspectionRequestHeaderDocNo.ToLower().Contains(term.ToLower()) ||
                                    m.Dimension1Name.ToLower().Contains(term.ToLower()) ||
                                    m.Dimension2Name.ToLower().Contains(term.ToLower()) ||
                                    m.Dimension3Name.ToLower().Contains(term.ToLower()) ||
                                    m.Dimension4Name.ToLower().Contains(term.ToLower()) ||
                                    m.ProductName.ToLower().Contains(term.ToLower()));
            }

            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                Query = Query.Where(m => contraDocTypes.Contains(m.DocTypeId.ToString()));
            }
            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                Query = Query.Where(m => contraSites.Contains(m.SiteId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.SiteId == InspectionReqCanHeader.SiteId);
            }
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                Query = Query.Where(m => contraDivisions.Contains(m.DivisionId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.DivisionId == InspectionReqCanHeader.DivisionId);
            }

            return((from p in Query
                    select new JobOrderInspectionRequestLineViewModel
            {
                JobOrderInspectionRequestHeaderDocNo = p.JobOrderInspectionRequestHeaderDocNo,
                JobOrderInspectionRequestLineId = p.JobOrderInspectionRequestLineId,
                Dimension1Name = p.Dimension1Name,
                Dimension2Name = p.Dimension2Name,
                Dimension3Name = p.Dimension3Name,
                Dimension4Name = p.Dimension4Name,
                ProductName = p.ProductName,
                BalanceQty = p.BalanceQty,
            }).Take(Limiter));
        }
        public IEnumerable <JobOrderInspectionRequestCancelLineViewModel> GetJobOrderInspectionRequestLineForMultiSelect(JobOrderInspectionRequestCancelFilterViewModel svm)
        {
            string[] ProductIdArr = null;
            if (!string.IsNullOrEmpty(svm.ProductId))
            {
                ProductIdArr = svm.ProductId.Split(",".ToCharArray());
            }
            else
            {
                ProductIdArr = new string[] { "NA" };
            }

            string[] SaleOrderIdArr = null;
            if (!string.IsNullOrEmpty(svm.JobOrderInspectionRequestId))
            {
                SaleOrderIdArr = svm.JobOrderInspectionRequestId.Split(",".ToCharArray());
            }
            else
            {
                SaleOrderIdArr = new string[] { "NA" };
            }

            string[] ProductGroupIdArr = null;
            if (!string.IsNullOrEmpty(svm.ProductGroupId))
            {
                ProductGroupIdArr = svm.ProductGroupId.Split(",".ToCharArray());
            }
            else
            {
                ProductGroupIdArr = new string[] { "NA" };
            }

            string[] Dime1IdArr = null;
            if (!string.IsNullOrEmpty(svm.Dimension1Id))
            {
                Dime1IdArr = svm.Dimension1Id.Split(",".ToCharArray());
            }
            else
            {
                Dime1IdArr = new string[] { "NA" };
            }

            string[] Dime2IdArr = null;
            if (!string.IsNullOrEmpty(svm.Dimension2Id))
            {
                Dime2IdArr = svm.Dimension2Id.Split(",".ToCharArray());
            }
            else
            {
                Dime2IdArr = new string[] { "NA" };
            }

            string[] Dime3IdArr = null;
            if (!string.IsNullOrEmpty(svm.Dimension3Id))
            {
                Dime3IdArr = svm.Dimension3Id.Split(",".ToCharArray());
            }
            else
            {
                Dime3IdArr = new string[] { "NA" };
            }

            string[] Dime4IdArr = null;
            if (!string.IsNullOrEmpty(svm.Dimension4Id))
            {
                Dime4IdArr = svm.Dimension4Id.Split(",".ToCharArray());
            }
            else
            {
                Dime4IdArr = new string[] { "NA" };
            }

            var InsReqCanHeader = db.JobOrderInspectionRequestCancelHeader.Find(svm.JobOrderInspectionRequestCancelHeaderId);

            var settings = new JobOrderInspectionRequestSettingsService(db).GetJobOrderInspectionRequestSettingsForDocument(InsReqCanHeader.DocTypeId, InsReqCanHeader.DivisionId, InsReqCanHeader.SiteId);

            string[] contraDocTypes = null;
            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                contraDocTypes = settings.filterContraDocTypes.Split(",".ToCharArray());
            }
            else
            {
                contraDocTypes = new string[] { "NA" };
            }

            string[] contraSites = null;
            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                contraSites = settings.filterContraSites.Split(",".ToCharArray());
            }
            else
            {
                contraSites = new string[] { "NA" };
            }

            string[] contraDivisions = null;
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                contraDivisions = settings.filterContraDivisions.Split(",".ToCharArray());
            }
            else
            {
                contraDivisions = new string[] { "NA" };
            }


            var Query = (from p in db.ViewJobOrderInspectionRequestBalance
                         join t in db.JobOrderInspectionRequestLine on p.JobOrderInspectionRequestLineId equals t.JobOrderInspectionRequestLineId into table
                         from tab in table.DefaultIfEmpty()
                         join jir in db.JobOrderInspectionRequestHeader on p.JobOrderInspectionRequestHeaderId equals jir.JobOrderInspectionRequestHeaderId
                         join Jol in db.JobOrderLine on tab.JobOrderLineId equals Jol.JobOrderLineId
                         join product in db.Product on p.ProductId equals product.ProductId into table2
                         from tab2 in table2.DefaultIfEmpty()
                         where p.BalanceQty > 0 && p.JobWorkerId == svm.JobWorkerId && jir.ProcessId == InsReqCanHeader.ProcessId
                         orderby p.RequestDate, p.JobOrderInspectionRequestNo, tab.Sr
                         select new
            {
                BalanceQty = p.BalanceQty,
                Qty = p.BalanceQty,
                JobOrderInspectionRequestDocNo = p.JobOrderInspectionRequestNo,
                ProductName = tab2.ProductName,
                ProductId = p.ProductId,
                JobOrderInspectionRequestCancelHeaderId = svm.JobOrderInspectionRequestCancelHeaderId,
                JobOrderInspectionRequestLineId = p.JobOrderInspectionRequestLineId,
                Dimension1Id = p.Dimension1Id,
                Dimension2Id = p.Dimension2Id,
                Dimension3Id = p.Dimension3Id,
                Dimension4Id = p.Dimension4Id,
                Dimension1Name = p.Dimension1.Dimension1Name,
                Dimension2Name = p.Dimension2.Dimension2Name,
                Dimension3Name = p.Dimension3.Dimension3Name,
                Dimension4Name = p.Dimension4.Dimension4Name,
                Specification = Jol.Specification,
                UnitId = tab2.UnitId,
                UnitName = tab2.Unit.UnitName,
                unitDecimalPlaces = tab2.Unit.DecimalPlaces,
                DealunitDecimalPlaces = Jol.DealUnit.DecimalPlaces,
                ProductUidName = (Jol.ProductUidHeaderId == null ? tab.ProductUid.ProductUidName : ""),
                ProductUidId = tab.ProductUidId,
                JobOrderInspectionRequestHeaderId = p.JobOrderInspectionRequestHeaderId,
                ProdOrderLineId = Jol.ProdOrderLineId,
                ProductGroupId = tab2.ProductGroupId,
                SiteId = p.SiteId,
                DivisionId = p.DivisionId,
                DocTypeId = jir.DocTypeId,
            });

            if (!string.IsNullOrEmpty(svm.ProductId))
            {
                Query = Query.Where(m => ProductIdArr.Contains(m.ProductId.ToString()));
            }

            if (!string.IsNullOrEmpty(svm.JobOrderInspectionRequestId))
            {
                Query = Query.Where(m => SaleOrderIdArr.Contains(m.JobOrderInspectionRequestHeaderId.ToString()));
            }

            if (!string.IsNullOrEmpty(svm.ProductGroupId))
            {
                Query = Query.Where(m => ProductGroupIdArr.Contains(m.ProductGroupId.ToString()));
            }

            if (!string.IsNullOrEmpty(svm.Dimension1Id))
            {
                Query = Query.Where(m => Dime1IdArr.Contains(m.Dimension1Id.ToString()));
            }

            if (!string.IsNullOrEmpty(svm.Dimension2Id))
            {
                Query = Query.Where(m => Dime2IdArr.Contains(m.Dimension2Id.ToString()));
            }

            if (!string.IsNullOrEmpty(svm.Dimension3Id))
            {
                Query = Query.Where(m => Dime3IdArr.Contains(m.Dimension3Id.ToString()));
            }

            if (!string.IsNullOrEmpty(svm.Dimension4Id))
            {
                Query = Query.Where(m => Dime4IdArr.Contains(m.Dimension4Id.ToString()));
            }

            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                Query = Query.Where(m => contraSites.Contains(m.SiteId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.SiteId == InsReqCanHeader.SiteId);
            }
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                Query = Query.Where(m => contraDivisions.Contains(m.DivisionId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.DivisionId == InsReqCanHeader.DivisionId);
            }

            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                Query = Query.Where(m => contraDocTypes.Contains(m.DocTypeId.ToString()));
            }

            return(from p in Query
                   select new JobOrderInspectionRequestCancelLineViewModel
            {
                BalanceQty = p.BalanceQty,
                Qty = p.BalanceQty,
                JobOrderInspectionRequestDocNo = p.JobOrderInspectionRequestDocNo,
                ProductName = p.ProductName,
                ProductId = p.ProductId,
                JobOrderInspectionRequestCancelHeaderId = svm.JobOrderInspectionRequestCancelHeaderId,
                JobOrderInspectionRequestLineId = p.JobOrderInspectionRequestLineId,
                Dimension1Id = p.Dimension1Id,
                Dimension2Id = p.Dimension2Id,
                Dimension3Id = p.Dimension3Id,
                Dimension4Id = p.Dimension4Id,
                Dimension1Name = p.Dimension1Name,
                Dimension2Name = p.Dimension2Name,
                Dimension3Name = p.Dimension3Name,
                Dimension4Name = p.Dimension4Name,
                Specification = p.Specification,
                UnitId = p.UnitId,
                UnitName = p.UnitName,
                unitDecimalPlaces = p.unitDecimalPlaces,
                DealunitDecimalPlaces = p.DealunitDecimalPlaces,
                ProductUidName = (p.ProductUidName),
                ProductUidId = p.ProductUidId,
                JobOrderInspectionRequestHeaderId = p.JobOrderInspectionRequestHeaderId,
                ProdOrderLineId = p.ProdOrderLineId,
            });
        }
        public IEnumerable <JobOrderHeaderListViewModel> GetPendingJobOrdersForAC(int HeaderId, string term, int Limiter)//Product Id
        {
            var DivisionId = (int)System.Web.HttpContext.Current.Session["DivisionId"];
            var SiteId     = (int)System.Web.HttpContext.Current.Session["SiteId"];

            var InspectionRequestHeader = db.JobOrderInspectionRequestHeader.Find(HeaderId);

            var settings = new JobOrderInspectionRequestSettingsService(db).GetJobOrderInspectionRequestSettingsForDocument(InspectionRequestHeader.DocTypeId, InspectionRequestHeader.DivisionId, InspectionRequestHeader.SiteId);

            string[] contraDocTypes = null;
            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                contraDocTypes = settings.filterContraDocTypes.Split(",".ToCharArray());
            }
            else
            {
                contraDocTypes = new string[] { "NA" };
            }

            string[] contraSites = null;
            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                contraSites = settings.filterContraSites.Split(",".ToCharArray());
            }
            else
            {
                contraSites = new string[] { "NA" };
            }

            string[] contraDivisions = null;
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                contraDivisions = settings.filterContraDivisions.Split(",".ToCharArray());
            }
            else
            {
                contraDivisions = new string[] { "NA" };
            }

            var Query = (from p in db.ViewJobOrderBalanceForInspectionRequest
                         join D1 in db.Dimension1 on p.Dimension1Id equals D1.Dimension1Id into Dimension1Table
                         from Dimension1Tab in Dimension1Table.DefaultIfEmpty()
                         join D2 in db.Dimension2 on p.Dimension2Id equals D2.Dimension2Id into Dimension2Table
                         from Dimension2Tab in Dimension2Table.DefaultIfEmpty()
                         join D3 in db.Dimension3 on p.Dimension3Id equals D3.Dimension3Id into Dimension3Table
                         from Dimension3Tab in Dimension3Table.DefaultIfEmpty()
                         join D4 in db.Dimension4 on p.Dimension4Id equals D4.Dimension4Id into Dimension4Table
                         from Dimension4Tab in Dimension4Table.DefaultIfEmpty()
                         join t3 in db.Product on p.ProductId equals t3.ProductId
                         join t4 in db.JobOrderHeader on p.JobOrderHeaderId equals t4.JobOrderHeaderId
                         where p.BalanceQty > 0 && p.JobWorkerId == InspectionRequestHeader.JobWorkerId && t4.ProcessId == InspectionRequestHeader.ProcessId
                         //&& ((string.IsNullOrEmpty(term) ? 1 == 1 : p.JobOrderNo.ToLower().Contains(term.ToLower()))
                         //|| (string.IsNullOrEmpty(term) ? 1 == 1 : dim1.Dimension1Name.ToLower().Contains(term.ToLower()))
                         //|| (string.IsNullOrEmpty(term) ? 1 == 1 : dim2.Dimension2Name.ToLower().Contains(term.ToLower()))
                         //|| (string.IsNullOrEmpty(term) ? 1 == 1 : t3.ProductName.ToLower().Contains(term.ToLower())))
                         orderby p.JobOrderNo
                         select new
            {
                DocNo = p.JobOrderNo,
                JobOrderLineId = p.JobOrderLineId,
                Dimension1Name = Dimension1Tab.Dimension1Name,
                Dimension2Name = Dimension2Tab.Dimension2Name,
                Dimension3Name = Dimension3Tab.Dimension3Name,
                Dimension4Name = Dimension4Tab.Dimension4Name,
                ProductName = t3.ProductName,
                BalanceQty = p.BalanceQty,
                SiteId = t4.SiteId,
                DivisionId = t4.DivisionId,
                DocTypeId = t4.DocTypeId,
            });

            if (!string.IsNullOrEmpty(term))
            {
                Query = Query.Where(m => m.DocNo.Contains(term.ToLower()) || m.Dimension1Name.Contains(term.ToLower()) ||
                                    m.Dimension2Name.Contains(term.ToLower()) ||
                                    m.Dimension3Name.Contains(term.ToLower()) ||
                                    m.Dimension4Name.Contains(term.ToLower()) ||
                                    m.ProductName.ToLower().Contains(term.ToLower()));
            }

            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                Query = Query.Where(m => contraSites.Contains(m.SiteId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.SiteId == InspectionRequestHeader.SiteId);
            }
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                Query = Query.Where(m => contraDivisions.Contains(m.DivisionId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.DivisionId == InspectionRequestHeader.DivisionId);
            }

            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                Query = Query.Where(m => contraDocTypes.Contains(m.DocTypeId.ToString()));
            }


            return((from p in Query
                    select new JobOrderHeaderListViewModel
            {
                DocNo = p.DocNo,
                JobOrderLineId = p.JobOrderLineId,
                Dimension1Name = p.Dimension1Name,
                Dimension2Name = p.Dimension2Name,
                Dimension3Name = p.Dimension3Name,
                Dimension4Name = p.Dimension4Name,
                ProductName = p.ProductName,
                BalanceQty = p.BalanceQty,
            }).Take(Limiter));
        }
        public IQueryable <ComboBoxResult> GetPendingProductHelpList(string term, int Id)
        {
            int CurrentSiteId     = (int)System.Web.HttpContext.Current.Session["SiteId"];
            int CurrentDivisionId = (int)System.Web.HttpContext.Current.Session["DivisionId"];

            var Header = new JobOrderInspectionRequestHeaderService(db).Find(Id);

            var settings = new JobOrderInspectionRequestSettingsService(db).GetJobOrderInspectionRequestSettingsForDocument(Header.DocTypeId, CurrentDivisionId, CurrentSiteId);

            string[] contraDocTypes = null;
            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                contraDocTypes = settings.filterContraDocTypes.Split(",".ToCharArray());
            }
            else
            {
                contraDocTypes = new string[] { "NA" };
            }

            string[] contraSites = null;
            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                contraSites = settings.filterContraSites.Split(",".ToCharArray());
            }
            else
            {
                contraSites = new string[] { "NA" };
            }

            string[] contraDivisions = null;
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                contraDivisions = settings.filterContraDivisions.Split(",".ToCharArray());
            }
            else
            {
                contraDivisions = new string[] { "NA" };
            }

            var Query = (from p in db.ViewJobOrderBalanceForInspectionRequest
                         join jrh in db.JobOrderHeader on p.JobOrderHeaderId equals jrh.JobOrderHeaderId
                         join t in db.Product on p.ProductId equals t.ProductId
                         where jrh.ProcessId == settings.ProcessId && jrh.JobWorkerId == Header.JobWorkerId && p.BalanceQty > 0
                         select new
            {
                Product = t.ProductName,
                Id = t.ProductId,
                DocTypeId = jrh.DocTypeId,
                SiteId = p.SiteId,
                DivisionId = p.DivisionId,
            }
                         );

            if (!string.IsNullOrEmpty(term))
            {
                Query = Query.Where(m => m.Product.ToLower().Contains(term.ToLower()));
            }
            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                Query = Query.Where(m => contraDocTypes.Contains(m.DocTypeId.ToString()));
            }
            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                Query = Query.Where(m => contraSites.Contains(m.SiteId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.SiteId == CurrentSiteId);
            }
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                Query = Query.Where(m => contraDivisions.Contains(m.DivisionId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.DivisionId == CurrentDivisionId);
            }

            return(from p in Query
                   group p by p.Id into g
                   orderby g.Max(m => m.Product)
                   select new ComboBoxResult
            {
                text = g.Max(m => m.Product),
                id = g.Key.ToString(),
            });
        }
        public IQueryable <ComboBoxResult> GetPendingJobOrdersForWizardFilters(string term, int Id)
        {
            int CurrentSiteId     = (int)System.Web.HttpContext.Current.Session["SiteId"];
            int CurrentDivisionId = (int)System.Web.HttpContext.Current.Session["DivisionId"];

            var settings = new JobOrderInspectionRequestSettingsService(db).GetJobOrderInspectionRequestSettingsForDocument(Id, CurrentDivisionId, CurrentSiteId);

            string[] contraDocTypes = null;
            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                contraDocTypes = settings.filterContraDocTypes.Split(",".ToCharArray());
            }
            else
            {
                contraDocTypes = new string[] { "NA" };
            }

            string[] contraSites = null;
            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                contraSites = settings.filterContraSites.Split(",".ToCharArray());
            }
            else
            {
                contraSites = new string[] { "NA" };
            }

            string[] contraDivisions = null;
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                contraDivisions = settings.filterContraDivisions.Split(",".ToCharArray());
            }
            else
            {
                contraDivisions = new string[] { "NA" };
            }

            var Query = (from p in db.ViewJobOrderBalanceForInspectionRequest
                         join t in db.JobOrderHeader on p.JobOrderHeaderId equals t.JobOrderHeaderId
                         where p.BalanceQty > 0 && t.ProcessId == settings.ProcessId
                         select new
            {
                DocNo = p.JobOrderNo,
                DocTypeId = t.DocTypeId,
                SiteId = p.SiteId,
                DivisionId = p.DivisionId,
                JobOrderHeaderId = p.JobOrderHeaderId,
            }
                         );

            if (!string.IsNullOrEmpty(term))
            {
                Query = Query.Where(m => m.DocNo.ToLower().Contains(term.ToLower()));
            }
            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                Query = Query.Where(m => contraDocTypes.Contains(m.DocTypeId.ToString()));
            }
            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                Query = Query.Where(m => contraSites.Contains(m.SiteId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.SiteId == CurrentSiteId);
            }
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                Query = Query.Where(m => contraDivisions.Contains(m.DivisionId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.DivisionId == CurrentDivisionId);
            }

            return(from p in Query
                   group p by p.JobOrderHeaderId into g
                   orderby g.Max(m => m.DocNo)
                   select new ComboBoxResult
            {
                text = g.Max(m => m.DocNo),
                id = g.Key.ToString(),
            });
        }
        public IEnumerable <JobOrderInspectionRequestLineViewModel> GetJobOrderLineForMultiSelect(JobOrderInspectionRequestFilterViewModel svm)
        {
            string[] ProductIdArr = null;
            if (!string.IsNullOrEmpty(svm.ProductId))
            {
                ProductIdArr = svm.ProductId.Split(",".ToCharArray());
            }
            else
            {
                ProductIdArr = new string[] { "NA" };
            }

            string[] SaleOrderIdArr = null;
            if (!string.IsNullOrEmpty(svm.JobOrderId))
            {
                SaleOrderIdArr = svm.JobOrderId.Split(",".ToCharArray());
            }
            else
            {
                SaleOrderIdArr = new string[] { "NA" };
            }

            string[] ProductGroupIdArr = null;
            if (!string.IsNullOrEmpty(svm.ProductGroupId))
            {
                ProductGroupIdArr = svm.ProductGroupId.Split(",".ToCharArray());
            }
            else
            {
                ProductGroupIdArr = new string[] { "NA" };
            }

            string[] Dime1IdArr = null;
            if (!string.IsNullOrEmpty(svm.Dimension1Id))
            {
                Dime1IdArr = svm.Dimension1Id.Split(",".ToCharArray());
            }
            else
            {
                Dime1IdArr = new string[] { "NA" };
            }

            string[] Dime2IdArr = null;
            if (!string.IsNullOrEmpty(svm.Dimension2Id))
            {
                Dime2IdArr = svm.Dimension2Id.Split(",".ToCharArray());
            }
            else
            {
                Dime2IdArr = new string[] { "NA" };
            }

            string[] Dime3IdArr = null;
            if (!string.IsNullOrEmpty(svm.Dimension3Id))
            {
                Dime3IdArr = svm.Dimension3Id.Split(",".ToCharArray());
            }
            else
            {
                Dime3IdArr = new string[] { "NA" };
            }

            string[] Dime4IdArr = null;
            if (!string.IsNullOrEmpty(svm.Dimension4Id))
            {
                Dime4IdArr = svm.Dimension4Id.Split(",".ToCharArray());
            }
            else
            {
                Dime4IdArr = new string[] { "NA" };
            }

            var Header = db.JobOrderInspectionRequestHeader.Find(svm.JobOrderInspectionRequestHeaderId);

            var settings = new JobOrderInspectionRequestSettingsService(db).GetJobOrderInspectionRequestSettingsForDocument(Header.DocTypeId, Header.DivisionId, Header.SiteId);

            string[] contraDocTypes = null;
            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                contraDocTypes = settings.filterContraDocTypes.Split(",".ToCharArray());
            }
            else
            {
                contraDocTypes = new string[] { "NA" };
            }

            string[] contraSites = null;
            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                contraSites = settings.filterContraSites.Split(",".ToCharArray());
            }
            else
            {
                contraSites = new string[] { "NA" };
            }

            string[] contraDivisions = null;
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                contraDivisions = settings.filterContraDivisions.Split(",".ToCharArray());
            }
            else
            {
                contraDivisions = new string[] { "NA" };
            }

            var Query = (from p in db.ViewJobOrderBalanceForInspectionRequest
                         join t in db.JobOrderLine on p.JobOrderLineId equals t.JobOrderLineId into table
                         from tab in table.DefaultIfEmpty()
                         join product in db.Product on p.ProductId equals product.ProductId into table2
                         from tab2 in table2.DefaultIfEmpty()
                         join jo in db.JobOrderHeader on p.JobOrderHeaderId equals jo.JobOrderHeaderId
                         join D1 in db.Dimension1 on p.Dimension1Id equals D1.Dimension1Id into Dimension1Table
                         from Dimension1Tab in Dimension1Table.DefaultIfEmpty()
                         join D2 in db.Dimension2 on p.Dimension2Id equals D2.Dimension2Id into Dimension2Table
                         from Dimension2Tab in Dimension2Table.DefaultIfEmpty()
                         join D3 in db.Dimension3 on p.Dimension3Id equals D3.Dimension3Id into Dimension3Table
                         from Dimension3Tab in Dimension3Table.DefaultIfEmpty()
                         join D4 in db.Dimension4 on p.Dimension4Id equals D4.Dimension4Id into Dimension4Table
                         from Dimension4Tab in Dimension4Table.DefaultIfEmpty()
                         join uni in db.Units on tab2.UnitId equals uni.UnitId into unittable
                         from unittab in unittable.DefaultIfEmpty()
                         join dealunit in db.Units on tab.DealUnitId equals dealunit.UnitId into dealunittable
                         from dealunittab in dealunittable.DefaultIfEmpty()
                         where p.BalanceQty > 0 && p.JobWorkerId == svm.JobWorkerId && jo.ProcessId == Header.ProcessId
                         orderby p.OrderDate, p.JobOrderNo, tab.Sr
                         select new
            {
                BalanceQty = p.BalanceQty,
                Qty = p.BalanceQty,
                JobOrderDocNo = p.JobOrderNo,
                ProductName = tab2.ProductName,
                ProductId = p.ProductId,
                JobOrderInspectionRequestHeaderId = svm.JobOrderInspectionRequestHeaderId,
                JobOrderLineId = p.JobOrderLineId,
                Dimension1Id = Dimension1Tab.Dimension1Id,
                Dimension2Id = Dimension2Tab.Dimension2Id,
                Dimension3Id = Dimension3Tab.Dimension3Id,
                Dimension4Id = Dimension4Tab.Dimension4Id,
                Dimension1Name = Dimension1Tab.Dimension1Name,
                Dimension2Name = Dimension2Tab.Dimension2Name,
                Dimension3Name = Dimension3Tab.Dimension3Name,
                Dimension4Name = Dimension4Tab.Dimension4Name,
                Specification = tab.Specification,
                UnitId = unittab.UnitId,
                UnitName = unittab.UnitName,
                unitDecimalPlaces = unittab.DecimalPlaces,
                DealunitDecimalPlaces = dealunittab.DecimalPlaces,
                ProductUidName = (tab.ProductUidHeaderId == null ? tab.ProductUid.ProductUidName : ""),
                ProductUidId = tab.ProductUidId,
                JobOrderHeaderId = p.JobOrderHeaderId,
                ProdOrderLineId = tab.ProdOrderLineId,
                ProductGroupId = tab2.ProductGroupId,
                SiteId = jo.SiteId,
                DivisionId = jo.DivisionId,
                DocTypeId = jo.DocTypeId,
            }
                         );

            if (!string.IsNullOrEmpty(svm.ProductId))
            {
                Query = Query.Where(m => ProductIdArr.Contains(m.ProductId.ToString()));
            }

            if (!string.IsNullOrEmpty(svm.JobOrderId))
            {
                Query = Query.Where(m => SaleOrderIdArr.Contains(m.JobOrderHeaderId.ToString()));
            }

            if (!string.IsNullOrEmpty(svm.ProductGroupId))
            {
                Query = Query.Where(m => ProductGroupIdArr.Contains(m.ProductGroupId.ToString()));
            }

            if (!string.IsNullOrEmpty(svm.Dimension1Id))
            {
                Query = Query.Where(m => Dime1IdArr.Contains(m.Dimension1Id.ToString()));
            }

            if (!string.IsNullOrEmpty(svm.Dimension2Id))
            {
                Query = Query.Where(m => Dime2IdArr.Contains(m.Dimension2Id.ToString()));
            }

            if (!string.IsNullOrEmpty(svm.Dimension3Id))
            {
                Query = Query.Where(m => Dime1IdArr.Contains(m.Dimension3Id.ToString()));
            }

            if (!string.IsNullOrEmpty(svm.Dimension4Id))
            {
                Query = Query.Where(m => Dime2IdArr.Contains(m.Dimension4Id.ToString()));
            }

            if (!string.IsNullOrEmpty(settings.filterContraSites))
            {
                Query = Query.Where(m => contraSites.Contains(m.SiteId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.SiteId == Header.SiteId);
            }
            if (!string.IsNullOrEmpty(settings.filterContraDivisions))
            {
                Query = Query.Where(m => contraDivisions.Contains(m.DivisionId.ToString()));
            }
            else
            {
                Query = Query.Where(m => m.DivisionId == Header.DivisionId);
            }

            if (!string.IsNullOrEmpty(settings.filterContraDocTypes))
            {
                Query = Query.Where(m => contraDocTypes.Contains(m.DocTypeId.ToString()));
            }


            return((from p in Query
                    select new JobOrderInspectionRequestLineViewModel
            {
                BalanceQty = p.BalanceQty,
                Qty = p.Qty,
                JobOrderDocNo = p.JobOrderDocNo,
                ProductName = p.ProductName,
                ProductId = p.ProductId,
                JobOrderInspectionRequestHeaderId = p.JobOrderInspectionRequestHeaderId,
                JobOrderLineId = p.JobOrderLineId,
                Dimension1Id = p.Dimension1Id,
                Dimension2Id = p.Dimension2Id,
                Dimension3Id = p.Dimension3Id,
                Dimension4Id = p.Dimension4Id,
                Dimension1Name = p.Dimension1Name,
                Dimension2Name = p.Dimension2Name,
                Dimension3Name = p.Dimension3Name,
                Dimension4Name = p.Dimension4Name,
                Specification = p.Specification,
                UnitId = p.UnitId,
                UnitName = p.UnitName,
                unitDecimalPlaces = p.unitDecimalPlaces,
                DealunitDecimalPlaces = p.DealunitDecimalPlaces,
                ProductUidName = p.ProductUidName,
                ProductUidId = p.ProductUidId,
                JobOrderHeaderId = p.JobOrderHeaderId,
                ProdOrderLineId = p.ProdOrderLineId,
            }).ToList());
        }