예제 #1
0
        public async Task <IActionResult> Index(string sortColumn, string sortDirection, int pageSize = 0, int pageNumber = 0, string filters = null)
        {
            var scope = AuthenticationService.GetScope(User);

            var queryOptions = new CommissionSplitRuleQueryOptions(scope, sortColumn, sortDirection, pageSize, pageNumber, filters);

            var commissions = await CommissionSplitService.GetCommissionSplitRules(queryOptions);

            return(Ok(commissions));
        }
예제 #2
0
        private async Task <CommissionSplitRule> GetDefaultCommissionSplitRule(ScopeOptions scope, Guid policyId)
        {
            var policy = await _context.Policy.FindAsync(policyId);

            if (policy == null)
            {
                return(null);
            }

            var options = new CommissionSplitRuleQueryOptions(scope, "", "", 0, 0);

            options.UserId.Add(policy.UserId);
            var rules = await _commissionSplitService.GetCommissionSplitRules(options);

            return(rules.Items.FirstOrDefault(r => r.IsDefault));
        }
예제 #3
0
        public async Task Index()
        {
            var rule = new CommissionSplitRule()
            {
                Id        = Guid.NewGuid(),
                Name      = Guid.NewGuid().ToString(),
                UserId    = Guid.NewGuid(),
                IsDefault = true,
                Split     = new List <CommissionSplit>(),
            };

            var pagedItems = new PagedItems <CommissionSplitRule>()
            {
                TotalItems = 1,
                Items      = new List <CommissionSplitRule>()
                {
                    rule
                }
            };

            var service     = new Mock <ICommissionSplitService>();
            var authService = TestHelper.MockAuthenticationService(Scope.Branch);

            CommissionSplitRuleQueryOptions queryOptions = null;

            service.Setup(c => c.GetCommissionSplitRules(It.IsAny <CommissionSplitRuleQueryOptions>()))
            .Callback((CommissionSplitRuleQueryOptions options) => queryOptions = options)
            .ReturnsAsync(pagedItems);

            var controller = new CommissionSplitRulesController(service.Object, authService.Object);

            var result = await controller.Index("Name", "desc", 15, 2, $"UserId={rule.UserId}");

            Assert.Equal(Scope.Branch, queryOptions.Scope.Scope);
            Assert.Equal("Name", queryOptions.SortOptions.Column);
            Assert.Equal(SortDirection.Descending, queryOptions.SortOptions.Direction);
            Assert.Equal(15, queryOptions.PageOptions.Size);
            Assert.Equal(2, queryOptions.PageOptions.Number);

            Assert.Equal(rule.UserId, queryOptions.UserId.Single());

            var okResult    = Assert.IsType <OkObjectResult>(result);
            var returnValue = Assert.IsType <PagedItems <CommissionSplitRule> >(okResult.Value);

            Assert.Same(pagedItems, returnValue);
        }
