Fix international order VAT and currencies in Enova and Optima

This commit is contained in:
janusz committed 2026-10-29 01:31:13 +01:00
1 parent 75ef1ed69c
commit 248d7bac64
23 files changed
+308 -34

No files matched your search

@@ -12,6 +12,7 @@ if (!options.TryGetValue("config", out var configPath) || string.IsNullOrWhiteSp
Console.Error.WriteLine(
"Użycie: --config <optima.json> [--negative-tests] [--write-order [--approved]] " +
"[--customer-id <ID> --product-code <KOD>] " +
"[--currency <ISO> --international --eu] " +
"[--attribute-code-1 <KOD> --attribute-value-1 <TEKST> --attribute-code-2 <KOD> --attribute-value-2 <TEKST>]");
return 2;
}
@@ -93,9 +94,12 @@ if (extraFields.Count > 0)
Console.WriteLine("PASS extraFields: nieznany kod nie utworzył zamówienia.");
}
var request = CreateRequest(runtime.AdapterConfiguration, customerId!, productCode!, "PLN", reference, saveMode, extraFields);
var currency = options.GetValueOrDefault("currency", "PLN")?.Trim().ToUpperInvariant() ?? "PLN";
var request = CreateRequest(runtime.AdapterConfiguration, customerId!, productCode!, currency, reference, saveMode, extraFields,
options.ContainsKey("international"), options.ContainsKey("eu"));
var result = adapter.CreateOrder(request);
await AssertSavedOrderAsync(sql, request, result);
if (extraFields.Count > 0)
{
await AssertSavedAttributesAsync(sql, result.Id, extraFields);
@@ -145,10 +149,14 @@ static OrderRequest CreateRequest(
string currency,
string reference,
OrderSaveMode saveMode = OrderSaveMode.Buffer,
IReadOnlyDictionary<string, string>? extraFields = null) => new()
IReadOnlyDictionary<string, string>? extraFields = null,
bool international = false,
bool eu = false) => new()
{
CurrencyIso = currency,
CustomerCode = customerId,
CompanyIsInternational = international,
CompanyIsInEuropeanUnion = eu,
CustomerReference = reference,
Notes = "Automatyczny test integracyjny SmartB2B",
DocumentDefinition = configuration.DocumentDefinition,
@@ -168,6 +176,28 @@ static OrderRequest CreateRequest(
]
};
static async Task AssertSavedOrderAsync(SqlRawHandler sql, OrderRequest request, OrderResult order)
{
var result = (SqlRawResult?)await sql.HandleAsync(new RpcInvocation([], new Dictionary<string, object?>
{
["query"] = "SELECT TrN_Export AS export_type, TrN_Waluta AS currency_iso, TrN_RazemNettoWal AS net, TrN_RazemBruttoWal AS gross " +
"FROM CDN.TraNag WHERE TrN_TrNID=@1; SELECT TrE_Stawka AS tax_rate FROM CDN.TraElem WHERE TrE_TrNId=@1;",
["params"] = new object?[] { order.Id }
}), CancellationToken.None);
var header = result?.Recordsets[0].Single() ?? throw new InvalidOperationException("Brak zapisanego nagłówka.");
var expectedExport = request.CompanyIsInternational ? (request.CompanyIsInEuropeanUnion ? 3 : 1) : 0;
var actualCurrency = Convert.ToString(header["currency_iso"])?.Trim();
if (string.IsNullOrEmpty(actualCurrency)) actualCurrency = "PLN";
if (Convert.ToInt32(header["export_type"]) != expectedExport || actualCurrency != request.CurrencyIso ||
Convert.ToDecimal(header["net"]) != order.Net || Convert.ToDecimal(header["gross"]) != order.Gross)
throw new InvalidOperationException("Zapisane waluta, rodzaj transakcji lub sumy w walucie nie zgadzają się z zamówieniem.");
var lines = result!.Recordsets[1];
if (lines.Count != request.Items.Count || (request.CompanyIsInternational &&
(lines.Any(line => Convert.ToDecimal(line["tax_rate"]) != 0m) || order.Vat != 0m)))
throw new InvalidOperationException("Nieprawidłowe pozycje lub stawki VAT na zamówieniu zagranicznym.");
Console.WriteLine("PASS waluta, rodzaj transakcji, sumy w walucie i VAT zapisanego dokumentu.");
}
static IReadOnlyDictionary<string, string> ReadTestAttributes(Dictionary<string, string?> options)
{
var names = new[] { "attribute-code-1", "attribute-value-1", "attribute-code-2", "attribute-value-2" };
@@ -263,6 +293,8 @@ static Dictionary<string, string?> ParseArguments(string[] arguments)
var name = argument[2..];
if (string.Equals(name, "write-order", StringComparison.OrdinalIgnoreCase) ||
string.Equals(name, "approved", StringComparison.OrdinalIgnoreCase) ||
string.Equals(name, "international", StringComparison.OrdinalIgnoreCase) ||
string.Equals(name, "eu", StringComparison.OrdinalIgnoreCase) ||
string.Equals(name, "negative-tests", StringComparison.OrdinalIgnoreCase))
{
result[name] = null;