-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUpdatePaymentRequestStatus.cshtml.cs
More file actions
281 lines (253 loc) · 11.9 KB
/
UpdatePaymentRequestStatus.cshtml.cs
File metadata and controls
281 lines (253 loc) · 11.9 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
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Unity.GrantManager.ApplicationForms;
using Unity.Payments.Domain.PaymentRequests;
using Unity.Payments.Domain.Services;
using Unity.Payments.Domain.Shared;
using Unity.Payments.Enums;
using Unity.Payments.PaymentConfigurations;
using Unity.Payments.PaymentRequests;
using Unity.Payments.Permissions;
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages;
namespace Unity.Payments.Web.Pages.PaymentApprovals
{
public class PaymentGrouping
{
public int GroupId { get; set; }
public PaymentRequestStatus ToStatus { get; set; }
public List<PaymentsApprovalModel> Items { get; set; } = new();
}
public class UpdatePaymentRequestStatus(
IPaymentsManager paymentsManager,
IPaymentRequestAppService paymentRequestAppService,
IApplicationFormAppService applicationFormAppService,
IPaymentRequestRepository paymentRepository,
IPaymentConfigurationAppService paymentConfigurationAppService,
IPermissionCheckerService permissionCheckerService) : AbpPageModel
{
[BindProperty] public List<PaymentGrouping> PaymentGroupings { get; set; } = new();
[BindProperty] public decimal? UserPaymentThreshold { get; set; }
[BindProperty] public decimal PaymentThreshold { get; set; }
[BindProperty] public bool DisableSubmit { get; set; }
[BindProperty] public bool HasPaymentConfiguration { get; set; }
[BindProperty] public bool IsApproval { get; set; }
[BindProperty] public bool IsErrors { get; set; }
[BindProperty]
public decimal TotalAmount { get; set; }
[BindProperty]
public string? Note { get; set; } = string.Empty;
public List<Guid> SelectedPaymentIds { get; set; } = new();
public string FromStatusText { get; set; } = string.Empty;
public async Task OnGetAsync(string paymentIds, bool isApprove)
{
await InitializeStateAsync(paymentIds, isApprove);
var payments = await paymentRequestAppService.GetListByPaymentIdsAsync(SelectedPaymentIds);
var paymentApprovals = await BuildPaymentApprovalsAsync(payments);
PaymentGroupings = paymentApprovals
.GroupBy(item => item.ToStatus)
.Select((group, index) => new PaymentGrouping
{
GroupId = index,
ToStatus = group.Key,
Items = group.ToList()
})
.ToList();
DisableSubmit = paymentApprovals.Count == 0 || !ModelState.IsValid;
}
private async Task InitializeStateAsync(string paymentIds, bool isApprove)
{
await GetFromStateForUserAsync();
IsApproval = isApprove;
SelectedPaymentIds = JsonSerializer.Deserialize<List<Guid>>(paymentIds) ?? new();
UserPaymentThreshold = await paymentRequestAppService.GetUserPaymentThresholdAsync();
HasPaymentConfiguration = await paymentConfigurationAppService.GetAsync() != null;
}
private async Task<List<PaymentsApprovalModel>> BuildPaymentApprovalsAsync(List<PaymentDetailsDto> payments)
{
var paymentApprovals = new List<PaymentsApprovalModel>();
foreach (var payment in payments)
{
var formThreshold = await applicationFormAppService.GetFormPaymentApprovalThresholdByApplicationIdAsync(payment.CorrelationId);
if (formThreshold.HasValue && UserPaymentThreshold.HasValue)
{
PaymentThreshold = formThreshold.Value < UserPaymentThreshold.Value ? formThreshold.Value : UserPaymentThreshold.Value;
}
else if (formThreshold.HasValue)
{
PaymentThreshold = formThreshold.Value;
}
else if (UserPaymentThreshold.HasValue)
{
PaymentThreshold = UserPaymentThreshold.Value;
}
else
{
PaymentThreshold = 0m;
}
var approvalModel = await CreateApprovalModel(payment);
ValidateApprovalModel(approvalModel);
if (await VerifyPermissionsAsync(payment.Status, approvalModel))
{
paymentApprovals.Add(approvalModel);
}
}
return paymentApprovals;
}
private async Task<PaymentsApprovalModel> CreateApprovalModel(PaymentDetailsDto payment)
{
bool isL3ApprovalRequired = payment.Amount > PaymentThreshold;
await UpdateExpenseApprovalsAsync(payment, isL3ApprovalRequired);
return new PaymentsApprovalModel
{
Id = payment.Id,
ReferenceNumber = payment.ReferenceNumber,
CorrelationId = payment.Id,
ApplicantName = payment.PayeeName,
Amount = payment.Amount,
Description = payment.Description,
InvoiceNumber = payment.InvoiceNumber,
Status = payment.Status,
IsL3ApprovalRequired = isL3ApprovalRequired,
ToStatus = payment.Status,
IsApproval = IsApproval,
HasUserPaymentThreshold = UserPaymentThreshold.HasValue,
PreviousL1Approver = payment.ExpenseApprovals.FirstOrDefault(x => x.Type == ExpenseApprovalType.Level1)?.DecisionUserId
};
}
private async Task UpdateExpenseApprovalsAsync(PaymentDetailsDto payment, bool isL3ApprovalRequired)
{
if (payment.Status == PaymentRequestStatus.L2Pending)
{
var l3Approval = payment.ExpenseApprovals.FirstOrDefault(x => x.Type == ExpenseApprovalType.Level3);
PaymentRequest paymentEntity = await paymentRepository.GetAsync(payment.Id);
if (isL3ApprovalRequired && l3Approval == null)
{
paymentEntity.ExpenseApprovals.Add(new ExpenseApproval(Guid.NewGuid(), ExpenseApprovalType.Level3));
}
else if (!isL3ApprovalRequired && l3Approval != null)
{
var l3ApprovalEntity = paymentEntity.ExpenseApprovals.FirstOrDefault(x => x.Type == ExpenseApprovalType.Level3);
if (l3ApprovalEntity != null)
{
paymentEntity.ExpenseApprovals.Remove(l3ApprovalEntity);
}
}
await paymentRepository.UpdateAsync(paymentEntity);
}
}
private void ValidateApprovalModel(PaymentsApprovalModel request)
{
var validationResults = new List<ValidationResult>();
request.IsValid = Validator.TryValidateObject(request, new ValidationContext(request, LazyServiceProvider, null), validationResults, true);
foreach (var validationResult in validationResults)
{
foreach (var memberName in validationResult.MemberNames)
{
ModelState.AddModelError(memberName, validationResult.ErrorMessage ?? "Validation error.");
}
}
}
private async Task<bool> VerifyPermissionsAsync(PaymentRequestStatus status, PaymentsApprovalModel request)
{
bool preventPayment = await paymentsManager.GetFormPreventPaymentStatusByPaymentRequestId(request.Id);
request.ToStatus = GetNextStatus(status, request, preventPayment, IsApproval);
var permission = GetRequiredPermission(status);
return permission != null && await permissionCheckerService.IsGrantedAsync(permission);
}
private static PaymentRequestStatus GetNextStatus(PaymentRequestStatus status, PaymentsApprovalModel request, bool preventPayment, bool isApproval)
{
if (status == PaymentRequestStatus.L2Pending && isApproval)
{
if (request.IsL3ApprovalRequired)
{
return PaymentRequestStatus.L3Pending;
}
if (preventPayment)
{
return PaymentRequestStatus.FSB;
}
return PaymentRequestStatus.Submitted;
}
return status switch
{
PaymentRequestStatus.L1Pending => isApproval ? PaymentRequestStatus.L2Pending : PaymentRequestStatus.L1Declined,
PaymentRequestStatus.L2Pending => PaymentRequestStatus.L2Declined,
PaymentRequestStatus.L3Pending => isApproval ? PaymentRequestStatus.Submitted : PaymentRequestStatus.L3Declined,
_ => request.ToStatus
};
}
private static string? GetRequiredPermission(PaymentRequestStatus status)
{
return status switch
{
PaymentRequestStatus.L1Pending => PaymentsPermissions.Payments.L1ApproveOrDecline,
PaymentRequestStatus.L2Pending => PaymentsPermissions.Payments.L2ApproveOrDecline,
PaymentRequestStatus.L3Pending => PaymentsPermissions.Payments.L3ApproveOrDecline,
_ => null
};
}
public async Task<IActionResult> OnPostAsync()
{
if (PaymentGroupings == null || PaymentGroupings.Count == 0 || !ModelState.IsValid) return NoContent();
var payments = PaymentGroupings.SelectMany(group => group.Items)
.Select(payment => new UpdatePaymentStatusRequestDto
{
PaymentRequestId = payment.Id,
IsApprove = IsApproval,
Note = Note ?? String.Empty
})
.ToList();
await paymentRequestAppService.UpdateStatusAsync(payments);
return NoContent();
}
private async Task GetFromStateForUserAsync()
{
var permissions = new[]
{
PaymentsPermissions.Payments.L1ApproveOrDecline,
PaymentsPermissions.Payments.L2ApproveOrDecline,
PaymentsPermissions.Payments.L3ApproveOrDecline
};
foreach (var permission in permissions)
{
if (await permissionCheckerService.IsGrantedAsync(permission))
{
FromStatusText = GetStatusText((PaymentRequestStatus)Array.IndexOf(permissions, permission));
break;
}
}
}
public static string GetStatusText(PaymentRequestStatus status)
{
return status.ToString() switch
{
"L1Pending" => "L1 Pending",
"L1Approved" => "L1 Approved",
"L1Declined" => "L1 Declined",
"L2Pending" => "L2 Pending",
"L2Approved" => "L2 Approved",
"L2Declined" => "L2 Declined",
"L3Pending" => "L3 Pending",
"L3Approved" => "L3 Approved",
"L3Declined" => "L3 Declined",
"Submitted" => "Submitted to CAS",
"FSB" => "Sent to Accounts Payable",
"Paid" => "Paid",
"PaymentFailed" => "Payment Failed",
_ => "L1 Pending",
};
}
public static string GetStatusTextColor(PaymentRequestStatus status) => status switch
{
PaymentRequestStatus.L1Declined or PaymentRequestStatus.L2Declined or PaymentRequestStatus.L3Declined => "#CE3E39",
PaymentRequestStatus.Submitted => "#5595D9",
PaymentRequestStatus.Paid => "#42814A",
_ => "#053662"
};
}
}