protected void FillForm()
    {
        try
        {
            if (!CheckIsValidStartEndDates())
            {
                return;
            }

            SeriesChartType chartType = SeriesChartType.StackedBar;
            if (ddlChartType.SelectedValue == "Pie")
            {
                chartType = SeriesChartType.Pie;
            }
            if (ddlChartType.SelectedValue == "Doughnut")
            {
                chartType = SeriesChartType.Doughnut;
            }
            if (ddlChartType.SelectedValue == "StackedBar")
            {
                chartType = SeriesChartType.StackedBar;
            }
            if (ddlChartType.SelectedValue == "Area")
            {
                chartType = SeriesChartType.Area;
            }
            if (ddlChartType.SelectedValue == "BoxPlot")
            {
                chartType = SeriesChartType.BoxPlot;
            }
            if (ddlChartType.SelectedValue == "Column")
            {
                chartType = SeriesChartType.Column;
            }
            if (ddlChartType.SelectedValue == "Line")
            {
                chartType = SeriesChartType.Line;
            }
            if (ddlChartType.SelectedValue == "Point")
            {
                chartType = SeriesChartType.Point;
            }
            if (ddlChartType.SelectedValue == "Range")
            {
                chartType = SeriesChartType.Range;
            }
            if (ddlChartType.SelectedValue == "RangeBar")
            {
                chartType = SeriesChartType.RangeBar;
            }
            if (ddlChartType.SelectedValue == "RangeColumn")
            {
                chartType = SeriesChartType.RangeColumn;
            }

            chartStaffStats.Series["Series1"].ChartType = chartType;


            DataTable tblStats = StaffDB.GetStats2(GetFromDate(), GetToDate(), chkIncDeleted.Checked, chkIncDeleted.Checked);

            int maxToShow = 350;
            if (tblStats.Rows.Count > maxToShow)
            {
                for (int i = tblStats.Rows.Count - 1; i >= maxToShow; i--)
                {
                    tblStats.Rows.RemoveAt(i);
                }
            }

            tblStats.Columns.Add("fullname", typeof(string));
            for (int i = 0; i < tblStats.Rows.Count; i++)
            {
                tblStats.Rows[i]["fullname"] = tblStats.Rows[i]["firstname"] + " " + tblStats.Rows[i]["surname"] +
                                               (tblStats.Rows[i]["staff_id"] == DBNull.Value || (int)tblStats.Rows[i]["staff_id"] < 0 || (!(bool)tblStats.Rows[i]["is_stakeholder"] && !(bool)tblStats.Rows[i]["is_master_admin"] && !(bool)tblStats.Rows[i]["is_admin"])  ? "" : " (Admin) ") +
                                               (tblStats.Rows[i]["staff_id"] == DBNull.Value || (int)tblStats.Rows[i]["staff_id"] < 0 || !(bool)tblStats.Rows[i]["is_provider"]  ? "" : " (Provider) ") +
                                               (tblStats.Rows[i]["is_external"] == DBNull.Value || tblStats.Rows[i]["staff_id"] == DBNull.Value || !((bool)tblStats.Rows[i]["is_external"]) || (int)tblStats.Rows[i]["staff_id"] < 0 ? "" : " (External) ");
            }

            DataView dv = tblStats.DefaultView;

            string field = string.Empty;
            if (ddlType.SelectedValue == "Total Bookings")
            {
                field = "total_bookings";
            }
            if (ddlType.SelectedValue == "New Bookings Added By")
            {
                field = "n_bookings";
            }
            if (ddlType.SelectedValue == "New Patients")
            {
                field = "n_patients";
            }
            if (ddlType.SelectedValue == "Completions")
            {
                field = "n_completions";
            }
            if (ddlType.SelectedValue == "Invoice Total")
            {
                field = "sum_inv_total";
            }
            if (ddlType.SelectedValue == "Receipts")
            {
                field = "sum_receipts";
            }

            dv.Sort = field;

            chartStaffStats.Titles[0].Text = ddlType.SelectedValue;
            chartStaffStats.Series["Series1"].Points.DataBindXY(dv, "fullname", dv, field);

            lblChartHeading.Text = ddlType.SelectedValue;

            if (chartType == SeriesChartType.StackedBar)
            {
                chartStaffStats.Height = new Unit((tblStats.Rows.Count * 30), UnitType.Pixel);
            }

            chartStaffStats.ChartAreas["ChartArea1"].AxisX.MinorTickMark.Enabled = false;
            chartStaffStats.ChartAreas["ChartArea1"].AxisX.Interval       = 1;
            chartStaffStats.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = true;
            //chartStaffStats.ChartAreas["ChartArea1").AxisX.LabelStyle.IsStaggered = True
            //chartStaffStats.ChartAreas["ChartArea1"].AxisX.LabelAutoFitStyle = System.Web.UI.DataVisualization.Charting.LabelAutoFitStyles.DecreaseFont;

            chartStaffStats.ChartAreas["ChartArea1"].AxisY.Enabled = System.Web.UI.DataVisualization.Charting.AxisEnabled.False;

            if (chartType == SeriesChartType.Pie || chartType == SeriesChartType.Doughnut)
            {
                chartStaffStats.Legends.Add("Legend1");
                chartStaffStats.Legends["Legend1"].Enabled         = true;
                chartStaffStats.Legends["Legend1"].Docking         = System.Web.UI.DataVisualization.Charting.Docking.Right;
                chartStaffStats.Legends["Legend1"].LegendItemOrder = System.Web.UI.DataVisualization.Charting.LegendItemOrder.SameAsSeriesOrder;
                chartStaffStats.Legends["Legend1"].Font            = new Font("Trebuchet MS", 10);

                // Add Color column
                LegendCellColumn firstColumn = new LegendCellColumn();
                firstColumn.ColumnType = LegendCellColumnType.SeriesSymbol;
                chartStaffStats.Legends["Legend1"].CellColumns.Add(firstColumn);

                // Add name cell column
                LegendCellColumn textColumn = new LegendCellColumn();
                textColumn.Alignment = System.Drawing.ContentAlignment.MiddleLeft;
                textColumn.Text      = "#LEGENDTEXT";
                textColumn.Name      = "nameColumn";
                chartStaffStats.Legends["Legend1"].CellColumns.Add(textColumn);

                // Add value cell column
                LegendCellColumn valColumn = new LegendCellColumn();
                valColumn.Text = "#VAL";
                valColumn.Name = "totalColumn";
                chartStaffStats.Legends["Legend1"].CellColumns.Add(valColumn);

                // Add percent cell column
                LegendCellColumn percentColumn = new LegendCellColumn();
                percentColumn.Text = "#PERCENT";
                percentColumn.Name = "percentColumn";
                chartStaffStats.Legends["Legend1"].CellColumns.Add(percentColumn);
            }
        }
        catch (CustomMessageException cmEx)
        {
            HideTableAndSetErrorMessage(cmEx.Message);
        }
        catch (Exception ex)
        {
            HideTableAndSetErrorMessage(ex.ToString());
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                main_content.Style["background"] = (Session["SystemVariables"] == null || ((SystemVariables)Session["SystemVariables"])["MainLogoBackground"] == null) ? "url(../imagesV2/login_bg.png) center top no-repeat #EDEDED" : "url(../imagesV2/" + ((SystemVariables)Session["SystemVariables"])["MainLogoBackground"].Value + ") center top no-repeat #EDEDED";
            }

            if (Session["StaffFirstname"] != null)
            {
                lblStaffName.Text = Session["StaffFirstname"].ToString();
            }

            if (Session["SiteName"] != null)
            {
                lblSiteName.Text = Session["SiteName"].ToString();
            }


            //lblTest.Text = PersonDB.GetFields("patient_person_", "patient_person");

            DataTable tblStats = StaffDB.GetStats2(
                DateTime.Today.AddDays(-30),
                DateTime.Today.AddDays(0),
                false, false);

            DataView dv = tblStats.DefaultView;
            dv.Sort = "n_bookings ASC";


            cTestChart2.Series["Series1"].Points.DataBindXY(dv, "firstname", dv, "n_bookings");

            cTestChart2.BackColor       = System.Drawing.Color.Transparent;
            cTestChart2.BorderlineColor = System.Drawing.Color.Transparent;

            cTestChart2.ChartAreas["ChartArea1"].AxisX.MinorTickMark.Enabled = false;
            cTestChart2.ChartAreas["ChartArea1"].AxisX.Interval       = 1;
            cTestChart2.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = true;
            //cht.ChartAreas["ChartArea1").AxisX.LabelStyle.IsStaggered = True
            //cTestChart2.ChartAreas["ChartArea1"].AxisX.LabelAutoFitStyle = System.Web.UI.DataVisualization.Charting.LabelAutoFitStyles.DecreaseFont;

            //cTestChart2.ChartAreas["ChartArea1"].AxisY.LabelStyle.Enabled = false;
            cTestChart2.ChartAreas["ChartArea1"].AxisY.Enabled = System.Web.UI.DataVisualization.Charting.AxisEnabled.False;
        }
        catch (CustomMessageException ex)
        {
            if (IsPostBack)
            {
                SetErrorMessage(ex.Message);
            }
            else
            {
                HideTableAndSetErrorMessage(ex.Message);
            }
        }
        catch (Exception ex)
        {
            if (IsPostBack)
            {
                SetErrorMessage("", ex.ToString());
            }
            else
            {
                HideTableAndSetErrorMessage("", ex.ToString());
            }
        }
    }
    protected void FillForm()
    {
        try
        {
            if (!CheckIsValidStartEndDates())
            {
                return;
            }

            DataTable tblStats = StaffDB.GetStats2(GetFromDate(), GetToDate(), chkIncDeleted.Checked, chkIncDeleted.Checked);

            tblStats.Columns.Add("total_owing", typeof(decimal));
            tblStats.Columns.Add("total_in", typeof(decimal));
            for (int i = 0; i < tblStats.Rows.Count; i++)
            {
                decimal sum_inv_total = tblStats.Rows[i]["sum_inv_total"] == DBNull.Value ? 0 : Convert.ToDecimal(tblStats.Rows[i]["sum_inv_total"]);
                decimal sum_receipts  = tblStats.Rows[i]["sum_receipts"] == DBNull.Value ? 0 : Convert.ToDecimal(tblStats.Rows[i]["sum_receipts"]);
                decimal sum_cr_notes  = tblStats.Rows[i]["sum_cr_notes"] == DBNull.Value ? 0 : Convert.ToDecimal(tblStats.Rows[i]["sum_cr_notes"]);
                tblStats.Rows[i]["total_owing"] = sum_inv_total - sum_receipts - sum_cr_notes;
                tblStats.Rows[i]["total_in"]    = sum_inv_total - sum_cr_notes;

                if (Convert.ToDecimal(tblStats.Rows[i]["total_owing"]) == 0)
                {
                    tblStats.Rows[i]["total_owing"] = DBNull.Value;
                }
                if (Convert.ToDecimal(tblStats.Rows[i]["sum_receipts"]) == 0)
                {
                    tblStats.Rows[i]["sum_receipts"] = DBNull.Value;
                }
                if (Convert.ToDecimal(tblStats.Rows[i]["total_in"]) == 0)
                {
                    tblStats.Rows[i]["total_in"] = DBNull.Value;
                }
            }

            lstStaffStats.DataSource = tblStats;
            lstStaffStats.DataBind();

            // get from footer
            Label lblSum_TotalBookings  = (Label)lstStaffStats.Controls[lstStaffStats.Controls.Count - 1].Controls[0].FindControl("lblSum_TotalBookings");
            Label lblSum_AvgConsultTime = (Label)lstStaffStats.Controls[lstStaffStats.Controls.Count - 1].Controls[0].FindControl("lblSum_AvgConsultTime");
            Label lblSum_Bookings       = (Label)lstStaffStats.Controls[lstStaffStats.Controls.Count - 1].Controls[0].FindControl("lblSum_Bookings");
            Label lblSum_Completions    = (Label)lstStaffStats.Controls[lstStaffStats.Controls.Count - 1].Controls[0].FindControl("lblSum_Completions");
            Label lblSum_Receipts       = (Label)lstStaffStats.Controls[lstStaffStats.Controls.Count - 1].Controls[0].FindControl("lblSum_Receipts");
            Label lblSum_Owing          = (Label)lstStaffStats.Controls[lstStaffStats.Controls.Count - 1].Controls[0].FindControl("lblSum_Owing");
            Label lblSum_total          = (Label)lstStaffStats.Controls[lstStaffStats.Controls.Count - 1].Controls[0].FindControl("lblSum_total");
            Label lblSum_Patients       = (Label)lstStaffStats.Controls[lstStaffStats.Controls.Count - 1].Controls[0].FindControl("lblSum_Patients");


            lblSum_TotalBookings.Text = String.Format("{0:n0}", tblStats.Compute("Sum(total_bookings)", ""));

            decimal total = 0;
            int     count = 0;
            for (int i = 0; i < tblStats.Rows.Count; i++)
            {
                if (tblStats.Rows[i]["avg_minutes"] == DBNull.Value)
                {
                    continue;
                }
                total += Convert.ToDecimal(tblStats.Rows[i]["avg_minutes"]);
                count++;
            }
            if (count > 0)
            {
                lblSum_AvgConsultTime.Text = String.Format("{0:n0}", total / count);
            }

            lblSum_Bookings.Text    = String.Format("{0:n0}", tblStats.Compute("Sum(n_bookings)", ""));
            lblSum_Completions.Text = String.Format("{0:n0}", tblStats.Compute("Sum(n_completions)", ""));

            object o_sum_receipts = tblStats.Compute("Sum(sum_receipts)", "");
            lblSum_Receipts.Text = String.Format("{0:C}", o_sum_receipts == DBNull.Value ? (object)DBNull.Value : Convert.ToDecimal(o_sum_receipts));
            object o_sum_owing = tblStats.Compute("Sum(total_owing)", "");
            lblSum_Owing.Text = String.Format("{0:C}", o_sum_owing == DBNull.Value ? (object)DBNull.Value : Convert.ToDecimal(o_sum_owing));
            object o_sum_total = tblStats.Compute("Sum(total_in)", "");
            lblSum_total.Text = String.Format("{0:C}", o_sum_total == DBNull.Value ? (object)DBNull.Value : Convert.ToDecimal(o_sum_total));

            lblSum_Patients.Text = tblStats.Compute("Sum(n_patients)", "").ToString();
        }
        catch (CustomMessageException cmEx)
        {
            HideTableAndSetErrorMessage(cmEx.Message);
        }
        catch (Exception ex)
        {
            HideTableAndSetErrorMessage(ex.ToString());
        }
    }