protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            List <int> months = new List <int>();
            for (int i = 1; i <= 12; i++)
            {
                months.Add(i);
            }

            MonthDropdown.DataSource = months;
            MonthDropdown.DataBind();
            MonthDropdown.SelectedIndex = (DateTime.Now.Month - 1);

            Dictionary <string, int> facilities = new Dictionary <string, int>();
            foreach (HealthFacility h in HealthFacility.GetHealthFacilityList().OrderBy(x => x.Name))
            {
                facilities.Add(h.Name, h.Id);
            }

            HealthFacilityDropdown.DataSource     = facilities;
            HealthFacilityDropdown.DataTextField  = "key";
            HealthFacilityDropdown.DataValueField = "value";
            HealthFacilityDropdown.DataBind();
        }

        UpdateHyperLink(null, null);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // Validation
        if (CurrentEnvironment.LoggedUser == null)
        {
            Response.Redirect("Default.aspx");
        }
        status.Visible = Page.IsPostBack;

        if (!Page.IsPostBack)
        {
            List <int> months = new List <int>();
            for (int i = 1; i <= 12; i++)
            {
                months.Add(i);
            }

            MonthDropdown.DataSource = months;
            MonthDropdown.DataBind();
            MonthDropdown.SelectedIndex = (DateTime.Now.Month - 1);

            Dictionary <string, int> facilities = new Dictionary <string, int>();
            var hf = HealthFacility.GetHealthFacilityById(CurrentEnvironment.LoggedUser.HealthFacilityId);
            facilities.Add(hf.Name, hf.Id);
            foreach (HealthFacility h in HealthFacility.GetHealthFacilityByParentId(CurrentEnvironment.LoggedUser.HealthFacilityId).OrderBy(x => x.Name))
            {
                facilities.Add(h.Name, h.Id);
            }


            HealthFacilityDropdown.DataSource     = facilities;
            HealthFacilityDropdown.DataTextField  = "key";
            HealthFacilityDropdown.DataValueField = "value";
            HealthFacilityDropdown.DataBind();
        }
        else
        {
            try
            {
                if (DocumentUpload.HasFile)
                {
                    Stream          stream = DocumentUpload.FileContent;
                    StreamReader    reader = new StreamReader(stream);
                    List <String[]> lines  = new List <string[]>();
                    while (!reader.EndOfStream)
                    {
                        String[] line = reader.ReadLine().Split(',').Skip(1).ToArray();
                        lines.Add(line);
                    }
                    foreach (string[] s in lines)
                    {
                        Debug.WriteLine(String.Join(" ", s));
                    }

                    string connString = WebConfigurationManager.ConnectionStrings["GiisConnectionString"].ConnectionString;
                    using (NpgsqlConnection con = new NpgsqlConnection(connString))
                    {
                        con.Open();
                        int      hf   = int.Parse(HealthFacilityDropdown.SelectedValue);
                        DateTime date = new DateTime(DateTime.Now.Year, MonthDropdown.SelectedIndex + 1, DateTime.Now.Day);
                        for (int i = 1; i < lines.Count; i++)
                        {
                            using (NpgsqlCommand command = new NpgsqlCommand("insert_new_weight", con)
                            {
                                CommandType = System.Data.CommandType.StoredProcedure
                            })
                            {
                                command.Parameters.Add(new NpgsqlParameter("hf", hf));
                                command.Parameters.Add(new NpgsqlParameter("date", date));
                                command.Parameters.Add(new NpgsqlParameter("age_group", lines[i][0]));
                                command.Parameters.Add(new NpgsqlParameter("m_gt_80", lines[i][1] == String.Empty ? 0 : int.Parse(lines[i][1])));
                                command.Parameters.Add(new NpgsqlParameter("m_60_80", lines[i][2] == String.Empty ? 0 : int.Parse(lines[i][2])));
                                command.Parameters.Add(new NpgsqlParameter("m_lt_60", lines[i][3] == String.Empty ? 0 : int.Parse(lines[i][3])));
                                command.Parameters.Add(new NpgsqlParameter("f_gt_80", lines[i][4] == String.Empty ? 0 : int.Parse(lines[i][4])));
                                command.Parameters.Add(new NpgsqlParameter("f_60_80", lines[i][5] == String.Empty ? 0 : int.Parse(lines[i][5])));
                                command.Parameters.Add(new NpgsqlParameter("f_lt_60", lines[i][6] == String.Empty ? 0 : int.Parse(lines[i][6])));
                                command.ExecuteNonQuery();
                                command.Parameters.Clear();
                            }
                        }
                        status.Text     = "Weight tally sheet submitted";
                        status.CssClass = "label label-success";
                    }
                }
                else
                {
                    status.Text     = "No file selected";
                    status.CssClass = "label label-danger";
                    return;
                }
            }
            catch (Exception ex)
            {
                status.Text     = ex.Message;
                status.CssClass = "label label-danger";
            }
        }
        Debug.WriteLine("page loaded");
    }
