diff --git a/Marco.Pms.Services/MappingProfiles/MappingProfile.cs b/Marco.Pms.Services/MappingProfiles/MappingProfile.cs index c8ffdee..14a5aed 100644 --- a/Marco.Pms.Services/MappingProfiles/MappingProfile.cs +++ b/Marco.Pms.Services/MappingProfiles/MappingProfile.cs @@ -288,7 +288,10 @@ namespace Marco.Pms.Services.MappingProfiles dest => dest.NextStrikeDate, opt => opt.MapFrom(src => src.StrikeDate.Date)); CreateMap(); - CreateMap(); + CreateMap() + .ForMember( + dest => dest.RecurringPaymentUId, + opt => opt.MapFrom(src => $"{src.UIDPrefix}/{src.UIDPostfix:D5}")); CreateMap() .ForMember( dest => dest.RecurringPaymentUID, diff --git a/Marco.Pms.Services/Service/ExpensesService.cs b/Marco.Pms.Services/Service/ExpensesService.cs index bc3ed9f..44c939d 100644 --- a/Marco.Pms.Services/Service/ExpensesService.cs +++ b/Marco.Pms.Services/Service/ExpensesService.cs @@ -3030,7 +3030,7 @@ namespace Marco.Pms.Services.Service var paymentRequests = new List(); // 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) var lastPR = await _context.PaymentRequests @@ -3044,13 +3044,18 @@ namespace Marco.Pms.Services.Service { if (recurringPayment.NextStrikeDate.HasValue) { + var nextStrikeDate = GetNextStrikeDate(recurringPayment.Frequency, recurringPayment.NextStrikeDate.Value); // Update latest generated date to today (UTC) recurringPayment.LatestPRGeneratedAt = DateTime.UtcNow.Date; - recurringPayment.NextStrikeDate = GetNextStrikeDate(recurringPayment.Frequency, recurringPayment.NextStrikeDate.Value, recurringPayment.EndDate); - if (recurringPayment.EndDate.Date < DateTime.UtcNow.Date) + if (nextStrikeDate.Date > recurringPayment.EndDate.Date) { + recurringPayment.NextStrikeDate = null; recurringPayment.StatusId = CompletedTemplateStatus; } + else + { + recurringPayment.NextStrikeDate = nextStrikeDate.Date; + } updatedRecurringPayments.Add(recurringPayment); // Create new payment request mapped from recurring payment data @@ -3101,12 +3106,27 @@ namespace Marco.Pms.Services.Service 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}", paymentRequests.Count, loggedInEmployee.Id, tenantId); - return ApiResponse.SuccessResponse(emails, $"{paymentRequests.Count} conversion(s) to payment request completed successfully.", 201); + var response = newRecurringPayments.Select(rp => new + { + RecurringPayment = _mapper.Map(rp), + Emails = rp.NotifyTo.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + }).ToList(); + + return ApiResponse.SuccessResponse(response, $"{paymentRequests.Count} conversion(s) to payment request completed successfully.", 201); } catch (DbUpdateException ex) { @@ -3881,7 +3901,7 @@ namespace Marco.Pms.Services.Service 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); switch (frequency) @@ -3909,10 +3929,6 @@ namespace Marco.Pms.Services.Service nextStrikeDate = currentStrikeDate.AddDays(7); break; } - if (endDate < nextStrikeDate) - { - return null; - } return nextStrikeDate; }