示例#1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest request,
            ILogger log)
        {
            Guid organisationId                = Guid.Parse(request.Query["organisationId"]);
            Guid commissionStatementId         = Guid.Parse(request.Query["commissionStatementId"]);
            Guid commissionStatementTemplateId = Guid.Parse(request.Query["commissionStatementTemplateId"]);

            var scope     = new ScopeOptions(organisationId, Guid.Empty, Guid.Empty, Scope.Organisation);
            var statement = await CommissionStatementService.GetCommissionStatement(scope, commissionStatementId);

            if (statement == null)
            {
                return(new NotFoundObjectResult(commissionStatementId));
            }

            var path  = new CommissionStatementPath(scope.OrganisationId, commissionStatementId);
            var files = await FileStorageService.GetFilesAsync(path);

            if (!files.Any())
            {
                return(Utils.GetBadRequestObject("Reimport failed as there are no existing statement files.", commissionStatementId.ToString()));
            }

            var queryOptions = new CommissionStatementTemplateQueryOptions("", "", 0, 0);

            queryOptions.CompanyId.Add(statement.CompanyId.Value);
            queryOptions.Date = statement.Date;

            var templates = (await CommissionStatementTemplateService.GetTemplates(queryOptions)).Items;

            if (!templates.Any(t => t.Id == commissionStatementTemplateId))
            {
                return(Utils.GetBadRequestObject("Reimport failed as the commissionStatementTemplateId is not valid.", commissionStatementTemplateId.ToString()));
            }

            var template = await CommissionStatementTemplateService.GetTemplate(commissionStatementTemplateId);

            await CommissionStatementService.DeleteCommissions(scope, commissionStatementId);

            var result = new ImportResult();

            foreach (var fileInfo in files)
            {
                using (var stream = new MemoryStream())
                {
                    await FileStorageService.GetFile(fileInfo.Url, stream);

                    var vatRate = await DirectoryLookupService.GetVATRate(statement.Date ?? DateTime.Now);

                    var reader = new CommissionImportReader(template.Config, vatRate);
                    var items  = reader.Read(stream);

                    result = await CommissionImportService.ImportCommissions(scope, commissionStatementId, items);
                }
            }

            return(new OkObjectResult(result));
        }