Beispiel #3
0
    protected void Page_Load(object sender, EventArgs e)
    {
        // Validation
        if (CurrentEnvironment.LoggedUser == null)
        {
            Response.Redirect("Default.aspx");
        }
        status.Visible = Page.IsPostBack;

        if (!Page.IsPostBack)
        {
            Dictionary <string, int> facilities = new Dictionary <string, int>();
            var hf = HealthFacility.GetHealthFacilityById(CurrentEnvironment.LoggedUser.HealthFacilityId);
            facilities.Add(hf.Name, hf.Id);
            foreach (HealthFacility h in HealthFacility.GetHealthFacilityByParentId(CurrentEnvironment.LoggedUser.HealthFacilityId).OrderBy(x => x.Name))
            {
                facilities.Add(h.Name, h.Id);
            }


            HealthFacilityDropdown.DataSource     = facilities;
            HealthFacilityDropdown.DataTextField  = "key";
            HealthFacilityDropdown.DataValueField = "value";
            HealthFacilityDropdown.DataBind();
        }
        else
        {
            try
            {
                if (DocumentUpload.HasFile)
                {
                    Stream          stream = DocumentUpload.FileContent;
                    StreamReader    reader = new StreamReader(stream);
                    List <String[]> lines  = new List <string[]>();
                    while (!reader.EndOfStream)
                    {
                        String[] line = reader.ReadLine().Split(',').ToArray();
                        lines.Add(line);
                    }
                    foreach (string[] s in lines)
                    {
                        Debug.WriteLine(String.Join(" ", s));
                    }

                    string connString = WebConfigurationManager.ConnectionStrings["GiisConnectionString"].ConnectionString;
                    using (NpgsqlConnection con = new NpgsqlConnection(connString))
                    {
                        con.Open();
                        int hf = int.Parse(HealthFacilityDropdown.SelectedValue);

                        for (int i = 1; i < lines.Count; i++)
                        {
                            using (NpgsqlCommand command = new NpgsqlCommand("insert_stock_tally", con)
                            {
                                CommandType = System.Data.CommandType.StoredProcedure
                            })
                            {
                                // First: Is this the right sheet?
                                if (!lines[i][0].StartsWith("BIDSTS"))
                                {
                                    throw new InvalidOperationException("Invalid CSV uploaded");
                                }

                                int    month  = Int32.Parse(lines[i][2]);
                                string gender = lines[i][1];
                                for (int c = 3; c < lines[i].Length; c++)
                                {
                                    var dose  = Dose.GetDoseByFullname(lines[0][c]);
                                    var count = Int32.Parse(String.IsNullOrEmpty(lines[i][c]) ? "0" : lines[i][c]);
                                    if (dose == null)
                                    {
                                        throw new InvalidOperationException(String.Format("Dose {0} is not a valid dose", lines[0][c]));
                                    }
                                    command.Parameters.Add(new NpgsqlParameter("hf", hf));
                                    command.Parameters.Add(new NpgsqlParameter("month", month));
                                    command.Parameters.Add(new NpgsqlParameter("gender", gender.Substring(0, 1)));
                                    command.Parameters.Add(new NpgsqlParameter("dose", dose.Id));
                                    command.Parameters.Add(new NpgsqlParameter("value", count));
                                    command.ExecuteNonQuery();

                                    // Hack: Drive down stock
                                    StockManagementLogic logic = new StockManagementLogic();
                                    for (int cd = 0; cd < count; cd++)
                                    {
                                        logic.Vaccinate(HealthFacility.GetHealthFacilityById(hf), new VaccinationEvent()
                                        {
                                            DoseId = dose.Id
                                        });
                                    }
                                    command.Parameters.Clear();
                                }
                            }
                        }
                        status.Text     = "Stock tally sheet submitted";
                        status.CssClass = "label label-success";
                    }
                }
                else
                {
                    status.Text     = "No file selected";
                    status.CssClass = "label label-danger";
                    return;
                }
            }
            catch (Exception ex)
            {
                status.Text     = ex.Message;
                status.CssClass = "label label-danger";
            }
        }
        Debug.WriteLine("page loaded");
    }