Beispiel #1
0
        public void ThenShouldThrowErrorIfAdjustmentLevyYtdIsNullAndIsNotANonPayment()
        {
            //Arrange
            var latestDeclaration = new DasDeclaration {
                LevyDueYtd = 20
            };


            _hmrcDateService.Setup(x => x.IsSubmissionEndOfYearAdjustment("16-17", 12, It.IsAny <DateTime>()))
            .Returns(true);

            _levyRepository.Setup(x => x.GetSubmissionByEmprefPayrollYearAndMonth(ExpectedEmpRef, "16-17", 8))
            .ReturnsAsync(latestDeclaration);

            var data = RefreshEmployerLevyDataCommandObjectMother.CreateEndOfYearAdjustment(ExpectedEmpRef, ExpectedAccountId);

            var newDeclaration = data.EmployerLevyData.First().Declarations.Declarations.First();

            newDeclaration.LevyDueYtd         = null;
            newDeclaration.NoPaymentForPeriod = false;

            //Act
            Func <Task> action = () => _refreshEmployerLevyDataCommandHandler.Handle(data);

            action.ShouldThrow <ArgumentNullException>();
        }
Beispiel #2
0
        private async Task UpdateEndOfYearAdjustment(string empRef, DasDeclaration yearEndAdjustment, DasDeclaration[] hmrcDeclarations)
        {
            if (yearEndAdjustment.LevyDueYtd == null && !yearEndAdjustment.NoPaymentForPeriod)
            {
                throw new ArgumentNullException(nameof(yearEndAdjustment));
            }

            yearEndAdjustment.EndOfYearAdjustment = true;

            if (yearEndAdjustment.NoPaymentForPeriod)
            {
                return;
            }

            var period12Declaration = await GetDeclarationEffectiveForPeriod12(empRef, yearEndAdjustment.PayrollYear, yearEndAdjustment.SubmissionDate, hmrcDeclarations);

            if (period12Declaration?.LevyDueYtd != null)
            {
                // Caution: the calculation here is incorrect (it should be the other way round). This results in negative values in LevyDeclaration EndOfYearAdjustmentAmount.
                // However, do not fix this as somewhere later (probably in a view or sproc) the EndOfYearAdjustmentAmount is being inverted when writing to the transaction line table.
                yearEndAdjustment.EndOfYearAdjustmentAmount =
                    period12Declaration.LevyDueYtd.Value - yearEndAdjustment.LevyDueYtd ?? 0;
            }
            else
            {
                yearEndAdjustment.EndOfYearAdjustmentAmount = -yearEndAdjustment.LevyDueYtd ?? 0;
            }
        }
Beispiel #3
0
 private bool IsAnEarlierYearEndAdjustment(DasDeclaration ld, string payrollYear, DateTime yearEndAdjustmentCutOff)
 {
     // We're interested in year end adjustments that pre-date the one we're looking for
     return(ld.PayrollYear == payrollYear &&
            ld.PayrollMonth.HasValue &&
            ld.EndOfYearAdjustment && ld.SubmissionDate < yearEndAdjustmentCutOff);
 }
        private List <DasDeclaration> CreateDasDeclarations(GetHMRCLevyDeclarationResponse levyDeclarationQueryResult)
        {
            var declarations = new List <DasDeclaration>();

            foreach (var declaration in levyDeclarationQueryResult.LevyDeclarations.Declarations)
            {
                _logger.Debug($"Creating Levy Declaration with submission Id {declaration.SubmissionId} from HMRC query results");

                var dasDeclaration = new DasDeclaration
                {
                    SubmissionDate           = declaration.SubmissionTime,
                    Id                       = declaration.Id,
                    PayrollMonth             = declaration.PayrollPeriod?.Month,
                    PayrollYear              = declaration.PayrollPeriod?.Year,
                    LevyAllowanceForFullYear = declaration.LevyAllowanceForFullYear,
                    LevyDueYtd               = declaration.LevyDueYearToDate,
                    NoPaymentForPeriod       = declaration.NoPaymentForPeriod,
                    DateCeased               = declaration.DateCeased,
                    InactiveFrom             = declaration.InactiveFrom,
                    InactiveTo               = declaration.InactiveTo,
                    SubmissionId             = declaration.SubmissionId
                };

                declarations.Add(dasDeclaration);
            }

            return(declarations);
        }