示例#2
0
        public async Task <IActionResult> Get(Guid commissionStatementId)
        {
            var scope = AuthenticationService.GetScope(User);

            var model = await CommissionStatementService.GetCommissionStatement(scope, commissionStatementId);

            if (model == null)
            {
                return(NotFound());
            }

            return(Ok(model));
        }
        private async Task SendZeroEntriesEmail(
            ScopeOptions scope,
            Guid commissionStatementId,
            CommissionStatementTemplateEdit template,
            Attachment attachment)
        {
            var organisation = await OrganisationService.GetOrganisation(scope, scope.OrganisationId);

            var user = await UserService.GetUser(scope, scope.UserId);

            var statement = await CommissionStatementService.GetCommissionStatement(scope, commissionStatementId);

            var company = await DirectoryLookupService.GetCompany(statement.CompanyId.Value);

            await EmailService.SendImportCommissionZeroEntriesEmail(Utils.GetEnvironment(), organisation, user, company, statement, template, attachment);
        }
        public async Task <IActionResult> Import(Guid commissionStatementId, [FromQuery] Guid commissionStatementTemplateId, [FromQuery] Guid?userId)
        {
            var scope = AuthenticationService.GetScope(User);

            var file = Request.Form.Files.FirstOrDefault();

            if (file == null)
            {
                return(BadRequest());
            }

            var statement = await CommissionStatementService.GetCommissionStatement(scope, commissionStatementId);

            var template = await CommissionStatementTemplateService.GetTemplate(commissionStatementTemplateId);

            string brokerFullName = null;

            if (template.BrokerSpecific)
            {
                if (!userId.HasValue)
                {
                    return(this.BadRequestMessage("UserId required for broker specific templates."));
                }

                var user = await UserService.GetUser(scope, userId.Value);

                if (user == null)
                {
                    return(this.BadRequestMessage("Invalid UserId."));
                }

                brokerFullName = $"{user.FirstName} {user.LastName}";
            }

            var config = template.Config;

            var result = new ImportResult();

            using (var stream = file.OpenReadStream())
            {
                var vatRate = await DirectoryLookupService.GetVATRate(statement.Date ?? DateTime.Now);

                var reader = new CommissionImportReader(config, vatRate, brokerFullName);
                var items  = reader.Read(stream);

                result = await CommissionImportService.ImportCommissions(scope, commissionStatementId, items);
            }

            if (result.UnknownCommissionTypeValues.Any())
            {
                using (var stream = file.OpenReadStream())
                {
                    var attachment = GetEmailAttachment(file, stream);
                    await SendUnkownCommissionTypesEmail(result, scope, commissionStatementId, template, attachment);
                }
            }

            if (!result.Results.Any(r => r.Success))
            {
                using (var stream = file.OpenReadStream())
                {
                    var attachment = GetEmailAttachment(file, stream);
                    await SendZeroEntriesEmail(scope, commissionStatementId, template, attachment);
                }
            }

            if (result.Results.Any())
            {
                using (var stream = file.OpenReadStream())
                {
                    var path = new CommissionStatementFilePath(scope.OrganisationId, commissionStatementId, file.FileName);
                    await FileStorageService.AddFileAsync(path, stream);
                }
            }

            return(Ok(result));
        }
        public async Task <IActionResult> Reimport(Guid commissionStatementId, [FromQuery] Guid commissionStatementTemplateId, [FromQuery] Guid?userId)
        {
            var scope = AuthenticationService.GetScope(User);

            var statement = await CommissionStatementService.GetCommissionStatement(scope, commissionStatementId);

            if (statement == null)
            {
                return(NotFound());
            }

            var path         = new CommissionStatementDirectoryPath(scope.OrganisationId, commissionStatementId);
            var fileInfoList = await FileStorageService.GetFileInfoListAsync(path);

            if (!fileInfoList.Any())
            {
                return(this.BadRequestMessage("Reimport failed as there are no existing statement files."));
            }

            var queryOptions = new CommissionStatementTemplateQueryOptions("", "", 0, 0);

            queryOptions.CompanyId.Add(statement.CompanyId.Value);
            queryOptions.Date = statement.Date;

            var templates = (await CommissionStatementTemplateService.GetTemplates(queryOptions)).Items;

            if (!templates.Any(t => t.Id == commissionStatementTemplateId))
            {
                return(this.BadRequestMessage("Reimport failed as the commissionStatementTemplateId is not valid."));
            }

            var template = await CommissionStatementTemplateService.GetTemplate(commissionStatementTemplateId);

            string brokerFullName = null;

            if (template.BrokerSpecific)
            {
                if (!userId.HasValue)
                {
                    return(this.BadRequestMessage("UserId required for broker specific templates."));
                }

                var user = await UserService.GetUser(scope, userId.Value);

                if (user == null)
                {
                    return(this.BadRequestMessage("Invalid UserId."));
                }

                brokerFullName = $"{user.FirstName} {user.LastName}";
            }

            await CommissionStatementService.DeleteCommissions(scope, commissionStatementId);

            var result = new ImportResult();

            foreach (var fileInfo in fileInfoList)
            {
                using (var stream = new MemoryStream())
                {
                    await FileStorageService.GetFile(fileInfo.Url, stream);

                    var vatRate = await DirectoryLookupService.GetVATRate(statement.Date ?? DateTime.Now);

                    var reader = new CommissionImportReader(template.Config, vatRate, brokerFullName);
                    var items  = reader.Read(stream);

                    result = await CommissionImportService.ImportCommissions(scope, commissionStatementId, items);

                    if (result.UnknownCommissionTypeValues.Any())
                    {
                        stream.Position = 0;
                        using (var copy = new MemoryStream())
                        {
                            await stream.CopyToAsync(copy);

                            copy.Position = 0;

                            var attachment = GetEmailAttachmentFromCloud(fileInfo, copy);
                            await SendUnkownCommissionTypesEmail(result, scope, commissionStatementId, template, attachment);
                        }
                    }

                    if (!result.Results.Any(r => r.Success))
                    {
                        stream.Position = 0;
                        using (var copy = new MemoryStream())
                        {
                            await stream.CopyToAsync(copy);

                            copy.Position = 0;

                            var attachment = GetEmailAttachmentFromCloud(fileInfo, copy);
                            await SendZeroEntriesEmail(scope, commissionStatementId, template, attachment);
                        }
                    }
                }
            }

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

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

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

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

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

                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 actual = await service.GetCommissionStatement(scope, cs2.Id);

                //Then
                Assert.Equal(cs2.Id, actual.Id);
                Assert.Equal(cs2.CompanyId, actual.CompanyId);
                Assert.Equal(cs2.Date, actual.Date);
                Assert.Equal(cs2.AmountIncludingVAT, actual.AmountIncludingVAT);
                Assert.Equal(cs2.VAT, actual.VAT);
                Assert.Equal(cs2.OrganisationId, user1.Organisation.Id);
                Assert.Equal(cs2.Processed, actual.Processed);
                Assert.Equal(cs2.Notes, actual.Notes);

                //Check scope
                scope  = TestHelper.GetScopeOptions(user2);
                actual = await service.GetCommissionStatement(scope, cs2.Id);

                Assert.Null(actual);
            }
        }