Пример #1
0
        public async Task <IActionResult> GetUserCompanyMonthlyCommissionData(string sortColumn = "AmountExcludingVAT", string sortDirection = "desc", int pageSize = 0, int pageNumber = 0, string filters = null)
        {
            var scope = AuthenticationService.GetScope(User);

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

            var data = await CommissionReportService.GetUserCompanyMonthlyCommissionData(queryOptions);

            return(Ok(data));
        }
        public async Task GetUserCompanyMonthlyCommissionData()
        {
            var data = new UserCompanyMonthlyCommissionData()
            {
                AmountExcludingVAT = 100,
                CompanyId          = Guid.NewGuid(),
            };

            var items = new List <UserCompanyMonthlyCommissionData>()
            {
                data
            };

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

            UserCompanyMonthlyCommissionQueryOptions queryOptions = null;

            service.Setup(c => c.GetUserCompanyMonthlyCommissionData(It.IsAny <UserCompanyMonthlyCommissionQueryOptions>()))
            .Callback((UserCompanyMonthlyCommissionQueryOptions options) => queryOptions = options)
            .ReturnsAsync(items);

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

            var companyId = Guid.NewGuid();
            var result    = await controller.GetUserCompanyMonthlyCommissionData("AmountExcludingVAT", "desc", 15, 2, $"companyId=" + companyId.ToString());

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

            Assert.Equal(companyId, queryOptions.CompanyId.Single());

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

            Assert.Same(items, returnValue);
        }
Пример #3
0
        public async Task <IEnumerable <UserCompanyMonthlyCommissionData> > GetUserCompanyMonthlyCommissionData(UserCompanyMonthlyCommissionQueryOptions queryOptions)
        {
            var userQuery = ScopeQuery.GetUserEntityQuery(_context, queryOptions.Scope);

            var query = from commission in _context.Commission
                        join statement in _context.CommissionStatement
                        on commission.CommissionStatementId equals statement.Id
                        join policy in _context.Policy
                        on commission.PolicyId equals policy.Id
                        join user in userQuery
                        on commission.UserId equals user.Id
                        select new
            {
                UserId             = user.Id,
                BranchId           = user.BranchId,
                Date               = statement.Date,
                CompanyId          = policy.CompanyId,
                AmountIncludingVAT = commission.AmountIncludingVAT,
                VAT = commission.VAT
            };

            //Apply filters ----------------------------------------------------------------------------------------
            if (queryOptions.CompanyId.Any())
            {
                query = query.Where(d => queryOptions.CompanyId.Contains(d.CompanyId));
            }

            if (queryOptions.UserId.Any())
            {
                query = query.Where(d => queryOptions.UserId.Contains(d.UserId));
            }

            if (queryOptions.BranchId.Any())
            {
                query = query.Where(d => queryOptions.BranchId.Contains(d.BranchId));
            }

            if (queryOptions.StartDate.HasValue)
            {
                query = query.Where(d => queryOptions.StartDate.Value.Date <= d.Date);
            }

            if (queryOptions.EndDate.HasValue)
            {
                query = query.Where(d => queryOptions.EndDate.Value.Date >= d.Date);
            }
            //------------------------------------------------------------------------------------------------------

            var groupQuery = from data in query
                             group new { data.AmountIncludingVAT, data.VAT } by new { data.CompanyId } into g
                select new UserCompanyMonthlyCommissionData()
            {
                CompanyId          = g.Key.CompanyId,
                AmountExcludingVAT = g.Sum(c => (c.AmountIncludingVAT - c.VAT)),
            };

            groupQuery = groupQuery.OrderBy(queryOptions.SortOptions.Column, queryOptions.SortOptions.Direction);

            return(await groupQuery.ToListAsync());
        }
