<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ProviderPayments.TestStack.Core;
using SFA.DAS.Payments.AcceptanceTests.Contexts;
using SFA.DAS.Payments.AcceptanceTests.DataHelpers;
using SFA.DAS.Payments.AcceptanceTests.ExecutionEnvironment;
using SFA.DAS.Payments.AcceptanceTests.StepDefinitions.Base;
using TechTalk.SpecFlow;
using IlrBuilder = SFA.DAS.Payments.AcceptanceTests.Builders.IlrBuilder;


namespace SFA.DAS.Payments.AcceptanceTests.StepDefinitions.Intermediate
{
    [Binding]
    public class EarningStepDefinitions : BaseStepDefinitions
    {
        public EarningStepDefinitions(StepDefinitionsContext earningAndPaymentsContext)
            :base(earningAndPaymentsContext)
        {

        }

        [Given(@"a provider has previously earned (.*) in period R01")]
        public void GivenProviderHasPreviouslyEarnedInPeriod(decimal previousAmount)
        {
            var environmentVariables = EnvironmentVariablesFactory.GetEnvironmentVariables();

            StepDefinitionsContext.SetDefaultProvider();

            var provider = StepDefinitionsContext.GetDefaultProvider();
            var learner = StepDefinitionsContext.CreateLearner(15000, new DateTime(2017, 08, 01), new DateTime(2018, 07, 01));

            SetupEarningsData(provider, learner);


            var committment = StepDefinitionsContext.ReferenceDataContext.Commitments.First();
            var account = StepDefinitionsContext.ReferenceDataContext.Employers.First(x => x.Name == committment.Employer);

            //Save the previous earning
            EarningsDataHelper.SaveEarnedAmount(provider.Ukprn,
                                                committment.Id,
                                                account.AccountId,
                                                learner.Uln,
                                                learner.LearnRefNumber,
                                                "R01",
                                                08,
                                                2016,
                                                1,
                                                previousAmount, environmentVariables);

        }

        [When(@"an earning of (.*) is calculated for period R01")]
        public void AnEarningIsCalculatedForThePeriod(decimal earnedAmount)
        {

            // Setup reference data
            var environmentVariables = EnvironmentVariablesFactory.GetEnvironmentVariables();
            var provider = StepDefinitionsContext.GetDefaultProvider();
            var learner = provider.Learners[0];

            //save the periodiosed values
            EarningsDataHelper.SavePeriodisedValuesForUkprn(StepDefinitionsContext.GetDefaultProvider().Ukprn,
                                                            learner.LearnRefNumber,
                                                            new Dictionary<int, decimal> { { 1, earnedAmount } },
                                                            learner.LearningDelivery.EpisodeStartDate,
                                                            learner.LearningDelivery.PriceEpisodeIdentifier,
                                                            environmentVariables);

            RunMonthEnd(new DateTime(2016, 09, 01));
        }


       


        [Then(@"a payment of (.*) is due")]
        public void ThenAPaymentIsDue(decimal dueAmount)
        {
            var environmentVariables = EnvironmentVariablesFactory.GetEnvironmentVariables();

            //Get the due amount 
            var earning = PaymentsDueDataHelper.GetPaymentsDueForPeriod(StepDefinitionsContext.GetDefaultProvider().Ukprn,
                                                                        2016,
                                                                        08,
                                                                        environmentVariables)
                                                                        .FirstOrDefault();

            if (dueAmount != 0)
            {
                Assert.IsNotNull(earning, $"Expected earning for the period but nothing found");
                Assert.AreEqual(dueAmount, earning.AmountDue, $"Expected earning of {dueAmount} for period R01 but found {earning.AmountDue}");
            }
            else
            {
                Assert.IsNull(earning, $"There was no expected earning for the period but earnigs data found");

            }
        }


        #region Earnings Distribution


        [When(@"the actual duration of learning is (.*) months")]
        public void WhenTheActualDurationOfLearningIsMonths(int actualCensusMonths)
        {
            ScenarioContext.Current.Add("actualCensusMonths", actualCensusMonths);
        }

      
       
        [When(@"the planned course duration covers (.*) months")]
        public void WhenThePlannedCourseDurationCoversMonths(int months)<commit_msg>change the month for collection period<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ProviderPayments.TestStack.Core;
using SFA.DAS.Payments.AcceptanceTests.Contexts;
using SFA.DAS.Payments.AcceptanceTests.DataHelpers;
using SFA.DAS.Payments.AcceptanceTests.ExecutionEnvironment;
using SFA.DAS.Payments.AcceptanceTests.StepDefinitions.Base;
using TechTalk.SpecFlow;
using IlrBuilder = SFA.DAS.Payments.AcceptanceTests.Builders.IlrBuilder;


namespace SFA.DAS.Payments.AcceptanceTests.StepDefinitions.Intermediate
{
    [Binding]
    public class EarningStepDefinitions : BaseStepDefinitions
    {
        public EarningStepDefinitions(StepDefinitionsContext earningAndPaymentsContext)
            :base(earningAndPaymentsContext)
        {

        }

        [Given(@"a provider has previously earned (.*) in period R01")]
        public void GivenProviderHasPreviouslyEarnedInPeriod(decimal previousAmount)
        {
            var environmentVariables = EnvironmentVariablesFactory.GetEnvironmentVariables();

            StepDefinitionsContext.SetDefaultProvider();

            var provider = StepDefinitionsContext.GetDefaultProvider();
            var learner = StepDefinitionsContext.CreateLearner(15000, new DateTime(2017, 08, 01), new DateTime(2018, 07, 01));

            SetupEarningsData(provider, learner);


            var committment = StepDefinitionsContext.ReferenceDataContext.Commitments.First();
            var account = StepDefinitionsContext.ReferenceDataContext.Employers.First(x => x.Name == committment.Employer);

            //Save the previous earning
            EarningsDataHelper.SaveEarnedAmount(provider.Ukprn,
                                                committment.Id,
                                                account.AccountId,
                                                learner.Uln,
                                                learner.LearnRefNumber,
                                                "R01",
                                                08,
                                                2016,
                                                1,
                                                previousAmount, environmentVariables);

        }

        [When(@"an earning of (.*) is calculated for period R01")]
        public void AnEarningIsCalculatedForThePeriod(decimal earnedAmount)
        {

            // Setup reference data
            var environmentVariables = EnvironmentVariablesFactory.GetEnvironmentVariables();
            var provider = StepDefinitionsContext.GetDefaultProvider();
            var learner = provider.Learners[0];

            //save the periodiosed values
            EarningsDataHelper.SavePeriodisedValuesForUkprn(StepDefinitionsContext.GetDefaultProvider().Ukprn,
                                                            learner.LearnRefNumber,
                                                            new Dictionary<int, decimal> { { 1, earnedAmount } },
                                                            learner.LearningDelivery.EpisodeStartDate,
                                                            learner.LearningDelivery.PriceEpisodeIdentifier,
                                                            environmentVariables);

            RunMonthEnd(new DateTime(2016, 09, 01));
        }


       


        [Then(@"a payment of (.*) is due")]
        public void ThenAPaymentIsDue(decimal dueAmount)
        {
            var environmentVariables = EnvironmentVariablesFactory.GetEnvironmentVariables();

            //Get the due amount 
            var earning = PaymentsDueDataHelper.GetPaymentsDueForPeriod(StepDefinitionsContext.GetDefaultProvider().Ukprn,
                                                                        2016,
                                                                        09,
                                                                        environmentVariables)
                                                                        .FirstOrDefault();

            if (dueAmount != 0)
            {
                Assert.IsNotNull(earning, $"Expected earning for the period but nothing found");
                Assert.AreEqual(dueAmount, earning.AmountDue, $"Expected earning of {dueAmount} for period R01 but found {earning.AmountDue}");
            }
            else
            {
                Assert.IsNull(earning, $"There was no expected earning for the period but earnigs data found");

            }
        }


        #region Earnings Distribution


        [When(@"the actual duration of learning is (.*) months")]
        public void WhenTheActualDurationOfLearningIsMonths(int actualCensusMonths)
        {
            ScenarioContext.Current.Add("actualCensusMonths", actualCensusMonths);
        }

      
       
        [When(@"the planned course duration covers (.*) months")]
        public void WhenThePlannedCourseDurationCoversMonths(int months)
{'fraction_non_alphanumeric': 0.06630164260826282, 'fraction_numerical': 0.01055251368840219, 'mean_word_length': 1.536868686868687, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 84, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2897855', 'n_tokens_mistral': 2258, 'n_tokens_neox': 2004, 'n_words': 440}