Returning the list recurring templates while creating payment request from recurring template

This commit is contained in:
ashutosh.nehete 2025-11-10 14:32:00 +05:30
parent ac711c7254
commit 14be381f5f
2 changed files with 30 additions and 11 deletions

View File

@ -288,7 +288,10 @@ namespace Marco.Pms.Services.MappingProfiles
dest => dest.NextStrikeDate, dest => dest.NextStrikeDate,
opt => opt.MapFrom(src => src.StrikeDate.Date)); opt => opt.MapFrom(src => src.StrikeDate.Date));
CreateMap<UpdateRecurringTemplateDto, RecurringPayment>(); CreateMap<UpdateRecurringTemplateDto, RecurringPayment>();
CreateMap<RecurringPayment, RecurringPaymentVM>(); CreateMap<RecurringPayment, RecurringPaymentVM>()
.ForMember(
dest => dest.RecurringPaymentUId,
opt => opt.MapFrom(src => $"{src.UIDPrefix}/{src.UIDPostfix:D5}"));
CreateMap<RecurringPayment, RecurringPaymentDetailsVM>() CreateMap<RecurringPayment, RecurringPaymentDetailsVM>()
.ForMember( .ForMember(
dest => dest.RecurringPaymentUID, dest => dest.RecurringPaymentUID,

View File

@ -3030,7 +3030,7 @@ namespace Marco.Pms.Services.Service
var paymentRequests = new List<PaymentRequest>(); var paymentRequests = new List<PaymentRequest>();
// Generate UID prefix for payment requests for this period (month/year) // Generate UID prefix for payment requests for this period (month/year)
string uIDPrefix = $"PR/{DateTime.Now:MM.yy}"; string uIDPrefix = $"PR/{DateTime.Now:MMyy}";
// Get last generated payment request for UID postfixing (to maintain unique numbering) // Get last generated payment request for UID postfixing (to maintain unique numbering)
var lastPR = await _context.PaymentRequests var lastPR = await _context.PaymentRequests
@ -3044,13 +3044,18 @@ namespace Marco.Pms.Services.Service
{ {
if (recurringPayment.NextStrikeDate.HasValue) if (recurringPayment.NextStrikeDate.HasValue)
{ {
var nextStrikeDate = GetNextStrikeDate(recurringPayment.Frequency, recurringPayment.NextStrikeDate.Value);
// Update latest generated date to today (UTC) // Update latest generated date to today (UTC)
recurringPayment.LatestPRGeneratedAt = DateTime.UtcNow.Date; recurringPayment.LatestPRGeneratedAt = DateTime.UtcNow.Date;
recurringPayment.NextStrikeDate = GetNextStrikeDate(recurringPayment.Frequency, recurringPayment.NextStrikeDate.Value, recurringPayment.EndDate); if (nextStrikeDate.Date > recurringPayment.EndDate.Date)
if (recurringPayment.EndDate.Date < DateTime.UtcNow.Date)
{ {
recurringPayment.NextStrikeDate = null;
recurringPayment.StatusId = CompletedTemplateStatus; recurringPayment.StatusId = CompletedTemplateStatus;
} }
else
{
recurringPayment.NextStrikeDate = nextStrikeDate.Date;
}
updatedRecurringPayments.Add(recurringPayment); updatedRecurringPayments.Add(recurringPayment);
// Create new payment request mapped from recurring payment data // Create new payment request mapped from recurring payment data
@ -3101,12 +3106,27 @@ namespace Marco.Pms.Services.Service
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
var emails = updatedRecurringPayments.Select(rp => rp.NotifyTo.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)).ToList(); var newRecurringTemplateIds = updatedRecurringPayments.Select(rp => rp.Id).ToList();
var newRecurringPayments = await _context.RecurringPayments
.Include(rp => rp.Currency)
.Include(rp => rp.ExpenseCategory)
.Include(rp => rp.Status)
.Include(rp => rp.Project)
.AsNoTracking()
.Where(rp => newRecurringTemplateIds.Contains(rp.Id))
.ToListAsync();
_logger.LogInfo("{Count} payment requests created successfully from recurring payments by EmployeeId: {EmployeeId} for TenantId: {TenantId}", _logger.LogInfo("{Count} payment requests created successfully from recurring payments by EmployeeId: {EmployeeId} for TenantId: {TenantId}",
paymentRequests.Count, loggedInEmployee.Id, tenantId); paymentRequests.Count, loggedInEmployee.Id, tenantId);
return ApiResponse<object>.SuccessResponse(emails, $"{paymentRequests.Count} conversion(s) to payment request completed successfully.", 201); var response = newRecurringPayments.Select(rp => new
{
RecurringPayment = _mapper.Map<RecurringPaymentVM>(rp),
Emails = rp.NotifyTo.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
}).ToList();
return ApiResponse<object>.SuccessResponse(response, $"{paymentRequests.Count} conversion(s) to payment request completed successfully.", 201);
} }
catch (DbUpdateException ex) catch (DbUpdateException ex)
{ {
@ -3881,7 +3901,7 @@ namespace Marco.Pms.Services.Service
return null; // Validation successful, no error return null; // Validation successful, no error
} }
private DateTime? GetNextStrikeDate(PLAN_FREQUENCY frequency, DateTime currentStrikeDate, DateTime endDate) private DateTime GetNextStrikeDate(PLAN_FREQUENCY frequency, DateTime currentStrikeDate)
{ {
var nextStrikeDate = currentStrikeDate.AddDays(1); var nextStrikeDate = currentStrikeDate.AddDays(1);
switch (frequency) switch (frequency)
@ -3909,10 +3929,6 @@ namespace Marco.Pms.Services.Service
nextStrikeDate = currentStrikeDate.AddDays(7); nextStrikeDate = currentStrikeDate.AddDays(7);
break; break;
} }
if (endDate < nextStrikeDate)
{
return null;
}
return nextStrikeDate; return nextStrikeDate;
} }