454 lines
26 KiB
C#
454 lines
26 KiB
C#
using FluentValidation;
|
|
using NTP.Application.Abstractions;
|
|
using NTP.Application.Access;
|
|
using NTP.Application.Common;
|
|
using NTP.Application.Grains.Reports;
|
|
using NTP.Application.Receipts;
|
|
|
|
namespace NTP.Tests.Receipts;
|
|
|
|
public class ReceiptRulesTests
|
|
{
|
|
[Theory]
|
|
[InlineData(327, "CRG", "IMPT", "10184", 1L, 1L, 1)] // double move, import, supply agent
|
|
[InlineData(327, "CRG", "FSEZ", "555", 1L, 1L, 2)] // double move, import, other agent
|
|
[InlineData(327, "", "EXPT", "555", 1L, 1L, 3)] // single unloading
|
|
[InlineData(327, "", "IMPT", "555", 1L, 1L, 4)] // single loading, not supply
|
|
[InlineData(327, "", "IMPT", "10184", 86L, 20L, 5)] // facilities, supply
|
|
[InlineData(327, "", "IMPT", "555", 86L, 20L, 6)] // facilities, not supply
|
|
[InlineData(327, "", "IMPT", "10184", 1L, 1L, 7)] // single loading, supply
|
|
[InlineData(366, "CRG", "IMPT", "0", 1L, 1L, 10)]
|
|
[InlineData(366, "", "IMPT", "0", 1L, 1L, 9)]
|
|
public void Receipt_type_follows_the_legacy_decision_order(long scac, string fm, string inbound, string agent, long origin, long dest, int expected) =>
|
|
Assert.Equal(expected, ReceiptRules.TypeOf(scac, fm, inbound, agent, origin, dest));
|
|
|
|
[Fact]
|
|
public void Unknown_company_has_no_type() => Assert.Null(ReceiptRules.TypeOf(999, "", "IMPT", "0", 1, 1));
|
|
|
|
[Fact]
|
|
public void Status_rules()
|
|
{
|
|
Assert.True(ReceiptRules.CanConfirm("NOT_RECEIVED"));
|
|
Assert.False(ReceiptRules.CanConfirm("RECEIVED"));
|
|
Assert.True(ReceiptRules.CanPay("UNPAID"));
|
|
Assert.True(ReceiptRules.CanUpdate("RECEIVED") && ReceiptRules.CanUpdate("UNPAID"));
|
|
Assert.False(ReceiptRules.CanUpdate("AUDITED"));
|
|
Assert.True(ReceiptRules.CanPrint("AUDITED") && ReceiptRules.CanPrint("PAID"));
|
|
Assert.False(ReceiptRules.CanPrint("RVKD"));
|
|
Assert.True(ReceiptRules.CanRevokeAudited("AUDITED"));
|
|
Assert.True(ReceiptRules.CanDecideRevoke("PENDING_RVKD"));
|
|
Assert.Equal("RECEIVED", ReceiptRules.StatusForNew(0));
|
|
Assert.Equal("UNPAID", ReceiptRules.StatusForNew(1));
|
|
Assert.True(ReceiptRules.IsDoubleMove(1) && ReceiptRules.IsDoubleMove(2) && ReceiptRules.IsDoubleMove(10));
|
|
Assert.False(ReceiptRules.IsDoubleMove(3));
|
|
}
|
|
|
|
[Fact]
|
|
public void Log_json_keeps_the_translated_status()
|
|
{
|
|
var (o, n) = ReceiptRules.StatusLog("NOT_RECEIVED", "RECEIVED");
|
|
Assert.Equal("{\"STATUSCODE\":\"غير مستلم\"}", o);
|
|
Assert.Equal("{\"STATUSCODE\":\"تم الاستلام\"}", n);
|
|
Assert.Equal("{\"STATUSCODE\":\"تم الاستلام\",\"NOTES\":\"\"}", ReceiptRules.CreateLog("RECEIVED").New);
|
|
}
|
|
}
|
|
|
|
public class ReceiptAuditTests
|
|
{
|
|
private static AuditLookups Lookups() => new()
|
|
{
|
|
CargoCodeByName = new Dictionary<string, string> { ["بضائع عامة"] = "CRG", ["تموين"] = "FUEL" },
|
|
OperationCodeByCaption = new Dictionary<string, string> { ["استيراد"] = "IMPT", ["تصدير"] = "EXPT" },
|
|
LocationIdByShortName = new Dictionary<string, long> { ["AQ"] = 1, ["GATE"] = 20 },
|
|
Types = [new ReceiptTypeRow { Id = 3, Fees = 6, Scac = 327 }, new ReceiptTypeRow { Id = 7, Fees = 0, Scac = 327 }],
|
|
};
|
|
|
|
private static AuditFileRow File_() => new()
|
|
{
|
|
Scac = "327", Nnpn = "9801", InboundTypeCode = "تصدير", ShipmentMode = "بضائع عامة", PermitStatus = "مكتمل", Pn = "1001", Tn = "123", Trn = "456",
|
|
OriginAddressId = "AQ", DestinationAddressId = "GATE", CaCode = "555",
|
|
};
|
|
|
|
private static ReceiptRow Receipt() => new()
|
|
{
|
|
Id = 1, ReceiptNo = 202000001, Pn = 1001, Scac = 327, Tn = "123", Trn = "456", Nnpn = "9801", ShipmentMode = "CRG", InboundTypeCode = "EXPT", OriginAddressId = 1,
|
|
DestinationAddressId = 20, CaCode = 555, Fees = 6, ReceiptType = 3, StatusCode = "RECEIVED",
|
|
};
|
|
|
|
[Fact]
|
|
public void A_receipt_equal_to_the_file_has_no_remarks() => Assert.Equal(string.Empty, ReceiptAudit.ValidRecordInfo(File_(), Receipt(), Lookups()));
|
|
|
|
[Fact]
|
|
public void Differences_are_listed_in_arabic()
|
|
{
|
|
var f = File_();
|
|
f.Tn = "999";
|
|
var r = Receipt();
|
|
r.Fees = 10;
|
|
var text = ReceiptAudit.ValidRecordInfo(f, r, Lookups());
|
|
Assert.Contains("رقم الشاحنة غير مطابق", text);
|
|
Assert.Contains("القيمة المالية غير متساوية", text);
|
|
Assert.DoesNotContain("رقم المقطورة", text);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_cargo_type_that_is_not_in_the_system_stops_the_fee_check()
|
|
{
|
|
var f = File_();
|
|
f.ShipmentMode = "غير معروف";
|
|
var r = Receipt();
|
|
r.Fees = 99;
|
|
var text = ReceiptAudit.ValidRecordInfo(f, r, Lookups());
|
|
Assert.Contains("نمط الحركة الاولى غير موجود", text);
|
|
Assert.DoesNotContain("القيمة المالية", text);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_cancelled_permit_with_an_employee_mistake_should_have_no_receipt()
|
|
{
|
|
var f = File_();
|
|
f.PermitStatus = ReceiptAudit.Cancelled;
|
|
f.IsEmployeeMistake = ReceiptAudit.Yes;
|
|
Assert.Contains("ملغي بسبب خطأ موظف", ReceiptAudit.ValidRecordInfo(f, Receipt(), Lookups()));
|
|
}
|
|
|
|
[Fact]
|
|
public void Compare_classifies_missing_single_duplicate_and_orphan_receipts()
|
|
{
|
|
var file = new List<AuditFileRow>
|
|
{
|
|
File_(), // 1001: one matching receipt
|
|
new() { Pn = "2002", PermitStatus = "مكتمل", Scac = "327", ShipmentMode = "بضائع عامة" }, // 2002: no receipt
|
|
new() { Pn = "3003", PermitStatus = "مكتمل" }, // 3003: two receipts
|
|
new() { Pn = "4004", PermitStatus = "مرفوض" }, // rejected permit: skipped
|
|
};
|
|
var dup1 = Receipt(); dup1.Id = 11; dup1.Pn = 3003;
|
|
var dup2 = Receipt(); dup2.Id = 12; dup2.Pn = 3003;
|
|
var orphan = Receipt(); orphan.Id = 13; orphan.Pn = 9999; orphan.Credit = 1; orphan.ConfirmationBy = "USER1";
|
|
var receipts = new List<ReceiptRow> { Receipt(), dup1, dup2, orphan };
|
|
|
|
var (lines, audited, identical) = ReceiptAudit.Compare(file, receipts, Lookups(), "user1", ReceiptRules.StatusCaption);
|
|
|
|
Assert.Equal(1, identical.Count); // only 1001 is identical
|
|
Assert.Equal(1L, identical[0]);
|
|
Assert.Contains(audited, a => a.Id == 1 && a.Result == "يوجد سند قبض" && a.Remarks == "");
|
|
Assert.Contains(audited, a => a.Id == 11 && a.Result == "سند قبض مكرر");
|
|
Assert.Contains(audited, a => a.Id == 12 && a.Result == "سند قبض مكرر");
|
|
Assert.Contains(audited, a => a.Id == 13 && a.Result == "لا يوجد تصريح للمطالبة المالية");
|
|
Assert.Contains(lines, l => l.Pn == "2002" && l.Result == "لا يوجد وصل");
|
|
Assert.DoesNotContain(lines, l => l.Pn == "4004");
|
|
Assert.Equal(1 + 1 + 1 + 2 + 1, lines.Count); // header + 1001 + 2002 + 2 duplicates + orphan
|
|
}
|
|
|
|
[Fact]
|
|
public void A_receipt_confirmed_by_another_user_is_not_an_orphan_of_this_audit()
|
|
{
|
|
var orphan = Receipt(); orphan.Id = 13; orphan.Pn = 9999; orphan.ConfirmationBy = "OTHER";
|
|
var (_, audited, _) = ReceiptAudit.Compare([File_()], [Receipt(), orphan], Lookups(), "user1", ReceiptRules.StatusCaption);
|
|
Assert.DoesNotContain(audited, a => a.Id == 13);
|
|
}
|
|
}
|
|
|
|
public class ReceiptServiceTests
|
|
{
|
|
private sealed class FakeUnit : IUnitOfWork
|
|
{
|
|
public Task BeginAsync(CancellationToken ct = default) => Task.CompletedTask;
|
|
public Task CommitAsync(CancellationToken ct = default) => Task.CompletedTask;
|
|
public Task RollbackAsync(CancellationToken ct = default) => Task.CompletedTask;
|
|
public Task<T> ExecuteInTransactionAsync<T>(Func<CancellationToken, Task<T>> work, CancellationToken ct = default) => work(ct);
|
|
}
|
|
|
|
private sealed class FakeUser(string role) : ICurrentUser
|
|
{
|
|
public bool IsAuthenticated => true;
|
|
public long Id => 5;
|
|
public string UserId => "TS1";
|
|
public string Name => "t";
|
|
public string RoleCode => role;
|
|
public string RoleName => role;
|
|
public string Domain => "ASEZA";
|
|
public long? LocationId => null;
|
|
public string LocationName => "";
|
|
public long? StkId => null;
|
|
public long? CompId => null;
|
|
public long? CompanyId => null;
|
|
public string CompanyName => "";
|
|
public string CompanyCode => "";
|
|
public string EmpNo => "";
|
|
public string Lang => "AR";
|
|
public bool IsStaff => false;
|
|
public bool IsInRole(string roleCode) => string.Equals(role, roleCode, StringComparison.OrdinalIgnoreCase);
|
|
public bool IsInAnyRole(params string[] roleCodes) => roleCodes.Any(IsInRole);
|
|
}
|
|
|
|
private sealed class FakePermissions(params string[] keys) : IPermissionService
|
|
{
|
|
public Task<PermissionsDto> GetForCurrentUserAsync(CancellationToken ct = default) => throw new NotSupportedException();
|
|
public Task<bool> IsAllowedAsync(string resourceKey, CancellationToken ct = default) => Task.FromResult(keys.Contains(resourceKey));
|
|
}
|
|
|
|
private sealed class FakeExcel : IReportExcelService
|
|
{
|
|
public List<string[]> Sheet = [];
|
|
public ReportTable? Last;
|
|
public byte[] Build(ReportTable table, string printedBy, DateTime printedAt, bool arabic) { Last = table; return [1]; }
|
|
public List<string[]> ReadFirstSheet(Stream stream) => Sheet;
|
|
}
|
|
|
|
private sealed class FakeRepo : IReceiptRepository
|
|
{
|
|
public ReceiptRow? Row;
|
|
public List<ReceiptRow> AuditRows = [];
|
|
public readonly List<string> Writes = [];
|
|
public bool Stale;
|
|
public bool UserExists = true;
|
|
public bool PermitOk = true;
|
|
|
|
public Task<PagedResult<ReceiptRow>> SearchAsync(ReceiptQuery query, PageRequest page, CancellationToken ct = default) =>
|
|
Task.FromResult(new PagedResult<ReceiptRow>(Row is null ? [] : [Row], 1, 16, Row is null ? 0 : 1));
|
|
public Task<ReceiptRow?> GetAsync(long id, CancellationToken ct = default) => Task.FromResult(Row);
|
|
public Task<PagedResult<ReceiptLogRow>> GetLogAsync(long id, PageRequest page, CancellationToken ct = default) => throw new NotSupportedException();
|
|
public Task<IReadOnlyList<ReceiptTypeRow>> GetTypesAsync(long? scac, long? exceptId, CancellationToken ct = default) =>
|
|
Task.FromResult<IReadOnlyList<ReceiptTypeRow>>([new ReceiptTypeRow { Id = 3, Fees = 6, Scac = 327 }]);
|
|
public Task<ReceiptTypeRow?> GetTypeAsync(long id, CancellationToken ct = default) =>
|
|
Task.FromResult<ReceiptTypeRow?>(id == 3 ? new ReceiptTypeRow { Id = 3, Fees = 6, Scac = 327 } : id == 10 ? new ReceiptTypeRow { Id = 10, Fees = 15, Scac = 366 } : null);
|
|
public Task<IReadOnlyList<NamedRow>> GetRejectReasonsAsync(CancellationToken ct = default) =>
|
|
Task.FromResult<IReadOnlyList<NamedRow>>([new NamedRow { Code = "1", Name = "خطا في رقم التصريح" }]);
|
|
public Task<IReadOnlyList<NamedRow>> GetCargoTypesAsync(CancellationToken ct = default) => Task.FromResult<IReadOnlyList<NamedRow>>([]);
|
|
public Task<bool> PermitMatchesTypeAsync(long receiptType, long pn, CancellationToken ct = default) => Task.FromResult(PermitOk);
|
|
public Task<long> InsertAsync(CreateReceiptRequest request, decimal fees, string status, string userLogin, CancellationToken ct = default)
|
|
{ Writes.Add($"insert:{status}:{fees}:{userLogin}"); return Task.FromResult(77L); }
|
|
public Task<long> InsertPaidCopyAsync(long sourceId, CancellationToken ct = default) { Writes.Add($"copy:{sourceId}"); return Task.FromResult(78L); }
|
|
public Task<bool> ConfirmAsync(long id, string userLogin, CancellationToken ct = default) { Writes.Add("confirm"); return Task.FromResult(!Stale); }
|
|
public Task<bool> UpdateAsync(long id, UpdateReceiptRequest request, string userLogin, CancellationToken ct = default) { Writes.Add("update"); return Task.FromResult(!Stale); }
|
|
public Task<bool> PayAsync(long id, CancellationToken ct = default) { Writes.Add("pay"); return Task.FromResult(!Stale); }
|
|
public Task<bool> RequestRevokeAsync(long id, long reason, string? note, CancellationToken ct = default) { Writes.Add($"pending:{reason}:{note}"); return Task.FromResult(!Stale); }
|
|
public Task<bool> RevokeAsync(long id, string fromStatus, long? reason, string? note, string userLogin, CancellationToken ct = default)
|
|
{ Writes.Add($"revoke:{fromStatus}:{reason}"); return Task.FromResult(!Stale); }
|
|
public Task<bool> RejectRevokeAsync(long id, CancellationToken ct = default) { Writes.Add("rejectRevoke"); return Task.FromResult(!Stale); }
|
|
public Task AddLogAsync(long receiptId, string method, string oldData, string newData, long userId, string? ip, string? fileName, CancellationToken ct = default)
|
|
{ Writes.Add($"log:{receiptId}:{method}:{newData}"); return Task.CompletedTask; }
|
|
public Task<ReceiptAutoFill?> GetGnPermitAsync(string tn, CancellationToken ct = default) => Task.FromResult<ReceiptAutoFill?>(null);
|
|
public Task<ReceiptAutoFill?> GetDriverAsync(string nnpn, CancellationToken ct = default) => Task.FromResult<ReceiptAutoFill?>(null);
|
|
public Task<bool> UserExistsAsync(string login, CancellationToken ct = default) => Task.FromResult(UserExists);
|
|
public Task<AuditLookups> GetAuditLookupsAsync(CancellationToken ct = default) => Task.FromResult(new AuditLookups
|
|
{
|
|
CargoCodeByName = new Dictionary<string, string> { ["بضائع عامة"] = "CRG" },
|
|
OperationCodeByCaption = new Dictionary<string, string> { ["تصدير"] = "EXPT" },
|
|
LocationIdByShortName = new Dictionary<string, long> { ["AQ"] = 1, ["GATE"] = 20 },
|
|
Types = [new ReceiptTypeRow { Id = 3, Fees = 6, Scac = 327 }],
|
|
});
|
|
public Task<IReadOnlyList<ReceiptRow>> GetForAuditAsync(IReadOnlyList<string> statuses, AuditFilter filter, CancellationToken ct = default)
|
|
{ Writes.Add("audit:" + string.Join('+', statuses)); return Task.FromResult<IReadOnlyList<ReceiptRow>>(AuditRows); }
|
|
public Task SetStatusAsync(IReadOnlyList<long> ids, string status, CancellationToken ct = default) { Writes.Add($"set:{string.Join(',', ids)}:{status}"); return Task.CompletedTask; }
|
|
}
|
|
|
|
private static ReceiptRow Row(string status = "RECEIVED") => new()
|
|
{
|
|
Id = 5, ReceiptNo = 202000001, Pn = 1001, Scac = 327, StatusCode = status, Fees = 6, ReceiptType = 3, Credit = 0,
|
|
};
|
|
|
|
private static (ReceiptService svc, FakeRepo repo, FakeExcel excel) Make(string role, ReceiptRow? row, params string[] keys)
|
|
{
|
|
var repo = new FakeRepo { Row = row };
|
|
var excel = new FakeExcel();
|
|
return (new ReceiptService(repo, new FakeUnit(), new FakePermissions(keys), excel, new FakeUser(role)), repo, excel);
|
|
}
|
|
|
|
private static CreateReceiptRequest NewReceipt() => new()
|
|
{
|
|
Scac = 327, Credit = 0, ReceiptType = 3, Pn = "1001", ShipmentMode = "CRG", Tn = "1", Trn = "2", Nnpn = "3", Drvr = "d", Dln = "9", Dled = "2030-01-01",
|
|
};
|
|
|
|
[Fact]
|
|
public async Task Create_takes_the_fees_of_the_type_and_the_status_of_the_kind()
|
|
{
|
|
var (svc, repo, _) = Make("TENDER_SUP", null, PermissionKeys.ReceiptsCreate);
|
|
var r = await svc.CreateAsync(NewReceipt(), default);
|
|
Assert.Equal(77, r.Id);
|
|
Assert.Equal(["insert:RECEIVED:6:TS1"], repo.Writes);
|
|
|
|
var claim = NewReceipt(); claim.Credit = 1;
|
|
await svc.CreateAsync(claim, default);
|
|
Assert.Equal("insert:UNPAID:6:TS1", repo.Writes[1]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_needs_its_key_and_every_required_field()
|
|
{
|
|
await Assert.ThrowsAsync<ForbiddenException>(() => Make("X", null).svc.CreateAsync(NewReceipt(), default));
|
|
var (svc, repo, _) = Make("TENDER_SUP", null, PermissionKeys.ReceiptsCreate);
|
|
var bad = NewReceipt(); bad.Drvr = " "; bad.Pn = "abc";
|
|
var ex = await Assert.ThrowsAsync<ValidationException>(() => svc.CreateAsync(bad, default));
|
|
Assert.Contains(ex.Errors, e => e.PropertyName == "drvr");
|
|
Assert.Contains(ex.Errors, e => e.PropertyName == "pn");
|
|
Assert.Empty(repo.Writes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_double_move_type_needs_the_first_cargo_type_and_unknown_types_are_refused()
|
|
{
|
|
var (svc, _, _) = Make("TENDER_SUP", null, PermissionKeys.ReceiptsCreate);
|
|
var dm = NewReceipt(); dm.ReceiptType = 10; dm.Scac = 366;
|
|
var ex = await Assert.ThrowsAsync<ValidationException>(() => svc.CreateAsync(dm, default));
|
|
Assert.Contains(ex.Errors, e => e.PropertyName == "fmShipmentMode");
|
|
var unknown = NewReceipt(); unknown.ReceiptType = 99;
|
|
await Assert.ThrowsAsync<BusinessException>(() => svc.CreateAsync(unknown, default));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_type_10_receipt_needs_a_permit_that_fits()
|
|
{
|
|
var (svc, repo, _) = Make("TENDER_SUP", null, PermissionKeys.ReceiptsCreate);
|
|
repo.PermitOk = false;
|
|
var dm = NewReceipt(); dm.ReceiptType = 10; dm.FmShipmentMode = "CRG";
|
|
var ex = await Assert.ThrowsAsync<BusinessException>(() => svc.CreateAsync(dm, default));
|
|
Assert.Equal(ReceiptErrors.DataCheck, ex.Code);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Confirm_logs_then_changes_a_not_received_receipt()
|
|
{
|
|
var (svc, repo, _) = Make("QUEUE_OP", Row("NOT_RECEIVED"), PermissionKeys.ReceiptsConfirm);
|
|
await svc.ConfirmAsync(5, "1.2.3.4", default);
|
|
Assert.Equal(["log:5:CONFIRM:{\"STATUSCODE\":\"تم الاستلام\"}", "confirm"], repo.Writes);
|
|
await Assert.ThrowsAsync<ForbiddenException>(() => Make("QUEUE_OP", Row("RECEIVED"), PermissionKeys.ReceiptsConfirm).svc.ConfirmAsync(5, null, default));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Paid_marks_the_claim_and_creates_a_cash_receipt_with_both_logs()
|
|
{
|
|
var (svc, repo, _) = Make("TENDER_SUP", Row("UNPAID"), PermissionKeys.ReceiptsPaid);
|
|
var r = await svc.PaidAsync(5, null, default);
|
|
Assert.Equal(78, r.Id);
|
|
Assert.Equal(["log:5:PAID:{\"STATUSCODE\":\"مسدد\"}", "pay", "copy:5", "log:78:CREATE:{\"STATUSCODE\":\"تم الاستلام\",\"NOTES\":\"\"}"], repo.Writes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Queue_operator_asks_for_the_cancellation_and_tender_supervisor_cancels_at_once()
|
|
{
|
|
var req = new RevokeReceiptRequest(1, " خطأ ");
|
|
var (queue, qrepo, _) = Make("QUEUE_OP", Row(), PermissionKeys.ReceiptsRevokeRequest, PermissionKeys.ReceiptsRevoke);
|
|
await queue.RevokeAsync(5, req, null, default);
|
|
Assert.Contains("pending:1:خطأ", qrepo.Writes);
|
|
|
|
var (sup, srepo, _) = Make("TENDER_SUP", Row(), PermissionKeys.ReceiptsRevokeRequest, PermissionKeys.ReceiptsRevoke);
|
|
await sup.RevokeAsync(5, req, null, default);
|
|
Assert.Contains("revoke:RECEIVED:1", srepo.Writes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Revoke_needs_a_known_reason_and_the_key_of_the_path()
|
|
{
|
|
var (svc, repo, _) = Make("TENDER_SUP", Row(), PermissionKeys.ReceiptsRevoke);
|
|
await Assert.ThrowsAsync<ValidationException>(() => svc.RevokeAsync(5, new RevokeReceiptRequest(null, null), null, default));
|
|
await Assert.ThrowsAsync<ValidationException>(() => svc.RevokeAsync(5, new RevokeReceiptRequest(42, null), null, default));
|
|
Assert.Empty(repo.Writes);
|
|
await Assert.ThrowsAsync<ForbiddenException>(() => Make("QUEUE_OP", Row(), PermissionKeys.ReceiptsRevoke).svc.RevokeAsync(5, new RevokeReceiptRequest(1, null), null, default));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task An_audited_receipt_is_cancelled_by_the_roles_with_that_key_only()
|
|
{
|
|
var (svc, repo, _) = Make("SAP", Row("AUDITED"), PermissionKeys.ReceiptsRevokeAudited);
|
|
await svc.RevokeAsync(5, new RevokeReceiptRequest(1, null), null, default);
|
|
Assert.Contains("revoke:AUDITED:1", repo.Writes);
|
|
await Assert.ThrowsAsync<ForbiddenException>(() => Make("SU", Row("AUDITED"), PermissionKeys.ReceiptsRevokeAudited).svc.RevokeAsync(5, new RevokeReceiptRequest(1, null), null, default));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Accept_and_reject_decide_a_pending_cancellation()
|
|
{
|
|
var (svc, repo, _) = Make("TENDER_SUP", Row("PENDING_RVKD"), PermissionKeys.ReceiptsAcceptRevoke, PermissionKeys.ReceiptsRejectRevoke);
|
|
await svc.AcceptRevokeAsync(5, null, default);
|
|
await svc.RejectRevokeAsync(5, null, default);
|
|
Assert.Equal(["log:5:ACCEPT_REVOKE:{\"STATUSCODE\":\"ملغي\"}", "revoke:PENDING_RVKD:", "log:5:REJECT_REVOKE:{\"STATUSCODE\":\"تم الاستلام\"}", "rejectRevoke"], repo.Writes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Print_refuses_zero_fees_and_logs_a_real_print()
|
|
{
|
|
var zero = Row(); zero.Fees = 0;
|
|
var ex = await Assert.ThrowsAsync<BusinessException>(() => Make("QUEUE_OP", zero, PermissionKeys.ReceiptsPrint).svc.PrintAsync(5, null, default));
|
|
Assert.Equal(ReceiptErrors.ZeroFees, ex.Code);
|
|
|
|
var (svc, repo, _) = Make("QUEUE_OP", Row(), PermissionKeys.ReceiptsPrint);
|
|
var p = await svc.PrintAsync(5, null, default);
|
|
Assert.Equal("تصريح واحد", p.Description);
|
|
Assert.True(p.ShowCargo);
|
|
Assert.Equal(["log:5:PRINT:{\"NOTES\":\"\"}"], repo.Writes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Detail_flags_follow_status_key_and_the_request_rule()
|
|
{
|
|
var byRequest = Row(); byRequest.RequestId = 9;
|
|
var (svc, _, _) = Make("TENDER_SUP", byRequest, PermissionKeys.ReceiptsUpdate, PermissionKeys.ReceiptsRevoke, PermissionKeys.ReceiptsPrint);
|
|
var a = (await svc.GetAsync(5, default)).Actions;
|
|
Assert.True(a.Update);
|
|
Assert.True(a.Print);
|
|
Assert.False(a.Revoke); // a receipt made by a permit request cannot be cancelled here
|
|
Assert.False(a.Confirm);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Audit_checks_its_inputs()
|
|
{
|
|
var (svc, repo, _) = Make("TENDER_SUP", null, PermissionKeys.ReceiptsAudit);
|
|
var empty = new AuditFilter(null, null, null, null, null);
|
|
await Assert.ThrowsAsync<BusinessException>(() => svc.AuditAsync(Stream.Null, "a.xlsx", empty, null, default));
|
|
await Assert.ThrowsAsync<BusinessException>(() => svc.AuditAsync(Stream.Null, "a.xlsx", new AuditFilter(null, new DateTime(2026, 1, 1), null, null, null), null, default));
|
|
await Assert.ThrowsAsync<BusinessException>(() => svc.AuditAsync(Stream.Null, "a.xlsx", new AuditFilter(null, new DateTime(2026, 2, 1), new DateTime(2026, 1, 1), null, null), null, default));
|
|
repo.UserExists = false;
|
|
await Assert.ThrowsAsync<BusinessException>(() => svc.AuditAsync(Stream.Null, "a.xlsx", new AuditFilter("ghost", null, null, null, null), null, default));
|
|
await Assert.ThrowsAsync<ForbiddenException>(() => Make("TENDER_SUP", null).svc.AuditAsync(Stream.Null, "a.xlsx", empty, null, default));
|
|
}
|
|
|
|
private static string[] FileRow() =>
|
|
["1", "327", "co", "9801", "drv", "تصدير", "بضائع عامة", "مكتمل", "1001", "123", "456", "AQ", "GATE", "2026-01-01", "", "555", "", "", ""];
|
|
|
|
[Fact]
|
|
public async Task Audit_moves_the_identical_receipts_on_and_logs_every_compared_one()
|
|
{
|
|
var (svc, repo, excel) = Make("TENDER_SUP", null, PermissionKeys.ReceiptsAudit);
|
|
excel.Sheet = [["t"], ["t"], ["h"], FileRow()];
|
|
repo.AuditRows = [new ReceiptRow
|
|
{
|
|
Id = 5, ReceiptNo = 202000001, Pn = 1001, Scac = 327, Tn = "123", Trn = "456", Nnpn = "9801", ShipmentMode = "CRG", InboundTypeCode = "EXPT", OriginAddressId = 1,
|
|
DestinationAddressId = 20, CaCode = 555, Fees = 6, ReceiptType = 3, StatusCode = "RECEIVED",
|
|
}];
|
|
var r = await svc.AuditAsync(Stream.Null, "permits.xlsx", new AuditFilter("ts1", null, null, null, null), "10.0.0.1", default);
|
|
|
|
Assert.Equal("permits_result.xlsx", r.FileName);
|
|
Assert.Equal(1, r.Compared);
|
|
Assert.Equal(1, r.Advanced);
|
|
Assert.Equal("audit:NOT_RECEIVED+ALL+RECEIVED+AUDITED+UNPAID", repo.Writes[0]);
|
|
Assert.Contains(repo.Writes, w => w.StartsWith("log:5:AUDIT:") && w.Contains("يوجد سند قبض"));
|
|
Assert.Contains("set:5:AUDITED", repo.Writes);
|
|
Assert.Single(excel.Last!.Rows);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task The_financial_audit_of_AM_completes_audited_receipts()
|
|
{
|
|
var (svc, repo, excel) = Make("AM", null, PermissionKeys.ReceiptsAudit);
|
|
excel.Sheet = [["t"], ["t"], ["h"], FileRow()];
|
|
repo.AuditRows = [new ReceiptRow { Id = 5, Pn = 1001, Scac = 327, Tn = "123", Trn = "456", Nnpn = "9801", ShipmentMode = "CRG", InboundTypeCode = "EXPT", OriginAddressId = 1, DestinationAddressId = 20, CaCode = 555, Fees = 6, ReceiptType = 3, StatusCode = "AUDITED" }];
|
|
await svc.AuditAsync(Stream.Null, "p.xlsx", new AuditFilter("x", null, null, null, null), null, default);
|
|
Assert.Equal("audit:AUDITED+COMPLETED", repo.Writes[0]);
|
|
Assert.Contains("set:5:COMPLETED", repo.Writes);
|
|
Assert.Contains(repo.Writes, w => w.StartsWith("log:5:FIN_AUDIT:"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Audit_without_receipts_for_the_user_is_an_error()
|
|
{
|
|
var (svc, repo, excel) = Make("TENDER_SUP", null, PermissionKeys.ReceiptsAudit);
|
|
excel.Sheet = [["t"], ["t"], ["h"], FileRow()];
|
|
var ex = await Assert.ThrowsAsync<BusinessException>(() => svc.AuditAsync(Stream.Null, "p.xlsx", new AuditFilter("x", null, null, null, null), null, default));
|
|
Assert.Equal(ReceiptErrors.NoReceipts, ex.Code);
|
|
Assert.DoesNotContain(repo.Writes, w => w.StartsWith("set"));
|
|
}
|
|
}
|