private void PopulateRoomData(DateTime period, int clientId)
        {
            dtRoom            = RoomBillingBL.GetRoomBillingDataByClientID(ContextBase, period, clientId);
            gvRoom.DataSource = dtRoom;
            gvRoom.DataBind();

            lblRoomsSum.Text = string.Empty;
            if (dtRoom.Rows.Count > 0)
            {
                double totalRoomCharge = Convert.ToDouble(dtRoom.Compute("SUM(LineCost)", string.Empty));
                lblRoom.Text = string.Format("Total room usage fees: {0:$#,##0.00}", totalRoomCharge);
                UpdateRoomSums(dtRoom, lblRoomsSum);
            }
            else
            {
                lblRoom.Text = "No room usage in this period";
            }

            lblRoom.Visible = true;
        }
        private void PopulateRoomDetailData(DateTime period, int clientId)
        {
            // gets either current or prior period data
            DataTable dtRoom = RoomBillingBL.GetRoomBillingDataByClientID(ContextBase, period, clientId);

            if (!dtRoom.Columns.Contains("IsParent"))
            {
                dtRoom.Columns.Add("IsParent", typeof(bool));
            }

            if (!dtRoom.Columns.Contains("ParentID"))
            {
                dtRoom.Columns.Add("ParentID", typeof(int));
            }

            if (!dtRoom.Columns.Contains("RowCssClass"))
            {
                dtRoom.Columns.Add("RowCssClass", typeof(string));
            }

            var rooms = CacheManager.Current.Rooms();

            foreach (DataRow dr in dtRoom.Rows)
            {
                var r = rooms.FirstOrDefault(x => x.RoomID == dr.Field <int>("RoomID"));

                if (r.ParentID.HasValue)
                {
                    dr.SetField("IsParent", false);
                    dr.SetField("ParentID", r.ParentID.Value);
                    dr.SetField("RowCssClass", "child");
                }
                else
                {
                    dr.SetField("IsParent", true);
                    dr.SetField("ParentID", r.RoomID);
                    dr.SetField("RowCssClass", "parent");
                }
            }

            dtRoom.DefaultView.Sort = "ParentID ASC, IsParent DESC, Room ASC";

            rptRoomDetail.DataSource = dtRoom.DefaultView;
            rptRoomDetail.DataBind();

            decimal totalCleanRoomHours = 0, totalWetChemHours = 0, totalTestLabHours = 0, totalOrganicsHours = 0, totalLnfHours = 0;

            foreach (DataRow dr in dtRoom.Rows)
            {
                LabRoom room = Rooms.GetRoom(dr.Field <int>("RoomID"));

                // Using Convert.ToDecimal because value might be decimal or double depending no if it is from RoomBilling or RoomBillingTemp
                decimal hours = Convert.ToDecimal(dr["Hours"]);

                if (room == LabRoom.CleanRoom)
                {
                    totalCleanRoomHours += hours;
                }
                else if (room == LabRoom.ChemRoom)
                {
                    totalWetChemHours += hours;
                }
                else if (room == LabRoom.TestLab)
                {
                    totalTestLabHours += hours;
                }
                else if (room == LabRoom.OrganicsBay)
                {
                    totalOrganicsHours += hours;
                }
                else if (room == LabRoom.LNF)
                {
                    totalLnfHours += hours;
                }
            }

            string lnfName = "LNF";
            string cleanRoomName = "Clean Room";
            string wetChemName   = "ROBIN";

            lblRoomHours.Text = $"|  {lnfName}: {totalLnfHours:#0.00} hours, {cleanRoomName}: {totalCleanRoomHours:#0.00} hours, {wetChemName}: {totalWetChemHours:#0.00} hours";

            lblRoomsSum.Text = string.Empty;
            if (dtRoom.Rows.Count > 0)
            {
                double totalRoomCharge = Convert.ToDouble(dtRoom.Compute("SUM(LineCost)", string.Empty));
                lblRoom.Text = string.Format("Total room usage fees: {0:$#,##0.00}", totalRoomCharge);
                UpdateRoomSums(dtRoom, lblRoomsSum);
            }
            else
            {
                lblRoom.Text = "No room usage in this period";
            }

            lblRoom.Visible = true;
        }
        protected override void RunReport(DateTime period, int clientId)
        {
            Session.Remove("UserUsageSummaryTables");
            Session.Remove("UserUsageSummaryTables20110701");

            lblRoom.Visible               = false;
            lblTool.Visible               = false;
            lblStore.Visible              = false;
            divTool.Visible               = false;
            lblActivatedToolFee.Visible   = false;
            lblCancelledToolFee.Visible   = false;
            lblRoomsSum.Visible           = false;
            lblRoomsSum.Text              = string.Empty;
            lblRoomsSumActivated.Visible  = false;
            lblRoomSumUnCancelled.Visible = false;
            lblRoomsSumActivated.Text     = string.Empty;
            lblRoomSumUnCancelled.Text    = string.Empty;

            DateTime CutoffStart = new DateTime(2009, 7, 1);
            DateTime CutoffEnd   = new DateTime(2010, 7, 1);

            if (period < CutoffStart)
            {
                Response.Redirect(string.Format("~/IndSumUsage.aspx?p={0:yyyy-MM-dd}&cid={1}", period, clientId));
                return;
            }

            if (period >= CutoffEnd)
            {
                Response.Redirect(string.Format("~/IndUserUsageSummary20100701.aspx?p={0:yyyy-MM-dd}&cid={1}", period, clientId));
                return;
            }

            //Room
            dtRoom            = RoomBillingBL.GetRoomBillingDataByClientID(ContextBase, period, clientId);
            gvRoom.DataSource = dtRoom;
            gvRoom.DataBind();

            if (dtRoom.Rows.Count > 0)
            {
                double totalRoomCharge = Convert.ToDouble(dtRoom.Compute("SUM(LineCost)", string.Empty));
                lblRoom.Text = string.Format("Total room usage fees: {0:$#,##0.00}", totalRoomCharge);
                UpdateRoomSums(dtRoom, lblRoomsSum);
            }
            else
            {
                lblRoom.Text = "No room usage during period";
            }

            lblRoom.Visible = true;

            //Tool
            DataSet dsTool = ToolBillingBL.GetToolBillingDataByClientID20110401(ContextBase, period, clientId);

            dtToolActivated   = dsTool.Tables[0];
            dtToolUncancelled = dsTool.Tables[1];
            dtToolForgiven    = dsTool.Tables[2];

            gvTool.DataSource = dtToolActivated;
            gvTool.DataBind();

            gvToolCancelled.DataSource = dtToolUncancelled;
            gvToolCancelled.DataBind();

            gvToolForgiven.DataSource = dtToolForgiven;
            gvToolForgiven.DataBind();

            double subTotalActivated   = 0;
            double subTotalUncancelled = 0;

            if (dtToolActivated.Rows.Count > 0)
            {
                subTotalActivated           = Convert.ToDouble(dtToolActivated.Compute("SUM(LineCost)", string.Empty));
                lblActivatedToolFee.Text    = string.Format("| Sub Total: {0:$#,##0.00}", subTotalActivated);
                lblActivatedToolFee.Visible = true;

                UpdateRoomSums(dtToolActivated, lblRoomsSumActivated);
            }

            if (dtToolUncancelled.Rows.Count > 0)
            {
                subTotalUncancelled         = Convert.ToDouble(dtToolUncancelled.Compute("SUM(LineCost)", string.Empty));
                lblCancelledToolFee.Text    = string.Format("| Sub Total: {0:$#,##0.00}", subTotalUncancelled);
                lblCancelledToolFee.Visible = true;

                UpdateRoomSums(dtToolUncancelled, lblRoomSumUnCancelled);
            }

            if (subTotalActivated == 0 && subTotalUncancelled == 0)
            {
                lblTool.Text = "No tool usage fees during this period";
            }
            else
            {
                lblTool.Text = string.Format("Total tool usage fees: {0:$#,##0.00}", subTotalActivated + subTotalUncancelled);
            }

            lblTool.Visible = true;
            divTool.Visible = true;

            //Store
            dtStore            = StoreBillingBL.GetStoreBillingDataByClientID(ContextBase, period, clientId);
            gvStore.DataSource = dtStore;
            gvStore.DataBind();

            if (dtStore.Rows.Count > 0)
            {
                double totalStoreCharge = Convert.ToDouble(dtStore.Compute("SUM(LineCost)", string.Empty));
                lblStore.Text = string.Format("Total store usage fees: {0:$#,##0.00}", totalStoreCharge);
            }
            else
            {
                lblStore.Text = "No store usage during period";
            }

            lblStore.Visible = true;

            DataTable dtSummary = new DataTable();

            dtSummary.Columns.Add("OrgID", typeof(int));
            dtSummary.Columns.Add("OrgName", typeof(string));
            dtSummary.Columns.Add("RoomTotal", typeof(double));
            dtSummary.Columns.Add("ToolTotal", typeof(double));
            dtSummary.Columns.Add("StoreTotal", typeof(double));

            int      currentOrgId, previousOrgId = -1;
            DataView dv = dtRoom.DefaultView;

            dv.Sort = "OrgID";

            foreach (DataRowView drv in dv)
            {
                currentOrgId = Convert.ToInt32(drv["OrgID"]);
                if (currentOrgId != previousOrgId)
                {
                    DataRow nr = dtSummary.NewRow();
                    nr["OrgID"]      = currentOrgId;
                    nr["OrgName"]    = drv["OrgName"];
                    nr["RoomTotal"]  = 0;
                    nr["ToolTotal"]  = 0;
                    nr["StoreTotal"] = 0;
                    dtSummary.Rows.Add(nr);
                }
                previousOrgId = currentOrgId;
            }

            foreach (DataRow dr in dtSummary.Rows)
            {
                SetTotalPerOrgCharge(dtRoom, dr, "RoomTotal");
                SetTotalPerOrgCharge(dtToolActivated, dr, "ToolTotal");
                SetTotalPerOrgCharge(dtToolUncancelled, dr, "ToolTotal");
                SetTotalPerOrgCharge(dtStore, dr, "StoreTotal");
            }

            dlSummary.DataSource = dtSummary;
            dlSummary.DataBind();

            //2009-10-12 future billing button display
            int billingTypeId = BillingTypes.Other;

            if (dtRoom.Rows.Count > 0)
            {
                billingTypeId = dtRoom.Rows[0].Field <int>("BillingTypeID");
            }
            else if (dsTool.Tables[0].Rows.Count > 0)
            {
                billingTypeId = dsTool.Tables[0].Rows[0].Field <int>("BillingTypeID");
            }
            else if (dsTool.Tables[1].Rows.Count > 0)
            {
                billingTypeId = dsTool.Tables[1].Rows[0].Field <int>("BillingTypeID");
            }
            else if (dsTool.Tables[2].Rows.Count > 0)
            {
                billingTypeId = dsTool.Tables[2].Rows[0].Field <int>("BillingTypeID");
            }

            int[] specialBillingTypesForSomeUnknownReason = { BillingTypes.Int_Ga, BillingTypes.Int_Si, BillingTypes.ExtAc_Ga, BillingTypes.ExtAc_Si };
            if (specialBillingTypesForSomeUnknownReason.Contains(billingTypeId))
            {
                btnCurrent.Visible   = true;
                btnCurrent.BackColor = System.Drawing.Color.LightGray;

                btnFuture.Visible   = true;
                btnFuture.BackColor = System.Drawing.Color.White;
            }
            else
            {
                btnFuture.Visible  = false;
                btnCurrent.Visible = false;
            }
        }