private Result ImportCommission(
            ScopeOptions scope,
            CommissionStatement commissionStatement,
            ImportCommission importCommission,
            Dictionary <string, Policy> policies,
            Dictionary <string, CommissionType> commissionTypes,
            List <CommissionSplitRule> commissionSplitRules,
            List <CommissionSplitRulePolicy> commissionSplitRulePolicies)
        {
            var validator = new ImportCommissionValidator();
            var result    = validator.Validate(importCommission).GetResult();

            if (!result.Success)
            {
                return(result);
            }

            //Cleanup
            importCommission.PolicyNumber = importCommission.PolicyNumber.TrimWhiteSpace();;

            var error = new CommissionErrorEntity()
            {
                Id = Guid.NewGuid(),
                CommissionStatementId = commissionStatement.Id,
                Data = importCommission,
            };

            CommissionType commissionType    = null;
            var            commissionTypeKey = importCommission.CommissionTypeCode.ToLowerInvariant();

            if (commissionTypes.ContainsKey(commissionTypeKey))
            {
                commissionType         = commissionTypes[commissionTypeKey];
                error.CommissionTypeId = commissionType.Id;
            }

            Policy policy          = null;
            var    policyNumberKey = importCommission.PolicyNumber.ToLowerInvariant();

            if (policies.ContainsKey(policyNumberKey))
            {
                policy = policies[policyNumberKey];
            }

            if (policy != null)
            {
                error.ClientId = policy.ClientId;
                error.PolicyId = policy.Id;
            }

            if (!IsCommissionErrorValid(error))
            {
                CommissionErrorsToInsert.Add(error);
                return(new Result(true));
            }

            var commission = LoadCommissionEdit(commissionStatement.Id, policy, commissionType, importCommission);

            var commissionsSplit = _commissionSplitService.SplitCommission(commission, policy, importCommission, commissionSplitRules, commissionSplitRulePolicies);

            CommissionsToInsert.AddRange(commissionsSplit.Select(c => MapCommissionToEntity(c)));

            return(new Result(true));
        }
Exemplo n.º 2
0
        public async Task <Result> ResolveMappingError(ScopeOptions scope, CommissionErrorEdit error)
        {
            var validator = new CommissionErrorValidator();
            var result    = validator.Validate(error).GetResult();

            if (!result.Success)
            {
                return(result);
            }

            var commission = new CommissionEdit();

            commission.PolicyId = error.PolicyId;
            commission.CommissionStatementId = error.CommissionStatementId;
            commission.CommissionTypeId      = error.CommissionTypeId;
            commission.AmountIncludingVAT    = Convert.ToDecimal(error.Data.AmountIncludingVAT);
            commission.VAT        = Convert.ToDecimal(error.Data.VAT);
            commission.SourceData = error.Data;

            var policyQueryOptions = new PolicyQueryOptions(scope, "", "", 0, 0);

            policyQueryOptions.Id = error.PolicyId;
            var policy = (await _policyService.GetPolicies(policyQueryOptions)).Items.Single();

            var commissionSplitRulesOptions = new CommissionSplitRuleQueryOptions(scope, "", "", 0, 0);
            var commissionSplitRules        = (await _commissionSplitService.GetCommissionSplitRules(commissionSplitRulesOptions)).Items.ToList();

            var commissionSplitRulePolicyQueryOptions = new CommissionSplitRulePolicyQueryOptions(scope, "", "", 0, 0);
            var commissionSplitRulePolicies           = (await _commissionSplitRulePolicyService.GetCommissionSplitRulePolicies(commissionSplitRulePolicyQueryOptions)).Items.ToList();

            var commissions = _commissionSplitService.SplitCommission(commission, policy, error.Data, commissionSplitRules, commissionSplitRulePolicies);

            foreach (var c in commissions)
            {
                result = await _commissionService.InsertCommission(scope, c);
            }

            //Update client details (if none exist)
            try
            {
                var client = await _clientService.GetClient(scope, error.ClientId.Value);

                client.FirstName   = string.IsNullOrEmpty(client.FirstName) ? error.Data.FirstName ?? "" : client.FirstName;
                client.LastName    = string.IsNullOrEmpty(client.LastName) ? error.Data.FullName ?? "" : client.LastName;
                client.LastName    = string.IsNullOrEmpty(client.LastName) ? error.Data.LastName ?? "" : client.LastName;
                client.Initials    = string.IsNullOrEmpty(client.Initials) ? error.Data.Initials ?? "" : client.Initials;
                client.DateOfBirth = !client.DateOfBirth.HasValue ? DateTime.Parse(error.Data.DateOfBirth) : client.DateOfBirth;

                if (!string.IsNullOrEmpty(error.Data.IdNumber))
                {
                    var idNumber = new IdNumber(error.Data.IdNumber);
                    if (idNumber.IsValid)
                    {
                        client.IdNumber = string.IsNullOrEmpty(client.IdNumber) ? error.Data.IdNumber : client.IdNumber;
                    }
                    else
                    {
                        client.AlternateIdNumber = string.IsNullOrEmpty(client.AlternateIdNumber) ? error.Data.IdNumber ?? "" : client.AlternateIdNumber;
                    }
                }
                await _clientService.UpdateClient(scope, client);

                //Add policy alias if policy number doesnt match policy id
                if (!policy.Number.IgnoreCaseEquals(error.Data.PolicyNumber))
                {
                    //First check if its not already and alias
                    if (!policy.NumberAliases.Any(n => n.IgnoreCaseEquals(error.Data.PolicyNumber)))
                    {
                        var policyEdit = await _policyService.GetPolicy(scope, policy.Id);

                        policyEdit.NumberAliases.Add(error.Data.PolicyNumber);
                        await _policyService.UpdatePolicy(scope, policyEdit);
                    }
                }
            }
            catch { }


            if (!result.Success)
            {
                return(result);
            }

            await DeleteError(scope, error.Id);

            result.Tag = commission;

            return(result);
        }