Billing: fix calc issue

This commit is contained in:
2023-10-19 18:54:47 +02:00
parent e56e209506
commit f19de3ae6e

View File

@ -181,19 +181,30 @@ namespace Elwig.Helpers.Billing {
}
}
var changes = new List<(int, int, int, int)>();
var negChanges = new List<(int, int, int, int)>();
var posChanges = new List<(int, int, int, int)>();
foreach (var (did, dpnr, _, w) in fittingDeliveries) {
int v = Math.Min(needed, w);
needed -= v;
changes.Add((did, dpnr, 1, v));
changes.Add((did, dpnr, 2, w - v));
posChanges.Add((did, dpnr, 1, v));
negChanges.Add((did, dpnr, 2, w - v));
if (needed == 0) break;
}
using (var cmd = cnx.CreateCommand()) {
cmd.CommandText = $"""
INSERT INTO delivery_part_bin (year, did, dpnr, binnr, discr, value)
VALUES {string.Join(",\n ", changes.Select(i => $"({Year}, {i.Item1}, {i.Item2}, {i.Item3}, '', {i.Item4})"))}
VALUES {string.Join(",\n ", posChanges.Select(i => $"({Year}, {i.Item1}, {i.Item2}, {i.Item3}, '', {i.Item4})"))}
ON CONFLICT DO UPDATE
SET value = value + excluded.value
""";
await cmd.ExecuteNonQueryAsync();
}
using (var cmd = cnx.CreateCommand()) {
cmd.CommandText = $"""
INSERT INTO delivery_part_bin (year, did, dpnr, binnr, discr, value)
VALUES {string.Join(",\n ", negChanges.Select(i => $"({Year}, {i.Item1}, {i.Item2}, {i.Item3}, '', {i.Item4})"))}
ON CONFLICT DO UPDATE
SET value = excluded.value
""";