diff --git a/Elwig/App.xaml.cs b/Elwig/App.xaml.cs index 1803d7f..846cd2d 100644 --- a/Elwig/App.xaml.cs +++ b/Elwig/App.xaml.cs @@ -296,6 +296,10 @@ namespace Elwig { return FocusWindow(() => new(year), w => w.Year == year); } + public static PaymentAdjustmentWindow FocusPaymentAdjustment(int year) { + return FocusWindow(() => new(year), w => w.Year == year); + } + public static ChartWindow FocusChartWindow(int year, int avnr) { return FocusWindow(() => new(year, avnr), w => w.Year == year && w.AvNr == avnr); } diff --git a/Elwig/Helpers/Billing/Billing.cs b/Elwig/Helpers/Billing/Billing.cs index c99bb61..02d314d 100644 --- a/Elwig/Helpers/Billing/Billing.cs +++ b/Elwig/Helpers/Billing/Billing.cs @@ -3,6 +3,7 @@ using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Threading.Tasks; @@ -33,15 +34,44 @@ namespace Elwig.Helpers.Billing { """); } - public async Task AutoAdjustBusinessShare() { + public async Task AutoAdjustBusinessShares(DateOnly date, int allowanceKg = 0, double allowanceBs = 0, int allowanceKgPerBs = 0, double allowanceRel = 0, int addMinBs = 1) { + if (addMinBs < 1) addMinBs = 1; using var cnx = await AppDbContext.ConnectAsync(); await AppDbContext.ExecuteBatch(cnx, $""" - INSERT INTO member_history (mgnr, date, business_shares, type) - SELECT u.mgnr, '{Utils.Today:yyyy-MM-dd}', u.diff / s.max_kg_per_bs AS bs, 'auto' + UPDATE member + SET business_shares = member.business_shares - h.business_shares + FROM member_history h + WHERE h.date = '{Year}-11-30' AND h.type = 'auto' AND h.mgnr = member.mgnr; + + INSERT INTO member_history (mgnr, date, type, business_shares) + SELECT u.mgnr, + '{date:yyyy-MM-dd}', + 'auto', + CEIL((u.diff - {allowanceKg}.0 - {allowanceKgPerBs}.0 * u.business_shares) / s.max_kg_per_bs + - {allowanceBs.ToString(CultureInfo.InvariantCulture)} + - {allowanceRel.ToString(CultureInfo.InvariantCulture)} * u.business_shares) AS bs FROM v_total_under_delivery u JOIN season s ON s.year = u.year - WHERE s.year = {Year} AND bs > 0 - ON CONFLICT DO NOTHING + WHERE s.year = {Year} AND bs >= {addMinBs} + ON CONFLICT DO UPDATE + SET business_shares = excluded.business_shares; + + UPDATE member + SET business_shares = member.business_shares + h.business_shares + FROM member_history h + WHERE h.date = '{Year}-11-30' AND h.type = 'auto' AND h.mgnr = member.mgnr; + """); + } + + public async Task UnAdjustBusinessShares() { + using var cnx = await AppDbContext.ConnectAsync(); + await AppDbContext.ExecuteBatch(cnx, $""" + UPDATE member + SET business_shares = member.business_shares - h.business_shares + FROM member_history h + WHERE h.date = '{Year}-11-30' AND h.type = 'auto' AND h.mgnr = member.mgnr; + + DELETE FROM member_history WHERE date = '{Year}-11-30' AND type = 'auto'; """); } diff --git a/Elwig/Windows/PaymentAdjustmentWindow.xaml b/Elwig/Windows/PaymentAdjustmentWindow.xaml new file mode 100644 index 0000000..b062018 --- /dev/null +++ b/Elwig/Windows/PaymentAdjustmentWindow.xaml @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +