diff --git a/OFM.Infrastructure.WebAPI/Extensions/TimeExtensions.cs b/OFM.Infrastructure.WebAPI/Extensions/TimeExtensions.cs index b155200..7024d1b 100644 --- a/OFM.Infrastructure.WebAPI/Extensions/TimeExtensions.cs +++ b/OFM.Infrastructure.WebAPI/Extensions/TimeExtensions.cs @@ -200,6 +200,20 @@ public static DateTime GetLastBusinessDayOfPaymentMonth(this DateTime PaymentDat return lastDayOfPaymentMonth; } + + public static DateTime GetFirstDayOfFollowingMonth(this DateTime PaymentDate, List holidays) + { + DateTime firstDayOfNextMonth = new DateTime(PaymentDate.Year, PaymentDate.Month, 1).AddMonths(1); + // Iterate backward to find the last business day of that payment month. + while (firstDayOfNextMonth.DayOfWeek == DayOfWeek.Saturday || + firstDayOfNextMonth.DayOfWeek == DayOfWeek.Sunday || + holidays.Exists(excludedDate => excludedDate.Date.Equals(firstDayOfNextMonth.Date))) + { + firstDayOfNextMonth = firstDayOfNextMonth.AddDays(1); + } + return firstDayOfNextMonth; + } + public static DateTime GetFirstDayOfFollowingNextMonth(this DateTime PaymentDate, List holidays) { DateTime firstDayOfNextMonth = new DateTime(PaymentDate.Year, PaymentDate.Month, 1).AddMonths(2); diff --git a/OFM.Infrastructure.WebAPI/Services/Processes/Payments/P520GenerateAllowancePaymentProvider.cs b/OFM.Infrastructure.WebAPI/Services/Processes/Payments/P520GenerateAllowancePaymentProvider.cs index bfc3032..32bc4d5 100644 --- a/OFM.Infrastructure.WebAPI/Services/Processes/Payments/P520GenerateAllowancePaymentProvider.cs +++ b/OFM.Infrastructure.WebAPI/Services/Processes/Payments/P520GenerateAllowancePaymentProvider.cs @@ -488,15 +488,23 @@ private async Task CreateSinglePayment(SupplementaryApplication appr DateTime invoiceDate = (paymentDate == approvedSA.ofm_start_date!.Value) ? paymentDate.GetLastBusinessDayOfThePreviousMonth(holidaysList) : paymentDate.GetCFSInvoiceDate(holidaysList, _BCCASApi.PayableInDays); DateTime invoiceReceivedDate = invoiceDate.AddBusinessDays(_BCCASApi.PayableInDays, holidaysList); DateTime effectiveDate = invoiceDate; - //this applies if supplementary application is submitted within 45 days. - if ((CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, firstAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, secondAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, fundingEndDate.Value))) + //this applies if supplementary application is submitted within last 45 days to last 30 days. + + if ((CheckSubmissionIsWithinLastMonth(approvedSA.ofm_submittedon.Value, firstAnniversaryDate) || CheckSubmissionIsWithinLastMonth(approvedSA.ofm_submittedon.Value, secondAnniversaryDate) || CheckSubmissionIsWithinLastMonth(approvedSA.ofm_submittedon.Value, fundingEndDate.Value))) { - invoiceReceivedDate = paymentDate.GetFirstDayOfFollowingNextMonth(holidaysList); - invoiceDate = invoiceReceivedDate.GetCFSInvoiceDate(holidaysList, _BCCASApi.PayableInDays); - effectiveDate = invoiceDate; + invoiceReceivedDate = paymentDate.GetFirstDayOfFollowingNextMonth(holidaysList); + invoiceDate = invoiceReceivedDate.GetCFSInvoiceDate(holidaysList, _BCCASApi.PayableInDays); + effectiveDate = invoiceDate; + } + else if ((CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, firstAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, secondAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, fundingEndDate.Value))) + { + invoiceReceivedDate = paymentDate.GetFirstDayOfFollowingMonth(holidaysList); + invoiceDate = invoiceReceivedDate.GetCFSInvoiceDate(holidaysList, _BCCASApi.PayableInDays); + effectiveDate = invoiceDate; } - if (approvedSA.ofm_retroactive_date is not null && !(CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, firstAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, secondAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, fundingEndDate.Value))) + + if (approvedSA.ofm_retroactive_date is not null && !(CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, firstAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, secondAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, fundingEndDate.Value))) { // Date calculation logic is different for mid-year supp application. Overriding regular date logic above // Invoice received date is always the last business date of previous month except for first payment of the First funding year, it is 5 business day after the last business day of the last month. @@ -560,14 +568,21 @@ private async Task CreatePaymentsInBatch(Application baseApplication DateTime invoiceDate = (paymentDate == startDate) ? startDate.GetLastBusinessDayOfThePreviousMonth(holidaysList) : paymentDate.GetCFSInvoiceDate(holidaysList, _BCCASApi.PayableInDays); DateTime invoiceReceivedDate = invoiceDate.AddBusinessDays(_BCCASApi.PayableInDays, holidaysList); DateTime effectiveDate = invoiceDate; + //this applies if supplementary application is submitted within 45 days. - if ((CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, firstAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, secondAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, fundingEndDate.Value))) + if ((CheckSubmissionIsWithinLastMonth(approvedSA.ofm_submittedon.Value, firstAnniversaryDate) || CheckSubmissionIsWithinLastMonth(approvedSA.ofm_submittedon.Value, secondAnniversaryDate) || CheckSubmissionIsWithinLastMonth(approvedSA.ofm_submittedon.Value, fundingEndDate.Value))) { invoiceReceivedDate = paymentDate.GetFirstDayOfFollowingNextMonth(holidaysList); invoiceDate = invoiceReceivedDate.GetCFSInvoiceDate(holidaysList, _BCCASApi.PayableInDays); effectiveDate = invoiceDate; + }else if ((CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, firstAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, secondAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, fundingEndDate.Value))) + { + invoiceReceivedDate = paymentDate.GetFirstDayOfFollowingMonth(holidaysList); + invoiceDate = invoiceReceivedDate.GetCFSInvoiceDate(holidaysList, _BCCASApi.PayableInDays); + effectiveDate = invoiceDate; } + if (approvedSA.ofm_retroactive_date is not null && !(CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, firstAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, secondAnniversaryDate) || CheckSubmissionIsWithinLast45days(approvedSA.ofm_submittedon.Value, fundingEndDate.Value))) { // Date calculation logic is different for mid-year supp application. Overriding regular date logic above @@ -628,9 +643,17 @@ private async Task GetNextInvoiceLineNumber() private static bool CheckSubmissionIsWithinLast45days(DateTime submittedDate, DateTime anniversayDate) { - DateTime cutoffDate = anniversayDate.AddDays(-45); + DateTime temp = anniversayDate.AddMonths(-1); + DateTime cutoffDate = new DateTime(temp.Year, temp.Month, 15, 0, 0, 0).ToUTC(); return submittedDate >= cutoffDate; } + + private static bool CheckSubmissionIsWithinLastMonth(DateTime submittedDate, DateTime anniversayDate) + { + DateTime temp = submittedDate.ToUniversalTime(); + + return temp.Year == anniversayDate.Year && temp.Month == anniversayDate.Month; + } } } \ No newline at end of file