diff --git a/fe-admin/src/components/pe/PeWorkflowPanel.tsx b/fe-admin/src/components/pe/PeWorkflowPanel.tsx
index 02e3ee9..9aa63de 100644
--- a/fe-admin/src/components/pe/PeWorkflowPanel.tsx
+++ b/fe-admin/src/components/pe/PeWorkflowPanel.tsx
@@ -45,12 +45,20 @@ export function PeWorkflowPanel({
})
const transition = useMutation({
- mutationFn: async () =>
- api.post(`/purchase-evaluations/${evaluation.id}/transitions`, {
+ mutationFn: async () => {
+ // Decision = Reject (2) khi:
+ // - target = TuChoi (huỷ phiếu)
+ // - target = DangSoanThao từ phase trung gian (= Trả lại — smart reject Mig 16
+ // set RejectedFromPhase + clear N-stage rows + Drafter resume jump-back)
+ const isReject = target === PurchaseEvaluationPhase.TuChoi
+ || (target === PurchaseEvaluationPhase.DangSoanThao
+ && evaluation.phase !== PurchaseEvaluationPhase.DangSoanThao)
+ return api.post(`/purchase-evaluations/${evaluation.id}/transitions`, {
targetPhase: target,
- decision: target === PurchaseEvaluationPhase.TuChoi ? 2 : 1,
+ decision: isReject ? 2 : 1,
comment: comment || null,
- }),
+ })
+ },
onSuccess: () => {
toast.success('Đã chuyển phase.')
qc.invalidateQueries({ queryKey: ['pe-detail', evaluation.id] })
@@ -101,22 +109,37 @@ export function PeWorkflowPanel({
{next.length > 0 && !readOnly && (
-
+
- {next.map(p => (
-
- ))}
+ {next.map(p => {
+ // Phân loại button theo hành động:
+ // - Trả lại = về DangSoanThao (từ phase trung gian) — red
+ // - Hủy/Từ chối = TuChoi (chỉ ở phase DangSoanThao đầu) — red
+ // - Duyệt = forward phase tiếp theo — brand
+ const isSendBack = p === PurchaseEvaluationPhase.DangSoanThao
+ && evaluation.phase !== PurchaseEvaluationPhase.DangSoanThao
+ const isCancel = p === PurchaseEvaluationPhase.TuChoi
+ const isDanger = isSendBack || isCancel
+ const label = isSendBack
+ ? '← Trả lại (về Drafter sửa)'
+ : isCancel
+ ? '✗ Hủy / Từ chối'
+ : `✓ Duyệt → ${PurchaseEvaluationPhaseLabel[p]}`
+ return (
+
+ )
+ })}
)}
@@ -126,20 +149,40 @@ export function PeWorkflowPanel({
)}
- {target !== null && (
-
- )}
+ {target !== null && (() => {
+ const isCancel = target === PurchaseEvaluationPhase.TuChoi
+ const isSendBack = target === PurchaseEvaluationPhase.DangSoanThao
+ && evaluation.phase !== PurchaseEvaluationPhase.DangSoanThao
+ const dialogTitle = isCancel
+ ? '✗ Từ chối phiếu (khoá hoàn toàn)'
+ : isSendBack
+ ? '← Trả lại Drafter sửa'
+ : `✓ Duyệt → ${PurchaseEvaluationPhaseLabel[target]}`
+ return (
+
+ )
+ })()}
{deptApprovals.length > 0 && (
diff --git a/fe-user/src/components/pe/PeWorkflowPanel.tsx b/fe-user/src/components/pe/PeWorkflowPanel.tsx
index 02e3ee9..9aa63de 100644
--- a/fe-user/src/components/pe/PeWorkflowPanel.tsx
+++ b/fe-user/src/components/pe/PeWorkflowPanel.tsx
@@ -45,12 +45,20 @@ export function PeWorkflowPanel({
})
const transition = useMutation({
- mutationFn: async () =>
- api.post(`/purchase-evaluations/${evaluation.id}/transitions`, {
+ mutationFn: async () => {
+ // Decision = Reject (2) khi:
+ // - target = TuChoi (huỷ phiếu)
+ // - target = DangSoanThao từ phase trung gian (= Trả lại — smart reject Mig 16
+ // set RejectedFromPhase + clear N-stage rows + Drafter resume jump-back)
+ const isReject = target === PurchaseEvaluationPhase.TuChoi
+ || (target === PurchaseEvaluationPhase.DangSoanThao
+ && evaluation.phase !== PurchaseEvaluationPhase.DangSoanThao)
+ return api.post(`/purchase-evaluations/${evaluation.id}/transitions`, {
targetPhase: target,
- decision: target === PurchaseEvaluationPhase.TuChoi ? 2 : 1,
+ decision: isReject ? 2 : 1,
comment: comment || null,
- }),
+ })
+ },
onSuccess: () => {
toast.success('Đã chuyển phase.')
qc.invalidateQueries({ queryKey: ['pe-detail', evaluation.id] })
@@ -101,22 +109,37 @@ export function PeWorkflowPanel({
{next.length > 0 && !readOnly && (
-
+
- {next.map(p => (
-
- ))}
+ {next.map(p => {
+ // Phân loại button theo hành động:
+ // - Trả lại = về DangSoanThao (từ phase trung gian) — red
+ // - Hủy/Từ chối = TuChoi (chỉ ở phase DangSoanThao đầu) — red
+ // - Duyệt = forward phase tiếp theo — brand
+ const isSendBack = p === PurchaseEvaluationPhase.DangSoanThao
+ && evaluation.phase !== PurchaseEvaluationPhase.DangSoanThao
+ const isCancel = p === PurchaseEvaluationPhase.TuChoi
+ const isDanger = isSendBack || isCancel
+ const label = isSendBack
+ ? '← Trả lại (về Drafter sửa)'
+ : isCancel
+ ? '✗ Hủy / Từ chối'
+ : `✓ Duyệt → ${PurchaseEvaluationPhaseLabel[p]}`
+ return (
+
+ )
+ })}
)}
@@ -126,20 +149,40 @@ export function PeWorkflowPanel({
)}
- {target !== null && (
-
- )}
+ {target !== null && (() => {
+ const isCancel = target === PurchaseEvaluationPhase.TuChoi
+ const isSendBack = target === PurchaseEvaluationPhase.DangSoanThao
+ && evaluation.phase !== PurchaseEvaluationPhase.DangSoanThao
+ const dialogTitle = isCancel
+ ? '✗ Từ chối phiếu (khoá hoàn toàn)'
+ : isSendBack
+ ? '← Trả lại Drafter sửa'
+ : `✓ Duyệt → ${PurchaseEvaluationPhaseLabel[target]}`
+ return (
+
+ )
+ })()}
{deptApprovals.length > 0 && (
diff --git a/src/Backend/SolutionErp.Domain/PurchaseEvaluations/PurchaseEvaluationPolicy.cs b/src/Backend/SolutionErp.Domain/PurchaseEvaluations/PurchaseEvaluationPolicy.cs
index b6faed8..67a8d2b 100644
--- a/src/Backend/SolutionErp.Domain/PurchaseEvaluations/PurchaseEvaluationPolicy.cs
+++ b/src/Backend/SolutionErp.Domain/PurchaseEvaluations/PurchaseEvaluationPolicy.cs
@@ -56,14 +56,20 @@ public static class PurchaseEvaluationPolicies
[(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.ChoPurchasing)] = [AppRoles.Drafter, AppRoles.DeptManager],
[(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.Drafter, AppRoles.DeptManager],
+ // Phase trung gian: 3 hành động — Duyệt forward / Trả lại Drafter (DangSoanThao) / Từ chối hoàn toàn (TuChoi).
+ // Trả lại = smart reject pattern Mig 16 (set RejectedFromPhase + về DangSoanThao + Drafter sửa).
+ // Từ chối = phiếu khoá hoàn toàn (Phase=TuChoi → 17 handler Mig 16 lock edit).
[(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.ChoCCM)] = [AppRoles.Procurement],
[(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Procurement],
+ [(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.Procurement],
[(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.ChoCEODuyetNCC)] = [AppRoles.CostControl],
[(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.CostControl],
+ [(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.CostControl],
[(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.DaDuyet)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
[(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
+ [(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
},
PhaseSla: DefaultSla,
ActivePhases:
@@ -85,20 +91,26 @@ public static class PurchaseEvaluationPolicies
[(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.ChoPurchasing)] = [AppRoles.Drafter, AppRoles.DeptManager],
[(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.Drafter, AppRoles.DeptManager],
+ // Phase trung gian: 3 hành động — Duyệt forward / Trả lại / Từ chối (xem comment NccOnly).
[(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.ChoDuAn)] = [AppRoles.Procurement],
[(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Procurement],
+ [(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.Procurement],
[(PurchaseEvaluationPhase.ChoDuAn, PurchaseEvaluationPhase.ChoCCM)] = [AppRoles.ProjectManager],
[(PurchaseEvaluationPhase.ChoDuAn, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.ProjectManager],
+ [(PurchaseEvaluationPhase.ChoDuAn, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.ProjectManager],
[(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.ChoCEODuyetPA)] = [AppRoles.CostControl],
[(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.CostControl],
+ [(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.CostControl],
[(PurchaseEvaluationPhase.ChoCEODuyetPA, PurchaseEvaluationPhase.ChoCEODuyetNCC)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
[(PurchaseEvaluationPhase.ChoCEODuyetPA, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
+ [(PurchaseEvaluationPhase.ChoCEODuyetPA, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
[(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.DaDuyet)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
[(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
+ [(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
},
PhaseSla: DefaultSla,
ActivePhases:
@@ -171,17 +183,21 @@ public static class PurchaseEvaluationPolicyRegistry
transitions[(prev.Value, s.Phase)] = roles;
if (userIds.Length > 0) userTransitions[(prev.Value, s.Phase)] = userIds;
- // Reject path back to Drafter (common pattern)
+ // 3 hành động phase trung gian — Duyệt forward + Trả lại Drafter + Từ chối hoàn toàn
if (prev.Value != PurchaseEvaluationPhase.DangSoanThao && s.Phase != PurchaseEvaluationPhase.DangSoanThao)
{
transitions.TryAdd((s.Phase, PurchaseEvaluationPhase.DangSoanThao), roles);
+ transitions.TryAdd((s.Phase, PurchaseEvaluationPhase.TuChoi), roles);
if (userIds.Length > 0)
+ {
userTransitions.TryAdd((s.Phase, PurchaseEvaluationPhase.DangSoanThao), userIds);
+ userTransitions.TryAdd((s.Phase, PurchaseEvaluationPhase.TuChoi), userIds);
+ }
}
}
prev = s.Phase;
}
- // First step có thể reject to TuChoi
+ // First step (DangSoanThao) — Drafter có thể TuChoi (huỷ phiếu)
if (steps.Count > 0)
transitions.TryAdd((steps[0].Phase, PurchaseEvaluationPhase.TuChoi),
[AppRoles.Drafter, AppRoles.DeptManager]);
diff --git a/src/Backend/SolutionErp.Infrastructure/Services/PurchaseEvaluationWorkflowService.cs b/src/Backend/SolutionErp.Infrastructure/Services/PurchaseEvaluationWorkflowService.cs
index b0508e3..05267ca 100644
--- a/src/Backend/SolutionErp.Infrastructure/Services/PurchaseEvaluationWorkflowService.cs
+++ b/src/Backend/SolutionErp.Infrastructure/Services/PurchaseEvaluationWorkflowService.cs
@@ -43,17 +43,25 @@ public class PurchaseEvaluationWorkflowService(
if (decision == ApprovalDecision.Reject)
{
- evaluation.RejectedFromPhase = fromPhase;
- targetPhase = PurchaseEvaluationPhase.DangSoanThao;
+ // 2 loại Reject (Session 14):
+ // - target=TuChoi: "Từ chối hoàn toàn" — phiếu khoá vĩnh viễn (Phase=TuChoi
+ // → 17 handler Mig 16 lock edit). Drafter phải tạo phiếu mới. KHÔNG set
+ // RejectedFromPhase + KHÔNG clear N-stage (không resume).
+ // - target khác (thường = DangSoanThao): "Trả lại" — smart reject pattern
+ // Mig 16. Set RejectedFromPhase + force DangSoanThao + clear N-stage rows
+ // tại fromPhase → Drafter sửa rồi trình lại jump-back tới phase đã reject.
+ if (targetPhase != PurchaseEvaluationPhase.TuChoi)
+ {
+ evaluation.RejectedFromPhase = fromPhase;
+ targetPhase = PurchaseEvaluationPhase.DangSoanThao;
- // N-stage state reset (Mig 18): clear inner step approval rows tại
- // fromPhase. User resume sẽ approve lại từ inner step đầu.
- var staleNStageRows = await db.PurchaseEvaluationDepartmentApprovals
- .Where(a => a.PurchaseEvaluationId == evaluation.Id
- && a.PhaseAtApproval == (int)fromPhase
- && a.InnerStepId != null)
- .ToListAsync(ct);
- foreach (var r in staleNStageRows) db.PurchaseEvaluationDepartmentApprovals.Remove(r);
+ var staleNStageRows = await db.PurchaseEvaluationDepartmentApprovals
+ .Where(a => a.PurchaseEvaluationId == evaluation.Id
+ && a.PhaseAtApproval == (int)fromPhase
+ && a.InnerStepId != null)
+ .ToListAsync(ct);
+ foreach (var r in staleNStageRows) db.PurchaseEvaluationDepartmentApprovals.Remove(r);
+ }
}
else if (isResumingAfterReject)
{
diff --git a/tests/SolutionErp.Infrastructure.Tests/Services/PeNStageApprovalTests.cs b/tests/SolutionErp.Infrastructure.Tests/Services/PeNStageApprovalTests.cs
index e946b2e..aafec55 100644
--- a/tests/SolutionErp.Infrastructure.Tests/Services/PeNStageApprovalTests.cs
+++ b/tests/SolutionErp.Infrastructure.Tests/Services/PeNStageApprovalTests.cs
@@ -278,11 +278,11 @@ public class PeNStageApprovalTests : IClassFixture
pe = await _db.PurchaseEvaluations.FirstAsync(x => x.Id == pe.Id);
- // Act: admin reject (skip 2-stage gate).
+ // Act: admin "Trả lại" (target=DangSoanThao + decision=Reject Session 14).
var admin = await _fx.CreateUserAsync($"adm-rej-{Guid.NewGuid():N}@test", "Admin", null, ["Admin"]);
await _service.TransitionAsync(
- pe, PurchaseEvaluationPhase.TuChoi, admin.Id, ["Admin"],
- ApprovalDecision.Reject, "reject test");
+ pe, PurchaseEvaluationPhase.DangSoanThao, admin.Id, ["Admin"],
+ ApprovalDecision.Reject, "trả lại test");
// Assert: phase = DangSoanThao, RejectedFromPhase = ChoPurchasing,
// N-stage rows tại ChoPurchasing đã clear.
diff --git a/tests/SolutionErp.Infrastructure.Tests/Services/PeTwoStageApprovalTests.cs b/tests/SolutionErp.Infrastructure.Tests/Services/PeTwoStageApprovalTests.cs
index 2cff73c..2fa8ebc 100644
--- a/tests/SolutionErp.Infrastructure.Tests/Services/PeTwoStageApprovalTests.cs
+++ b/tests/SolutionErp.Infrastructure.Tests/Services/PeTwoStageApprovalTests.cs
@@ -197,24 +197,41 @@ public class PeTwoStageApprovalTests : IClassFixture
}
[Fact]
- public async Task Reject_Sets_RejectedFromPhase_And_Forces_DangSoanThao()
+ public async Task Reject_To_DangSoanThao_Sets_RejectedFromPhase_TraLai()
{
- // Arrange: PE phase=ChoCCM. Drafter reject.
+ // Session 14: "Trả lại" semantic — target=DangSoanThao + decision=Reject.
+ // Service set RejectedFromPhase + force về DangSoanThao + Drafter resume jump-back.
var actor = await _fx.CreateUserAsync(
$"ccm-{Guid.NewGuid():N}@test", "CCM TPB", _deptCcm, ["DeptManager", "CostControl"]);
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoCCM);
- // Act: reject (target irrelevant — service forces về DangSoanThao).
await _service.TransitionAsync(
- pe, PurchaseEvaluationPhase.TuChoi, actor.Id, ["DeptManager", "CostControl"],
- ApprovalDecision.Reject, "không phù hợp");
+ pe, PurchaseEvaluationPhase.DangSoanThao, actor.Id, ["DeptManager", "CostControl"],
+ ApprovalDecision.Reject, "trả lại Drafter sửa");
- // Assert.
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
fresh.Phase.Should().Be(PurchaseEvaluationPhase.DangSoanThao);
fresh.RejectedFromPhase.Should().Be(PurchaseEvaluationPhase.ChoCCM);
}
+ [Fact]
+ public async Task Reject_To_TuChoi_Locks_Permanently_No_RejectedFromPhase()
+ {
+ // Session 14: "Từ chối" semantic — target=TuChoi + decision=Reject.
+ // Service KHÔNG override target + KHÔNG set RejectedFromPhase (phiếu khoá vĩnh viễn).
+ var actor = await _fx.CreateUserAsync(
+ $"ccm-{Guid.NewGuid():N}@test", "CCM TPB cancel", _deptCcm, ["DeptManager", "CostControl"]);
+ var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoCCM);
+
+ await _service.TransitionAsync(
+ pe, PurchaseEvaluationPhase.TuChoi, actor.Id, ["DeptManager", "CostControl"],
+ ApprovalDecision.Reject, "từ chối hoàn toàn");
+
+ var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
+ fresh.Phase.Should().Be(PurchaseEvaluationPhase.TuChoi);
+ fresh.RejectedFromPhase.Should().BeNull();
+ }
+
[Fact]
public async Task Resume_After_Reject_Jumps_Back_To_RejectedPhase()
{