Beispiel #5
0
 private bool IsAPossibleEffectivePeriod12Declaration(DasDeclaration ld, string payrollYear)
 {
     // We're interested in an LD if it were submitted on time (i.e. pre-cut off)
     return(ld.PayrollYear == payrollYear &&
            ld.PayrollMonth.HasValue &&
            !ld.EndOfYearAdjustment &&
            _hmrcDateService.IsDateOntimeForPayrollPeriod(ld.PayrollYear, ld.PayrollMonth.Value, ld.SubmissionDate));
 }
Beispiel #6
0
        /// <summary>
        ///     Returns the latest declaration for the year (which is the one that will be effective for period 12).
        /// </summary>
        /// <param name="yearEndAdjustmentCutOff">
        ///     If we are processing an adjustment we need to find the P12 value, which might be another adjustment. In that case, it must
        ///     be an earlier adjustment (i.e. the immediately preceding adjustment).
        /// </param>
        /// <param name="hmrcDeclarations">
        ///     The declarations retrieved from HMRC (these are in-memory and have not yet been written to the database.
        /// </param>
        /// <returns>
        ///     The declaration that was effective for period 12 or null if one was not found.
        /// </returns>
        /// <remarks>
        ///     The database contains all the declarations retrieved to date from HMRC, <see cref="hmrcDeclarations"/>
        ///     contains the declarations that HMRC has that we have not retrieved previously (which is determined by submission date).
        /// </remarks>
        private async Task <DasDeclaration> GetDeclarationEffectiveForPeriod12(string empRef, string payrollYear, DateTime yearEndAdjustmentCutOff, DasDeclaration[] hmrcDeclarations)
        {
            DasDeclaration period12Declaration = await GetEffectivePeriod12SubmissionFromLatestHmrcFeed(hmrcDeclarations, payrollYear, yearEndAdjustmentCutOff);

            if (period12Declaration == null)
            {
                period12Declaration = await GetEffectivePeriod12SubmissionFromDatabase(empRef, payrollYear, yearEndAdjustmentCutOff);
            }

            return(period12Declaration);
        }
        private async Task ProcessScheme(string payeRef, GetEnglishFractionUpdateRequiredResponse englishFractionUpdateResponse, ICollection <EmployerLevyData> employerDataList)
        {
            if (HmrcProcessingEnabled || FractionProcessingOnly)
            {
                await _mediator.SendAsync(new UpdateEnglishFractionsCommand
                {
                    EmployerReference             = payeRef,
                    EnglishFractionUpdateResponse = englishFractionUpdateResponse
                });

                await _dasAccountService.UpdatePayeScheme(payeRef);
            }

            var levyDeclarationQueryResult = HmrcProcessingEnabled || DeclarationProcessingOnly ?
                                             await _mediator.SendAsync(new GetHMRCLevyDeclarationQuery { EmpRef = payeRef }) : null;

            var employerData = new EmployerLevyData();

            if (levyDeclarationQueryResult?.LevyDeclarations?.Declarations != null)
            {
                foreach (var declaration in levyDeclarationQueryResult.LevyDeclarations.Declarations)
                {
                    var dasDeclaration = new DasDeclaration
                    {
                        SubmissionDate           = DateTime.Parse(declaration.SubmissionTime),
                        Id                       = declaration.Id,
                        PayrollMonth             = declaration.PayrollPeriod?.Month,
                        PayrollYear              = declaration.PayrollPeriod?.Year,
                        LevyAllowanceForFullYear = declaration.LevyAllowanceForFullYear,
                        LevyDueYtd               = declaration.LevyDueYearToDate,
                        NoPaymentForPeriod       = declaration.NoPaymentForPeriod,
                        DateCeased               = declaration.DateCeased,
                        InactiveFrom             = declaration.InactiveFrom,
                        InactiveTo               = declaration.InactiveTo,
                        SubmissionId             = declaration.SubmissionId
                    };

                    employerData.EmpRef = payeRef;
                    employerData.Declarations.Declarations.Add(dasDeclaration);
                }

                employerDataList.Add(employerData);
            }
        }
