public int InsertPO(POs PO)
 {
     //context.POs.Add(PO);
     return _IPORepository.CreatePO(PO);
 }
        public POs GetPOsForClientStoreOrganization(POs PO)
        {
            try
            {
                //using (var dataContext = new WorkersInMotionDB())
                //{
                //    return (from p in dataContext.POs
                //            where
                //            (PO.PONumber == p.PONumber)
                //            && (p.OrganizationGUID == PO.OrganizationGUID)
                //            && (p.MarketID == PO.MarketID)
                //            && (p.PlaceID == PO.PlaceID)
                //            select p).FirstOrDefault();

                //}

                SqlParameter[] Param = new SqlParameter[4];
                Param[0] = new SqlParameter("@pOrganizationGUID", SqlDbType.UniqueIdentifier);
                Param[0].Value = (object)PO.OrganizationGUID ?? DBNull.Value;
                Param[1] = new SqlParameter("@pPONumber", SqlDbType.NVarChar, 50);
                Param[1].Value = (object)PO.PONumber ?? DBNull.Value;
                Param[2] = new SqlParameter("@pMarketID", SqlDbType.NVarChar, 50);
                Param[2].Value = (object)PO.MarketID ?? DBNull.Value;
                Param[3] = new SqlParameter("@pPlaceID", SqlDbType.NVarChar, 50);
                Param[3].Value = (object)PO.PlaceID ?? DBNull.Value;

                return context.Database.SqlQuery<POs>("select * from POs where OrganizationGUID=@pOrganizationGUID and"
                            + " PONumber=@pPONumber and MarketID=@pMarketID and PlaceID=@pPlaceID", Param).FirstOrDefault();

            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public POs GetPOs(POs PO)
        {
            try
            {
                //using (var dataContext = new WorkersInMotionDB())
                //{
                //    return (from p in dataContext.POs
                //            where
                //            (PO.POGUID == Guid.Empty || p.POGUID == PO.POGUID)
                //            && (string.IsNullOrEmpty(PO.PONumber) || p.PONumber == PO.PONumber)
                //            select p).FirstOrDefault();

                //}
                SqlParameter[] Param = new SqlParameter[2];
                Param[0] = new SqlParameter("@pPOGUID", SqlDbType.UniqueIdentifier);
                Param[0].Value = PO.POGUID;
                Param[1] = new SqlParameter("@pPONumber", SqlDbType.NVarChar, 50);
                Param[1].Value = (object)PO.PONumber ?? DBNull.Value;

                return context.Database.SqlQuery<POs>("select * from POs where"
                                    + "(POGUID=@pPOGUID or @pPOGUID=(cast(cast(0 as binary) as uniqueidentifier)))"
                                    + " AND (PONumber=@pPONumber OR @pPONumber is NULL or @pPONumber='')", Param).FirstOrDefault();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public ActionResult Index(string customerid = "", string selection = "", string RowCount = "", int page = 1, string search = "")
        {
            Logger.Debug("Inside Job Controller- Index");
            try
            {
                int totalPage = 0;
                int totalRecord = 0;
                int pCount = 0;
                if (Session["OrganizationGUID"] != null)
                {
                    if (!string.IsNullOrEmpty(RowCount))
                    {
                        int.TryParse(RowCount, out pCount);
                        pageCountList(pCount);

                    }
                    else
                    {
                        pageCountList(pCount);
                    }


                    var poList = new POViewLists();
                    poList.POViewList = new List<POView>();
                    var poGroup = new List<POs>();
                    POs lpo = new POs();
                    if (!string.IsNullOrEmpty(customerid))
                    {
                        Place _place = _IPlaceRepository.GetPlaceByID(new Guid(customerid));
                        if (_place != null)
                        {
                            poGroup = _IPORepository.GetPOListByPlaceID(new Guid(Session["OrganizationGUID"].ToString()), _place.PlaceID).ToList();
                        }

                    }
                    else
                    {
                        poGroup = _IPORepository.GetPOList(new Guid(Session["OrganizationGUID"].ToString())).ToList();
                    }

                    StringBuilder sb = new StringBuilder();
                    sb.Append("<div class='actions'>");
                    sb.Append("<div class='btn-group'>");
                    if (!string.IsNullOrEmpty(customerid))
                    {
                        sb.Append("<a href='#' id='ulaworkergroup' class='btn green' data-toggle='dropdown'><i class='icon-map-marker'></i> " + _IPlaceRepository.GetPlaceByID(new Guid(customerid)).PlaceName + " <i class='icon-angle-down'></i></a>");
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(selection) && selection == "All")
                        {
                            sb.Append("<a href='#' id='ulaworkergroup' class='btn green' data-toggle='dropdown'><i class='icon-map-marker'></i> All <i class='icon-angle-down'></i></a>");
                        }
                        else
                        {
                            sb.Append("<a href='#' id='ulaworkergroup' class='btn green' data-toggle='dropdown'><i class='icon-map-marker'></i> Select Client <i class='icon-angle-down'></i></a>");
                        }
                    }
                    sb.Append("<ul id='ulworkgroup' style='height:100px;overflow-y:scroll' class='dropdown-menu pull-right'>");
                    if (string.IsNullOrEmpty(selection) || selection != "All")
                    {
                        sb.Append("<li><a href=" + Url.Action("Index", "PO", new { selection = "All" }) + ">All</a></li>");
                    }
                    List<Place> placeList = _IPlaceRepository.GetPlaceByOrganizationGUID(new Guid(Session["OrganizationGUID"].ToString())).ToList();
                    if (placeList != null)
                    {
                        foreach (Place item in placeList)
                        {
                            sb.Append("<li><a href=" + Url.Action("Index", "PO", new { customerid = item.PlaceGUID.ToString() }) + " data-groupguid=" + item.PlaceGUID + ">" + item.PlaceName + "</a></li>");
                        }
                    }
                    sb.Append("</ul>");
                    sb.Append("</div>");
                    sb.Append("</div>");
                    ViewBag.CustomerList = sb.ToString();
                    if (poGroup != null && poGroup.Count > 0)
                    {
                        if (!string.IsNullOrEmpty(search))
                        {
                            search = search.ToLower();
                            poGroup = poGroup.Where(x => (x.PlaceID != null ? _IPlaceRepository.GetPlaceByID(x.PlaceID, new Guid(x.OrganizationGUID.ToString())).PlaceName.ToLower() : "").Contains(search)
                                    || (!String.IsNullOrEmpty(x.PONumber) && GetJobName(x.PONumber).ToLower().Contains(search))
                                    || (!String.IsNullOrEmpty(x.PONumber) && x.PONumber.ToLower().Contains(search))
                                    || (!String.IsNullOrEmpty(x.MarketID) && x.MarketID.ToLower().Contains(search))
                                    || (!String.IsNullOrEmpty(x.InstallerName) && x.InstallerName.ToLower().Contains(search))
                                    || (!String.IsNullOrEmpty(x.POCustomerName) && x.POCustomerName.ToLower().Contains(search))
                                    || (!String.IsNullOrEmpty(x.POCustomerPhone) && x.POCustomerPhone.ToLower().Contains(search))
                                    ).ToList();


                            poList.POViewList = poList.POViewList.Where(
                                p => (p.PlaceName.ToLower().StartsWith(search))
                                || (p.PONumber.ToLower().StartsWith(search)) 
                                || (p.MarketID.ToLower().StartsWith(search)) 
                                || (p.InstallerName.ToLower().StartsWith(search)) 
                                || (p.POCustomerName.ToLower().StartsWith(search)) 
                                || (p.POCustomerPhone.ToLower().StartsWith(search))).ToList();
                        }

                        totalRecord = poGroup.ToList().Count;
                        totalPage = (totalRecord / (int)ViewBag.pageCountValue) + ((totalRecord % (int)ViewBag.pageCountValue) > 0 ? 1 : 0);

                        ViewBag.TotalRows = totalRecord;

                        poGroup = poGroup.OrderBy(a => a.OrganizationGUID).Skip(((page - 1) * (int)ViewBag.pageCountValue)).Take((int)ViewBag.pageCountValue).ToList();


                        foreach (var pos in poGroup.ToList())
                        {
                            POView Pos = new POView();
                            Pos.POGUID = pos.POGUID;
                            Pos.OrganizationGUID = pos.OrganizationGUID;
                            Pos.RegionGUID = pos.RegionGUID;
                            Pos.TerritoryGUID = pos.TerritoryGUID;
                            Pos.PONumber = pos.PONumber;
                            Pos.RegionName = pos.RegionGUID != null && pos.RegionGUID != Guid.Empty ? _IRegionRepository.GetRegionNameByRegionGUID(new Guid(pos.RegionGUID.ToString())) : "";
                            Pos.TerritoryName = pos.TerritoryGUID != null && pos.TerritoryGUID != Guid.Empty ? _ITerritoryRepository.GetTerritoryNameByTerritoryGUID(new Guid(pos.TerritoryGUID.ToString())) : "";
                            Pos.InstallerName = pos.InstallerName;
                            Pos.POCustomerName = pos.POCustomerName;
                            Pos.POCustomerPhone = pos.POCustomerPhone;
                            Pos.PoCustomerMobile = pos.POCustomerMobile;
                            if (!string.IsNullOrEmpty(pos.MarketID) && !string.IsNullOrEmpty(pos.PlaceID))
                            {
                                Market _market = _IMarketRepository.GetMarketByCustomerID(new Guid(pos.OrganizationGUID.ToString()), pos.PlaceID, pos.MarketID);
                                if (_market != null)
                                {
                                    Pos.StoreName = _market.MarketName;
                                }
                                else
                                {
                                    Pos.StoreName = "";
                                }
                            }
                            else
                            {
                                Pos.StoreName = "";
                            }
                            if (!string.IsNullOrEmpty(pos.PlaceID))
                            {
                                Place _place = _IPlaceRepository.GetPlaceByID(pos.PlaceID, new Guid(pos.OrganizationGUID.ToString()));
                                if (_place != null)
                                {
                                    Pos.PlaceName = _place.PlaceName;
                                }
                                else
                                {
                                    Pos.PlaceName = "";
                                }
                            }
                            else
                            {
                                Pos.PlaceName = "";
                            }
                            Pos.EmailID = !string.IsNullOrEmpty(pos.MarketID) ? GetEmails(pos.OrganizationGUID, pos.MarketID, pos.PlaceID) : "";
                            Pos.JobName = GetJobName(pos.PONumber);
                            Pos.Status = pos.Status;
                            Pos.Description = pos.Description;
                            Pos.PlaceID = pos.PlaceID;
                            Pos.LocationType = pos.LocationType;
                            Pos.MarketID = pos.MarketID;
                            Pos.EndCustomerAddress = pos.EndCustomerAddress;
                            Pos.EndCustomerName = pos.EndCustomerName;
                            Pos.EndCustomerPhone = pos.EndCustomerPhone;
                            Pos.WorkerName = pos.WorkerName;
                            Pos.CustomBooleanValue = pos.CustomBooleanValue;
                            Pos.JobClass = pos.JobClass;
                            Pos.OrderDate = pos.OrderDate.ToString();
                            Pos.PreferredDateTime = pos.PreferredDateTime.ToString();
                            Pos.EstimatedCost = pos.EstimatedCost;
                            Pos.CreateDate = pos.CreateDate.ToString();
                            Pos.CreateBy = pos.CreateBy;
                            Pos.LastModifiedDate = pos.LastModifiedDate.ToString();
                            Pos.LastModifiedBy = pos.LastModifiedBy;
                            poList.POViewList.Add(Pos);

                        }
                    }                    

                    return View(poList);
                }
                else
                {
                    return RedirectToAction("SessionTimeOut", "User");
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return RedirectToAction("../User/Login");
            }
        }
        private int InsertPO(POs NewPO)
        {
            //context.POs.Add(NewPO);

            SqlParameter[] Param = new SqlParameter[25];
            Param[0] = new SqlParameter("@pPOGUID", SqlDbType.UniqueIdentifier);
            Param[0].Value = NewPO.POGUID;
            Param[1] = new SqlParameter("@pOrganizationGUID", SqlDbType.UniqueIdentifier);
            Param[1].Value = (object)NewPO.OrganizationGUID ?? DBNull.Value;
            Param[2] = new SqlParameter("@pRegionGUID", SqlDbType.UniqueIdentifier);
            Param[2].Value = (object)NewPO.RegionGUID ?? DBNull.Value;
            Param[3] = new SqlParameter("@pTerritoryGUID", SqlDbType.UniqueIdentifier);
            Param[3].Value = (object)NewPO.TerritoryGUID ?? DBNull.Value;
            Param[4] = new SqlParameter("@pPONumber", SqlDbType.NVarChar, 50);
            Param[4].Value = (object)NewPO.PONumber ?? DBNull.Value;
            Param[5] = new SqlParameter("@pStatus", SqlDbType.SmallInt);
            Param[5].Value = (object)NewPO.Status ?? DBNull.Value;
           
            Param[6] = new SqlParameter("@pPlaceID", SqlDbType.NVarChar, 50);
            Param[6].Value = (object)NewPO.PlaceID ?? DBNull.Value;
            //Heroku
            //Param[8] = new SqlParameter("@pLocationType", SqlDbType.SmallInt);
            //Param[8].Value = (object)NewPO.LocationType ?? DBNull.Value;
            Param[7] = new SqlParameter("@pMarketID", SqlDbType.NVarChar, 50);
            Param[7].Value = (object)NewPO.MarketID ?? DBNull.Value;
            Param[8] = new SqlParameter("@pEndCustomerAddress", SqlDbType.NVarChar, -1);
            Param[8].Value = (object)NewPO.EndCustomerAddress ?? DBNull.Value;
            Param[9] = new SqlParameter("@pEndCustomerName", SqlDbType.NVarChar, -1);
            Param[9].Value = (object)NewPO.EndCustomerName ?? DBNull.Value;
            Param[10] = new SqlParameter("@pEndCustomerPhone", SqlDbType.NVarChar, -1);
            Param[10].Value = (object)NewPO.EndCustomerPhone ?? DBNull.Value;
           
           
            Param[11] = new SqlParameter("@pCreateDate", SqlDbType.DateTime);
            Param[11].Value = (object)NewPO.CreateDate ?? DBNull.Value;
            Param[12] = new SqlParameter("@pCreateBy", SqlDbType.UniqueIdentifier);
            Param[12].Value = (object)NewPO.CreateBy ?? DBNull.Value;
            Param[13] = new SqlParameter("@pLastModifiedDate", SqlDbType.DateTime);
            Param[13].Value = (object)NewPO.LastModifiedDate ?? DBNull.Value;
            Param[14] = new SqlParameter("@pLastModifiedBy", SqlDbType.UniqueIdentifier);
            Param[14].Value = (object)NewPO.LastModifiedBy ?? DBNull.Value;
           
            Param[15] = new SqlParameter("@pTerritoryID", SqlDbType.NVarChar, 50);
            Param[15].Value = (object)NewPO.TerritoryID ?? DBNull.Value;
          
            Param[16] = new SqlParameter("@pRMUserID", SqlDbType.NVarChar, 50);
            Param[16].Value = (object)NewPO.RMUserID ?? DBNull.Value;
            Param[17] = new SqlParameter("@pFMUserID", SqlDbType.NVarChar, 50);
            Param[17].Value = (object)NewPO.FMUserID ?? DBNull.Value;
            Param[18] = new SqlParameter("@pPOJobType", SqlDbType.NVarChar, 50);
            Param[18].Value = (object)NewPO.POJobType ?? DBNull.Value;
            Param[19] = new SqlParameter("@pPOJobCode", SqlDbType.NVarChar, 50);
            Param[19].Value = (object)NewPO.POJobCode ?? DBNull.Value;
            Param[20] = new SqlParameter("@pPOJson", SqlDbType.NVarChar, -1);
            Param[20].Value = (object)NewPO.POJson ?? DBNull.Value;
            Param[21] = new SqlParameter("@pInstallerName", SqlDbType.NVarChar, 50);
            Param[21].Value = (object)NewPO.InstallerName ?? DBNull.Value;
            Param[22] = new SqlParameter("@pPOCustomerName", SqlDbType.NVarChar, 50);
            Param[22].Value = (object)NewPO.POCustomerName ?? DBNull.Value;
            Param[23] = new SqlParameter("@pPOCustomerPhone", SqlDbType.NVarChar, 50);
            Param[23].Value = (object)NewPO.POCustomerPhone ?? DBNull.Value;
            Param[24] = new SqlParameter("@pPOCustomerMobile", SqlDbType.NVarChar, 50);
            Param[24].Value = (object)NewPO.POCustomerMobile ?? DBNull.Value;


            return context.Database.ExecuteSqlCommand("insert into POs(POGUID,OrganizationGUID,RegionGUID,TerritoryGUID,PONumber,Status,PlaceID"
                + " ,MarketID,EndCustomerAddress,EndCustomerName,EndCustomerPhone"
                + ",CreateDate,CreateBy,LastModifiedDate,LastModifiedBy,TerritoryID,RMUserID,FMUserID,POJobType"
                + ",POJobCode,POJson,InstallerName,POCustomerName,POCustomerPhone,POCustomerMobile)"
                + "values(@pPOGUID,@pOrganizationGUID,@pRegionGUID,@pTerritoryGUID,@pPONumber"
                + ",@pStatus,@pPlaceID,@pMarketID,@pEndCustomerAddress,@pEndCustomerName,@pEndCustomerPhone"
                + ",@pCreateDate,@pCreateBy,@pLastModifiedDate,@pLastModifiedBy,@pTerritoryID"
                + ",@pRMUserID,@pFMUserID,@pPOJobType,@pPOJobCode,@pPOJson,@pInstallerName,@pPOCustomerName,@pPOCustomerPhone,@pPOCustomerMobile)", Param);
        }
 public int CreatePO(POs NewPO)
 {
     return InsertPO(NewPO);
     //return Save();
 }