using System.Globalization; namespace NTP.Application.Grains.Reports; /// Weighbridge transaction codes of ws.cfc addWeightBridgeEvent (errorcode 0..4). public static class WbCodes { public const string Ok = "0"; public const string WrongStatus = "1"; public const string LocationNotOnRoute = "2"; public const string AssignDestError = "3"; public const string Generic = "4"; public const string MsgOk = "تمت العملية بنجاح"; public const string MsgWrongStatus = "حالة السيارة غير صحيحة"; public const string MsgLocationNotOnRoute = "الموقع غير موجود على مسار الشاحنة"; } /// The decision table of ws.cfc addWeightBridgeEvent (pure: unit tested). public sealed record WbPlan(string EventType, string CurrentStatus, string? NewPermitStatus, string? Action, bool UseProvidedEventDate); public static class WbRules { public enum LocationType { None, Origin, Destination } public static LocationType Locate(long? originId, long? destinationId, long locationId) => originId == locationId ? LocationType.Origin : destinationId == locationId ? LocationType.Destination : LocationType.None; /// /// Returns the plan for (location type, EN/EX, current permit status) or null when the status is not valid for that step /// (errorcode 1). Unknown trxType -> null plan with = true (errorcode 4). /// public static WbPlan? Plan(LocationType loc, string trxType, string status, out bool generic) { generic = false; var t = (trxType ?? "").Trim().ToUpperInvariant(); switch (loc, t) { case (LocationType.Origin, "EN"): // LTW, status unchanged return status is "ACCPT" or "RESD" ? new WbPlan("LTW", status, null, null, true) : null; case (LocationType.Origin, "EX"): // LGW -> ACTRL return status is "ACCPT" or "RESD" or "ACTRL" ? new WbPlan("LGW", "ACTRL", "ACTRL", "printPage1", true) : null; case (LocationType.Destination, "EN"): // DGW -> CLRPR return status is "ACCPT" or "RESD" or "ACTRL" or "CLRPR" ? new WbPlan("DGW", "CLRPR", "CLRPR", null, false) : null; case (LocationType.Destination, "EX"): // DTW -> CLOSE return status == "CLRPR" ? new WbPlan("DTW", "CLOSE", "CLOSE", "printPage2", false) : null; default: generic = true; return null; } } /// Accepts the legacy "yyyymmdd HH24miss" and the compact "yyyymmddHH24miss" (what grains.cfc sends). public static bool TryParseEventDate(string? s, out DateTime value) { string[] fmts = ["yyyyMMdd HHmmss", "yyyyMMddHHmmss"]; return DateTime.TryParseExact((s ?? "").Trim(), fmts, CultureInfo.InvariantCulture, DateTimeStyles.None, out value); } } /// grains.cfc generatePermitNumber. public static class PermitNumber { private static string Pad(long n, int width) => n.ToString(CultureInfo.InvariantCulture).PadLeft(width, '0'); /// Returns null for a cargo type other than BULK/PCKD (legacy left PN empty). public static string? Generate(string tt, long seq, string companyCode, long tenderCount) { var badiya = companyCode == GrainsReportConstants.BadiyaScac; var pckdTender = 41 + tenderCount; return (badiya, (tt ?? "").ToUpperInvariant()) switch { (true, "BULK") => "30" + Pad(tenderCount, 2) + Pad(seq, 6), (true, "PCKD") => "3" + pckdTender.ToString(CultureInfo.InvariantCulture) + Pad(seq, 7), (false, "BULK") => "1" + Pad(tenderCount, 2) + Pad(seq, 6), (false, "PCKD") => "1" + pckdTender.ToString(CultureInfo.InvariantCulture) + Pad(seq, 6), _ => null }; } }