Beispiel #8
0
        private Task <DasDeclaration> GetEffectivePeriod12SubmissionFromLatestHmrcFeed(DasDeclaration[] declarations, string payrollYear, DateTime yearEndAdjustmentCutOff)
        {
            DasDeclaration period12Declaration = null;

            period12Declaration =
                declarations
                .Where(ld => ld.EndOfYearAdjustment)
                .OrderByDescending(ld => ld.SubmissionDate)
                .FirstOrDefault(ld => IsAnEarlierYearEndAdjustment(ld, payrollYear, yearEndAdjustmentCutOff));

            if (period12Declaration == null)
            {
                period12Declaration = declarations
                                      .Where(ld => !ld.EndOfYearAdjustment)
                                      .OrderByDescending(ld => ld.SubmissionDate)
                                      .FirstOrDefault(ld => IsAPossibleEffectivePeriod12Declaration(ld, payrollYear));
            }

            return(Task.FromResult(period12Declaration));
        }
Beispiel #9
0
        public async Task CreateEmployerDeclaration(DasDeclaration dasDeclaration, string empRef, long accountId)
        {
            await WithConnection(async c =>
            {
                var parameters = new DynamicParameters();
                parameters.Add("@LevyDueYtd", dasDeclaration.LevyDueYtd, DbType.Decimal);
                parameters.Add("@LevyAllowanceForYear", dasDeclaration.LevyAllowanceForFullYear, DbType.Decimal);
                parameters.Add("@AccountId", accountId, DbType.Int64);
                parameters.Add("@EmpRef", empRef, DbType.String);
                parameters.Add("@PayrollYear", dasDeclaration.PayrollYear, DbType.String);
                parameters.Add("@PayrollMonth", dasDeclaration.PayrollMonth, DbType.Int16);
                parameters.Add("@SubmissionDate", dasDeclaration.SubmissionDate, DbType.DateTime);
                parameters.Add("@SubmissionId", dasDeclaration.Id, DbType.String);
                parameters.Add("@HmrcSubmissionId", dasDeclaration.SubmissionId, DbType.Int64);
                parameters.Add("@CreatedDate", DateTime.UtcNow, DbType.DateTime);
                if (dasDeclaration.DateCeased.HasValue && dasDeclaration.DateCeased != DateTime.MinValue)
                {
                    parameters.Add("@DateCeased", dasDeclaration.DateCeased, DbType.DateTime);
                }
                if (dasDeclaration.InactiveFrom.HasValue && dasDeclaration.InactiveFrom != DateTime.MinValue)
                {
                    parameters.Add("@InactiveFrom", dasDeclaration.InactiveFrom, DbType.DateTime);
                }
                if (dasDeclaration.InactiveTo.HasValue && dasDeclaration.InactiveTo != DateTime.MinValue)
                {
                    parameters.Add("@InactiveTo", dasDeclaration.InactiveTo, DbType.DateTime);
                }

                parameters.Add("@EndOfYearAdjustment", dasDeclaration.EndOfYearAdjustment, DbType.Boolean);
                parameters.Add("@EndOfYearAdjustmentAmount", dasDeclaration.EndOfYearAdjustmentAmount, DbType.Decimal);

                return(await c.ExecuteAsync(
                           sql: "[employer_financial].[CreateDeclaration]",
                           param: parameters,
                           commandType: CommandType.StoredProcedure));
            });
        }
Beispiel #10
0
 private bool IsSubmissionForFuturePeriod(DasDeclaration dasDeclaration)
 {
     return(dasDeclaration.PayrollMonth.HasValue && _hmrcDateService.IsSubmissionForFuturePeriod(dasDeclaration.PayrollYear, dasDeclaration.PayrollMonth.Value, DateTime.UtcNow));
 }
Beispiel #11
0
 private bool DoesSubmissionPreDateTheLevy(DasDeclaration dasDeclaration)
 {
     return(_hmrcDateService.DoesSubmissionPreDateLevy(dasDeclaration.PayrollYear));
 }
Beispiel #12
0
        private async Task GetLevyFromPreviousSubmission(EmployerLevyData employerLevyData, DasDeclaration dasDeclaration)
        {
            var previousSubmission = await _dasLevyRepository.GetLastSubmissionForScheme(employerLevyData.EmpRef);

            dasDeclaration.LevyDueYtd = previousSubmission?.LevyDueYtd ?? 0;
            dasDeclaration.LevyAllowanceForFullYear = previousSubmission?.LevyAllowanceForFullYear ?? 0;
        }
