Example #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 CommissionStatementQueryOptions(scope, sortColumn, sortDirection, pageSize, pageNumber, filters);
            var pagedItems   = await CommissionStatementService.GetCommissionStatements(queryOptions);

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

            var user1 = TestHelper.InsertUserDetailed(options);

            var company = TestHelper.InsertCompany(options);

            var cs1 = new CommissionStatementEntity
            {
                Id             = Guid.NewGuid(),
                CompanyId      = company.Id,
                Date           = new DateTime(2000, 1, 1),
                OrganisationId = user1.Organisation.Id
            };

            var cs2 = new CommissionStatementEntity
            {
                Id             = Guid.NewGuid(),
                CompanyId      = company.Id,
                Date           = new DateTime(2000, 1, 20),
                OrganisationId = user1.Organisation.Id
            };

            var cs3 = new CommissionStatementEntity
            {
                Id             = Guid.NewGuid(),
                CompanyId      = company.Id,
                Date           = new DateTime(2000, 1, 31),
                OrganisationId = user1.Organisation.Id
            };

            var cs4 = new CommissionStatementEntity
            {
                Id             = Guid.NewGuid(),
                CompanyId      = company.Id,
                Date           = new DateTime(2000, 2, 1),
                OrganisationId = user1.Organisation.Id
            };

            using (var context = new DataContext(options))
            {
                context.CommissionStatement.Add(cs1);
                context.CommissionStatement.Add(cs2);
                context.CommissionStatement.Add(cs3);
                context.CommissionStatement.Add(cs4);

                context.SaveChanges();
            }

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

                //When
                var scope        = TestHelper.GetScopeOptions(user1);
                var filters      = "startDate=1999-12-01;endDate=1999-12-31";
                var queryOptions = new CommissionStatementQueryOptions(scope, "", "", 0, 0, filters);
                var statements   = await service.GetCommissionStatements(queryOptions);

                Assert.Equal(0, statements.TotalItems);
                Assert.Empty(statements.Items);

                filters      = "startDate=2000-01-01;endDate=2000-01-21";
                queryOptions = new CommissionStatementQueryOptions(scope, "Date", "desc", 0, 0, filters);
                statements   = await service.GetCommissionStatements(queryOptions);

                //Then
                Assert.Equal(2, statements.TotalItems);

                var items = statements.Items.ToList();
                Assert.Equal(cs2.Id, items[0].Id);
                Assert.Equal(cs1.Id, items[1].Id);

                filters      = "startDate=2000-01-01;endDate=2000-01-31";
                queryOptions = new CommissionStatementQueryOptions(scope, "Date", "desc", 0, 0, filters);
                statements   = await service.GetCommissionStatements(queryOptions);

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

                items = statements.Items.ToList();
                Assert.Equal(cs3.Id, items[0].Id);
                Assert.Equal(cs2.Id, items[1].Id);
                Assert.Equal(cs1.Id, items[2].Id);
            }
        }
        public async Task GetCommissionStatements()
        {
            var options = TestHelper.GetDbContext("GetCommissionStatements");

            var user1 = TestHelper.InsertUserDetailed(options);
            var user2 = TestHelper.InsertUserDetailed(options);

            var company = TestHelper.InsertCompany(options);

            var cs1 = new CommissionStatementEntity
            {
                Id                 = Guid.NewGuid(),
                CompanyId          = company.Id,
                AmountIncludingVAT = 111,
                VAT                = 11,
                Date               = DateTime.Now,
                Processed          = true,
                OrganisationId     = user1.Organisation.Id,
                Notes              = "note 1"
            };

            var cs2 = new CommissionStatementEntity
            {
                Id                 = Guid.NewGuid(),
                CompanyId          = company.Id,
                AmountIncludingVAT = 222,
                VAT                = 22,
                Date               = DateTime.Now.AddDays(-1),
                Processed          = false,
                OrganisationId     = user1.Organisation.Id,
                Notes              = "note 2"
            };

            var cs3 = new CommissionStatementEntity
            {
                Id                 = Guid.NewGuid(),
                CompanyId          = company.Id,
                AmountIncludingVAT = 333,
                VAT                = 33,
                Date               = DateTime.Now.AddDays(-2),
                Processed          = false,
                OrganisationId     = user1.Organisation.Id,
                Notes              = "note 3"
            };

            var cs4 = new CommissionStatementEntity
            {
                Id                 = Guid.NewGuid(),
                CompanyId          = company.Id,
                AmountIncludingVAT = 444,
                VAT                = 44,
                Date               = DateTime.Now.AddDays(-2),
                Processed          = false,
                OrganisationId     = user2.Organisation.Id,
                Notes              = "note 4"
            };

            var commission1a = new CommissionEntity
            {
                Id                    = Guid.NewGuid(),
                PolicyId              = Guid.NewGuid(),
                CommissionTypeId      = Guid.NewGuid(),
                AmountIncludingVAT    = 50,
                VAT                   = 5,
                CommissionStatementId = cs1.Id,
                UserId                = user1.User.Id
            };

            var commission1b = new CommissionEntity
            {
                Id                    = Guid.NewGuid(),
                PolicyId              = Guid.NewGuid(),
                CommissionTypeId      = Guid.NewGuid(),
                AmountIncludingVAT    = 50,
                VAT                   = 5,
                CommissionStatementId = cs1.Id,
                UserId                = user1.User.Id,
            };

            var commission2 = new CommissionEntity
            {
                Id                    = Guid.NewGuid(),
                PolicyId              = Guid.NewGuid(),
                CommissionTypeId      = Guid.NewGuid(),
                AmountIncludingVAT    = 200,
                VAT                   = 20,
                CommissionStatementId = cs2.Id,
                UserId                = user1.User.Id,
            };

            var commission3 = new CommissionEntity
            {
                Id                    = Guid.NewGuid(),
                PolicyId              = Guid.NewGuid(),
                CommissionTypeId      = Guid.NewGuid(),
                AmountIncludingVAT    = 300,
                VAT                   = 30,
                CommissionStatementId = cs3.Id,
                UserId                = user1.User.Id,
            };

            var commission4 = new CommissionEntity
            {
                Id                    = Guid.NewGuid(),
                PolicyId              = Guid.NewGuid(),
                CommissionTypeId      = Guid.NewGuid(),
                AmountIncludingVAT    = 40,
                VAT                   = 400,
                CommissionStatementId = cs4.Id,
                UserId                = user2.User.Id,
            };

            using (var context = new DataContext(options))
            {
                context.CommissionStatement.Add(cs1);
                context.CommissionStatement.Add(cs2);
                context.CommissionStatement.Add(cs3);
                context.CommissionStatement.Add(cs4);

                context.Commission.Add(commission1a);
                context.Commission.Add(commission1b);
                context.Commission.Add(commission2);
                context.Commission.Add(commission3);
                context.Commission.Add(commission4);

                context.SaveChanges();
            }

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

                //When
                var scope        = TestHelper.GetScopeOptions(user1);
                var queryOptions = new CommissionStatementQueryOptions(scope, "", "", 0, 0);
                var statements   = await service.GetCommissionStatements(queryOptions);

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

                Assert.Equal(600, statements.SumAmountIncludingVAT);
                Assert.Equal(60, statements.SumVAT);

                var items  = statements.Items.ToList();
                var actual = items[0];
                Assert.Equal(cs1.Id, actual.Id);
                Assert.Equal(cs1.CompanyId, actual.CompanyId);
                Assert.Equal(cs1.Date, actual.Date);
                Assert.Equal(cs1.AmountIncludingVAT, actual.AmountIncludingVAT);
                Assert.Equal(cs1.VAT, actual.VAT);
                Assert.Equal(cs1.Processed, actual.Processed);
                Assert.Equal(cs1.Notes, actual.Notes);
                Assert.Equal(cs1.OrganisationId, user1.Organisation.Id);
                Assert.Equal(100, actual.ActualAmountIncludingVAT);
                Assert.Equal(10, actual.ActualVAT);
                Assert.Equal(2, actual.CommissionCount);

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

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

                //Check scope
                scope        = TestHelper.GetScopeOptions(user2);
                queryOptions = new CommissionStatementQueryOptions(scope, "", "", 0, 0);
                statements   = await service.GetCommissionStatements(queryOptions);

                Assert.Single(statements.Items);

                actual = statements.Items.First();
                Assert.Equal(cs4.Id, actual.Id);
            }
        }