Example #1
0
        public IHttpActionResult CreateQuote(AddPolicyMobileViewModel addPolicy)
        {
            if (addPolicy == null)
            {
                throw new Exception("Internal error: Empty Policy");
            }
            int result = SavePolicyHelper.SavePolicyFromMobile(addPolicy, _ps, _us, _iss, _pis, _acs);

            if (result != -1)
            {
                return(Ok(new { QuoteID = result }));
            }
            else
            {
                throw new Exception("Internal error: Not saved");
            }
        }
        public static int SavePolicyFromMobile(AddPolicyMobileViewModel addPolicyMobile,
                                               IPolicyService _ps,
                                               IUserService _us,
                                               IInsuredsService _iss,
                                               IPolicyInsuredService _pis,
                                               IAdditionalChargesService _acs)
        {
            try
            {
                //add policy details
                var username = addPolicyMobile.Username;

                var policy = _ps.Create();
                policy.Policy_Number           = _ps.CreatePolicyNumber();
                policy.Exchange_RateID         = addPolicyMobile.Exchange_RateID;
                policy.CountryID               = addPolicyMobile.CountryID;
                policy.Policy_TypeID           = addPolicyMobile.Policy_TypeID;
                policy.Retaining_RiskID        = addPolicyMobile.Retaining_RiskID;
                policy.Start_Date              = addPolicyMobile.Start_Date;
                policy.End_Date                = addPolicyMobile.End_Date;
                policy.Valid_Days              = addPolicyMobile.Valid_Days;
                policy.Travel_NumberID         = addPolicyMobile.Travel_NumberID;
                policy.Travel_Insurance_TypeID = addPolicyMobile.Travel_Insurance_TypeID;
                policy.Total_Premium           = addPolicyMobile.Total_Premium;
                policy.Created_By              = addPolicyMobile.Created_By;
                policy.Date_Created            = DateTime.UtcNow;
                policy.Payment_Status          = false;

                //add policy holder
                var policyHolder = _iss.GetInsuredBySsnAndCreatedBy(addPolicyMobile.SSN, addPolicyMobile.Created_By);
                if (policyHolder == null)
                {
                    var policyHolderID = SaveInsuredHelper.SaveInsured(_iss, addPolicyMobile.Name,
                                                                       addPolicyMobile.LastName,
                                                                       addPolicyMobile.SSN,
                                                                       addPolicyMobile.Email,
                                                                       addPolicyMobile.DateBirth,
                                                                       addPolicyMobile.Phone_Number,
                                                                       addPolicyMobile.Passport_Number_IdNumber,
                                                                       addPolicyMobile.Address,
                                                                       addPolicyMobile.City,
                                                                       addPolicyMobile.Postal_Code,
                                                                       addPolicyMobile.Created_By);
                    policy.Policy_HolderID = policyHolderID;
                }
                else
                {
                    policy.Policy_HolderID = policyHolder.ID;
                }

                var policyID = _ps.AddPolicy(policy);

                if (addPolicyMobile.Insureds != null)
                {
                    foreach (var insured in addPolicyMobile.Insureds)
                    {
                        var addInsured   = _iss.GetInsuredBySsnAndCreatedBy(insured.SSN, addPolicyMobile.Created_By);
                        var addInsuredID = SaveInsuredHelper.SaveInsured(_iss, insured.Name,
                                                                         insured.Lastname,
                                                                         insured.SSN,
                                                                         insured.Email,
                                                                         insured.DateBirth,
                                                                         insured.Phone_Number,
                                                                         insured.Passport_Number_IdNumber,
                                                                         insured.Address,
                                                                         insured.City,
                                                                         insured.Postal_Code,
                                                                         addPolicyMobile.Created_By);

                        var policyInsured = _pis.Create();
                        policyInsured.InsuredID = addInsuredID;
                        policyInsured.PolicyID  = policyID;
                        _pis.Add(policyInsured);
                    }
                }

                if (addPolicyMobile.Additional_charges != null)
                {
                    foreach (var additionalChargeId in addPolicyMobile.Additional_charges)
                    {
                        var additionalChargeObject = _acs.GetAdditionalChargeById(additionalChargeId);
                        if (additionalChargeObject != null)
                        {
                            var addNewCharge = _acs.Create();
                            addNewCharge.PolicyID            = policyID;
                            addNewCharge.Additional_ChargeID = additionalChargeObject.ID;
                            _acs.AddAdditionalChargesPolicy(addNewCharge);
                        }
                    }
                }
                return(policyID);
            }
            catch (Exception e)
            {
                return(-1);
            }
        }