From 48970de6529300702f81d34b053bc7ba8bee6d9b Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Wed, 14 Aug 2024 10:45:03 +0200 Subject: [PATCH] Billing: Fix error when no deliveries exist in season --- Elwig/Helpers/Billing/Billing.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Elwig/Helpers/Billing/Billing.cs b/Elwig/Helpers/Billing/Billing.cs index 1d32746..92564e0 100644 --- a/Elwig/Helpers/Billing/Billing.cs +++ b/Elwig/Helpers/Billing/Billing.cs @@ -157,13 +157,15 @@ namespace Elwig.Helpers.Billing { lastMgNr = mgnr; } - await AppDbContext.ExecuteBatch(cnx, $""" - UPDATE delivery_part_bucket SET value = 0 WHERE year = {Year}; - INSERT INTO delivery_part_bucket (year, did, dpnr, bktnr, discr, value) - VALUES {string.Join(",\n ", inserts.Select(i => $"({Year}, {i.Item1}, {i.Item2}, {i.Item3}, '{i.Item4}', {i.Item5})"))} - ON CONFLICT DO UPDATE - SET discr = excluded.discr, value = value + excluded.value; - """); + await AppDbContext.ExecuteBatch(cnx, $"UPDATE delivery_part_bucket SET value = 0 WHERE year = {Year}"); + if (inserts.Count > 0) { + await AppDbContext.ExecuteBatch(cnx, $""" + INSERT INTO delivery_part_bucket (year, did, dpnr, bktnr, discr, value) + VALUES {string.Join(",\n ", inserts.Select(i => $"({Year}, {i.Item1}, {i.Item2}, {i.Item3}, '{i.Item4}', {i.Item5})"))} + ON CONFLICT DO UPDATE + SET discr = excluded.discr, value = value + excluded.value; + """); + } if (!avoidUnderDlvrs) { if (ownCnx) await cnx.DisposeAsync();