-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCreatePaymentRequests.cshtml.cs
More file actions
295 lines (245 loc) · 12.2 KB
/
CreatePaymentRequests.cshtml.cs
File metadata and controls
295 lines (245 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System;
using Unity.Payments.Suppliers;
using Unity.Payments.PaymentRequests;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages;
using Unity.Payments.PaymentConfigurations;
using Unity.GrantManager.GrantApplications;
using System.Text.Json;
using Unity.Payments.Domain.Suppliers;
using System.Linq;
using Unity.GrantManager.Payments;
using Unity.GrantManager.Applications;
using Unity.GrantManager.ApplicationForms;
namespace Unity.Payments.Web.Pages.Payments
{
public class CreatePaymentRequestsModel(
IGrantApplicationAppService applicationService,
IPaymentRequestAppService paymentRequestAppService,
IPaymentConfigurationAppService paymentConfigurationAppService,
ISupplierAppService iSupplierAppService,
ISiteRepository siteRepository,
IPaymentSettingsAppService paymentSettingsAppService,
IApplicationLinksService applicationLinksService,
IApplicationFormRepository applicationFormRepository,
IApplicationFormAppService applicationFormAppService
) : AbpPageModel
{
public List<Guid> SelectedApplicationIds { get; set; } = [];
[BindProperty]
public List<PaymentsModel> ApplicationPaymentRequestForm { get; set; } = [];
[BindProperty]
public bool DisableSubmit { get; set; }
[BindProperty]
public bool HasPaymentConfiguration { get; set; }
[BindProperty]
public string BatchNumberDisplay { get; set; } = string.Empty;
[BindProperty]
public decimal TotalAmount { get; set; }
public decimal ApplicationPaymentRequestFormTotalAmount
{
get
{
return ApplicationPaymentRequestForm?.Sum(x => x.Amount) ?? 0m;
}
}
public async Task OnGetAsync(string applicationIds)
{
var paymentConfiguration = await paymentConfigurationAppService.GetAsync();
if (paymentConfiguration != null)
{
HasPaymentConfiguration = true;
}
else
{
DisableSubmit = true;
HasPaymentConfiguration = false;
}
SelectedApplicationIds = JsonSerializer.Deserialize<List<Guid>>(applicationIds) ?? [];
var applications = await applicationService.GetApplicationDetailsListAsync(SelectedApplicationIds);
foreach (var application in applications)
{
decimal remainingAmount = await GetRemainingAmountAllowedByApplicationAsync(application);
// Grabs the Account Coding ID from the Application Form and if there is none then the Payment Configuration
// If neither exist then an error on the payment request will be shown
Guid? accountCodingId = await paymentSettingsAppService.GetAccountCodingIdByApplicationIdAsync(application.Id);
// Load ApplicationForm with hierarchy information
var applicationForm = await applicationFormRepository.GetAsync(application.ApplicationForm.Id);
PaymentsModel request = new()
{
CorrelationId = application.Id,
ApplicantName = application.Applicant.ApplicantName == "" ? "Applicant Name" : application.Applicant.ApplicantName,
SubmissionConfirmationCode = application.ReferenceNo,
Amount = remainingAmount,
Description = "",
InvoiceNumber = application.ReferenceNo,
ContractNumber = application.ContractNumber,
RemainingAmount = remainingAmount,
AccountCodingId = accountCodingId
};
var supplier = await GetSupplierByApplicationAync(application);
string supplierNumber = supplier?.Number?? string.Empty;
Guid siteId = application.Applicant.SiteId;
Site? site = null;
if(siteId != Guid.Empty) {
site = await siteRepository.GetAsync(siteId);
var siteName = $"{site.Number} ({supplierNumber}, {site.City})";
request.SiteName = siteName;
request.SiteId = siteId;
}
request.SupplierName = supplier?.Name;
request.SupplierNumber = supplierNumber;
request.ErrorList = await GetErrorlist(supplier, site, application, applicationForm, remainingAmount, accountCodingId);
if (request.ErrorList != null && request.ErrorList.Count > 0)
{
request.DisableFields = true;
}
ApplicationPaymentRequestForm!.Add(request);
}
var batchName = await paymentRequestAppService.GetNextBatchInfoAsync();
BatchNumberDisplay = batchName;
TotalAmount = ApplicationPaymentRequestForm?.Sum(x => x.Amount) ?? 0m;
}
private async Task<List<string>> GetErrorlist(SupplierDto? supplier, Site? site, GrantApplicationDto application, ApplicationForm applicationForm, decimal remainingAmount, Guid? accountCodingId)
{
bool missingFields = false;
List<string> errorList = [];
if (supplier == null || site == null || supplier.Number == null)
{
missingFields = true;
}
if (remainingAmount <= 0)
{
errorList.Add("There is no remaining amount for this application.");
}
if (missingFields)
{
errorList.Add("Some payment information is missing for this applicant, please make sure Supplier info is provided and default site is selected.");
}
if (application.StatusCode != GrantApplicationState.GRANT_APPROVED)
{
errorList.Add("The selected Application is not Approved. To continue please remove the item from the list.");
}
if (!application.ApplicationForm.Payable)
{
errorList.Add("The selected application is not Payable. To continue please remove the item from the list.");
}
if(accountCodingId == null || accountCodingId == Guid.Empty)
{
errorList.Add("The selected application form does not have an Account Coding or no default Account Coding is set.");
}
// Add form hierarchy and parent link validation
var hierarchyErrors = await ValidateFormHierarchyAndParentLink(application, applicationForm);
errorList.AddRange(hierarchyErrors);
return errorList;
}
private async Task<decimal> GetRemainingAmountAllowedByApplicationAsync(GrantApplicationDto application)
{
decimal remainingAmount = 0;
// Calculate the "Future paid amount" and if it is more than Approved Amount, the system shall:
// Highlight the record
// Show error message: This payment exceeds the Approved Amount.
// Future paid amount: Total Pending Amount + Total Paid amount + Amount that is in the current payment request
if (application.ApprovedAmount > 0)
{
decimal approvedAmmount = application.ApprovedAmount;
decimal totalFutureRequested = await paymentRequestAppService.GetTotalPaymentRequestAmountByCorrelationIdAsync(application.Id);
if (approvedAmmount > totalFutureRequested)
{
remainingAmount = approvedAmmount - totalFutureRequested;
}
}
return remainingAmount;
}
private async Task<SupplierDto?> GetSupplierByApplicationAync(GrantApplicationDto application)
{
if(application.Applicant.SupplierId != Guid.Empty)
{
SupplierDto? supplierDto = await iSupplierAppService.GetAsync(application.Applicant.SupplierId);
if (supplierDto != null)
{
return supplierDto;
}
}
return await iSupplierAppService.GetByCorrelationAsync(new GetSupplierByCorrelationDto()
{
CorrelationId = application.Applicant.Id,
CorrelationProvider = GrantManager.Payments.PaymentConsts.ApplicantCorrelationProvider,
IncludeDetails = true
});
}
private async Task<List<string>> ValidateFormHierarchyAndParentLink(
GrantApplicationDto application,
ApplicationForm applicationForm)
{
List<string> errors = [];
// Only validate if form is payable and has Child hierarchy
if (!applicationForm.Payable ||
!applicationForm.FormHierarchy.HasValue ||
applicationForm.FormHierarchy.Value != FormHierarchyType.Child)
{
return errors; // No validation needed
}
// Check if ParentFormId and ParentFormVersionId are set
if (!applicationForm.ParentFormId.HasValue ||
!applicationForm.ParentFormVersionId.HasValue)
{
// Configuration issue - should not happen if validation works
return errors;
}
// Get parent links for this application
var allLinks = await applicationLinksService.GetListByApplicationAsync(application.Id);
var parentLink = allLinks.Find(link => link.LinkType == ApplicationLinkType.Parent);
// Rule 2: No parent link exists
if (parentLink == null)
{
errors.Add("Payment Configuration for this form requires a valid parent application link before payments can be processed.");
return errors;
}
// Rule 1: Parent link exists but doesn't match Payment Configuration
// Get the parent application's form version details
var parentFormDetails = await applicationFormAppService.GetFormDetailsByApplicationIdAsync(parentLink.ApplicationId);
// Validate both ParentFormId and ParentFormVersionId
bool formIdMatches = parentFormDetails.ApplicationFormId == applicationForm.ParentFormId.Value;
bool versionIdMatches = parentFormDetails.ApplicationFormVersionId == applicationForm.ParentFormVersionId.Value;
if (!formIdMatches || !versionIdMatches)
{
errors.Add("The selected parent form in Payment Configuration does not match the application's linked parent. Please verify and try again.");
}
return errors;
}
public async Task<IActionResult> OnPostAsync()
{
if (ApplicationPaymentRequestForm == null) return NoContent();
var payments = MapPaymentRequests();
await paymentRequestAppService.CreateAsync(payments);
return NoContent();
}
private List<CreatePaymentRequestDto> MapPaymentRequests()
{
var payments = new List<CreatePaymentRequestDto>();
if (ApplicationPaymentRequestForm == null) return payments;
foreach (var payment in ApplicationPaymentRequestForm)
{
payments.Add(new CreatePaymentRequestDto()
{
Amount = payment.Amount,
CorrelationId = payment.CorrelationId,
SiteId = payment.SiteId,
Description = payment.Description,
InvoiceNumber = payment.InvoiceNumber,
ContractNumber = payment.ContractNumber ?? string.Empty,
SupplierName = payment.SupplierName ?? string.Empty,
SupplierNumber = payment.SupplierNumber ?? string.Empty,
PayeeName = payment.ApplicantName ?? string.Empty,
SubmissionConfirmationCode = payment.SubmissionConfirmationCode ?? string.Empty,
CorrelationProvider = PaymentConsts.ApplicationCorrelationProvider,
AccountCodingId = payment.AccountCodingId,
});
}
return payments;
}
}
}