using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using System.Configuration;
using MySql.Data.MySqlClient;
using MySql.Data;
using CLS_BL;
using CLS_DL;
using CLS_PL;


namespace SUNWEB
{
    public partial class SunMaster : System.Web.UI.MasterPage
    {

        BL_SELECT ObjSel = new BL_SELECT();
        BLCompany ObjBL = new BLCompany();
        PLCOMMON ObjPL = new PLCOMMON();
        BL_USER ObjUserBL = new BL_USER(); //This Object for All User and Page Related fuctions included 
        PLCUSTOMER ObjCustPL = new PLCUSTOMER();
        MYSQLCLOUD ObjMain = new MYSQLCLOUD();
        string cd_ID = string.Empty;
        string cm_ID = string.Empty;
        string CompanyName = string.Empty;
        DataTable Dt = new DataTable();
        // Notification Variable
        string OrderCnt = string.Empty;
        string ReOrderCnt = string.Empty;
        string ActiveCallvalue = string.Empty;
        string ChkTableExist = string.Empty;

        // Reorder Variables 

        DataTable DtResult = new DataTable();
        DataTable DTFinalReorderdata = new DataTable();
        

        // Other Variables 

        string Branchid = string.Empty;
        string BranchName = string.Empty;
        string DatabaseName = string.Empty;
        string RCount = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            //  Changed Code on 24-03-23
            if (Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            if (!string.IsNullOrEmpty(Session["UserName"] as string))
            {
                lblSessionUser.Text = Session["UserName"].ToString();
            }
          
            cd_ID = Session["Cd_Id"].ToString();
            cm_ID = ObjSel.GETcmid(cd_ID);
            CompanyName = ObjSel.GetComp_Name(cm_ID);
            lblCompanyName.Text = CompanyName;
            lblCompanyLable.Text = CompanyName;

            Dt = ObjSel.GetNotificationList();
            dtNotification.DataSource = Dt;
            dtNotification.DataBind();

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Dashboard.aspx", StringComparison.OrdinalIgnoreCase))
            {
                if (Session["UserName"].ToString() == "lph1" || Session["UserName"].ToString() == "Lph1")
                {
                    Response.Redirect("DashboardNew.aspx", false);
                }

            }

            #region    Active Page Selection

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Dashboard.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rDashboard.Attributes.Add("Style", "color:#fff");
            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/SummaryReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rSummaryReport.Attributes.Add("Style", "color:#fff");
            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/ItemSale.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rItemSale.Attributes.Add("Style", "color:#fff");
            }
            
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/TaxAnalysis.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rTaxAnalysis.Attributes.Add("Style", "color:#fff");
            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/TopsaleNew.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rTopsaleNew.Attributes.Add("Style", "color:#fff");
            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/MonthlySale.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rMonthlySale.Attributes.Add("Style", "color:#fff");
            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/HourlySale.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rHourlySale.Attributes.Add("Style", "color:#fff");
            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/GroupSale.aspx", StringComparison.OrdinalIgnoreCase))
            {
               rGroupSale.Attributes.Add("Style", "color:#fff");
            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/CondimentReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rCondimentReport.Attributes.Add("Style", "color:#fff");
            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/JournalReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rJournalReport.Attributes.Add("Style", "color:#fff");
            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/SpecialDiscount.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rSpecialDiscount.Attributes.Add("Style", "color:#fff");
            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/RAPOReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rRAPOReport.Attributes.Add("Style", "color:#fff");
            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/InventoryListReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rInventoryListReport.Attributes.Add("Style", "color:#fff");
            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/RetailPayment.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rRetailPayment.Attributes.Add("Style", "color:#fff");
            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/DetailstockReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rDetailstockReport.Attributes.Add("Style", "color:#fff");
            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/StockMovementNew.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rStockMovementNew.Attributes.Add("Style", "color:#fff");
            }

                     
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/ReorderReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rReorderReport.Attributes.Add("Style", "color:#fff");
            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/TimeInOutReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
              rTimeInOutReport.Attributes.Add("Style", "color:#fff");
            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/TimedurationReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
              rTimedurationReport.Attributes.Add("Style", "color:#fff");

            }
            // Module

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/InventoryMaster.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rInventoryMaster.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/InventoryPackage.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rInventoryPackage.Attributes.Add("Style", "color:#fff");

            }


            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/CategoryMaster.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rCategoryMaster.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/GroupMaster.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rGroupMaster.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/NewGroupCondiment.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rNewGroupCondiment.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Modifier.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rModifier.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/TaxMaster.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rTaxMaster.Attributes.Add("Style", "color:#fff");

            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/InventoryPromotions.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rInventoryPromotions.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Inventory.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rInventory.Attributes.Add("Style", "color:#fff");

            }

            //if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/StockAdjust.aspx", StringComparison.OrdinalIgnoreCase))
            //{
            //    rStockAdjust.Attributes.Add("Style", "color:#fff");

            //}

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/InventoryStockCollector.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rInventoryStockCollector.Attributes.Add("Style", "color:#fff");

            }
            //Stock

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Stocktransfer.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rStocktransfer.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/StockReceiving.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rStockReceiving.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/StockReturn.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rStockReturn.Attributes.Add("Style", "color:#fff");

            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/StockReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rStockReport.Attributes.Add("Style", "color:#fff");

            }
            //Synchronise
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/UpdateInventory.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rUpdateInventory.Attributes.Add("Style", "color:#fff");

            }


            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/MemberHistory.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rMemberHistory.Attributes.Add("Style", "color:#fff");

            }

            //if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/PointsHistory.aspx", StringComparison.OrdinalIgnoreCase))
            //{
            //    rPointsHistory.Attributes.Add("Style", "color:#fff");

            //}
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/ConsumerBehaviourAnalysis.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rConsumerBehaviourAnalysis.Attributes.Add("Style", "color:#fff");

            }

            //Web
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/CategoryCustomize.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rCategoryCustomize.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/PriceCaptionSetting.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rPriceCaptionSetting.Attributes.Add("Style", "color:#fff");

            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Iorderlasetting.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rIorderlasetting.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/BannerSchedule.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rBannertimeschedule.Attributes.Add("Style", "color:#fff");

            }



            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Layoutsetting.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rLayoutsetting.Attributes.Add("Style", "color:#fff");

            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/MenuGroupCreation.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rMenugroupcreation.Attributes.Add("Style", "color:#fff");

            }


            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/UpsellingSetting.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rUpsellingSetting.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/PromoCode.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rPromoCode.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/OperatingHours.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rOperatingHours.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/BulkImagePostToS3.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rBulkImagePostToS3.Attributes.Add("Style", "color:#fff");

            }

            // CRM


            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/CRMCustomerMaster.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rCRMCustomerMaster3.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/CRMMembershipcampaign.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rCRMMembershipcampaign.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/CampaignAdjustment.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rcampaignAdjustment.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/CampaignReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rCampaignReport.Attributes.Add("Style", "color:#fff");

            }
             //USER

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/UserCreation.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rUserCreation.Attributes.Add("Style", "color:#fff");

            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/UserRights.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rUserRights.Attributes.Add("Style", "color:#fff");

            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Logout.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rLogout.Attributes.Add("Style", "color:#fff");

            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/ZeroIntegration.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rzerointegration.Attributes.Add("Style", "color:#fff");

            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/SupplierwisecostandcollectionReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rSupplierwiseCostReport.Attributes.Add("Style", "color:#fff");

            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/InteractiveDashboard.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rInteractiveDashboard.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Zoho.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rzoho.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/OneTimePromocode.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rOnetimePromoCode.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/InventoryBatchAdjust.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rInventoryBatchAdjust.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/PDPAContends.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rPDPAContends.Attributes.Add("Style", "color:#fff");

            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/PriceSwitch.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rPriceSwitch.Attributes.Add("Style", "color:#fff");

            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Sales.aspx", StringComparison.OrdinalIgnoreCase))
            {
              rSales.Attributes.Add("Style", "color:#fff");

            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/StkMovement.aspx", StringComparison.OrdinalIgnoreCase))
            {
               rstkMovement.Attributes.Add("Style", "color:#fff");

            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/DetailStkReport.aspx", StringComparison.OrdinalIgnoreCase))
            {
               rDetailstockReportNew.Attributes.Add("Style", "color:#fff");
            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/SalesInvoice.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rSalesInvoice.Attributes.Add("Style", "color:#fff");
            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/SalesInvoiceList.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rSalesInvoiceList.Attributes.Add("Style", "color:#fff");
            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/KioskLandingpage.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rKioskLandingpage.Attributes.Add("Style", "color:#fff");
            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/KioskBanner.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rKioskbanner.Attributes.Add("Style", "color:#fff");
            }
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/WebStockSetting.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rWebstockSetting.Attributes.Add("Style", "color:#fff");
            }

            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Quickbook.aspx", StringComparison.OrdinalIgnoreCase))
            {
                rQuickbook.Attributes.Add("Style", "color:#fff");
            }

            



            #endregion


            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Dashboard.aspx", StringComparison.OrdinalIgnoreCase) || Page.TemplateControl.AppRelativeVirtualPath.Equals("~/OnlineKDS.aspx", StringComparison.OrdinalIgnoreCase) || Page.TemplateControl.AppRelativeVirtualPath.Equals("~/OnlineKDSDetails.aspx", StringComparison.OrdinalIgnoreCase))
            {

                string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
                ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);


                ChkTableExist = ObjMain.ExecuteScalar("SELECT table_name FROM information_schema.tables WHERE table_schema = '" + ActiveCallvalue + "' AND table_name = 'olkdsnotification';");

                if (ChkTableExist != "")
                {
                    OrderCnt = ObjMain.ExecuteScalar("select count(*) as Cnt from   " + ActiveCallvalue + ".olkdsnotification where Status=0");


                    if (OrderCnt != "")
                    {
                        if (Convert.ToInt32(OrderCnt) != 0)
                        {
                            lblOrderCnt.Text = OrderCnt;


                            // Sound Alert Code : Works in Local : Issue on IIS 
                            //  System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"D:\KARTHIK\Developement\SUNWEB\SUNWEB\IMG\Sample.wav");
                            // System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\inetpub\wwwroot\TEST\IMG\Sample.wav");
                            // player.Play();


                        }
                        else
                        {
                            dvOrderCnt.Visible = false;
                        }


                    }
                    else
                    {
                        dvOrderCnt.Visible = false;
                    }



                }
                else
                {
                    dvOrderCnt.Visible = false;

                }





            }
            else
            {
                dvOrderCnt.Visible = false;
            }




          
            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/SummaryReport.aspx", StringComparison.OrdinalIgnoreCase))
            {

                liReorder.Visible = false;

                string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
                ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);

                string query = "Select Count(I.InventoryID) as Count "
                          + " From "+ ActiveCallvalue + ".Inventory I Left Outer Join " + ActiveCallvalue + ".InventoryCategories IC On I.CategoryID=IC.CategoryID"
                          + " Left Outer Join " + ActiveCallvalue + ".InventoryCostAverage ICA On I.InventoryID=ICA.InventoryID "
                          + " Where I.Reorderlevel>0"
                          + " And ICA.PQuantity-ICA.CutQuantity-ICA.RetailQty<I.Reorderlevel;";

                ReOrderCnt = ObjMain.ExecuteScalar(query);
                   if (ReOrderCnt != "")
                    {
                        if (Convert.ToInt32(ReOrderCnt) != 0)
                        {
                            lblReorderCount.Text = ReOrderCnt;



                        }
                        else
                        {
                            DivReOrder.Visible = false;
                        }


                    }
                    else
                    {
                    DivReOrder.Visible = false;
                    }



               



            }
            else
            {
                DivReOrder.Visible = false;
            }
           


            if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/Dashboard.aspx", StringComparison.OrdinalIgnoreCase) || Page.TemplateControl.AppRelativeVirtualPath.Equals("~/ReorderReport.aspx", StringComparison.OrdinalIgnoreCase) )
            {
                liReorder.Visible = true;
                GetCumulativeCount();


            }
            else
            {
                liReorder.Visible = false;
            }


           




        }
        /// <summary>
        ///  This Function Get the Reorder Notifications 
        /// </summary>
        public void GetCumulativeCount()
        {

            DataTable DtBranchList = new DataTable();
            // Checking the Valid Branch List Based On Multi  Branch Rights 
            string ReadBranch = " select  P.cd_branchName,  P.cd_BranchAlias, P.cd_BranchId  from  portaldb.companydetails as P   inner join portaldb.subcompmaster as S  on P.cd_BranchId=S.cd_Branchid "
                                + " where S.userid="+ Session["UserId"].ToString() + " and Multi_Access<>'N' And P.status='A' and P.cm_id="+ Session["CM_ID"].ToString() + "  and P.cd_BranchId<>1 ;";
            DtBranchList = ObjMain.GetData_DT(ReadBranch);

            if (DtBranchList.Rows.Count > 0)
            {

                for (int i = 0; i < DtBranchList.Rows.Count; i++)
                {
                    DataRow dr = DtBranchList.Rows[i];
                    Branchid = dr["cd_BranchId"].ToString();
                    BranchName = dr["cd_branchName"].ToString();
                    DatabaseName = dr["cd_BranchAlias"].ToString();

                    if (DTFinalReorderdata.Rows.Count > 0)
                    {

                        

                        DataRow Dr = DTFinalReorderdata.NewRow();
                        Dr["BranchId"] = Branchid;
                        Dr["BranchName"] = BranchName;
                        Dr["DatabaseName"] = DatabaseName;
                        string query = "Select Count(I.InventoryID) as Count "
                        + " From " + DatabaseName + ".Inventory I Left Outer Join " + DatabaseName + ".InventoryCategories IC On I.CategoryID=IC.CategoryID"
                        + " Left Outer Join " + DatabaseName + ".InventoryCostAverage ICA On I.InventoryID=ICA.InventoryID "
                        + " Where I.Reorderlevel>0"
                        + " And ICA.PQuantity-ICA.CutQuantity-ICA.RetailQty<I.Reorderlevel;";

                        RCount = ObjMain.ExecuteScalar(query);
                        Dr["ReorderCount"] = RCount;
                        DTFinalReorderdata.Rows.Add(Dr);


                    }
                    else
                    {
                        DTFinalReorderdata.Columns.Add("BranchId", typeof(string));
                        DTFinalReorderdata.Columns.Add("BranchName", typeof(string));
                        DTFinalReorderdata.Columns.Add("DatabaseName", typeof(string));
                        DTFinalReorderdata.Columns.Add("ReorderCount", typeof(Int32));

                        DataRow Dr = DTFinalReorderdata.NewRow();
                        Dr["BranchId"] = Branchid;
                        Dr["BranchName"] = BranchName;
                        Dr["DatabaseName"] = DatabaseName;

                        string query = "Select Count(I.InventoryID) as Count "
                         + " From " + DatabaseName + ".Inventory I Left Outer Join " + DatabaseName + ".InventoryCategories IC On I.CategoryID=IC.CategoryID"
                         + " Left Outer Join " + DatabaseName + ".InventoryCostAverage ICA On I.InventoryID=ICA.InventoryID "
                         + " Where I.Reorderlevel>0"
                         + " And ICA.PQuantity-ICA.CutQuantity-ICA.RetailQty<I.Reorderlevel;";

                        RCount = ObjMain.ExecuteScalar(query);
                        Dr["ReorderCount"] = RCount;
                        
                        DTFinalReorderdata.Rows.Add(Dr);


                    }




                }


              



                var customers = from customer in DTFinalReorderdata.AsEnumerable()
                                where customer.Field<Int32>("ReorderCount")  >= 1
                                select new
                                {
                                    BranchId = customer.Field<string>("BranchId"),
                                    BranchName = customer.Field<string>("BranchName"),
                                    DatabaseName = customer.Field<string>("DatabaseName"),
                                    ReorderCount = customer.Field<Int32>("ReorderCount")


                                };




                if (customers.Count() > 0)
                {
                    // Calling the Reorder Temporary Datatable values in Datalist 
                    DtReorderList.DataSource = customers;
                    DtReorderList.DataBind();
                }
                else
                {
                    liReorder.Visible = false;
                }
               



            }



        }






    }
}