public void UploadData(OmrPageOutput page)
        {
            if (!Connectivity.CheckForInternetConnection())
            {
                MessageBox.Show(
                    "Username is empty, This is usually caused by lack of internet connectivity. Please try again later");
                Exception e = new Exception("No internet connection");
                Trace.TraceError("Error:{0}", e);
                throw e;
            }

            StatusDialog dlg = new StatusDialog();

            dlg.Show();


            try
            {
                int facilityId = FacilitySelectionContext.FacilityId;

                // Non-remembered facility
                if (facilityId == 0)
                {
                    LocationSelectionBox location = new LocationSelectionBox();
                    if (location.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        throw new InvalidOperationException("Cannot upload data without selecting a facility");
                    }
                    if (location.Remember)
                    {
                        FacilitySelectionContext.FacilityId = location.FacilityId;
                    }
                    facilityId = location.FacilityId;
                }

                var monthBubble = page.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "month");
                if (monthBubble == null)
                {
                    Err += "Must select month!; ";
                    return;
                }

                RestUtil restUtil  = new RestUtil(new Uri(ConfigurationManager.AppSettings["GIIS_URL"]));
                var      userInfo  = restUtil.Get <User>("UserManagement.svc/GetUserInfo", new KeyValuePair <string, object>("username", restUtil.GetCurrentUserName));
                var      placeInfo = restUtil.Get <Place[]>("PlaceManagement.svc/GetPlaceByHealthFacilityId", new KeyValuePair <string, object>("hf_id", facilityId));

                foreach (var dtl in page.Details.OfType <OmrRowData>())
                {
                    string rowNum = dtl.Id.Substring(dtl.Id.Length - 1, 1);

                    if (dtl.Details.Count == 0)
                    {
                        continue;
                    }

                    // (string barcodeId, string firstname1, string firstname2, string lastname1, DateTime birthdate, bool gender,
                    // int healthFacilityId, int birthplaceId, int domicileId, string address, string phone, string motherFirstname,
                    // string motherLastname, string notes, int userId, DateTime modifiedOn)
                    OmrBarcodeData omrBarcode = dtl.Details.OfType <OmrBarcodeData>().FirstOrDefault();
                    OmrBubbleData  omrDobDay  =
                        dtl.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "dobDay"),
                                   omrDobDay10  = dtl.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "dobDay10"),
                                   omrDobMonth  = dtl.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "dobMonth"),
                                   omrDobYear   = dtl.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "dobYear"),
                                   omrGender    = dtl.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "gender"),
                                   omrOutreach  = dtl.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "Outreach"),
                                   omrVaccDay10 = dtl.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "vaccDay10"),
                                   omrVaccDay   = dtl.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "vaccDay"),
                                   omrNew       = dtl.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "newInfo"),
                                   omrUpdate    = dtl.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "correct"),
                                   omrIgnore    = dtl.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "moved");
                    OmrBubbleData[] omrBcg      = dtl.Details.OfType <OmrBubbleData>().Where(o => o.Key == "BCG").ToArray(),
                    omrOpv     = dtl.Details.OfType <OmrBubbleData>().Where(o => o.Key == "OPV").ToArray(),
                    omrPenta   = dtl.Details.OfType <OmrBubbleData>().Where(o => o.Key == "PENTA").ToArray(),
                    omrPcv     = dtl.Details.OfType <OmrBubbleData>().Where(o => o.Key == "PCV").ToArray(),
                    omrRota    = dtl.Details.OfType <OmrBubbleData>().Where(o => o.Key == "ROTA").ToArray(),
                    omrMr      = dtl.Details.OfType <OmrBubbleData>().Where(o => o.Key == "MR").ToArray(),
                    omrVaccine = dtl.Details.OfType <OmrBubbleData>().Where(o => o.Key == "vaccine").ToArray();

                    // From WCF Call
                    // Create row data for the verification form
                    OmrBarcodeField barcodeField =
                        page.Template.FlatFields.Find(o => o.Id == String.Format("{0}Barcode", dtl.Id)) as
                        OmrBarcodeField;
                    OmrBubbleField outreachField = page.Template.FlatFields.OfType <OmrBubbleField>()
                                                   .SingleOrDefault(o => o.AnswerRowGroup == dtl.Id && o.Question == "Outreach"),
                                   monthField = page.Template.FlatFields.OfType <OmrBubbleField>()
                                                .SingleOrDefault(o => o.AnswerRowGroup == dtl.Id && o.Question == "dobMonth" &&
                                                                 o.Value == "12");

                    // Barcode bounds
                    Tz02RowData rowData = new Tz02RowData()
                    {
                        RowBounds = new RectangleF()
                        {
                            Location = new PointF(barcodeField.TopLeft.X, monthField.TopLeft.Y - 20),
                            Width    = outreachField.TopLeft.X - barcodeField.TopLeft.X,
                            Height   = barcodeField.BottomLeft.Y - monthField.TopLeft.Y - 20
                        },
                        UserInfo   = userInfo,
                        Page       = page,
                        FacilityId = facilityId
                    };


                    if (omrBarcode != null)
                    {
                        rowData.Barcode = omrBarcode.BarcodeData;
                    }
                    if (omrDobDay != null && omrDobDay10 != null &&
                        omrDobMonth != null && omrDobYear != null)
                    {
                        try
                        {
                            rowData.DateOfBirth = new DateTime((int)omrDobYear.ValueAsFloat,
                                                               (int)omrDobMonth.ValueAsFloat,
                                                               (int)omrDobDay10.ValueAsFloat + (int)omrDobDay.ValueAsFloat);
                        }
                        catch
                        {
                        }
                    }
                    if (omrGender != null)
                    {
                        rowData.Gender = omrGender.Value == "M" ? true : false;
                    }
                    if (omrOutreach != null)
                    {
                        rowData.Outreach = omrOutreach.Value == "T";
                    }

                    // Doses
                    rowData.Doses = new List <Dose>();
                    if (omrBcg != null && omrBcg.Length > 0)
                    {
                        rowData.Doses.Add(this.FindDoseOrThrow("BCG"));
                    }
                    if (omrOpv != null)
                    {
                        foreach (var bub in omrOpv)
                        {
                            VaccinationEvent opvEvent = null;

                            rowData.Doses.Add(ReferenceData.Current.Doses
                                              .Single(d => (d.DoseNumber == Helper.ConvertToInt(bub.Value) &&
                                                            d.ScheduledVaccinationId == (ReferenceData.Current.Vaccines
                                                                                         .Single(v => v.Name.ToLower().Contains("opv")).Id))));
                        }
                    }
                    if (omrPenta != null)
                    {
                        foreach (var bub in omrPenta)
                        {
                            rowData.Doses.Add(ReferenceData.Current.Doses
                                              .Single(d => (d.DoseNumber == Helper.ConvertToInt(bub.Value) &&
                                                            d.ScheduledVaccinationId == (ReferenceData.Current.Vaccines
                                                                                         .Single(v => (v.Name.ToLower().Contains("dtp") ||
                                                                                                       v.Name.ToLower().Contains("penta"))).Id))));
                        }
                    }
                    if (omrPcv != null)
                    {
                        foreach (var bub in omrPcv)
                        {
                            rowData.Doses.Add(ReferenceData.Current.Doses
                                              .Single(d => (d.DoseNumber == Helper.ConvertToInt(bub.Value) &&
                                                            d.ScheduledVaccinationId == (ReferenceData.Current.Vaccines
                                                                                         .Single(v => v.Name.ToLower().Contains("pcv")).Id))));
                        }
                    }
                    if (omrRota != null)
                    {
                        foreach (var bub in omrRota)
                        {
                            rowData.Doses.Add(ReferenceData.Current.Doses
                                              .Single(d => (d.DoseNumber == Helper.ConvertToInt(bub.Value) &&
                                                            d.ScheduledVaccinationId == (ReferenceData.Current.Vaccines
                                                                                         .Single(v => v.Name.ToLower().Contains("rota")).Id))));
                        }
                    }
                    if (omrMr != null)
                    {
                        foreach (var bub in omrMr)
                        {
                            rowData.Doses.Add(ReferenceData.Current.Doses
                                              .Single(d => (d.DoseNumber == Helper.ConvertToInt(bub.Value) &&
                                                            d.ScheduledVaccinationId == (ReferenceData.Current.Vaccines
                                                                                         .Single(v => v.Name.ToLower().Contains("mr"))
                                                                                         .Id))));
                        }
                    }
                    // Given vaccines
                    rowData.VaccineGiven = new List <ScheduledVaccination>();
                    foreach (var vacc in omrVaccine)
                    {
                        string antigenName = vacc.Value;
                        if (antigenName == "ROTA")
                        {
                            antigenName = ReferenceData.Current.Vaccines
                                          .Single(d => d.Name.ToLower().Contains("rota")).Name;
                        }
                        else if (antigenName == "PENTA")
                        {
                            antigenName = ReferenceData.Current.Vaccines.Single(d => (d.Name.ToLower().Contains("dtp") || d.Name.ToLower().Contains("penta"))).Name;
                        }
                        else if (antigenName == "MR")
                        {
                            antigenName = ReferenceData.Current.Vaccines
                                          .Single(d => (d.Name.ToLower().Contains("mr")))
                                          .Name;
                        }
                        else if (antigenName == "PCV")
                        {
                            antigenName = ReferenceData.Current.Vaccines
                                          .Single(d => d.Name.ToLower().Contains("pcv")).Name;
                        }
                        else if (antigenName == "OPV")
                        {
                            antigenName = ReferenceData.Current.Vaccines
                                          .Single(d => d.Name.ToLower().Contains("opv")).Name;
                        }


                        var refData = ReferenceData.Current.Vaccines.FirstOrDefault(v => v.Name == antigenName);
                        if (refData != null)
                        {
                            rowData.VaccineGiven.Add(refData);
                        }
                        else
                        {
                            MessageBox.Show(String.Format("The form expected a vaccination named {0} but the server did not respond with such a vaccine. Server vaccinations: [{1}]", antigenName, String.Join(",", ReferenceData.Current.Vaccines.Select(o => o.Name).ToArray())));
                        }
                    }

                    // Date of vaccination
                    rowData.VaccineDate = DateTime.Now;
                    if (omrVaccDay10 != null && omrVaccDay != null)
                    {
                        rowData.VaccineDate = new DateTime(DateTime.Now.Month < monthBubble.ValueAsFloat ? DateTime.Now.Year - 1 : DateTime.Now.Year, (int)monthBubble.ValueAsFloat, (int)omrVaccDay10.ValueAsFloat + (int)omrVaccDay.ValueAsFloat);
                    }

                    // Determine what to do
                    if (omrUpdate?.Value == "T")
                    {
                        ChildSearch bc = new ChildSearch(rowData);
                        if (bc.ShowDialog() == DialogResult.OK)
                        {
                            rowData.Barcode = bc.Child.BarcodeId;
                            rowData.ChildId = bc.Child.Id;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (omrIgnore?.Value == "T")
                    {
                        continue;
                    }

                    if (BarcodeUtil.HasData(page, barcodeField) ||
                        dtl.Details.Count > 2)
                    {
                        Registration registration = new Registration(rowData);
                        registration.ShowDialog();
                    }
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("Error:{0}", e);
                throw;
            }
            finally
            {
                dlg.Close();
            }
        }
        public void UploadData(OmrPageOutput page)
        {
            if (!Connectivity.CheckForInternetConnection())
            {
                MessageBox.Show(
                    "Username is empty, This is usually caused by lack of internet connectivity. Please try again later");
                Exception e = new Exception("No internet connection");
                Trace.TraceError("Error:{0}", e);
                throw e;
            }

            StatusDialog dlg = new StatusDialog();

            dlg.Show();

            try
            {
                int facilityId = Int32.Parse(page.Parameters[0]);
                int month      = Int32.Parse(page.Parameters[1]);
                int year       = Int32.Parse(page.Parameters[2]);
                this.Err = "";



                Trace.TraceInformation("Reporting for facility {0} of {1}-{2}", facilityId, month, year);

                RestUtil restUtil = new RestUtil(new Uri(ConfigurationManager.AppSettings["GIIS_URL"]));



                User userInfo = null;
                try
                {
                    userInfo = restUtil.Get <User>("UserManagement.svc/GetUserInfo", new KeyValuePair <string, object>("username", restUtil.GetCurrentUserName));
                }
                catch
                {
                    userInfo = restUtil.Get <User>("UserManagement.svc/GetUserInfo", new KeyValuePair <string, object>("username", restUtil.GetCurrentUserName));
                }
                var placeInfo = restUtil.Get <Place[]>("PlaceManagement.svc/GetPlaceByHealthFacilityId",
                                                       new KeyValuePair <string, object>("hf_id", facilityId));

                foreach (var patientRow in page.Details.OfType <OmrRowData>().Where(o => !o.Id.Contains("-")))
                {
                    // Master patient row processing
                    string rowNum = patientRow.Id.Substring(patientRow.Id.Length - 1, 1);


                    if (patientRow.Details.Count == 0)
                    {
                        continue;
                    }



                    // Create row data for the verification form
                    OmrBarcodeField barcodeField    = page.Template.FlatFields.Find(o => o.Id == String.Format("{0}Barcode", patientRow.Id)) as OmrBarcodeField,
                                    omrStickerField = page.Template.FlatFields.Find(o => o.Id == String.Format("{0}Sticker", patientRow.Id)) as OmrBarcodeField;

                    Tz01RowData rowData = new Tz01RowData()
                    {
                        RowBounds = new RectangleF()
                        {
                            Location = barcodeField.TopLeft,
                            Width    = page.BottomRight.X - barcodeField.TopLeft.X,
                            Height   = omrStickerField.BottomRight.Y - barcodeField.TopLeft.Y
                        },
                        UserInfo = userInfo,
                        Page     = page
                    };

                    // Barcodes only at this level
                    OmrBarcodeData omrBarcode = patientRow.Details.OfType <OmrBarcodeData>().FirstOrDefault(o => o.Id == String.Format("{0}Barcode", patientRow.Id)),
                                   omrSticker = patientRow.Details.OfType <OmrBarcodeData>().FirstOrDefault(o => o.Id == String.Format("{0}Sticker", patientRow.Id));


                    if (omrBarcode == null)
                    {
                        rowData.Barcode = String.Empty;
                    }
                    else
                    {
                        rowData.Barcode = omrBarcode.BarcodeData;
                    }
                    rowData.StickerValue = omrSticker?.BarcodeData;

                    // Try to lookup the poor chap
                    if (rowData.Barcode.StartsWith("T")) // TEMP ID AUTH
                    {
                        rowData.StickerValue = omrSticker?.BarcodeData;
                        var childDataList = restUtil.Get <List <ChildEntity> >("ChildManagement.svc/GetChildById",
                                                                               new KeyValuePair <string, object>("childId", rowData.Barcode.Replace("T", ""))
                                                                               );
                        if (childDataList == null)
                        {
                            throw new InvalidOperationException("Could not deserialize response");
                        }
                        else if (childDataList.Count == 0)
                        {
                            throw new InvalidOperationException("Child with barcode " + rowData.Barcode + " not found!");
                        }
                        rowData.Child = childDataList[0].childEntity;
                    }
                    else
                    {
                        var childDataList = restUtil.Get <List <ChildEntity> >("ChildManagement.svc/SearchByBarcode",
                                                                               new KeyValuePair <string, object>("barcodeId", rowData.Barcode));
                        if (childDataList == null)
                        {
                            throw new InvalidOperationException("Could not deserialize response");
                        }
                        else if (childDataList.Count == 0)
                        {
                            if (!String.IsNullOrEmpty(rowData.StickerValue))
                            {
                                childDataList = restUtil.Get <List <ChildEntity> >("ChildManagement.svc/SearchByBarcode",
                                                                                   new KeyValuePair <string, object>("barcodeId", rowData.StickerValue));
                            }

                            if (childDataList == null)
                            {
                                throw new InvalidOperationException("Could not deserialize response");
                            }
                            else if (childDataList.Count == 0)
                            {
                                throw new InvalidOperationException("Child with barcode " + rowData.Barcode + " not found!");
                            }
                        }
                        rowData.Child = childDataList[0].childEntity;
                    }

                    // Get appointments and vaccinations for child
                    VaccinationAppointment[] appts            = restUtil.Get <VaccinationAppointment[]>("VaccinationAppointmentManagement.svc/GetVaccinationAppointmentsByChildId", new KeyValuePair <string, object>("childId", rowData.Child.Id));
                    VaccinationEvent[]       vaccinationEvent = restUtil.Get <VaccinationEvent[]>("VaccinationEvent.svc/GetVaccinationEventListByChildId", new KeyValuePair <string, object>("childId", rowData.Child.Id));
                    rowData.Appointment = new List <Tz01Vaccination>();

                    // iterate over the sub-rows
                    foreach (var aptRow in page.Details.OfType <OmrRowData>().Where(o => o.Id.StartsWith(String.Format("{0}-", patientRow.Id))).OrderBy(r => r.Id))
                    {
                        OmrBarcodeData omrAptId = aptRow.Details.OfType <OmrBarcodeData>().FirstOrDefault();

                        OmrBubbleData omrDay10    = aptRow.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "day10"),
                                      omrDay      = aptRow.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "day"),
                                      omrOutreach = aptRow.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "outreach"),
                                      omrVaccine  = aptRow.Details.OfType <OmrBubbleData>().FirstOrDefault(o => o.Key == "vaccine");

                        // First, get the appointment data
                        Tz01Vaccination vaccineData = new Tz01Vaccination();

                        if (omrAptId == null)
                        {
                            barcodeField = page.Template.Fields.FirstOrDefault(o => o.AnswerRowGroup == aptRow.Id) as OmrBarcodeField;
                            // Show a correction form ...
                            BarcodeCorrection bc = new BarcodeCorrection(page, barcodeField);
                            if (BarcodeUtil.HasData(page, barcodeField) && bc.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                            {
                                vaccineData.Appointment = appts.FirstOrDefault(o => o.ScheduledDate.Date.Equals(DateTime.ParseExact(bc.BarcodeId, "MM-dd-yyyy", CultureInfo.InvariantCulture)));
                            }
                        }
                        else
                        {
                            vaccineData.Appointment = appts.FirstOrDefault(o => o.Id.ToString() == omrAptId.BarcodeData);
                        }

                        // Validate we got an appointment
                        if (vaccineData.Appointment == null)
                        {
                            throw new InvalidOperationException(String.Format("Could not find appointment data for update on row {0}", rowNum));
                        }

                        vaccineData.All      = omrVaccine?.Value == "all";
                        vaccineData.Outreach = Boolean.Parse(omrOutreach?.Value ?? "false");
                        if (omrDay != null || omrDay10 != null)
                        {
                            try
                            {
                                vaccineData.Date = new DateTime(vaccineData.Appointment.ScheduledDate.Year, vaccineData.Appointment.ScheduledDate.Month, (int)(omrDay?.ValueAsFloat + omrDay10?.ValueAsFloat));
                                if (vaccineData.Date > DateTime.Now)
                                {
                                    vaccineData.Date = new DateTime(year, month, (int)(omrDay.ValueAsFloat + omrDay10.ValueAsFloat));
                                }
                            }
                            catch (Exception e) {
                                Trace.TraceError(e.ToString());
                            }
                        }
                        rowData.Appointment.Add(vaccineData);
                    }

                    if (rowData.Appointment[0].Date.HasValue ||
                        rowData.Appointment[1].Date.HasValue ||
                        rowData.Appointment[0].All ||
                        rowData.Appointment[1].All ||
                        BarcodeUtil.HasData(page, omrStickerField))
                    {
                        VaccineCorrection vc = new VaccineCorrection(rowData, vaccinationEvent, ReferenceData.Current.Doses);
                        vc.ShowDialog();
                    }
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("Error:{0}", e);
                throw;
            }
            finally
            {
                dlg.Close();
            }
        }