[CLAUDE] PE workflow 3-button Duyệt/Trả lại/Từ chối (Task 4)
All checks were successful
Deploy SOLUTION_ERP / build-deploy (push) Successful in 3m6s

User chỉ thị thay 2-button hiện tại bằng 3 hành động rõ ràng:
- Duyệt = forward phase tiếp theo
- Trả lại = về DangSoanThao + Drafter sửa → workflow tự jump tới phase
  đã reject (smart reject Mig 16 pattern + clear N-stage rows)
- Từ chối = phiếu khoá hoàn toàn (Phase=TuChoi → 17 handler Mig 16 lock
  edit). Drafter phải tạo phiếu mới.

Domain (PurchaseEvaluationPolicy.cs):
- NccOnly + NccWithPlan: thêm (X → TuChoi) transition cho mọi phase
  trung gian (ChoPurchasing/ChoCCM/ChoCEODuyetNCC/ChoDuAn/ChoCEODuyetPA)
  với roles của phase đó. Trước đây chỉ DangSoanThao → TuChoi (Drafter).
- FromDefinition expand: mỗi step (trừ DangSoanThao) thêm
  (step.Phase → TuChoi) với roles của step.

Service (PurchaseEvaluationWorkflowService.cs):
- Reject branch tách 2 case:
  * target=TuChoi → giữ nguyên (KHÔNG override + KHÔNG set
    RejectedFromPhase + KHÔNG clear N-stage rows). Phiếu khoá vĩnh viễn.
  * target khác (thường DangSoanThao) → smart reject (set
    RejectedFromPhase + force DangSoanThao + clear N-stage rows).

FE (PeWorkflowPanel.tsx, fe-admin + fe-user mirror):
- next.phases render 3 button rõ ràng:
  * "✓ Duyệt → <label>" brand (forward)
  * "← Trả lại (về Drafter sửa)" red (target=DangSoanThao + isSendBack)
  * "✗ Hủy / Từ chối" red (target=TuChoi)
- Decision logic: target=TuChoi || isSendBack → Reject (2), else Approve (1)
- Dialog confirm:
  * Title rõ theo loại hành động
  * Cancel case: warning red "Phiếu sẽ bị khoá hoàn toàn"
  * SendBack case: hint amber "Phiếu sẽ về Đang soạn thảo, Drafter sửa
    rồi trình lại — workflow tự jump tới phase này"

Tests update + add 1 test mới:
- Reject_Sets_RejectedFromPhase_And_Forces_DangSoanThao →
  Reject_To_DangSoanThao_Sets_RejectedFromPhase_TraLai (rename + change
  target từ TuChoi → DangSoanThao để test Trả lại pattern)
- + Reject_To_TuChoi_Locks_Permanently_No_RejectedFromPhase (NEW test
  Từ chối — phase=TuChoi + RejectedFromPhase null)
- NStage_Reject_Clears_InnerStep_Rows_At_Phase: target TuChoi →
  DangSoanThao (test Trả lại + clear N-stage rows pattern)

Verify:
- dotnet build 0 error
- dotnet test 95 → **96 pass** (+1 test mới Từ chối)
- npm build fe-admin + fe-user pass

Pending Task 2: Sample data seed N-stage.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
pqhuy1987
2026-05-07 19:25:44 +07:00
parent b06bdce694
commit 0d776987e4
6 changed files with 214 additions and 87 deletions

View File

