public static int SaveInsured(IInsuredsService _iss, string FirstName, string LastName, string SSN,
                                      string Email, DateTime BirthDate, string PhoneNumber, string PassportNumber_ID, string Address,
                                      string City, string PostalCode, string CreatedById)
        {
            var insuredId = _iss.GetInsuredIdBySsn(SSN);

            if (insuredId != -1)
            {
                insured updateInsuredData = new insured();
                updateInsuredData.ID                       = insuredId;
                updateInsuredData.Name                     = FirstName;
                updateInsuredData.Lastname                 = LastName;
                updateInsuredData.SSN                      = SSN;
                updateInsuredData.Email                    = Email;
                updateInsuredData.DateBirth                = BirthDate;
                updateInsuredData.Phone_Number             = PhoneNumber;
                updateInsuredData.Passport_Number_IdNumber = PassportNumber_ID;
                updateInsuredData.City                     = City;
                updateInsuredData.Postal_Code              = PostalCode;
                updateInsuredData.Address                  = Address;
                updateInsuredData.Date_Modified            = DateTime.UtcNow;
                updateInsuredData.Modified_By              = CreatedById;
                _iss.UpdateInsuredData(updateInsuredData);

                return(insuredId);
            }
            var newInsured = _iss.Create();

            newInsured.Name                     = FirstName;
            newInsured.Lastname                 = LastName;
            newInsured.SSN                      = SSN;
            newInsured.Email                    = Email;
            newInsured.DateBirth                = BirthDate;
            newInsured.Phone_Number             = PhoneNumber;
            newInsured.Passport_Number_IdNumber = PassportNumber_ID;
            newInsured.City                     = City;
            newInsured.Postal_Code              = PostalCode;
            newInsured.Address                  = Address;
            newInsured.Date_Created             = DateTime.UtcNow;
            newInsured.Created_By               = CreatedById;
            try
            {
                insuredId = _iss.AddInsured(newInsured);
            }
            finally { }
            return(insuredId);
        }
        public static int SavePolicy(Policy p,
                                     IPolicyService _ps,
                                     IUserService _us,
                                     IInsuredsService _iss,
                                     IPolicyInsuredService _pis,
                                     IAdditionalChargesService _acs)
        {
            var policy   = _ps.Create();
            var username = "";

            if (p.isMobile)
            {
                username          = p.username;
                policy.Created_By = _us.GetUserIdByUsername(p.username);
            }
            else
            {
                username          = System.Web.HttpContext.Current.User.Identity.Name;
                policy.Created_By = _us.GetUserIdByUsername(username);
            }

            policy.Date_Created            = DateTime.UtcNow;
            policy.Policy_Number           = _ps.CreatePolicyNumber();
            policy.CountryID               = p.CountryID;
            policy.Exchange_RateID         = (p.Exchange_RateID.HasValue) ? p.Exchange_RateID.Value : 1;
            policy.Policy_TypeID           = p.Policy_TypeID;
            policy.Retaining_RiskID        = p.Retaining_RiskID;
            policy.Start_Date              = p.Start_Date;
            policy.End_Date                = p.End_Date;
            policy.Valid_Days              = p.Valid_Days;
            policy.Travel_NumberID         = p.Travel_NumberID;
            policy.Total_Premium           = p.Total_Premium;
            policy.Payment_Status          = false;
            policy.Travel_Insurance_TypeID = p.Travel_Insurance_TypeID;

            RoleAuthorize r = new RoleAuthorize();

            if (p.IsSamePolicyHolderInsured && (p.isMobile || r.IsUser("End user")))
            {
                _us.UpdateSsnById(policy.Created_By, p.SSN);

                var PolicyHolderId = SaveInsuredHelper.SaveInsured(_iss, p.Name, p.LastName, p.SSN, p.Email, p.BirthDate, p.PhoneNumber, p.PassportNumber_ID, p.Address, p.City, p.PostalCode, policy.Created_By);
                policy.Policy_HolderID = PolicyHolderId;
            }
            else if (p.IsSamePolicyHolderInsured && (r.IsUser("Admin") || r.IsUser("Broker") || r.IsUser("Broker manager")))
            {
                if (p.IsExistentPolicyHolder)
                {
                    policy.Policy_HolderID = _iss.GetInsuredBySsn(p.SSN).ID;
                }
                else
                {
                    var PolicyHolderId = SaveInsuredHelper.SaveInsured(_iss, p.PolicyHolderName, p.PolicyHolderLastName, p.PolicyHolderSSN, p.PolicyHolderEmail, p.PolicyHolderBirthDate.Value, p.PolicyHolderPhoneNumber, p.PolicyHolderPassportNumber_ID, p.PolicyHolderAddress, p.PolicyHolderCity, p.PolicyHolderPostalCode, policy.Created_By);
                    policy.Policy_HolderID = PolicyHolderId;
                }
            }

            if (!p.IsSamePolicyHolderInsured)
            {
                var ssn = "";
                if (r.IsUser("Admin") || r.IsUser("Broker") || r.IsUser("Broker manager"))
                {
                    ssn = p.PolicyHolderSSN;
                    var policyHolderId = _iss.GetInsuredIdBySsnAndCreatedBy(ssn, policy.Created_By);
                    if (policyHolderId != -1)
                    {
                        insured updateInsuredData = new insured();

                        policy.Policy_HolderID     = policyHolderId;
                        updateInsuredData.ID       = policyHolderId;
                        updateInsuredData.Name     = p.PolicyHolderName;
                        updateInsuredData.Lastname = p.PolicyHolderLastName;
                        updateInsuredData.SSN      = p.PolicyHolderSSN;

                        updateInsuredData.Email        = p.PolicyHolderEmail;
                        updateInsuredData.DateBirth    = p.PolicyHolderBirthDate ?? DateTime.UtcNow;
                        updateInsuredData.Phone_Number = p.PolicyHolderPhoneNumber;

                        updateInsuredData.Passport_Number_IdNumber = p.PolicyHolderPassportNumber_ID;

                        updateInsuredData.City        = p.PolicyHolderCity;
                        updateInsuredData.Postal_Code = p.PolicyHolderPostalCode;
                        updateInsuredData.Address     = p.PolicyHolderAddress;

                        updateInsuredData.Date_Modified = DateTime.Now;
                        updateInsuredData.Modified_By   = policy.Created_By;

                        _iss.UpdateInsuredData(updateInsuredData);
                    }
                    else
                    {
                        var newInsured = _iss.Create();

                        newInsured.Name     = p.PolicyHolderName;
                        newInsured.Lastname = p.PolicyHolderLastName;
                        newInsured.SSN      = p.PolicyHolderSSN;

                        newInsured.Email        = p.PolicyHolderEmail;
                        newInsured.DateBirth    = p.PolicyHolderBirthDate ?? DateTime.UtcNow;
                        newInsured.Phone_Number = p.PolicyHolderPhoneNumber;

                        newInsured.Passport_Number_IdNumber = p.PolicyHolderPassportNumber_ID;

                        newInsured.City        = p.PolicyHolderCity;
                        newInsured.Postal_Code = p.PolicyHolderPostalCode;
                        newInsured.Address     = p.PolicyHolderAddress;

                        newInsured.Date_Created = DateTime.Now;
                        newInsured.Created_By   = policy.Created_By;
                        try
                        {
                            var Id = _iss.AddInsured(newInsured);
                            policy.Policy_HolderID = Id;
                        }
                        finally { }
                    }
                }
                else if (r.IsUser("End user"))
                {
                    ssn = _us.GetUserSsnByUsername(username);
                    var policyHolderId = _iss.GetInsuredIdBySsnAndCreatedBy(ssn, policy.Created_By);
                    if (policyHolderId != -1)
                    {
                        policy.Policy_HolderID = policyHolderId;
                    }
                    else
                    {
                        var currentEndUser = _us.GetUserDataByUsername(username);
                        var newInsured     = _iss.Create();

                        newInsured.Name     = currentEndUser.FirstName;
                        newInsured.Lastname = currentEndUser.LastName;
                        newInsured.SSN      = currentEndUser.EMBG;

                        newInsured.Email        = currentEndUser.Email;
                        newInsured.DateBirth    = currentEndUser.DateOfBirth.HasValue == true ? currentEndUser.DateOfBirth.Value: DateTime.Now;
                        newInsured.Phone_Number = currentEndUser.PhoneNumber;

                        newInsured.Passport_Number_IdNumber = currentEndUser.PassportNumber;

                        newInsured.City        = currentEndUser.City;
                        newInsured.Postal_Code = currentEndUser.PostalCode;
                        newInsured.Address     = currentEndUser.Address;

                        newInsured.Date_Created = DateTime.Now;
                        newInsured.Created_By   = policy.Created_By;
                        try
                        {
                            var Id = _iss.AddInsured(newInsured);
                            policy.Policy_HolderID = Id;
                        }
                        finally { }
                    }
                }
            }

            var policyID  = _ps.AddPolicy(policy);
            var insuredId = _iss.GetInsuredIdBySsnAndCreatedBy(p.SSN, policy.Created_By);

            if (insuredId != -1)
            {
                // da se update
                var policyInsured = _pis.Create();
                policyInsured.InsuredID = insuredId;
                policyInsured.PolicyID  = policyID;
                _pis.Add(policyInsured);
            }
            else
            {
                var newInsured = _iss.Create();
                newInsured.Date_Created = DateTime.Now;
                newInsured.Created_By   = policy.Created_By;
                newInsured.Name         = p.Name;
                newInsured.Lastname     = p.LastName;
                newInsured.SSN          = p.SSN;

                newInsured.Email        = p.Email;
                newInsured.DateBirth    = p.BirthDate;
                newInsured.Phone_Number = p.PhoneNumber;

                newInsured.Passport_Number_IdNumber = p.PassportNumber_ID;

                newInsured.City        = p.City;
                newInsured.Postal_Code = p.PostalCode;
                newInsured.Address     = p.Address;
                try
                {
                    var            insuredIdNew  = _iss.AddInsured(newInsured);
                    policy_insured policyInsured = new policy_insured();
                    policyInsured.InsuredID = insuredIdNew;
                    policyInsured.PolicyID  = policyID;
                    _pis.Add(policyInsured);
                }
                finally { }
            }

            if (p.isMobile)
            {
                if (p.AdditionalChargeId1 != 1)
                {
                    var addChargeNew = _acs.Create();
                    addChargeNew.PolicyID            = policyID;
                    addChargeNew.Additional_ChargeID = p.AdditionalChargeId1;
                    _acs.AddAdditionalChargesPolicy(addChargeNew);
                }

                if (p.AdditionalChargeId2 != 1)
                {
                    var addChargeNew = _acs.Create();
                    addChargeNew.PolicyID            = policyID;
                    addChargeNew.Additional_ChargeID = p.AdditionalChargeId2;
                    _acs.AddAdditionalChargesPolicy(addChargeNew);
                }
            }
            else
            {
                if (p.additional_charges != null)
                {
                    foreach (additional_charge additionalCharge in p.additional_charges)
                    {
                        if (additionalCharge.ID != 1)
                        {
                            var addChargeNew = _acs.Create();
                            addChargeNew.PolicyID            = policyID;
                            addChargeNew.Additional_ChargeID = additionalCharge.ID;
                            _acs.AddAdditionalChargesPolicy(addChargeNew);
                        }
                    }
                }
            }
            return(policyID);
        }