Пример #4
0
        public async Task GetUserCompanyMonthlyCommissionData_UserFilter()
        {
            var options = TestHelper.GetDbContext("GetUserCompanyMonthlyCommissionData_UserFilter");

            var companyId1 = Guid.NewGuid();
            var companyId2 = Guid.NewGuid();

            var user1   = TestHelper.InsertUserDetailed(options);
            var user2   = TestHelper.InsertUserDetailed(options, user1.Organisation);
            var client1 = TestHelper.InsertClient(options, user1.Organisation);
            var client2 = TestHelper.InsertClient(options, user1.Organisation);

            var statement1 = TestHelper.InsertCommissionStatement(options, user1.Organisation);
            var statement2 = TestHelper.InsertCommissionStatement(options, user1.Organisation);

            var user3      = TestHelper.InsertUserDetailed(options);
            var client3    = TestHelper.InsertClient(options, user3.Organisation);
            var statement3 = TestHelper.InsertCommissionStatement(options, user3.Organisation);

            var usr1_policy1 = new PolicyEntity
            {
                Id        = Guid.NewGuid(),
                Number    = Guid.NewGuid().ToString(),
                CompanyId = statement1.CompanyId,
                ClientId  = client1.Client.Id,
                UserId    = user1.User.Id
            };

            var usr2_policy1 = new PolicyEntity
            {
                Id        = Guid.NewGuid(),
                CompanyId = statement2.CompanyId,
                ClientId  = client2.Client.Id,
                UserId    = user2.User.Id
            };

            var usr3_policy1 = new PolicyEntity
            {
                Id        = Guid.NewGuid(),
                CompanyId = statement3.CompanyId,
                ClientId  = client3.Client.Id,
                UserId    = user3.User.Id
            };

            //Given
            var usr1_policy1_comm1 = new CommissionEntity
            {
                Id                    = Guid.NewGuid(),
                PolicyId              = usr1_policy1.Id,
                UserId                = user1.User.Id,
                CommissionTypeId      = Guid.NewGuid(),
                AmountIncludingVAT    = 110,
                VAT                   = 10,
                CommissionStatementId = statement1.Id
            };

            var usr2_policy1_comm1 = new CommissionEntity
            {
                Id                    = Guid.NewGuid(),
                PolicyId              = usr2_policy1.Id,
                UserId                = user2.User.Id,
                CommissionTypeId      = Guid.NewGuid(),
                AmountIncludingVAT    = 220,
                VAT                   = 20,
                CommissionStatementId = statement2.Id
            };

            var usr2_policy1_comm2 = new CommissionEntity
            {
                Id                    = Guid.NewGuid(),
                PolicyId              = usr2_policy1.Id,
                UserId                = user2.User.Id,
                CommissionTypeId      = Guid.NewGuid(),
                AmountIncludingVAT    = 330,
                VAT                   = 30,
                CommissionStatementId = statement2.Id
            };

            var usr2_policy1_comm3 = new CommissionEntity
            {
                Id                    = Guid.NewGuid(),
                PolicyId              = usr2_policy1.Id,
                UserId                = user2.User.Id,
                CommissionTypeId      = Guid.NewGuid(),
                AmountIncludingVAT    = 440,
                VAT                   = 40,
                CommissionStatementId = statement2.Id
            };

            var usr3_policy1_comm1 = new CommissionEntity
            {
                Id                    = Guid.NewGuid(),
                PolicyId              = usr3_policy1.Id,
                UserId                = user3.User.Id,
                CommissionTypeId      = Guid.NewGuid(),
                AmountIncludingVAT    = 550,
                VAT                   = 50,
                CommissionStatementId = statement3.Id
            };

            using (var context = new DataContext(options))
            {
                context.Policy.Add(usr1_policy1);
                context.Policy.Add(usr2_policy1);
                context.Policy.Add(usr3_policy1);

                context.Commission.Add(usr1_policy1_comm1);
                context.Commission.Add(usr2_policy1_comm1);
                context.Commission.Add(usr2_policy1_comm2);
                context.Commission.Add(usr2_policy1_comm3);
                context.Commission.Add(usr3_policy1_comm1);

                context.SaveChanges();
            }

            using (var context = new DataContext(options))
            {
                var service = new CommissionReportService(context);

                //When
                var scope        = TestHelper.GetScopeOptions(user1);
                var queryOptions = new UserCompanyMonthlyCommissionQueryOptions(scope, "", "", 0, 0);
                queryOptions.UserId.Add(user2.User.Id);
                var items = (await service.GetUserCompanyMonthlyCommissionData(queryOptions)).ToList();

                //Then
                Assert.Single(items);

                var actual = items[0];
                Assert.Equal(statement2.CompanyId, actual.CompanyId);
                Assert.Equal(900, actual.AmountExcludingVAT); //200 + 300 + 400
            }
        }