public static List <VaccinationAppointment> GetVaccinationAppointmentAsList(DataTable dt)
        {
            List <VaccinationAppointment> oList = new List <VaccinationAppointment>();

            foreach (DataRow row in dt.Rows)
            {
                try
                {
                    VaccinationAppointment o = new VaccinationAppointment();
                    o.Id                  = Helper.ConvertToInt(row["ID"]);
                    o.ChildId             = Helper.ConvertToInt(row["CHILD_ID"]);
                    o.ScheduledFacilityId = Helper.ConvertToInt(row["SCHEDULED_FACILITY_ID"]);
                    o.ScheduledDate       = Helper.ConvertToDate(row["SCHEDULED_DATE"]);
                    o.Notes               = row["NOTES"].ToString();
                    o.IsActive            = Helper.ConvertToBoolean(row["IS_ACTIVE"]);
                    o.ModifiedOn          = Helper.ConvertToDate(row["MODIFIED_ON"]);
                    o.ModifiedBy          = Helper.ConvertToInt(row["MODIFIED_BY"]);
                    o.Aefi                = Helper.ConvertToBoolean(row["AEFI"]);
                    o.AefiDate            = Helper.ConvertToDate(row["AEFI_DATE"]);
                    o.Outreach            = Helper.ConvertToBoolean(row["OUTREACH"]);
                    oList.Add(o);
                }
                catch (Exception ex)
                {
                    Log.InsertEntity("VaccinationAppointment", "GetVaccinationAppointmentAsList", 1, ex.StackTrace.Replace("'", ""), ex.Message.Replace("'", ""));
                    throw ex;
                }
            }
            return(oList);
        }
 public static int Insert(VaccinationAppointment o)
 {
     try
     {
         string query = @"INSERT INTO ""VACCINATION_APPOINTMENT"" (""CHILD_ID"", ""SCHEDULED_FACILITY_ID"", ""SCHEDULED_DATE"", ""NOTES"", ""IS_ACTIVE"", ""MODIFIED_ON"", ""MODIFIED_BY"", ""AEFI"", ""AEFI_DATE"", ""OUTREACH"") VALUES (@ChildId, @ScheduledFacilityId, @ScheduledDate, @Notes, @IsActive, @ModifiedOn, @ModifiedBy, @Aefi, @AefiDate, @Outreach) returning ""ID"" ";
         List <Npgsql.NpgsqlParameter> parameters = new List <NpgsqlParameter>()
         {
             new NpgsqlParameter("@ChildId", DbType.Int32)
             {
                 Value = o.ChildId
             },
             new NpgsqlParameter("@ScheduledFacilityId", DbType.Int32)
             {
                 Value = o.ScheduledFacilityId
             },
             new NpgsqlParameter("@ScheduledDate", DbType.Date)
             {
                 Value = o.ScheduledDate
             },
             new NpgsqlParameter("@Notes", DbType.String)
             {
                 Value = (object)o.Notes ?? DBNull.Value
             },
             new NpgsqlParameter("@IsActive", DbType.Boolean)
             {
                 Value = o.IsActive
             },
             new NpgsqlParameter("@ModifiedOn", DbType.DateTime)
             {
                 Value = o.ModifiedOn
             },
             new NpgsqlParameter("@ModifiedBy", DbType.Int32)
             {
                 Value = o.ModifiedBy
             },
             new NpgsqlParameter("@Aefi", DbType.Boolean)
             {
                 Value = (object)o.Aefi ?? DBNull.Value
             },
             new NpgsqlParameter("@AefiDate", DbType.Date)
             {
                 Value = (object)o.AefiDate ?? DBNull.Value
             },
             new NpgsqlParameter("@Outreach", DbType.Boolean)
             {
                 Value = o.Outreach
             }
         };
         object id = DBManager.ExecuteScalarCommand(query, CommandType.Text, parameters);
         AuditTable.InsertEntity("VaccinationAppointment", id.ToString(), 1, DateTime.Now, o.ModifiedBy);
         return(int.Parse(id.ToString()));
     }
     catch (Exception ex)
     {
         Log.InsertEntity("VaccinationAppointment", "Insert", 1, ex.StackTrace.Replace("'", ""), ex.Message.Replace("'", ""));
     }
     return(-1);
 }
        public static int InsertVaccinationsForChild(int childId, int userId)
        {
            try
            {
                Child child = Child.GetChildById(childId);

                List <Dose> vaccineDoseList = Dose.GetDosesByDates(child.Birthdate);

                foreach (Dose vaccineDose in vaccineDoseList)
                {
                    VaccinationAppointment o = new VaccinationAppointment();
                    o.ChildId             = childId;
                    o.ScheduledFacilityId = child.HealthcenterId;
                    o.ScheduledDate       = child.Birthdate.AddDays(vaccineDose.AgeDefinition.Days);
                    o.Notes      = String.Empty;
                    o.IsActive   = true;
                    o.ModifiedOn = DateTime.Now;
                    o.ModifiedBy = userId;

                    string where = string.Format(" \"CHILD_ID\" = {0} AND \"SCHEDULED_DATE\" = '{1}' ", child.Id, o.ScheduledDate.ToString("yyyy-MM-dd"));
                    List <VaccinationAppointment> list = GetVaccinationAppointmentForList(where);
                    int count = list.Count;

                    if (count == 0)
                    {
                        int lastApp = Insert(o);
                        if (lastApp > 0)
                        {
                            VaccinationEvent ve = new VaccinationEvent();

                            ve.AppointmentId     = lastApp;
                            ve.ChildId           = childId;
                            ve.DoseId            = vaccineDose.Id;
                            ve.HealthFacilityId  = child.HealthcenterId;
                            ve.ScheduledDate     = o.ScheduledDate;
                            ve.VaccinationDate   = o.ScheduledDate;
                            ve.Notes             = String.Empty;
                            ve.VaccinationStatus = false;
                            int dosenum = vaccineDose.DoseNumber - 1;

                            if (VaccinationEvent.OtherDose(vaccineDose.ScheduledVaccinationId, childId, dosenum) && dosenum > 0)
                            {
                                ve.IsActive = false;
                            }
                            else
                            {
                                ve.IsActive = true;
                            }
                            ve.ModifiedOn = DateTime.Now;
                            ve.ModifiedBy = userId;
                            int i = VaccinationEvent.Insert(ve);
                            if (!(i > 0))
                            {
                                VaccinationAppointment.DeleteByChild(childId);
                                Child.Delete(childId);
                                return(0);
                            }
                        }
                        else
                        {
                            Child.Delete(childId);
                            return(0);
                        }
                    }
                    else
                    {
                        VaccinationAppointment appointment = list[0];
                        VaccinationEvent       ve          = new VaccinationEvent();

                        ve.AppointmentId     = appointment.Id;
                        ve.ChildId           = childId;
                        ve.DoseId            = vaccineDose.Id;
                        ve.HealthFacilityId  = child.HealthcenterId;
                        ve.ScheduledDate     = o.ScheduledDate;
                        ve.VaccinationDate   = o.ScheduledDate;
                        ve.Notes             = String.Empty;
                        ve.VaccinationStatus = false;
                        int dosenum = vaccineDose.DoseNumber - 1;

                        if (VaccinationEvent.OtherDose(vaccineDose.ScheduledVaccinationId, childId, dosenum) && dosenum > 0)
                        {
                            ve.IsActive = false;
                        }
                        else
                        {
                            ve.IsActive = true;
                        }
                        ve.ModifiedOn = DateTime.Now;
                        ve.ModifiedBy = userId;
                        int i = VaccinationEvent.Insert(ve);
                        if (!(i > 0))
                        {
                            VaccinationAppointment.DeleteByChild(childId);
                            Child.Delete(childId);
                            return(0);
                        }
                    }
                }
                return(1);
            }
            catch (Exception ex)
            {
                Log.InsertEntity("VaccinationAppointment", "InsertVaccinationsForChild", 4, ex.StackTrace.Replace("'", ""), ex.Message.Replace("'", ""));
                throw ex;
            }

            // return 0;
        }
 public static int Update(VaccinationAppointment o)
 {
     try
     {
         string query = @"UPDATE ""VACCINATION_APPOINTMENT"" SET ""ID"" = @Id, ""CHILD_ID"" = @ChildId, ""SCHEDULED_FACILITY_ID"" = @ScheduledFacilityId, ""SCHEDULED_DATE"" = @ScheduledDate, ""NOTES"" = @Notes, ""IS_ACTIVE"" = @IsActive, ""MODIFIED_ON"" = @ModifiedOn, ""MODIFIED_BY"" = @ModifiedBy, ""AEFI"" = @Aefi, ""AEFI_DATE"" = @AefiDate, ""OUTREACH"" = @Outreach WHERE ""ID"" = @Id ";
         List <Npgsql.NpgsqlParameter> parameters = new List <NpgsqlParameter>()
         {
             new NpgsqlParameter("@ChildId", DbType.Int32)
             {
                 Value = o.ChildId
             },
             new NpgsqlParameter("@ScheduledFacilityId", DbType.Int32)
             {
                 Value = o.ScheduledFacilityId
             },
             new NpgsqlParameter("@ScheduledDate", DbType.Date)
             {
                 Value = o.ScheduledDate
             },
             new NpgsqlParameter("@Notes", DbType.String)
             {
                 Value = (object)o.Notes ?? DBNull.Value
             },
             new NpgsqlParameter("@IsActive", DbType.Boolean)
             {
                 Value = o.IsActive
             },
             new NpgsqlParameter("@ModifiedOn", DbType.DateTime)
             {
                 Value = o.ModifiedOn
             },
             new NpgsqlParameter("@ModifiedBy", DbType.Int32)
             {
                 Value = o.ModifiedBy
             },
             new NpgsqlParameter("@Aefi", DbType.Boolean)
             {
                 Value = (object)o.Aefi ?? DBNull.Value
             },
             new NpgsqlParameter("@AefiDate", DbType.Date)
             {
                 Value = (object)o.AefiDate ?? DBNull.Value
             },
             new NpgsqlParameter("@Outreach", DbType.Boolean)
             {
                 Value = o.Outreach
             },
             new NpgsqlParameter("@Id", DbType.Int32)
             {
                 Value = o.Id
             }
         };
         int rowAffected = DBManager.ExecuteNonQueryCommand(query, CommandType.Text, parameters);
         AuditTable.InsertEntity("VaccinationAppointment", o.Id.ToString(), 2, DateTime.Now, o.ModifiedBy);
         return(rowAffected);
     }
     catch (Exception ex)
     {
         Log.InsertEntity("VaccinationAppointment", "Update", 2, ex.StackTrace.Replace("'", ""), ex.Message.Replace("'", ""));
     }
     return(-1);
 }