public async Task FetchResultsAsync_WithUnsubmittedReturnData_ReturnsEmptyList()
        {
            using (Weee.Tests.Core.Model.DatabaseWrapper wrapper = new Weee.Tests.Core.Model.DatabaseWrapper())
            {
                Organisation organisation = Organisation.CreateSoleTrader("Test Organisation");
                Scheme scheme = new Scheme(organisation);

                Quarter quarter = new Quarter(2099, QuarterType.Q1);
                DataReturn dataReturn = new DataReturn(scheme, quarter);

                DataReturnVersion version = new DataReturnVersion(dataReturn);

                WeeeCollectedAmount amount1 = new WeeeCollectedAmount(
                    WeeeCollectedAmountSourceType.Dcf,
                    ObligationType.B2C,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m);
                version.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(amount1);

                wrapper.WeeeContext.DataReturns.Add(dataReturn);
                await wrapper.WeeeContext.SaveChangesAsync();

                // Act
                FetchSummaryCsvDataAccess dataAccess = new FetchSummaryCsvDataAccess(wrapper.WeeeContext);

                List<DataReturnSummaryCsvData> results = await dataAccess.FetchResultsAsync(scheme.Id, 2099);

                // Assert
                Assert.NotNull(results);
                Assert.Empty(results);
            }
        }
        private void AddReturnItemCollectedFromDcf(WeeeCollectedAmount weeeCollectedAmount)
        {
            Guard.ArgumentNotNull(() => weeeCollectedAmount, weeeCollectedAmount);

            if (WeeeCollectedAmounts
                .Where(r => r.SourceType == WeeeCollectedAmountSourceType.Dcf)
                .Where(r => r.WeeeCategory == weeeCollectedAmount.WeeeCategory)
                .Where(r => (r.ObligationType & weeeCollectedAmount.ObligationType) != ObligationType.None)
                .Any())
            {
                string errorMessage = "A return item with this obligation type and category has already been added.";
                throw new InvalidOperationException(errorMessage);
            }

            WeeeCollectedAmounts.Add(weeeCollectedAmount);
        }
 public void AddWeeeCollectedAmount(WeeeCollectedAmount weeeCollectedAmount)
 {
     Guard.ArgumentNotNull(() => weeeCollectedAmount, weeeCollectedAmount);
     switch (weeeCollectedAmount.SourceType)
     {
         case WeeeCollectedAmountSourceType.Dcf:
             AddReturnItemCollectedFromDcf(weeeCollectedAmount);
             break;
         case WeeeCollectedAmountSourceType.Distributor:
             AddB2cWeeeFromDistributor(weeeCollectedAmount);
             break;
         case WeeeCollectedAmountSourceType.FinalHolder:
             AddB2cWeeeFromFinalHolder(weeeCollectedAmount);
             break;
     }
 }
        private void AddB2cWeeeFromFinalHolder(WeeeCollectedAmount weeeCollectedAmount)
        {
            Guard.ArgumentNotNull(() => weeeCollectedAmount, weeeCollectedAmount);

            if (weeeCollectedAmount.ObligationType != ObligationType.B2C)
            {
                string errorMessage = "Only return items with an obligation type of B2C can be added under \"B2C WEEE from final holders\".";
                throw new InvalidOperationException(errorMessage);
            }

            if (WeeeCollectedAmounts
                .Where(r => r.SourceType == WeeeCollectedAmountSourceType.FinalHolder)
                .Where(r => r.WeeeCategory == weeeCollectedAmount.WeeeCategory)
                .Where(r => (r.ObligationType & weeeCollectedAmount.ObligationType) != ObligationType.None)
                .Any())
            {
                string errorMessage = "A return item with this obligation type and category has already been added.";
                throw new InvalidOperationException(errorMessage);
            }

            WeeeCollectedAmounts.Add(weeeCollectedAmount);
        }
        public async Task Build_ExistingLatestSubmittedDataReturnVersion_WithSomeExistingWeeeCollectedAmounts_ReturnsDataReturnVersionWithExistingWeeeCollectedAmounts()
        {
            var weeeCollectedAmount1 = new WeeeCollectedAmount(WeeeCollectedAmountSourceType.Dcf, ObligationType.B2C, WeeeCategory.DisplayEquipment, 100);
            var weeeCollectedAmount2 = new WeeeCollectedAmount(WeeeCollectedAmountSourceType.Dcf, ObligationType.B2C, WeeeCategory.ConsumerEquipment, 100);
            var weeeCollectedAmount3 = new WeeeCollectedAmount(WeeeCollectedAmountSourceType.Distributor, ObligationType.B2C, WeeeCategory.ConsumerEquipment, 100);
            var weeeCollectedAmount4 = new WeeeCollectedAmount(WeeeCollectedAmountSourceType.FinalHolder, ObligationType.B2C, WeeeCategory.ConsumerEquipment, 100);

            var weeeCollectedReturnVersion = new WeeeCollectedReturnVersion();
            weeeCollectedReturnVersion.AddWeeeCollectedAmount(weeeCollectedAmount1);
            weeeCollectedReturnVersion.AddWeeeCollectedAmount(weeeCollectedAmount2);
            weeeCollectedReturnVersion.AddWeeeCollectedAmount(weeeCollectedAmount3);
            weeeCollectedReturnVersion.AddWeeeCollectedAmount(weeeCollectedAmount4);

            var dataReturn = new DataReturn(A.Dummy<Scheme>(), A.Dummy<Quarter>());
            var dataReturnVersion = new DataReturnVersion(dataReturn, weeeCollectedReturnVersion,
                A.Dummy<WeeeDeliveredReturnVersion>(), A.Dummy<EeeOutputReturnVersion>());
            dataReturn.SetCurrentVersion(dataReturnVersion);

            var helper = new DataReturnVersionBuilderHelper();

            A.CallTo(() => helper.DataAccess.FetchDataReturnOrDefault())
               .Returns(dataReturn);

            var builder = helper.Create();
            await builder.AddWeeeCollectedAmount(WeeeCollectedAmountSourceType.Dcf, WeeeCategory.ConsumerEquipment, ObligationType.B2C, 100);
            await builder.AddWeeeCollectedAmount(WeeeCollectedAmountSourceType.Distributor, WeeeCategory.ConsumerEquipment, ObligationType.B2C, 100);

            var result = await builder.Build();

            Assert.NotSame(weeeCollectedReturnVersion, result.DataReturnVersion.WeeeCollectedReturnVersion);
            Assert.Collection(result.DataReturnVersion.WeeeCollectedReturnVersion.WeeeCollectedAmounts,
                r1 => Assert.Same(weeeCollectedAmount2, r1),
                r2 => Assert.Same(weeeCollectedAmount3, r2));
        }
        public async Task FetchResultsAsync_WithReturnDataForTowQuarters_ReturnsOneResultForEachQuarter()
        {
            using (Weee.Tests.Core.Model.DatabaseWrapper wrapper = new Weee.Tests.Core.Model.DatabaseWrapper())
            {
                // Arrange
                Organisation organisation = Organisation.CreateSoleTrader("Test Organisation");
                Scheme scheme = new Scheme(organisation);

                Quarter quarter1 = new Quarter(2099, QuarterType.Q1);
                DataReturn dataReturnQ1 = new DataReturn(scheme, quarter1);

                DataReturnVersion versionQ1 = new DataReturnVersion(dataReturnQ1);

                WeeeCollectedAmount amountQ1 = new WeeeCollectedAmount(
                    WeeeCollectedAmountSourceType.Dcf,
                    ObligationType.B2C,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m);
                versionQ1.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(amountQ1);

                wrapper.WeeeContext.DataReturns.Add(dataReturnQ1);
                await wrapper.WeeeContext.SaveChangesAsync();

                dataReturnQ1.SetCurrentVersion(versionQ1);
                await wrapper.WeeeContext.SaveChangesAsync();

                Quarter quarter2 = new Quarter(2099, QuarterType.Q2);
                DataReturn dataReturnQ2 = new DataReturn(scheme, quarter2);

                DataReturnVersion versionQ2 = new DataReturnVersion(dataReturnQ2);

                WeeeCollectedAmount amountQ2 = new WeeeCollectedAmount(
                    WeeeCollectedAmountSourceType.Dcf,
                    ObligationType.B2C,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m);
                versionQ2.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(amountQ2);

                wrapper.WeeeContext.DataReturns.Add(dataReturnQ2);
                await wrapper.WeeeContext.SaveChangesAsync();

                dataReturnQ2.SetCurrentVersion(versionQ2);
                await wrapper.WeeeContext.SaveChangesAsync();

                // Act
                FetchSummaryCsvDataAccess dataAccess = new FetchSummaryCsvDataAccess(wrapper.WeeeContext);

                List<DataReturnSummaryCsvData> results = await dataAccess.FetchResultsAsync(scheme.Id, 2099);

                // Assert
                Assert.NotNull(results);
                Assert.Equal(2, results.Count);

                Assert.Collection(results,
                    r => Assert.Equal(1, r.Quarter),
                    r => Assert.Equal(2, r.Quarter));
            }
        }
        public async Task FetchResultsAsync_WithOneAmountOfEachTypeAndSourceAndObligationType_ReturnsResultForEachAmountWithCorrectOrdering()
        {
            using (Weee.Tests.Core.Model.DatabaseWrapper wrapper = new Weee.Tests.Core.Model.DatabaseWrapper())
            {
                // Arrange
                Organisation organisation = Organisation.CreateSoleTrader("Test Organisation");
                Scheme scheme = new Scheme(organisation);

                RegisteredProducer registeredProducer1 = new RegisteredProducer("WEE/AA9365AA", 2099, scheme);

                Quarter quarter = new Quarter(2099, QuarterType.Q1);
                DataReturn dataReturn = new DataReturn(scheme, quarter);

                DataReturnVersion version = new DataReturnVersion(dataReturn);

                WeeeCollectedAmount amountCollectedDcfB2C = new WeeeCollectedAmount(
                    WeeeCollectedAmountSourceType.Dcf,
                    ObligationType.B2C,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m);
                version.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(amountCollectedDcfB2C);

                WeeeCollectedAmount amountCollectedDistributor = new WeeeCollectedAmount(
                    WeeeCollectedAmountSourceType.Distributor,
                    ObligationType.B2C,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m);
                version.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(amountCollectedDistributor);

                WeeeCollectedAmount amountCollectedFinalHolder = new WeeeCollectedAmount(
                    WeeeCollectedAmountSourceType.FinalHolder,
                    ObligationType.B2C,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m);
                version.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(amountCollectedFinalHolder);

                WeeeDeliveredAmount amountDeliveredAatfB2C = new WeeeDeliveredAmount(
                    ObligationType.B2C,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m,
                    new AatfDeliveryLocation("WEE/AA6845AA/ATF", string.Empty));
                version.WeeeDeliveredReturnVersion.AddWeeeDeliveredAmount(amountDeliveredAatfB2C);

                WeeeDeliveredAmount amountDeliveredAeB2C = new WeeeDeliveredAmount(
                    ObligationType.B2C,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m,
                    new AeDeliveryLocation("WEE/AA2658AA/AE", string.Empty));
                version.WeeeDeliveredReturnVersion.AddWeeeDeliveredAmount(amountDeliveredAeB2C);

                EeeOutputAmount amountOutputB2C = new EeeOutputAmount(
                    ObligationType.B2C,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m,
                    registeredProducer1);
                version.EeeOutputReturnVersion.AddEeeOutputAmount(amountOutputB2C);

                WeeeCollectedAmount amountCollectedDcfB2B = new WeeeCollectedAmount(
                    WeeeCollectedAmountSourceType.Dcf,
                    ObligationType.B2B,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m);
                version.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(amountCollectedDcfB2B);

                WeeeDeliveredAmount amountDeliveredAatfB2B = new WeeeDeliveredAmount(
                    ObligationType.B2B,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m,
                    new AatfDeliveryLocation("WEE/AA7445AA/ATF", string.Empty));
                version.WeeeDeliveredReturnVersion.AddWeeeDeliveredAmount(amountDeliveredAatfB2B);

                WeeeDeliveredAmount amountDeliveredAeB2B = new WeeeDeliveredAmount(
                    ObligationType.B2B,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m,
                    new AeDeliveryLocation("WEE/AA3658AA/AE", string.Empty));
                version.WeeeDeliveredReturnVersion.AddWeeeDeliveredAmount(amountDeliveredAeB2B);

                EeeOutputAmount amountOutputB2B = new EeeOutputAmount(
                    ObligationType.B2B,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m,
                    registeredProducer1);
                version.EeeOutputReturnVersion.AddEeeOutputAmount(amountOutputB2B);

                wrapper.WeeeContext.DataReturns.Add(dataReturn);
                await wrapper.WeeeContext.SaveChangesAsync();

                dataReturn.SetCurrentVersion(version);
                await wrapper.WeeeContext.SaveChangesAsync();

                // Act
                FetchSummaryCsvDataAccess dataAccess = new FetchSummaryCsvDataAccess(wrapper.WeeeContext);

                List<DataReturnSummaryCsvData> results = await dataAccess.FetchResultsAsync(scheme.Id, 2099);

                // Assert
                Assert.NotNull(results);
                Assert.Equal(10, results.Count);
                Assert.Collection(results,
                    r => { Assert.Equal(0, r.Type); Assert.Equal(0, r.Source); Assert.Equal("B2C", r.ObligationType); },
                    r => { Assert.Equal(0, r.Type); Assert.Equal(0, r.Source); Assert.Equal("B2B", r.ObligationType); },
                    r => { Assert.Equal(0, r.Type); Assert.Equal(1, r.Source); Assert.Equal("B2C", r.ObligationType); },
                    r => { Assert.Equal(0, r.Type); Assert.Equal(2, r.Source); Assert.Equal("B2C", r.ObligationType); },
                    r => { Assert.Equal(1, r.Type); Assert.Equal(0, r.Source); Assert.Equal("B2C", r.ObligationType); },
                    r => { Assert.Equal(1, r.Type); Assert.Equal(0, r.Source); Assert.Equal("B2B", r.ObligationType); },
                    r => { Assert.Equal(1, r.Type); Assert.Equal(1, r.Source); Assert.Equal("B2C", r.ObligationType); },
                    r => { Assert.Equal(1, r.Type); Assert.Equal(1, r.Source); Assert.Equal("B2B", r.ObligationType); },
                    r => { Assert.Equal(2, r.Type); Assert.Equal(null, r.Source); Assert.Equal("B2C", r.ObligationType); },
                    r => { Assert.Equal(2, r.Type); Assert.Equal(null, r.Source); Assert.Equal("B2B", r.ObligationType); });
            }
        }
        public async Task FetchResultsAsync_WithLargeHouseHoldAppliancesAmount_ReturnsNullForAllOtherCategories()
        {
            using (Weee.Tests.Core.Model.DatabaseWrapper wrapper = new Weee.Tests.Core.Model.DatabaseWrapper())
            {
                // Arrange
                Organisation organisation = Organisation.CreateSoleTrader("Test Organisation");
                Scheme scheme = new Scheme(organisation);

                Quarter quarter = new Quarter(2099, QuarterType.Q1);
                DataReturn dataReturn = new DataReturn(scheme, quarter);

                DataReturnVersion version = new DataReturnVersion(dataReturn);

                WeeeCollectedAmount amount1 = new WeeeCollectedAmount(
                    WeeeCollectedAmountSourceType.Dcf,
                    ObligationType.B2C,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m);
                version.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(amount1);

                wrapper.WeeeContext.DataReturns.Add(dataReturn);
                await wrapper.WeeeContext.SaveChangesAsync();

                dataReturn.SetCurrentVersion(version);
                await wrapper.WeeeContext.SaveChangesAsync();

                // Act
                FetchSummaryCsvDataAccess dataAccess = new FetchSummaryCsvDataAccess(wrapper.WeeeContext);

                List<DataReturnSummaryCsvData> results = await dataAccess.FetchResultsAsync(scheme.Id, 2099);

                // Assert
                Assert.NotNull(results);
                Assert.Equal(1, results.Count);

                DataReturnSummaryCsvData result = results[0];
                Assert.NotNull(result);

                Assert.NotNull(result.Category1);
                Assert.Null(result.Category2);
                Assert.Null(result.Category3);
                Assert.Null(result.Category4);
                Assert.Null(result.Category5);
                Assert.Null(result.Category6);
                Assert.Null(result.Category7);
                Assert.Null(result.Category8);
                Assert.Null(result.Category9);
                Assert.Null(result.Category10);
                Assert.Null(result.Category11);
                Assert.Null(result.Category12);
                Assert.Null(result.Category13);
                Assert.Null(result.Category14);
            }
        }
        public async Task FetchResultsAsync_WithQ1ReturnDataWithCollectedFromFinalHolderB2CAmount_ReturnsOneResultForQ1Type0Source2B2C()
        {
            using (Weee.Tests.Core.Model.DatabaseWrapper wrapper = new Weee.Tests.Core.Model.DatabaseWrapper())
            {
                // Arrange
                Organisation organisation = Organisation.CreateSoleTrader("Test Organisation");
                Scheme scheme = new Scheme(organisation);

                Quarter quarter = new Quarter(2099, QuarterType.Q1);
                DataReturn dataReturn = new DataReturn(scheme, quarter);

                DataReturnVersion version = new DataReturnVersion(dataReturn);

                WeeeCollectedAmount amount1 = new WeeeCollectedAmount(
                    WeeeCollectedAmountSourceType.FinalHolder,
                    ObligationType.B2C,
                    WeeeCategory.LargeHouseholdAppliances,
                    123.456m);
                version.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(amount1);

                wrapper.WeeeContext.DataReturns.Add(dataReturn);
                await wrapper.WeeeContext.SaveChangesAsync();

                dataReturn.SetCurrentVersion(version);
                await wrapper.WeeeContext.SaveChangesAsync();

                // Act
                FetchSummaryCsvDataAccess dataAccess = new FetchSummaryCsvDataAccess(wrapper.WeeeContext);

                List<DataReturnSummaryCsvData> results = await dataAccess.FetchResultsAsync(scheme.Id, 2099);

                // Assert
                Assert.NotNull(results);
                Assert.Equal(1, results.Count);

                DataReturnSummaryCsvData result = results[0];
                Assert.NotNull(result);

                Assert.Equal(1, result.Quarter);
                Assert.Equal(0, result.Type);
                Assert.Equal(2, result.Source);
                Assert.Equal("B2C", result.ObligationType);
            }
        }
        public void CreateResults_WithSeveralDcfCollectedAmountsInDifferentQuarters_PopulatesDcfTotalWithSum()
        {
            // Arrange
            DataReturn dataReturn1 = new DataReturn(A.Dummy<Domain.Scheme.Scheme>(), new Quarter(2016, QuarterType.Q2));

            DataReturnVersion dataReturnVersion1 = new DataReturnVersion(dataReturn1);

            WeeeCollectedAmount weeeCollectedAmount1 = new WeeeCollectedAmount(
                WeeeCollectedAmountSourceType.Dcf,
                DomainObligationType.B2C,
                WeeeCategory.LargeHouseholdAppliances,
                100);

            dataReturnVersion1.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(weeeCollectedAmount1);

            dataReturn1.SetCurrentVersion(dataReturnVersion1);

            DataReturn dataReturn2 = new DataReturn(A.Dummy<Domain.Scheme.Scheme>(), new Quarter(2016, QuarterType.Q4));

            DataReturnVersion dataReturnVersion2 = new DataReturnVersion(dataReturn2);

            WeeeCollectedAmount weeeCollectedAmount2 = new WeeeCollectedAmount(
                WeeeCollectedAmountSourceType.Dcf,
                DomainObligationType.B2C,
                WeeeCategory.LargeHouseholdAppliances,
                200);

            dataReturnVersion2.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(weeeCollectedAmount2);

            dataReturn2.SetCurrentVersion(dataReturnVersion2);

            GetUkWeeeCsvHandler handler = new GetUkWeeeCsvHandler(
                A.Dummy<IWeeeAuthorization>(),
                A.Dummy<IGetUkWeeeCsvDataAccess>(),
                A.Dummy<CsvWriterFactory>());

            // Act
            IEnumerable<GetUkWeeeCsvHandler.CsvResult> results = handler.CreateResults(new List<DataReturn>() { dataReturn1, dataReturn2 });

            // Assert
            GetUkWeeeCsvHandler.CsvResult result = results
                .Single(r => r.Category == WeeeCategory.LargeHouseholdAppliances && r.ObligationType == DomainObligationType.B2C);
            Assert.NotNull(result);

            Assert.Equal(300, result.DcfTotal);
        }
        public void CreateResults_WithQ1FinalHolderCollectedAmountOfZero_PopulatesFinalHolderQ1AndFinalHolderTotalWithZero()
        {
            // Arrange
            DataReturn dataReturn = new DataReturn(A.Dummy<Domain.Scheme.Scheme>(), new Quarter(2016, QuarterType.Q1));

            DataReturnVersion dataReturnVersion = new DataReturnVersion(dataReturn);

            WeeeCollectedAmount weeeCollectedAmount = new WeeeCollectedAmount(
                WeeeCollectedAmountSourceType.FinalHolder,
                Domain.Obligation.ObligationType.B2C,
                WeeeCategory.LargeHouseholdAppliances,
                0);

            dataReturnVersion.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(weeeCollectedAmount);

            dataReturn.SetCurrentVersion(dataReturnVersion);

            GetUkWeeeCsvHandler handler = new GetUkWeeeCsvHandler(
                A.Dummy<IWeeeAuthorization>(),
                A.Dummy<IGetUkWeeeCsvDataAccess>(),
                A.Dummy<CsvWriterFactory>());

            // Act
            IEnumerable<GetUkWeeeCsvHandler.CsvResult> results = handler.CreateResults(new List<DataReturn>() { dataReturn });

            // Assert
            GetUkWeeeCsvHandler.CsvResult result = results
                .Single(r => r.Category == WeeeCategory.LargeHouseholdAppliances && r.ObligationType == DomainObligationType.B2C);
            Assert.NotNull(result);

            Assert.Equal(0, result.FinalHolderQ1);
            Assert.Equal(null, result.FinalHolderQ2);
            Assert.Equal(null, result.FinalHolderQ3);
            Assert.Equal(null, result.FinalHolderQ4);
            Assert.Equal(0, result.FinalHolderTotal);
        }