private DataMessage Validate(SelfRenewal renewal)
        {
            var dataMessage = new DataMessage();
            var context     = new ImisDB();
            var insuree     = new TblInsuree();

            //InsuranceNumberNotFound = 3007,
            insuree = context.TblInsuree
                      .Where(i => i.Chfid == renewal.InsuranceNumber && i.ValidityTo == null)
                      .Include(i => i.Family).ThenInclude(f => f.TblPolicy).ThenInclude(p => p.Prod).ThenInclude(pr => pr.ConversionProd)
                      .FirstOrDefault();
            if (insuree == null)
            {
                dataMessage.Code         = (int)Errors.Renewal.InsuranceNumberNotFound;
                dataMessage.MessageValue = "Insuree with given insurance number not found";
                dataMessage.ErrorOccured = true;
                return(dataMessage);
            }

            //RenewalAlreadyRequested = 3008,
            if (insuree.Family.TblPolicy.Where(p => p.Prod.ProductCode.ToUpper() == renewal.ProductCode.ToUpper() && p.PolicyStatus == 1 && p.ValidityTo == null).FirstOrDefault() != null)
            {
                dataMessage.Code         = (int)Errors.Renewal.RenewalAlreadyRequested;
                dataMessage.MessageValue = "Renewal already created";
                dataMessage.ErrorOccured = true;
                return(dataMessage);
            }

            //NoPreviousPolicyFoundToRenew = 3009,
            var prevPolicy = insuree.Family.TblPolicy.Where(p => p.Prod.ProductCode.ToUpper() == renewal.ProductCode.ToUpper() && p.ValidityTo == null).FirstOrDefault();
            var conProd    = insuree.Family.TblPolicy.Any(p => p.Prod.ConversionProd != null);

            TblPolicy convPolicy = null;

            if (conProd == true)
            {
                convPolicy = insuree.Family.TblPolicy.Where(p => p.Prod.ConversionProd.ProductCode.ToUpper() == renewal.ProductCode.ToUpper() && p.ValidityTo == null).FirstOrDefault();
            }

            if (prevPolicy == null && convPolicy == null)
            {
                dataMessage.Code         = (int)Errors.Renewal.NoPreviousPolicyFoundToRenew;
                dataMessage.MessageValue = "No previous policy found with given renewal product";
                dataMessage.ErrorOccured = true;
                return(dataMessage);
            }

            return(dataMessage);
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        /// TODO: solve the TypeOf assign
        public TblInsuree ToTblInsuree()
        {
            TblInsuree tblInsuree = new TblInsuree()
            {
                InsureeId      = this.InsureeId,
                Passport       = this.IdentificationNumber,
                Chfid          = this.CHFID,
                LastName       = this.LastName,
                OtherNames     = this.OtherNames,
                Dob            = this.DOB,
                IsHead         = this.IsHead,
                Phone          = this.Phone,
                Gender         = this.Gender,
                Marital        = this.Marital,
                TypeOfId       = this.TypeOfId,
                CardIssued     = this.CardIssued,
                Relationship   = this.Relationship,
                Profession     = this.Profession,
                Education      = this.Education,
                Email          = this.Email,
                Hfid           = this.HFID,
                CurrentAddress = this.CurrentAddress,
                GeoLocation    = this.GeoLocation,
                //CurrentVillage = this.CurrentVillage == null ? DBNull.Value : this.CurrentVillage.LocationId, // todo: is there any link missing?
                //Photo = this.Photo.ToTblPhoto(),
                //TypeOf = this.TypeOf.IdentificationTypes,
                IsOffline = this.IsOffline,
            };

            if (this.CurrentVillage != null)
            {
                tblInsuree.CurrentVillage = this.CurrentVillage.LocationId;                 // todo: is there any link missing?
            }

            if (this.Photo != null)
            {
                tblInsuree.Photo = this.Photo.ToTblPhoto();
            }

            return(tblInsuree);
        }
        //public InsureeModel(TblInsuree tblInsuree):base()
        //{
        //	this.FromTblInsuree(tblInsuree);
        //}

        public static InsureeModel FromTblInsuree(TblInsuree tblInsuree)
        {
            if (tblInsuree == null)
            {
                return(null);
            }

            InsureeModel insuree = new InsureeModel()
            {
                InsureeId            = tblInsuree.InsureeId,
                IdentificationNumber = tblInsuree.Passport,
                CHFID               = tblInsuree.Chfid,
                LastName            = tblInsuree.LastName,
                OtherNames          = tblInsuree.OtherNames,
                DOB                 = tblInsuree.Dob,
                IsHead              = tblInsuree.IsHead,
                Phone               = tblInsuree.Phone,
                Gender              = tblInsuree.Gender,
                Marital             = tblInsuree.Marital,
                TypeOfId            = tblInsuree.TypeOfId,
                CardIssued          = tblInsuree.CardIssued,
                Email               = tblInsuree.Email,
                CurrentAddress      = tblInsuree.CurrentAddress,
                GeoLocation         = tblInsuree.GeoLocation,
                IdentificationTypes = tblInsuree.TypeOfId,                 /// todo: link to the table value

                ValidFrom = tblInsuree.ValidityFrom,
                ValidTo   = TypeCast.GetValue <DateTime>(tblInsuree.ValidityTo)
            };

            if (tblInsuree.Relationship != null)
            {
                insuree.Relationship = (short)tblInsuree.Relationship;                 /// TODO: link to the detailed table
            }
            if (tblInsuree.Profession != null)
            {
                insuree.Profession = TypeCast.GetValue <short>(tblInsuree.Profession);               /// TODO: link to the detailed table
            }
            if (tblInsuree.Education != null)
            {
                insuree.Education = TypeCast.GetValue <short>(tblInsuree.Education);               /// TODO: link to the detailed table
            }
            if (tblInsuree.Hfid != null)
            {
                insuree.HFID = (short)tblInsuree.Hfid;
            }
            if (tblInsuree.IsOffline != null)
            {
                insuree.IsOffline = (bool)tblInsuree.IsOffline;
            }
            if (tblInsuree.CurrentVillage != null)
            {
                insuree.CurrentVillage = new LocationModel()
                {
                    LocationId = TypeCast.GetValue <int>(tblInsuree.CurrentVillage)                    // todo: is there any link missing?
                };
            }

            //if (tblInsuree.Gender != null) {
            //	insuree.Gender = tblInsuree.Gender[0];
            //}
            //if (tblInsuree.Marital != null)
            //{
            //	insuree.Marital = tblInsuree.Marital[0];
            //}
            //if (tblInsuree.TypeOfId!= null)
            //{
            //	insuree.TypeOfId = tblInsuree.TypeOfId[0];
            //}
            if (tblInsuree.Photo != null)
            {
                insuree.Photo = PhotoModel.FromTblPhoto(tblInsuree.Photo);
            }
            return(insuree);
        }
        public static InsureeModel FromTblInsuree(TblInsuree tblInsuree)
        {
            if (tblInsuree == null)
            {
                return(null);
            }

            InsureeModel insuree = new InsureeModel()
            {
                InsureeId            = tblInsuree.InsureeId,
                InsureeUUID          = tblInsuree.InsureeUUID,
                IdentificationNumber = tblInsuree.Passport,
                CHFID               = tblInsuree.Chfid,
                LastName            = tblInsuree.LastName,
                OtherNames          = tblInsuree.OtherNames,
                DOB                 = tblInsuree.Dob.ToString("yyyy-MM-dd"),
                IsHead              = tblInsuree.IsHead,
                Phone               = tblInsuree.Phone,
                Gender              = tblInsuree.Gender,
                Marital             = tblInsuree.Marital,
                TypeOfId            = tblInsuree.TypeOfId,
                CardIssued          = tblInsuree.CardIssued,
                Email               = tblInsuree.Email,
                CurrentAddress      = tblInsuree.CurrentAddress,
                GeoLocation         = tblInsuree.GeoLocation,
                IdentificationTypes = tblInsuree.TypeOfId
            };

            if (tblInsuree.Relationship != null)
            {
                insuree.Relationship = (short)tblInsuree.Relationship;
            }

            if (tblInsuree.Profession != null)
            {
                insuree.Profession = TypeCast.GetValue <short>(tblInsuree.Profession);
            }

            if (tblInsuree.Education != null)
            {
                insuree.Education = TypeCast.GetValue <short>(tblInsuree.Education);
            }

            if (tblInsuree.Hfid != null)
            {
                insuree.HFID = (short)tblInsuree.Hfid;
            }

            if (tblInsuree.CurrentVillage != null)
            {
                insuree.CurVillage = tblInsuree.CurrentVillage.ToString();
            }

            if (tblInsuree.IsOffline != null)
            {
                insuree.IsOffline = (bool)tblInsuree.IsOffline;
            }

            if (tblInsuree.Photo != null)
            {
                insuree.PhotoPath = tblInsuree.Photo.PhotoFileName;
            }

            return(insuree);
        }