Esempio n. 1
0
        public async Task Index()
        {
            var statement = new CommissionStatement()
            {
                Id = Guid.NewGuid(),
                AmountIncludingVAT = 350000,
                VAT = 25000,
                ActualAmountIncludingVAT = 300000,
                ActualVAT         = 20000,
                CommissionCount   = 1000,
                CompanyId         = Guid.NewGuid(),
                CompanyName       = "Comp 1",
                Date              = DateTime.Now,
                MappingErrorCount = 100,
                Processed         = true
            };

            var pagedItems = new PagedCommissionStatements()
            {
                TotalItems            = 1,
                SumAmountIncludingVAT = 300,
                SumVAT = 400,
                Items  = new List <CommissionStatement>()
                {
                    statement
                }
            };

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

            CommissionStatementQueryOptions queryOptions = null;

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

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

            var result = await controller.Index("date", "desc", 15, 2, $"commissionStatementId={statement.Id}");

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

            Assert.Equal(statement.Id, queryOptions.CommissionStatementId);

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

            Assert.Same(pagedItems, returnValue);
        }
        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));
        }