@ -45,12 +45,20 @@ export function PeWorkflowPanel({
}) })
const transition = useMutation({ const transition = useMutation({
mutationFn: async () => mutationFn: async () => {
api.post(`/purchase-evaluations/${evaluation.id}/transitions`, { // 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, targetPhase: target,
decision: target === PurchaseEvaluationPhase.TuChoi ? 2 : 1, decision: isReject ? 2 : 1,
comment: comment || null, comment: comment || null,
}), })
},
onSuccess: () => { onSuccess: () => {
toast.success('Đã chuyển phase.') toast.success('Đã chuyển phase.')
qc.invalidateQueries({ queryKey: ['pe-detail', evaluation.id] }) qc.invalidateQueries({ queryKey: ['pe-detail', evaluation.id] })
@ -101,22 +109,37 @@ export function PeWorkflowPanel({
{next.length > 0 && !readOnly && ( {next.length > 0 && !readOnly && (
<div> <div>
<Label className="text-xs">Chuyển tiếp:</Label> <Label className="text-xs">Hành đng:</Label>
<div className="mt-1 flex flex-wrap gap-1.5"> <div className="mt-1 flex flex-wrap gap-1.5">
{next.map(p => ( {next.map(p => {
<button // Phân loại button theo hành động:
key={p} // - Trả lại = về DangSoanThao (từ phase trung gian) — red
onClick={() => setTarget(p)} // - Hủy/Từ chối = TuChoi (chỉ ở phase DangSoanThao đầu) — red
className={cn( // - Duyệt = forward phase tiếp theo — brand
'rounded border px-2 py-1 text-[11px] transition', const isSendBack = p === PurchaseEvaluationPhase.DangSoanThao
p === PurchaseEvaluationPhase.TuChoi && evaluation.phase !== PurchaseEvaluationPhase.DangSoanThao
? 'border-red-200 text-red-700 hover:bg-red-50' const isCancel = p === PurchaseEvaluationPhase.TuChoi
: 'border-brand-300 text-brand-700 hover:bg-brand-50', const isDanger = isSendBack || isCancel
)} const label = isSendBack
> ? '← Trả lại (về Drafter sửa)'
{PurchaseEvaluationPhaseLabel[p]} : isCancel
</button> ? '✗ Hủy / Từ chối'
))} : `✓ Duyệt → ${PurchaseEvaluationPhaseLabel[p]}`
return (
<button
key={p}
onClick={() => setTarget(p)}
className={cn(
'rounded border px-2 py-1 text-[11px] font-medium transition',
isDanger
? 'border-red-200 text-red-700 hover:bg-red-50'
: 'border-brand-300 text-brand-700 hover:bg-brand-50',
)}
>
{label}
</button>
)
})}
</div> </div>
</div> </div>
)} )}
@ -126,20 +149,40 @@ export function PeWorkflowPanel({
</div> </div>
)} )}
{target !== null && ( {target !== null && (() => {
<Dialog const isCancel = target === PurchaseEvaluationPhase.TuChoi
open const isSendBack = target === PurchaseEvaluationPhase.DangSoanThao
onClose={() => setTarget(null)} && evaluation.phase !== PurchaseEvaluationPhase.DangSoanThao
title={`Chuyển → ${PurchaseEvaluationPhaseLabel[target]}`} const dialogTitle = isCancel
footer={<> ? '✗ Từ chối phiếu (khoá hoàn toàn)'
<Button variant="ghost" onClick={() => setTarget(null)}>Hủy</Button> : isSendBack
<Button onClick={() => transition.mutate()} disabled={transition.isPending}>Xác nhận</Button> ? '← Trả lại Drafter sửa'
</>} : `✓ Duyệt → ${PurchaseEvaluationPhaseLabel[target]}`
> return (
<Label>Ghi chú (tùy chọn)</Label> <Dialog
<Textarea value={comment} onChange={e => setComment(e.target.value)} rows={3} /> open
</Dialog> onClose={() => setTarget(null)}
)} title={dialogTitle}
footer={<>
<Button variant="ghost" onClick={() => setTarget(null)}>Hủy</Button>
<Button onClick={() => transition.mutate()} disabled={transition.isPending}>Xác nhận</Button>
</>}
>
{isCancel && (
<div className="mb-3 rounded border border-red-200 bg-red-50 px-3 py-2 text-[11px] text-red-800">
Phiếu sẽ bị khoá hoàn toàn (không edit/transition đưc nữa). Drafter cần tạo phiếu mới nếu muốn làm lại.
</div>
)}
{isSendBack && (
<div className="mb-3 rounded border border-amber-200 bg-amber-50 px-3 py-2 text-[11px] text-amber-800">
Phiếu sẽ về &ldquo;Đang soạn thảo&rdquo;. Drafter thể sửa rồi trình lại workflow tự jump tới phase này.
</div>
)}
<Label>Ghi chú (tùy chọn)</Label>
<Textarea value={comment} onChange={e => setComment(e.target.value)} rows={3} />
</Dialog>
)
})()}
{deptApprovals.length > 0 && ( {deptApprovals.length > 0 && (
<div className="border-t border-slate-200 pt-4"> <div className="border-t border-slate-200 pt-4">

View File

@ -45,12 +45,20 @@ export function PeWorkflowPanel({
}) })
const transition = useMutation({ const transition = useMutation({
mutationFn: async () => mutationFn: async () => {
api.post(`/purchase-evaluations/${evaluation.id}/transitions`, { // 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, targetPhase: target,
decision: target === PurchaseEvaluationPhase.TuChoi ? 2 : 1, decision: isReject ? 2 : 1,
comment: comment || null, comment: comment || null,
}), })
},
onSuccess: () => { onSuccess: () => {
toast.success('Đã chuyển phase.') toast.success('Đã chuyển phase.')
qc.invalidateQueries({ queryKey: ['pe-detail', evaluation.id] }) qc.invalidateQueries({ queryKey: ['pe-detail', evaluation.id] })
@ -101,22 +109,37 @@ export function PeWorkflowPanel({
{next.length > 0 && !readOnly && ( {next.length > 0 && !readOnly && (
<div> <div>
<Label className="text-xs">Chuyển tiếp:</Label> <Label className="text-xs">Hành đng:</Label>
<div className="mt-1 flex flex-wrap gap-1.5"> <div className="mt-1 flex flex-wrap gap-1.5">
{next.map(p => ( {next.map(p => {
<button // Phân loại button theo hành động:
key={p} // - Trả lại = về DangSoanThao (từ phase trung gian) — red
onClick={() => setTarget(p)} // - Hủy/Từ chối = TuChoi (chỉ ở phase DangSoanThao đầu) — red
className={cn( // - Duyệt = forward phase tiếp theo — brand
'rounded border px-2 py-1 text-[11px] transition', const isSendBack = p === PurchaseEvaluationPhase.DangSoanThao
p === PurchaseEvaluationPhase.TuChoi && evaluation.phase !== PurchaseEvaluationPhase.DangSoanThao
? 'border-red-200 text-red-700 hover:bg-red-50' const isCancel = p === PurchaseEvaluationPhase.TuChoi
: 'border-brand-300 text-brand-700 hover:bg-brand-50', const isDanger = isSendBack || isCancel
)} const label = isSendBack
> ? '← Trả lại (về Drafter sửa)'
{PurchaseEvaluationPhaseLabel[p]} : isCancel
</button> ? '✗ Hủy / Từ chối'
))} : `✓ Duyệt → ${PurchaseEvaluationPhaseLabel[p]}`
return (
<button
key={p}
onClick={() => setTarget(p)}
className={cn(
'rounded border px-2 py-1 text-[11px] font-medium transition',
isDanger
? 'border-red-200 text-red-700 hover:bg-red-50'
: 'border-brand-300 text-brand-700 hover:bg-brand-50',
)}
>
{label}
</button>
)
})}
</div> </div>
</div> </div>
)} )}
@ -126,20 +149,40 @@ export function PeWorkflowPanel({
</div> </div>
)} )}
{target !== null && ( {target !== null && (() => {
<Dialog const isCancel = target === PurchaseEvaluationPhase.TuChoi
open const isSendBack = target === PurchaseEvaluationPhase.DangSoanThao
onClose={() => setTarget(null)} && evaluation.phase !== PurchaseEvaluationPhase.DangSoanThao
title={`Chuyển → ${PurchaseEvaluationPhaseLabel[target]}`} const dialogTitle = isCancel
footer={<> ? '✗ Từ chối phiếu (khoá hoàn toàn)'
<Button variant="ghost" onClick={() => setTarget(null)}>Hủy</Button> : isSendBack
<Button onClick={() => transition.mutate()} disabled={transition.isPending}>Xác nhận</Button> ? '← Trả lại Drafter sửa'
</>} : `✓ Duyệt → ${PurchaseEvaluationPhaseLabel[target]}`
> return (
<Label>Ghi chú (tùy chọn)</Label> <Dialog
<Textarea value={comment} onChange={e => setComment(e.target.value)} rows={3} /> open
</Dialog> onClose={() => setTarget(null)}
)} title={dialogTitle}
footer={<>
<Button variant="ghost" onClick={() => setTarget(null)}>Hủy</Button>
<Button onClick={() => transition.mutate()} disabled={transition.isPending}>Xác nhận</Button>
</>}
>
{isCancel && (
<div className="mb-3 rounded border border-red-200 bg-red-50 px-3 py-2 text-[11px] text-red-800">
Phiếu sẽ bị khoá hoàn toàn (không edit/transition đưc nữa). Drafter cần tạo phiếu mới nếu muốn làm lại.
</div>
)}
{isSendBack && (
<div className="mb-3 rounded border border-amber-200 bg-amber-50 px-3 py-2 text-[11px] text-amber-800">
Phiếu sẽ về &ldquo;Đang soạn thảo&rdquo;. Drafter thể sửa rồi trình lại workflow tự jump tới phase này.
</div>
)}
<Label>Ghi chú (tùy chọn)</Label>
<Textarea value={comment} onChange={e => setComment(e.target.value)} rows={3} />
</Dialog>
)
})()}
{deptApprovals.length > 0 && ( {deptApprovals.length > 0 && (
<div className="border-t border-slate-200 pt-4"> <div className="border-t border-slate-200 pt-4">

View File

@ -56,14 +56,20 @@ public static class PurchaseEvaluationPolicies
[(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.ChoPurchasing)] = [AppRoles.Drafter, AppRoles.DeptManager], [(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.ChoPurchasing)] = [AppRoles.Drafter, AppRoles.DeptManager],
[(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.TuChoi)] = [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.ChoCCM)] = [AppRoles.Procurement],
[(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Procurement], [(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Procurement],
[(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.Procurement],
[(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.ChoCEODuyetNCC)] = [AppRoles.CostControl], [(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.ChoCEODuyetNCC)] = [AppRoles.CostControl],
[(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.DangSoanThao)] = [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.DaDuyet)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
[(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Director, AppRoles.AuthorizedSigner], [(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
[(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
}, },
PhaseSla: DefaultSla, PhaseSla: DefaultSla,
ActivePhases: ActivePhases:
@ -85,20 +91,26 @@ public static class PurchaseEvaluationPolicies
[(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.ChoPurchasing)] = [AppRoles.Drafter, AppRoles.DeptManager], [(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.ChoPurchasing)] = [AppRoles.Drafter, AppRoles.DeptManager],
[(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.TuChoi)] = [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.ChoDuAn)] = [AppRoles.Procurement],
[(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Procurement], [(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Procurement],
[(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.Procurement],
[(PurchaseEvaluationPhase.ChoDuAn, PurchaseEvaluationPhase.ChoCCM)] = [AppRoles.ProjectManager], [(PurchaseEvaluationPhase.ChoDuAn, PurchaseEvaluationPhase.ChoCCM)] = [AppRoles.ProjectManager],
[(PurchaseEvaluationPhase.ChoDuAn, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.ProjectManager], [(PurchaseEvaluationPhase.ChoDuAn, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.ProjectManager],
[(PurchaseEvaluationPhase.ChoDuAn, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.ProjectManager],
[(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.ChoCEODuyetPA)] = [AppRoles.CostControl], [(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.ChoCEODuyetPA)] = [AppRoles.CostControl],
[(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.DangSoanThao)] = [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.ChoCEODuyetNCC)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
[(PurchaseEvaluationPhase.ChoCEODuyetPA, PurchaseEvaluationPhase.DangSoanThao)] = [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.DaDuyet)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
[(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Director, AppRoles.AuthorizedSigner], [(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.DangSoanThao)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
[(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.TuChoi)] = [AppRoles.Director, AppRoles.AuthorizedSigner],
}, },
PhaseSla: DefaultSla, PhaseSla: DefaultSla,
ActivePhases: ActivePhases:
@ -171,17 +183,21 @@ public static class PurchaseEvaluationPolicyRegistry
transitions[(prev.Value, s.Phase)] = roles; transitions[(prev.Value, s.Phase)] = roles;
if (userIds.Length > 0) userTransitions[(prev.Value, s.Phase)] = userIds; 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) if (prev.Value != PurchaseEvaluationPhase.DangSoanThao && s.Phase != PurchaseEvaluationPhase.DangSoanThao)
{ {
transitions.TryAdd((s.Phase, PurchaseEvaluationPhase.DangSoanThao), roles); transitions.TryAdd((s.Phase, PurchaseEvaluationPhase.DangSoanThao), roles);
transitions.TryAdd((s.Phase, PurchaseEvaluationPhase.TuChoi), roles);
if (userIds.Length > 0) if (userIds.Length > 0)
{
userTransitions.TryAdd((s.Phase, PurchaseEvaluationPhase.DangSoanThao), userIds); userTransitions.TryAdd((s.Phase, PurchaseEvaluationPhase.DangSoanThao), userIds);
userTransitions.TryAdd((s.Phase, PurchaseEvaluationPhase.TuChoi), userIds);
}
} }
} }
prev = s.Phase; prev = s.Phase;
} }
// First step có thể reject to TuChoi // First step (DangSoanThao) — Drafter có thể TuChoi (huỷ phiếu)
if (steps.Count > 0) if (steps.Count > 0)
transitions.TryAdd((steps[0].Phase, PurchaseEvaluationPhase.TuChoi), transitions.TryAdd((steps[0].Phase, PurchaseEvaluationPhase.TuChoi),
[AppRoles.Drafter, AppRoles.DeptManager]); [AppRoles.Drafter, AppRoles.DeptManager]);

View File

@ -43,17 +43,25 @@ public class PurchaseEvaluationWorkflowService(
if (decision == ApprovalDecision.Reject) if (decision == ApprovalDecision.Reject)
{ {
evaluation.RejectedFromPhase = fromPhase; // 2 loại Reject (Session 14):
targetPhase = PurchaseEvaluationPhase.DangSoanThao; // - 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 var staleNStageRows = await db.PurchaseEvaluationDepartmentApprovals
// fromPhase. User resume sẽ approve lại từ inner step đầu. .Where(a => a.PurchaseEvaluationId == evaluation.Id
var staleNStageRows = await db.PurchaseEvaluationDepartmentApprovals && a.PhaseAtApproval == (int)fromPhase
.Where(a => a.PurchaseEvaluationId == evaluation.Id && a.InnerStepId != null)
&& a.PhaseAtApproval == (int)fromPhase .ToListAsync(ct);
&& a.InnerStepId != null) foreach (var r in staleNStageRows) db.PurchaseEvaluationDepartmentApprovals.Remove(r);
.ToListAsync(ct); }
foreach (var r in staleNStageRows) db.PurchaseEvaluationDepartmentApprovals.Remove(r);
} }
else if (isResumingAfterReject) else if (isResumingAfterReject)
{ {

View File

@ -278,11 +278,11 @@ public class PeNStageApprovalTests : IClassFixture<IdentityFixture>
pe = await _db.PurchaseEvaluations.FirstAsync(x => x.Id == pe.Id); 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"]); var admin = await _fx.CreateUserAsync($"adm-rej-{Guid.NewGuid():N}@test", "Admin", null, ["Admin"]);
await _service.TransitionAsync( await _service.TransitionAsync(
pe, PurchaseEvaluationPhase.TuChoi, admin.Id, ["Admin"], pe, PurchaseEvaluationPhase.DangSoanThao, admin.Id, ["Admin"],
ApprovalDecision.Reject, "reject test"); ApprovalDecision.Reject, "trả lại test");
// Assert: phase = DangSoanThao, RejectedFromPhase = ChoPurchasing, // Assert: phase = DangSoanThao, RejectedFromPhase = ChoPurchasing,
// N-stage rows tại ChoPurchasing đã clear. // N-stage rows tại ChoPurchasing đã clear.

View File

@ -197,24 +197,41 @@ public class PeTwoStageApprovalTests : IClassFixture<IdentityFixture>
} }
[Fact] [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( var actor = await _fx.CreateUserAsync(
$"ccm-{Guid.NewGuid():N}@test", "CCM TPB", _deptCcm, ["DeptManager", "CostControl"]); $"ccm-{Guid.NewGuid():N}@test", "CCM TPB", _deptCcm, ["DeptManager", "CostControl"]);
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoCCM); var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoCCM);
// Act: reject (target irrelevant — service forces về DangSoanThao).
await _service.TransitionAsync( await _service.TransitionAsync(
pe, PurchaseEvaluationPhase.TuChoi, actor.Id, ["DeptManager", "CostControl"], pe, PurchaseEvaluationPhase.DangSoanThao, actor.Id, ["DeptManager", "CostControl"],
ApprovalDecision.Reject, "không phù hợp"); ApprovalDecision.Reject, "trả lại Drafter sửa");
// Assert.
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id); var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
fresh.Phase.Should().Be(PurchaseEvaluationPhase.DangSoanThao); fresh.Phase.Should().Be(PurchaseEvaluationPhase.DangSoanThao);
fresh.RejectedFromPhase.Should().Be(PurchaseEvaluationPhase.ChoCCM); 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] [Fact]
public async Task Resume_After_Reject_Jumps_Back_To_RejectedPhase() public async Task Resume_After_Reject_Jumps_Back_To_RejectedPhase()
{ {