Beispiel #13
0
        private async Task UpdateEndOfYearAdjustment(EmployerLevyData employerLevyData, DasDeclaration dasDeclaration)
        {
            var adjustmentDeclaration = await _dasLevyRepository.GetSubmissionByEmprefPayrollYearAndMonth(employerLevyData.EmpRef, dasDeclaration.PayrollYear, dasDeclaration.PayrollMonth.Value);

            dasDeclaration.EndOfYearAdjustment       = true;
            dasDeclaration.EndOfYearAdjustmentAmount = adjustmentDeclaration?.LevyDueYtd - dasDeclaration.LevyDueYtd ?? 0;
        }
Beispiel #14
0
 private bool IsEndOfYearAdjustment(DasDeclaration dasDeclaration)
 {
     return(dasDeclaration.PayrollMonth.HasValue && _hmrcDateService.IsSubmissionEndOfYearAdjustment(dasDeclaration.PayrollYear, dasDeclaration.PayrollMonth.Value, dasDeclaration.SubmissionDate));
 }
Beispiel #15
0
        public RefreshEmployerLevyDataCommandHandlerTestsFixture SetLastSubmissionForScheme(string empRef, DasDeclaration lastDeclaration)
        {
            _dasLevyRepository.Setup(x => x.GetLastSubmissionForScheme(empRef)).ReturnsAsync(lastDeclaration);

            return(this);
        }
Beispiel #16
0
        public LevyImportCleanerStrategyTestFixtures WithDeclaration(DasDeclaration declaration)
        {
            Declarations.Add(declaration);

            return(this);
        }
        public void WhenIHaveTheFollowingSubmissions(Table table)
        {
            var accountId         = (long)ScenarioContext.Current["AccountId"];
            var dasLevyRepository = _container.GetInstance <IDasLevyRepository>();
            //For each row in the table insert into the levy_Declarations table
            var lineCount = 1;

            var emprefDictionary = new Dictionary <string, decimal>();

            foreach (var tableRow in table.Rows)
            {
                var dasDeclaration = new DasDeclaration
                {
                    LevyDueYtd               = Convert.ToDecimal(tableRow["LevyDueYtd"]),
                    PayrollYear              = tableRow["Payroll_Year"],
                    PayrollMonth             = Convert.ToInt16(tableRow["Payroll_Month"]),
                    LevyAllowanceForFullYear = 15000,
                    SubmissionDate           = DateTime.Parse(tableRow["SubmissionDate"]),
                    Id = lineCount.ToString()
                };
                lineCount++;
                if (!emprefDictionary.ContainsKey(tableRow["Paye_scheme"]))
                {
                    emprefDictionary.Add(tableRow["Paye_scheme"], Convert.ToDecimal(tableRow["English_Fraction"]));
                }
                if (tableRow.ContainsKey("EndOfYearAdjustment"))
                {
                    dasDeclaration.EndOfYearAdjustment = Convert.ToInt16(tableRow["EndOfYearAdjustment"]) == 1;
                }
                if (tableRow.ContainsKey("EndOfYearAdjustmentAmount"))
                {
                    dasDeclaration.EndOfYearAdjustmentAmount = Convert.ToDecimal(tableRow["EndOfYearAdjustmentAmount"]);
                }

                dasLevyRepository.CreateEmployerDeclaration(dasDeclaration, tableRow["Paye_scheme"], accountId).Wait();
            }

            var englishFractionRepository = _container.GetInstance <IEnglishFractionRepository>();

            foreach (var empRef in emprefDictionary)
            {
                englishFractionRepository.CreateEmployerFraction(new DasEnglishFraction
                {
                    Amount         = empRef.Value,
                    DateCalculated = new DateTime(2016, 01, 01),
                    EmpRef         = empRef.Key
                }, empRef.Key).Wait();
            }

            dasLevyRepository.ProcessDeclarations().Wait();

            lineCount = 1;

            var updateTransactionLine = _container.GetInstance <IUpdateTransactionLine>();

            foreach (var tableRow in table.Rows)
            {
                var subId = lineCount;

                if (tableRow.ContainsKey("CreatedDate") && !string.IsNullOrEmpty(tableRow["CreatedDate"]))
                {
                    updateTransactionLine.Execute(subId, DateTime.Parse(tableRow["CreatedDate"])).Wait();
                }
                lineCount++;
            }
        }