예제 #4
0
        public async Task <PagedItems <CommissionSplitRule> > GetCommissionSplitRules(CommissionSplitRuleQueryOptions queryOptions)
        {
            var query = GetCommissionSplitRuleQuery(queryOptions.Scope);

            //Apply filters ----------------------------------------------------------------------------------------
            if (queryOptions.UserId.Any())
            {
                query = query.Where(c => queryOptions.UserId.Contains(c.UserId.Value));
            }
            //------------------------------------------------------------------------------------------------------

            var pagedItems = new PagedItems <CommissionSplitRule>();

            //Get total items
            pagedItems.TotalItems = await query.CountAsync();

            //Ordering
            query = query.OrderBy(queryOptions.SortOptions.Column, queryOptions.SortOptions.Direction);

            //Paging
            pagedItems.Items = await query.TakePage(queryOptions.PageOptions.Number, queryOptions.PageOptions.Size).ToListAsync();

            return(pagedItems);
        }
        public async Task GetCommissionSplitRules()
        {
            var options = TestHelper.GetDbContext("GetCommissionSplitRules");

            var user1 = TestHelper.InsertUserDetailed(options);
            var user2 = TestHelper.InsertUserDetailed(options, user1.Organisation);
            var user3 = TestHelper.InsertUserDetailed(options);

            var csr1 = new CommissionSplitRuleEntity
            {
                Id        = Guid.NewGuid(),
                UserId    = user1.User.Id,
                Name      = "Com Split Rule 1",
                IsDefault = true,
                Split     = new List <CommissionSplit>()
                {
                    new CommissionSplit()
                    {
                        UserId     = user1.User.Id,
                        Percentage = 100
                    }
                }
            };

            var csr2 = new CommissionSplitRuleEntity
            {
                Id        = Guid.NewGuid(),
                UserId    = user2.User.Id,
                Name      = "Com Split Rule 2",
                IsDefault = true,
                Split     = new List <CommissionSplit>()
                {
                    new CommissionSplit()
                    {
                        UserId     = user2.User.Id,
                        Percentage = 100
                    }
                }
            };

            var csr3 = new CommissionSplitRuleEntity
            {
                Id        = Guid.NewGuid(),
                UserId    = user3.User.Id,
                Name      = "Com Split Rule 3",
                IsDefault = true,
                Split     = new List <CommissionSplit>()
                {
                    new CommissionSplit()
                    {
                        UserId     = user3.User.Id,
                        Percentage = 100
                    }
                }
            };

            var csr4 = new CommissionSplitRuleEntity
            {
                Id        = Guid.NewGuid(),
                UserId    = user1.User.Id,
                Name      = "Com Split Rule 4",
                IsDefault = false,
                Split     = new List <CommissionSplit>()
                {
                    new CommissionSplit()
                    {
                        UserId     = user1.User.Id,
                        Percentage = 100
                    }
                }
            };

            using (var context = new DataContext(options))
            {
                context.CommissionSplitRule.Add(csr1);
                context.CommissionSplitRule.Add(csr2);
                context.CommissionSplitRule.Add(csr3);
                context.CommissionSplitRule.Add(csr4);

                context.SaveChanges();
            }

            using (var context = new DataContext(options))
            {
                var auditService = new AuditServiceMock();
                var service      = new CommissionSplitService(context, auditService);

                //When
                var scope        = TestHelper.GetScopeOptions(user1);
                var queryOptions = new CommissionSplitRuleQueryOptions(scope, "", "", 0, 0);
                var rules        = await service.GetCommissionSplitRules(queryOptions);

                //Then
                Assert.Equal(3, rules.TotalItems);
                Assert.Equal(3, rules.Items.Count());

                var items  = rules.Items.ToList();
                var actual = items[0];
                Assert.Equal(csr1.Id, actual.Id);
                Assert.Equal(csr1.Name, actual.Name);
                Assert.Equal(csr1.UserId, actual.UserId);
                Assert.Equal(csr1.IsDefault, actual.IsDefault);
                Assert.Equal(csr1.Split, actual.Split);

                actual = items[1];
                Assert.Equal(csr2.Id, actual.Id);

                actual = items[2];
                Assert.Equal(csr4.Id, actual.Id);
                Assert.Equal(csr4.IsDefault, actual.IsDefault);

                //Filter check (user id)
                scope        = TestHelper.GetScopeOptions(user1);
                queryOptions = new CommissionSplitRuleQueryOptions(scope, "", "", 0, 0);
                queryOptions.UserId.Add(user2.User.Id);
                rules = await service.GetCommissionSplitRules(queryOptions);

                Assert.Single(rules.Items);

                actual = rules.Items.First();
                Assert.Equal(csr2.Id, actual.Id);


                //Check scope
                scope        = TestHelper.GetScopeOptions(user3, Scope.Organisation);
                queryOptions = new CommissionSplitRuleQueryOptions(scope, "", "", 0, 0);
                rules        = await service.GetCommissionSplitRules(queryOptions);

                Assert.Single(rules.Items);

                actual = rules.Items.First();
                Assert.Equal(csr3.Id, actual.Id);
            }
        }
        public async Task <ImportResult> ImportCommissions(ScopeOptions scope, Guid commissionStatementId, IEnumerable <ImportCommission> importData)
        {
            var importResult = new ImportResult();

            CommissionsToInsert      = new List <CommissionEntity>();
            CommissionErrorsToInsert = new List <CommissionErrorEntity>();

            //Scope check
            var queryOptions = new CommissionStatementQueryOptions(scope, "", "", 0, 0);

            queryOptions.CommissionStatementId = commissionStatementId;
            var statements = await _commissionStatementService.GetCommissionStatements(queryOptions);

            if (!statements.Items.Any())
            {
                return(importResult);
            }

            var statement = statements.Items.Single();

            var commissionTypes = await _commissionLookupService.GetCommissionTypes();

            var commissionTypesDictionary = BuildCommissionTypesDictionary(commissionTypes);
            var company = await _lookupService.GetCompany(statement.CompanyId);

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

            policyQueryOptions.CompanyId.Add(statement.CompanyId);
            var policies         = (await _policyService.GetPolicies(policyQueryOptions)).Items.ToList();
            var policyDictionary = BuildPolicyDictionary(policies, company.CommissionPolicyNumberPrefixes.ToList());

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

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

            foreach (var data in importData)
            {
                var result = ImportCommission(scope, statement, data, policyDictionary, commissionTypesDictionary, commissionSplitRules, commissionSplitRulePolicies);

                importResult.Results.Add(result);

                if (data.CommissionTypeCode == CommissionType.COMMISSION_TYPE_UNKNOWN_CODE)
                {
                    importResult.AddUnknownCommissionTypeValue(data.CommissionTypeValue);
                }
            }

            if (CommissionsToInsert.Any())
            {
                await _bulkActions.BulkInsertCommissionsAsync(_context, CommissionsToInsert);
            }

            if (CommissionErrorsToInsert.Any())
            {
                await _bulkActions.BulkInsertCommissionErrorsAsync(_context, CommissionErrorsToInsert);
            }

            await _auditService.InsertAuditLog(scope, "Import", "Commission", statement.Id,
                                               new
            {
                commissionStatementId = commissionStatementId,
                importCount           = importResult.ImportCount,
                errorCount            = importResult.ErrorCount,
                errors = importResult.Results.Where(r => !r.Success).ToList()
            }
                                               );

            return(importResult);
        }
예제 #7
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);
        }