Skip to content

Commit

Permalink
Payment branch created based on DEV branch (#265)
Browse files Browse the repository at this point in the history
* Payment branch created based on DEV branch

* Pushing payment changes based on 2 year and 3 year contract logic
  • Loading branch information
Prashanthi-Sivanarayana authored Nov 5, 2024
1 parent 2a5618a commit 1ffbcd8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 29 deletions.
12 changes: 12 additions & 0 deletions OFM.Infrastructure.WebAPI/Extensions/TimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ public static DateTime GetLastBusinessDayOfPaymentMonth(this DateTime PaymentDat

return lastDayOfPaymentMonth;
}
public static DateTime GetFirstDayOfFollowingMonth(this DateTime PaymentDate, List<DateTime> 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 GetPreviousBusinessDay(this DateTime targetDate, List<DateTime> holidays)
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,38 @@ public async Task<JsonObject> RunProcessAsync(ID365AppUserService appUserService
return ProcessResult.Completed(ProcessId).SimpleProcessResult;
}
_baseApplicationId = deserializedApplicationData.First().Id;
Funding? funding = deserializedApplicationData.First()?.ofm_application_funding?.FirstOrDefault();
if (funding is null)
{
_logger.LogError(CustomLogEvent.Process, "Unable to retrieve Funding record with Id {FundingId}", processParams.Funding!.FundingId);
return ProcessResult.Completed(ProcessId).SimpleProcessResult;
}
var fundingEndDate = funding.ofm_end_date;
var fundingStartDate = funding.ofm_start_date;
DateTime intermediateDate;
DateTime firstAnniversary;
DateTime secondAnniversary;
if (fundingEndDate is null)
{
_logger.LogError(CustomLogEvent.Process, "Unable to retrieve Funding record with Id {ofm_end_date}", fundingEndDate);
return ProcessResult.Completed(ProcessId).SimpleProcessResult;
}

//Two year contract or three year contract
if (fundingEndDate.Value.Year - fundingStartDate.Value.Year == 2)
{
intermediateDate = fundingEndDate.Value.AddYears(-1);
firstAnniversary = intermediateDate;
secondAnniversary = fundingEndDate.Value;
}
else
{
intermediateDate = fundingEndDate.Value.AddYears(-2);
firstAnniversary = intermediateDate;
intermediateDate = fundingEndDate.Value.AddYears(-1);
secondAnniversary = intermediateDate;
}

ProcessData allPaymentsData = await GetAllPaymentsByApplicationIdDataAsync();
_allPayments = JsonSerializer.Deserialize<List<D365PaymentLine>>(allPaymentsData.Data.ToString());
if (_allPayments is not null && _allPayments.Count > 0)
Expand All @@ -358,7 +390,7 @@ public async Task<JsonObject> RunProcessAsync(ID365AppUserService appUserService
#endregion

await ProcessSupportNeedsOrIndigenousPayments(deserializedApplicationData.First(), approvedSA, processParams, fiscalYears, holidaysList);
await ProcessTransportationPayments(deserializedApplicationData.First(), approvedSA, processParams, fiscalYears, holidaysList);
await ProcessTransportationPayments(deserializedApplicationData.First(), approvedSA, processParams, fiscalYears, holidaysList,firstAnniversary, secondAnniversary, fundingEndDate);

_logger.LogInformation(CustomLogEvent.Process, "Finished payments generation for the supplementary application {allowanceId}", processParams.SupplementaryApplication!.allowanceId);

Expand All @@ -378,18 +410,28 @@ private async Task<JsonObject> ProcessSupportNeedsOrIndigenousPayments(Applicati
return ProcessResult.Completed(ProcessId).SimpleProcessResult;
}

private async Task<JsonObject> ProcessTransportationPayments(Application baseApplication, SupplementaryApplication approvedSA, ProcessParameter processParams, List<D365FiscalYear> fiscalYears, List<DateTime> holidaysList)
private async Task<JsonObject> ProcessTransportationPayments(Application baseApplication, SupplementaryApplication approvedSA, ProcessParameter processParams, List<D365FiscalYear> fiscalYears, List<DateTime> holidaysList, DateTime firstAnniversaryDate, DateTime secondAnniversaryDate, DateTime? fundingEndDate)
{
if (approvedSA.ofm_allowance_type == ecc_allowance_type.Transportation)
{
// Process future payments
await CreatePaymentsInBatch(baseApplication!, approvedSA!, approvedSA.ofm_start_date.Value, approvedSA.ofm_end_date.Value, approvedSA.ofm_monthly_amount!.Value, false, ecc_payment_type.Transportation, processParams, fiscalYears, holidaysList);
if ((approvedSA.ofm_start_date.Value.Month == firstAnniversaryDate.Month && approvedSA.ofm_start_date.Value.Year == firstAnniversaryDate.Year) || (approvedSA.ofm_start_date.Value.Month == secondAnniversaryDate.Month && approvedSA.ofm_start_date.Value.Year == secondAnniversaryDate.Year) || (approvedSA.ofm_start_date.Value.Month == fundingEndDate?.Month && approvedSA.ofm_start_date.Value.Year == fundingEndDate?.Year))
{
int retroActiveMonthsCount = approvedSA.ofm_retroactive_date!.HasValue ? (approvedSA.ofm_start_date.Value.Year - approvedSA.ofm_retroactive_date!.Value.Year) * 12 + approvedSA.ofm_start_date.Value.Month - approvedSA.ofm_retroactive_date.Value.Month : 0;
decimal retroActiveAmount = retroActiveMonthsCount > 0 ? approvedSA.ofm_monthly_amount!.Value * retroActiveMonthsCount : 0;
var endTermLumpSumPayment = approvedSA.ofm_monthly_amount!.Value + retroActiveAmount;
await CreateSinglePayment(approvedSA, approvedSA.ofm_start_date!.Value, endTermLumpSumPayment, false, ecc_payment_type.Transportation, baseApplication!, processParams, fiscalYears, holidaysList);

// Process retroactive payment
int retroActiveMonthsCount = approvedSA.ofm_retroactive_date!.HasValue ? (approvedSA.ofm_start_date.Value.Year - approvedSA.ofm_retroactive_date!.Value.Year) * 12 + approvedSA.ofm_start_date.Value.Month - approvedSA.ofm_retroactive_date.Value.Month : 0;
await ProcessRetroActivePayment(baseApplication!, approvedSA, processParams, fiscalYears, holidaysList, approvedSA.ofm_monthly_amount!.Value, retroActiveMonthsCount);

_logger.LogInformation(CustomLogEvent.Process, "Finished payments generation for the {allowancetype} application with Id {allowanceId}", approvedSA.ofm_allowance_type, processParams.SupplementaryApplication!.allowanceId);
_logger.LogInformation(CustomLogEvent.Process, "Finished payments generation for the {allowancetype} application with Id {allowanceId}", approvedSA.ofm_allowance_type, processParams.SupplementaryApplication!.allowanceId);
}
else
{
// Process future payments
await CreatePaymentsInBatch(baseApplication!, approvedSA!, approvedSA.ofm_start_date.Value, approvedSA.ofm_end_date.Value, approvedSA.ofm_monthly_amount!.Value, false, ecc_payment_type.Transportation, processParams, fiscalYears, holidaysList);
// Process retroactive payment
int retroActiveMonthsCount = approvedSA.ofm_retroactive_date!.HasValue ? (approvedSA.ofm_start_date.Value.Year - approvedSA.ofm_retroactive_date!.Value.Year) * 12 + approvedSA.ofm_start_date.Value.Month - approvedSA.ofm_retroactive_date.Value.Month : 0;
await ProcessRetroActivePayment(baseApplication!, approvedSA, processParams, fiscalYears, holidaysList, approvedSA.ofm_monthly_amount!.Value, retroActiveMonthsCount);
_logger.LogInformation(CustomLogEvent.Process, "Finished payments generation for the {allowancetype} application with Id {allowanceId}", approvedSA.ofm_allowance_type, processParams.SupplementaryApplication!.allowanceId);
}

}

Expand Down Expand Up @@ -440,19 +482,10 @@ private async Task<JsonObject> CreateSinglePayment(SupplementaryApplication appr
List<D365FiscalYear> fiscalYears,
List<DateTime> holidaysList)
{
DateTime invoiceDate = (paymentDate == approvedSA.ofm_start_date!.Value) ? paymentDate.GetLastBusinessDayOfThePreviousMonth(holidaysList) : paymentDate.GetCFSInvoiceDate(holidaysList, _BCCASApi.PayableInDays);
DateTime invoiceDate = paymentDate.GetFirstDayOfFollowingMonth(holidaysList);
DateTime invoiceReceivedDate = invoiceDate.AddBusinessDays(_BCCASApi.PayableInDays, holidaysList);
DateTime effectiveDate = invoiceDate;

if (approvedSA.ofm_retroactive_date is not null)
{
// 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.
invoiceReceivedDate = paymentDate.GetLastBusinessDayOfThePreviousMonth(holidaysList);
invoiceDate = invoiceReceivedDate.GetCFSInvoiceDate(holidaysList, _BCCASApi.PayableInDays);
effectiveDate = invoiceDate;
}

Guid fiscalYear = invoiceDate.MatchFiscalYear(fiscalYears);

var payload = new JsonObject()
Expand Down Expand Up @@ -501,19 +534,10 @@ private async Task<JsonObject> CreatePaymentsInBatch(Application baseApplication
for (DateTime paymentDate = startDate; paymentDate <= endDate; paymentDate = paymentDate.AddMonths(1))
{

DateTime invoiceDate = (paymentDate == startDate) ? startDate.GetLastBusinessDayOfThePreviousMonth(holidaysList) : paymentDate.GetCFSInvoiceDate(holidaysList, _BCCASApi.PayableInDays);
DateTime invoiceDate = paymentDate.GetFirstDayOfFollowingMonth(holidaysList);
DateTime invoiceReceivedDate = invoiceDate.AddBusinessDays(_BCCASApi.PayableInDays, holidaysList);
DateTime effectiveDate = invoiceDate;

if (approvedSA.ofm_retroactive_date is not null)
{
// 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.
invoiceReceivedDate = paymentDate.GetLastBusinessDayOfThePreviousMonth(holidaysList);
invoiceDate = invoiceReceivedDate.GetCFSInvoiceDate(holidaysList, _BCCASApi.PayableInDays);
effectiveDate = invoiceDate;
}

Guid? fiscalYear = invoiceDate.MatchFiscalYear(fiscalYears);
var paymentToCreate = new JsonObject()
{
Expand Down

0 comments on commit 1ffbcd8

Please sign in to comment.