Refinery wage claims: the claim screen تسديد keeps the legacy close (trips COMPLETE); the grid action only changes the claim status
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
90260edd8e
commit
dc72cf2869
|
|
@ -63,6 +63,11 @@ public sealed class WageClaimsController(IWageClaimService claims) : ControllerB
|
|||
[RequirePermission(PermissionKeys.RefineryWageClaimsClose)]
|
||||
public Task<ClaimResult> Close(long id, CancellationToken ct) => claims.CloseAsync(id, Ip, ct);
|
||||
|
||||
/// <summary>Grid status change NEW -> CLOSE: only the claim record, trips untouched. Same permission as the close.</summary>
|
||||
[HttpPost("{id:long}/status-close")]
|
||||
[RequirePermission(PermissionKeys.RefineryWageClaimsClose)]
|
||||
public Task<ClaimResult> StatusClose(long id, CancellationToken ct) => claims.SetClosedAsync(id, Ip, ct);
|
||||
|
||||
/// <summary>Reopen (CLOSE -> NEW): new action of this system, same permission as the close.</summary>
|
||||
[HttpPost("{id:long}/reopen")]
|
||||
[RequirePermission(PermissionKeys.RefineryWageClaimsClose)]
|
||||
|
|
|
|||
|
|
@ -28,6 +28,9 @@ public interface IWageClaimRepository
|
|||
Task<long> InsertClaimAsync(string createdBy, CancellationToken ct = default);
|
||||
Task<int> AttachTripsAsync(long claimId, IReadOnlyList<string> statuses, DateOnly from, DateOnly to, CancellationToken ct = default);
|
||||
|
||||
Task<IReadOnlyList<string>> ClaimTripStatusesAsync(long claimId, CancellationToken ct = default);
|
||||
/// <summary>TRIP_COMPLETE for every trip of the claim: status COMPLETE plus one REFINERY_TRIPS_LOG row each. Returns the number of trips.</summary>
|
||||
Task<int> CompleteClaimTripsAsync(long claimId, string userLogin, long userId, string? ip, CancellationToken ct = default);
|
||||
/// <summary>STATUS_CODE of a commission claim (null when it does not exist).</summary>
|
||||
Task<string?> CommissionClaimStatusAsync(long commissionClaimId, CancellationToken ct = default);
|
||||
Task SetClaimStatusAsync(long claimId, string status, string userLogin, CancellationToken ct = default);
|
||||
|
|
|
|||
|
|
@ -58,6 +58,13 @@ public static class WageClaimRules
|
|||
if (from.Value > to.Value) throw new BusinessException("FROM_GREATER_THAN_TO", ClaimMessages.FromGreaterThanTo);
|
||||
}
|
||||
|
||||
/// <summary>factory.CLAIM_CLOSE: a claim is paid only when it has trips and every one of them is EXCHANGED.</summary>
|
||||
public static bool CanCloseClaim(IEnumerable<string> tripStatuses)
|
||||
{
|
||||
var distinct = tripStatuses.Distinct().ToList();
|
||||
return distinct.Count == 1 && distinct[0] == "EXCHANGED";
|
||||
}
|
||||
|
||||
/// <summary>factory.TRANSFER: the Jopetrol answer must contain this text (findNoCase) for the trip to move to TRIP_STATUS 7.</summary>
|
||||
public static bool IsStatus7Success(string? serviceAnswer) =>
|
||||
serviceAnswer is not null && serviceAnswer.Contains("Status Updated successfully To 7", StringComparison.OrdinalIgnoreCase);
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ public interface IWageClaimService
|
|||
Task<PagedResult<ClaimLogRowDto>> LogAsync(long id, PageRequest page, CancellationToken ct);
|
||||
Task<ClaimResult> CreateAsync(CreateWageClaimRequest request, string? ip, CancellationToken ct);
|
||||
Task<ClaimResult> CloseAsync(long id, string? ip, CancellationToken ct);
|
||||
Task<ClaimResult> SetClosedAsync(long id, string? ip, CancellationToken ct);
|
||||
Task<ClaimResult> ReopenAsync(long id, string? ip, CancellationToken ct);
|
||||
Task<ClaimResult> TransferAsync(long id, string? ip, CancellationToken ct);
|
||||
Task<ExportFile> ExportAsync(long id, CancellationToken ct);
|
||||
|
|
@ -82,14 +83,32 @@ public sealed class WageClaimService(IWageClaimRepository repo, IUnitOfWork unit
|
|||
return new ClaimResult(ClaimMessages.Success, id);
|
||||
}
|
||||
|
||||
/// <summary>factory.CLAIM_CLOSE (تسديد): NEW claim -> CLOSE (+ log). Only the claim record changes, the trips keep their status.</summary>
|
||||
/// <summary>factory.CLAIM_CLOSE (تسديد, claim screen): NEW claim whose trips are all EXCHANGED -> trips COMPLETE (+ trip log), claim CLOSE (+ log).</summary>
|
||||
public async Task<ClaimResult> CloseAsync(long id, string? ip, CancellationToken ct)
|
||||
{
|
||||
var claim = await GetAsync(id, ct);
|
||||
if (claim.StatusCode != WageClaimRules.New) throw new BusinessException("NO_AUTHORITY", ClaimMessages.NoAuthority);
|
||||
await EnsureCommissionNewAsync(claim, ct);
|
||||
if (!WageClaimRules.CanCloseClaim(await repo.ClaimTripStatusesAsync(id, ct)))
|
||||
throw new BusinessException("CLOSE_CLAIM", ClaimMessages.CloseClaim);
|
||||
|
||||
await unit.ExecuteInTransactionAsync(async c =>
|
||||
{
|
||||
await repo.CompleteClaimTripsAsync(id, user.UserId, user.Id, ip, c);
|
||||
await repo.SetClaimStatusAsync(id, WageClaimRules.Close, user.UserId, c);
|
||||
await repo.AddLogAsync(id, "CLAIM_CLOSE", ClaimLogJson.Status(ClaimMessages.StatusNew), ClaimLogJson.Status(ClaimMessages.StatusClose), user.Id, ip, c);
|
||||
return 0;
|
||||
}, ct);
|
||||
return new ClaimResult(ClaimMessages.Success, id);
|
||||
}
|
||||
|
||||
/// <summary>Grid status change NEW -> CLOSE: only the REFINERY_WAGE_CLAIM record (+ log); the trips keep their status.</summary>
|
||||
public async Task<ClaimResult> SetClosedAsync(long id, string? ip, CancellationToken ct)
|
||||
{
|
||||
var claim = await GetAsync(id, ct);
|
||||
if (claim.StatusCode != WageClaimRules.New) throw new BusinessException("NO_AUTHORITY", ClaimMessages.NoAuthority);
|
||||
await EnsureCommissionNewAsync(claim, ct);
|
||||
|
||||
// only the claim record changes: the trips (permits) keep their status
|
||||
await unit.ExecuteInTransactionAsync(async c =>
|
||||
{
|
||||
await repo.SetClaimStatusAsync(id, WageClaimRules.Close, user.UserId, c);
|
||||
|
|
|
|||
|
|
@ -122,6 +122,27 @@ internal sealed class WageClaimRepository(IDbSession session, ILogger<WageClaimR
|
|||
return ExecuteAsync("UPDATE REFINERY_TRIPS t SET t.WAGE_CLAIM_ID = :ClaimId" + CandidateWhere(p, statuses, from, to), p, ct);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<string>> ClaimTripStatusesAsync(long claimId, CancellationToken ct = default) =>
|
||||
await QueryAsync<string>("SELECT DISTINCT STATUS_CODE FROM REFINERY_TRIPS WHERE WAGE_CLAIM_ID = :ClaimId", new { ClaimId = claimId }, ct);
|
||||
|
||||
public async Task<int> CompleteClaimTripsAsync(long claimId, string userLogin, long userId, string? ip, CancellationToken ct = default)
|
||||
{
|
||||
// logs.saveLog(session, 'REFINERYTRIP', 'TRIP_COMPLETE', ...): old/new status captions, ids from SEQ_REFINERY_TRIPS_LOG
|
||||
await ExecuteAsync("""
|
||||
INSERT INTO REFINERY_TRIPS_LOG (ID, ENTITIY_ID, METHOD_NAME, LOG_DATE, USER_ID, USER_IP, OLD_DATA, NEW_DATA)
|
||||
SELECT SEQ_REFINERY_TRIPS_LOG.NEXTVAL, t.ID, 'TRIP_COMPLETE', SYSDATE, :UserId, :Ip, :OldData, :NewData
|
||||
FROM REFINERY_TRIPS t WHERE t.WAGE_CLAIM_ID = :ClaimId
|
||||
""", new
|
||||
{
|
||||
UserId = userId.ToString(System.Globalization.CultureInfo.InvariantCulture), Ip = Cut(ip, 50), ClaimId = claimId,
|
||||
OldData = ClaimLogJson.Status(ClaimMessages.TripExchanged), NewData = ClaimLogJson.Status(ClaimMessages.TripComplete),
|
||||
}, ct);
|
||||
return await ExecuteAsync("""
|
||||
UPDATE REFINERY_TRIPS SET STATUS_CODE = 'COMPLETE', LAST_UPDATE_DATE = SYSDATE, UPDATED_BY = :UserLogin
|
||||
WHERE WAGE_CLAIM_ID = :ClaimId
|
||||
""", new { UserLogin = userLogin, ClaimId = claimId }, ct);
|
||||
}
|
||||
|
||||
public async Task<string?> CommissionClaimStatusAsync(long commissionClaimId, CancellationToken ct = default) =>
|
||||
(await QueryAsync<string>("SELECT STATUS_CODE FROM REFINERY_COMMISSION_CLAIM WHERE ID = :Id", new { Id = commissionClaimId }, ct)).FirstOrDefault();
|
||||
|
||||
|
|
|
|||
|
|
@ -29,6 +29,15 @@ public class WageClaimRulesTests
|
|||
WageClaimRules.ValidateCreate(ok, from, to);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Claim_closes_only_when_it_has_trips_and_all_are_exchanged()
|
||||
{
|
||||
Assert.True(WageClaimRules.CanCloseClaim(["EXCHANGED", "EXCHANGED"]));
|
||||
Assert.False(WageClaimRules.CanCloseClaim([]));
|
||||
Assert.False(WageClaimRules.CanCloseClaim(["EXCHANGED", "DISCHARGED"]));
|
||||
Assert.False(WageClaimRules.CanCloseClaim(["READY_TO_PAY"]));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Status Updated successfully To 7", true)]
|
||||
[InlineData("OK: status updated SUCCESSFULLY to 7 for 123", true)]
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ public class WageClaimServiceTests
|
|||
public List<TransferTripRow> ToTransfer = [];
|
||||
public int Candidates = 5;
|
||||
public long? CommissionId;
|
||||
public List<string> TripStatuses = ["EXCHANGED"];
|
||||
public string? CommissionStatus = "NEW";
|
||||
public readonly List<string> Writes = [];
|
||||
|
||||
|
|
@ -65,6 +66,8 @@ public class WageClaimServiceTests
|
|||
public Task<int> CountCandidateTripsAsync(IReadOnlyList<string> statuses, DateOnly from, DateOnly to, CancellationToken ct = default) => Task.FromResult(Candidates);
|
||||
public Task<long> InsertClaimAsync(string createdBy, CancellationToken ct = default) { Writes.Add("insert:" + createdBy); return Task.FromResult(9L); }
|
||||
public Task<int> AttachTripsAsync(long claimId, IReadOnlyList<string> statuses, DateOnly from, DateOnly to, CancellationToken ct = default) { Writes.Add("attach:" + claimId + ":" + string.Join('+', statuses)); return Task.FromResult(Candidates); }
|
||||
public Task<IReadOnlyList<string>> ClaimTripStatusesAsync(long claimId, CancellationToken ct = default) => Task.FromResult<IReadOnlyList<string>>(TripStatuses);
|
||||
public Task<int> CompleteClaimTripsAsync(long claimId, string userLogin, long userId, string? ip, CancellationToken ct = default) { Writes.Add("tripsComplete:" + userLogin + ":" + userId); return Task.FromResult(1); }
|
||||
public Task<string?> CommissionClaimStatusAsync(long commissionClaimId, CancellationToken ct = default) => Task.FromResult(CommissionStatus);
|
||||
public Task SetClaimStatusAsync(long claimId, string status, string userLogin, CancellationToken ct = default) { Writes.Add("status:" + status); return Task.CompletedTask; }
|
||||
public Task<IReadOnlyList<TransferTripRow>> TripsToTransferAsync(long claimId, CancellationToken ct = default) => Task.FromResult<IReadOnlyList<TransferTripRow>>(ToTransfer);
|
||||
|
|
@ -98,10 +101,10 @@ public class WageClaimServiceTests
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Close_changes_only_the_claim_and_logs_it()
|
||||
public async Task Grid_status_close_changes_only_the_claim_and_logs_it()
|
||||
{
|
||||
var repo = new FakeRepo();
|
||||
await Service(repo).CloseAsync(5, null, default);
|
||||
await Service(repo).SetClosedAsync(5, null, default);
|
||||
Assert.Equal(["status:CLOSE", "log:CLAIM_CLOSE:{\"STATUS_CODE\":\"جديد\"}:{\"STATUS_CODE\":\"تم التسديد\"}"], repo.Writes);
|
||||
}
|
||||
|
||||
|
|
@ -179,6 +182,23 @@ public class WageClaimServiceTests
|
|||
Assert.Empty(close.Writes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Claim_screen_close_completes_the_trips_and_the_claim()
|
||||
{
|
||||
var repo = new FakeRepo();
|
||||
await Service(repo).CloseAsync(5, null, default);
|
||||
Assert.Equal(["tripsComplete:TESTER:77", "status:CLOSE", "log:CLAIM_CLOSE:{\"STATUS_CODE\":\"جديد\"}:{\"STATUS_CODE\":\"تم التسديد\"}"], repo.Writes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Claim_screen_close_is_refused_while_a_trip_is_not_exchanged()
|
||||
{
|
||||
var repo = new FakeRepo { TripStatuses = ["EXCHANGED", "DISCHARGED"] };
|
||||
var ex = await Assert.ThrowsAsync<BusinessException>(() => Service(repo).CloseAsync(5, null, default));
|
||||
Assert.Contains("جاهز للدفع او تم التفريغ", ex.Message);
|
||||
Assert.Empty(repo.Writes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Close_and_transfer_need_a_new_claim()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ export function useCreateWageClaim() {
|
|||
}
|
||||
|
||||
/** After close / transfer the claim, its trips and its log change. */
|
||||
function useClaimAction(action: 'close' | 'transfer' | 'reopen') {
|
||||
function useClaimAction(action: 'close' | 'transfer' | 'reopen' | 'status-close') {
|
||||
const qc = useQueryClient();
|
||||
return useWrite((id: number) => api.post<ClaimResult>(`${BASE}/${id}/${action}`), {
|
||||
invalidate: [gridKey(GRID_WAGE_CLAIMS)],
|
||||
|
|
@ -57,5 +57,7 @@ function useClaimAction(action: 'close' | 'transfer' | 'reopen') {
|
|||
});
|
||||
}
|
||||
export const useCloseWageClaim = () => useClaimAction('close');
|
||||
/** Grid action: only the claim status changes, the trips are not touched. */
|
||||
export const useSetClosedWageClaim = () => useClaimAction('status-close');
|
||||
export const useReopenWageClaim = () => useClaimAction('reopen');
|
||||
export const useTransferWageClaim =() => useClaimAction('transfer');
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ import { useConfirm } from '@/providers/ConfirmProvider';
|
|||
import { useToast } from '@/providers/ToastProvider';
|
||||
import { downloadFile } from '../../grains-orders/api';
|
||||
import {
|
||||
BASE, useCandidateTrips, useClaimTrips, useCloseWageClaim, useCreateWageClaim, useReopenWageClaim, useTransferWageClaim, useWageClaim,
|
||||
BASE, useCandidateTrips, useClaimTrips, useCloseWageClaim, useCreateWageClaim, useReopenWageClaim, useSetClosedWageClaim, useTransferWageClaim, useWageClaim,
|
||||
} from './api';
|
||||
import { Block, ClaimLogSection, TripsTable } from './components';
|
||||
import { claimableTripStatuses, claimStatuses, tripStatusLabelKey, wageClaimGrid } from './meta';
|
||||
|
|
@ -33,14 +33,14 @@ export function WageClaimsPage() {
|
|||
const { can } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const confirm = useConfirm();
|
||||
const close = useCloseWageClaim();
|
||||
const setClosed = useSetClosedWageClaim();
|
||||
const reopen = useReopenWageClaim();
|
||||
const canChange = can('refinery.wageClaims.close');
|
||||
const meta = useMemo(() => wageClaimGrid((r) => navigate(`${ROOT}/${r.id}`), {
|
||||
canChange,
|
||||
onClose: async (r) => { if (await confirm({ message: t('wc.closeConfirm') })) close.mutate(r.id); },
|
||||
onClose: async (r) => { if (await confirm({ message: t('wc.statusCloseConfirm') })) setClosed.mutate(r.id); },
|
||||
onReopen: async (r) => { if (await confirm({ message: t('wc.reopenConfirm') })) reopen.mutate(r.id); },
|
||||
}), [navigate, canChange, confirm, close, reopen, t]);
|
||||
}), [navigate, canChange, confirm, setClosed, reopen, t]);
|
||||
return (
|
||||
<>
|
||||
<PageHeader
|
||||
|
|
|
|||
|
|
@ -73,7 +73,8 @@ export default defineStrings({
|
|||
'wc.reopenConfirm': { en: 'Return this claim to New?', ar: 'هل تريد اعادة هذه المطالبة الى حالة جديد؟' },
|
||||
'wc.log.CLAIM_REOPEN': { en: 'Reopen', ar: 'اعادة فتح' },
|
||||
'wc.log.prop.STATUS_CODE': { en: 'Status', ar: 'الحالة' },
|
||||
'wc.closeConfirm': { en: 'Do you want to mark this claim as paid?', ar: 'هل تريد تسديد هذه المطالبة؟' }, // new
|
||||
'wc.closeConfirm': { en: 'Do you want to mark this claim as paid? Its trips become complete.', ar: 'هل تريد تسديد هذه المطالبة؟ ستصبح ارسالياتها مكتملة.' }, // new
|
||||
'wc.statusCloseConfirm': { en: 'Change the status of this claim to Paid?', ar: 'هل تريد تغيير حالة هذه المطالبة الى تم التسديد؟' },
|
||||
'wc.transfer': { en: 'Transfer', ar: 'ترحيل' }, // REFINERY_WAGE_CLAIM.TRANSFER
|
||||
'wc.transferConfirm': { en: 'Send this claim to the refinery (Jopetrol)?', ar: 'هل تريد ترحيل هذه المطالبة إلى المصفاة؟' }, // new
|
||||
'wc.report': { en: 'Report', ar: 'تقرير' }, // PUBLIC.REPORT
|
||||
|
|
|
|||
Loading…
Reference in New Issue