Ejemplo n.º 1
0
        public async Task <CSVFileData> HandleAsync(FetchInvoiceRunCsv message)
        {
            authorization.EnsureCanAccessInternalArea();

            var items = await context.StoredProcedures.SpgInvoiceRunChargeBreakdown(message.InvoiceRunId);

            var csvWriter = csvWriterFactory.Create <PCSChargesCSVData>();

            csvWriter.DefineColumn(@"Scheme name", i => i.SchemeName);
            csvWriter.DefineColumn(@"Compliance year", i => i.ComplianceYear);
            csvWriter.DefineColumn(@"Submission date and time (GMT)", i => i.SubmissionDate.ToString("dd/MM/yyyy HH:mm:ss"));
            csvWriter.DefineColumn(@"Producer name", i => i.ProducerName);
            csvWriter.DefineColumn(@"PRN", i => i.PRN);
            csvWriter.DefineColumn(@"Charge value (GBP)", i => i.ChargeValue);
            csvWriter.DefineColumn(@"Charge band", i => i.ChargeBandType);
            csvWriter.DefineColumn(@"Reg. Off. or PPoB country", i => i.RegOfficeOrPBoBCountry);
            csvWriter.DefineColumn(@"Includes annual charge", i => i.HasAnnualCharge);

            string fileContent = csvWriter.Write(items);

            var invoiceRun = await commonDataAccess.FetchInvoiceRunAsync(message.InvoiceRunId);

            string fileName = string.Format("invoicerun_{0}_{1}.csv", invoiceRun.CompetentAuthority.Abbreviation, invoiceRun.IssuedDate.ToString("ddMMyyyy"));

            return(new CSVFileData
            {
                FileContent = fileContent,
                FileName = fileName
            });
        }
Ejemplo n.º 2
0
        public async Task HandleAysnc_HappyPath_ReturnsFileInfoWithCorrectFileName()
        {
            // Arrange
            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();

            UKCompetentAuthority authority = new UKCompetentAuthority(
                A.Dummy <Guid>(),
                "Environment Agency",
                "EA",
                A.Dummy <Country>(),
                "*****@*****.**",
                0);

            Scheme scheme = A.Dummy <Scheme>();

            A.CallTo(() => scheme.CompetentAuthority).Returns(authority);

            MemberUpload memberUpload = new MemberUpload(
                A.Dummy <Guid>(),
                scheme,
                A.Dummy <string>(),
                A.Dummy <string>());

            memberUpload.Submit(A.Dummy <User>());

            List <MemberUpload> memberUploads = new List <MemberUpload>();

            memberUploads.Add(memberUpload);

            SystemTime.Freeze(new DateTime(2015, 12, 31));
            InvoiceRun invoiceRun = new InvoiceRun(authority, memberUploads, A.Dummy <User>());

            SystemTime.Unfreeze();

            ulong fileID = 123;

            IbisFileData ibisFileData = new IbisFileData(fileID, "Customer File.dat", "data", "Transaction File.dat", "data");

            invoiceRun.SetIbisFileData(ibisFileData);

            ICommonDataAccess dataAccess = A.Fake <ICommonDataAccess>();

            A.CallTo(() => dataAccess.FetchInvoiceRunAsync(A <Guid> ._)).Returns(invoiceRun);

            FetchInvoiceRunIbisZipFileHandler handler = new FetchInvoiceRunIbisZipFileHandler(
                authorization,
                dataAccess);

            // Act
            FileInfo fileInfo = await handler.HandleAsync(A.Dummy <Requests.Charges.FetchInvoiceRunIbisZipFile>());

            // Assert
            Assert.NotNull(fileInfo);
            Assert.Equal("WEEE invoice files 00123 2015-12-31.zip", fileInfo.FileName);
        }
Ejemplo n.º 3
0
        public async Task HandleAsync_WithInvoiceRunWithout1B1SFileData_ThrowsInvalidOperationException()
        {
            // Arrange
            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();

            UKCompetentAuthority authority = A.Dummy <UKCompetentAuthority>();

            Scheme scheme = A.Dummy <Scheme>();

            A.CallTo(() => scheme.CompetentAuthority).Returns(authority);

            MemberUpload memberUpload = new MemberUpload(
                A.Dummy <Guid>(),
                scheme,
                A.Dummy <string>(),
                A.Dummy <string>());

            memberUpload.Submit(A.Dummy <User>());

            List <MemberUpload> memberUploads = new List <MemberUpload>();

            memberUploads.Add(memberUpload);

            InvoiceRun invoiceRun = new InvoiceRun(authority, memberUploads, A.Dummy <User>());

            ICommonDataAccess dataAccess = A.Fake <ICommonDataAccess>();

            A.CallTo(() => dataAccess.FetchInvoiceRunAsync(A <Guid> ._)).Returns(invoiceRun);

            FetchInvoiceRunIbisZipFileHandler handler = new FetchInvoiceRunIbisZipFileHandler(
                authorization,
                dataAccess);

            // Act
            Func <Task <FileInfo> > testCode = async() => await handler.HandleAsync(A.Dummy <Requests.Charges.FetchInvoiceRunIbisZipFile>());

            // Assert
            await Assert.ThrowsAsync <InvalidOperationException>(testCode);
        }
        public async Task <FileInfo> HandleAsync(Requests.Charges.FetchInvoiceRunIbisZipFile message)
        {
            authorization.EnsureCanAccessInternalArea(true);

            InvoiceRun invoiceRun = await dataAccess.FetchInvoiceRunAsync(message.InvoiceRunId);

            if (invoiceRun.IbisFileData == null)
            {
                string errorMessage = string.Format(
                    "The invoice run 1B1S zip file cannot be fetched for the invoice run " +
                    "with ID \"{0}\" as it does not have 1B1S file data.",
                    message.InvoiceRunId);

                throw new InvalidOperationException(errorMessage);
            }

            string fileName = string.Format(
                "WEEE invoice files {0:D5} {1:yyyy-MM-dd}.zip",
                invoiceRun.IbisFileData.FileId,
                invoiceRun.IssuedDate);

            byte[] data = null;
            using (var stream = new System.IO.MemoryStream())
            {
                using (ZipFile zip = new ZipFile())
                {
                    zip.AddEntry(invoiceRun.IbisFileData.CustomerFileName, invoiceRun.IbisFileData.CustomerFileData);
                    zip.AddEntry(invoiceRun.IbisFileData.TransactionFileName, invoiceRun.IbisFileData.TransactionFileData);
                    zip.Save(stream);

                    data = stream.ToArray();
                }
            }

            return(new FileInfo(fileName, data));
        }