Billing: Add possibility to automatically add business shares
This commit is contained in:
@ -18,6 +18,7 @@ namespace Elwig.Documents {
|
||||
public string MemberModifier;
|
||||
public IEnumerable<(string Name, int Kg, decimal Amount)>? MemberUnderDeliveries;
|
||||
public decimal MemberTotalUnderDelivery;
|
||||
public decimal MemberAutoBusinessShares;
|
||||
|
||||
public CreditNote(AppDbContext ctx, PaymentMember p, CreditNoteData data, Dictionary<string, UnderDelivery>? underDeliveries = null) :
|
||||
base($"{Name} {(p.Credit != null ? $"Nr. {p.Credit.Year}/{p.Credit.TgNr:000}" : p.Member.Name)} – {p.Variant.Name}", p.Member) {
|
||||
@ -36,6 +37,12 @@ namespace Elwig.Documents {
|
||||
var total = data.Rows.SelectMany(r => r.Buckets).Sum(b => b.Value);
|
||||
var totalUnderDelivery = total - p.Member.BusinessShares * season.MinKgPerBusinessShare;
|
||||
MemberTotalUnderDelivery = totalUnderDelivery < 0 ? totalUnderDelivery * (season.PenaltyPerKg ?? 0) - (season.PenaltyAmount ?? 0) : 0;
|
||||
var fromDate = $"{season.Year}-06-01";
|
||||
var toDate = $"{season.Year + 1}-06-01";
|
||||
MemberAutoBusinessShares = ctx.MemberHistory
|
||||
.Where(h => h.MgNr == p.Member.MgNr && h.Type == "auto")
|
||||
.Where(h => h.DateString.CompareTo(fromDate) >= 0 && h.DateString.CompareTo(toDate) < 0)
|
||||
.Sum(h => h.BusinessShares) * (-season.BusinessShareValue ?? 0);
|
||||
if (total == 0) MemberTotalUnderDelivery -= (season.PenaltyNone ?? 0);
|
||||
Aside = Aside.Replace("</table>", "") +
|
||||
$"<thead><tr><th colspan='2'>Gutschrift</th></tr></thead><tbody>" +
|
||||
|
@ -138,7 +138,11 @@
|
||||
}
|
||||
@if (Model.MemberTotalUnderDelivery != 0) {
|
||||
@Raw(FormatRow("Unterlieferung (GA)", Model.MemberTotalUnderDelivery, add: true));
|
||||
penalty += Math.Round(Model.MemberTotalUnderDelivery, 2, MidpointRounding.AwayFromZero);
|
||||
penalty += Model.MemberTotalUnderDelivery;
|
||||
}
|
||||
@if (Model.MemberAutoBusinessShares != 0) {
|
||||
@Raw(FormatRow("Autom. Nachz. von GA", Model.MemberAutoBusinessShares, add: true));
|
||||
penalty += Model.MemberAutoBusinessShares;
|
||||
}
|
||||
|
||||
@if (Model.Credit == null) {
|
||||
|
@ -44,6 +44,7 @@ namespace Elwig.Helpers {
|
||||
public DbSet<Member> Members { get; private set; }
|
||||
public DbSet<BillingAddr> BillingAddresses { get; private set; }
|
||||
public DbSet<MemberTelNr> MemberTelephoneNrs { get; private set; }
|
||||
public DbSet<MemberHistory> MemberHistory { get; private set; }
|
||||
public DbSet<AreaCom> AreaCommitments { get; private set; }
|
||||
public DbSet<Season> Seasons { get; private set; }
|
||||
public DbSet<Modifier> Modifiers { get; private set; }
|
||||
|
@ -31,6 +31,18 @@ namespace Elwig.Helpers.Billing {
|
||||
""");
|
||||
}
|
||||
|
||||
public async Task AutoAdjustBusinessShare() {
|
||||
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'
|
||||
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
|
||||
""");
|
||||
}
|
||||
|
||||
public async Task CalculateBuckets(bool allowAttrsIntoLower, bool avoidUnderDeliveries, bool honorGebunden) {
|
||||
var attrVals = Context.WineAttributes.ToDictionary(a => a.AttrId, a => (a.IsStrict, a.FillLower));
|
||||
var attrForced = attrVals.Where(a => a.Value.IsStrict && a.Value.FillLower == 0).Select(a => a.Key).ToArray();
|
||||
|
@ -52,7 +52,7 @@ namespace Elwig.Helpers.Billing {
|
||||
ROUND(
|
||||
IIF({Data.ConsiderContractPenalties}, COALESCE(u.total_penalty, 0) / POW(10, 4 - 2), 0) +
|
||||
IIF({Data.ConsiderTotalPenalty}, COALESCE(b.total_penalty, 0), 0) +
|
||||
IIF({Data.ConsiderAutoBusinessShares}, 0, 0)
|
||||
IIF({Data.ConsiderAutoBusinessShares}, -COALESCE(a.business_shares * s.bs_value, 0), 0) / POW(10, s.precision - 2)
|
||||
) AS modifiers,
|
||||
lc.modifiers AS prev_modifiers
|
||||
FROM season s
|
||||
@ -84,6 +84,11 @@ namespace Elwig.Helpers.Billing {
|
||||
FROM v_total_under_delivery u
|
||||
JOIN season s ON s.year = u.year
|
||||
WHERE u.diff < 0) b ON (b.year, b.mgnr) = (s.year, m.mgnr)
|
||||
LEFT JOIN (SELECT h.mgnr, h.business_shares
|
||||
FROM member_history h
|
||||
WHERE type = 'auto' AND
|
||||
date >= '{Year}-06-01' AND
|
||||
date < '{Year + 1}-06-01') a ON a.mgnr = m.mgnr
|
||||
WHERE s.year = {Year} AND v.avnr = {AvNr};
|
||||
|
||||
UPDATE payment_variant SET test_variant = FALSE WHERE (year, avnr) = ({Year}, {AvNr});
|
||||
|
@ -62,12 +62,8 @@ namespace Elwig.Models.Entities {
|
||||
|
||||
[NotMapped]
|
||||
public DateOnly? EntryDate {
|
||||
get {
|
||||
return EntryDateString != null ? DateOnly.ParseExact(EntryDateString, "yyyy-MM-dd") : null;
|
||||
}
|
||||
set {
|
||||
EntryDateString = value?.ToString("yyyy-MM-dd");
|
||||
}
|
||||
get => EntryDateString != null ? DateOnly.ParseExact(EntryDateString, "yyyy-MM-dd") : null;
|
||||
set => EntryDateString = value?.ToString("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
[Column("exit_date")]
|
||||
@ -75,12 +71,8 @@ namespace Elwig.Models.Entities {
|
||||
|
||||
[NotMapped]
|
||||
public DateOnly? ExitDate {
|
||||
get {
|
||||
return ExitDateString != null ? DateOnly.ParseExact(ExitDateString, "yyyy-MM-dd") : null;
|
||||
}
|
||||
set {
|
||||
ExitDateString = value?.ToString("yyyy-MM-dd");
|
||||
}
|
||||
get => ExitDateString != null ? DateOnly.ParseExact(ExitDateString, "yyyy-MM-dd") : null;
|
||||
set => ExitDateString = value?.ToString("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
[Column("business_shares")]
|
||||
|
31
Elwig/Models/Entities/MemberHistory.cs
Normal file
31
Elwig/Models/Entities/MemberHistory.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Elwig.Models.Entities {
|
||||
[Table("member_history"), PrimaryKey("MgNr", "DateString")]
|
||||
public class MemberHistory {
|
||||
[Column("mgnr")]
|
||||
public int MgNr { get; set; }
|
||||
|
||||
[Column("date")]
|
||||
public string DateString { get; set; }
|
||||
[NotMapped]
|
||||
public DateOnly Date {
|
||||
get => DateOnly.ParseExact(DateString, "yyyy-MM-dd");
|
||||
set => value.ToString("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
[Column("business_shares")]
|
||||
public int BusinessShares { get; set; }
|
||||
|
||||
[Column("type")]
|
||||
public string Type { get; set; }
|
||||
|
||||
[Column("comment")]
|
||||
public string? Comment { get; set; }
|
||||
|
||||
[ForeignKey("MgNr")]
|
||||
public virtual Member Member { get; private set; }
|
||||
}
|
||||
}
|
@ -55,6 +55,14 @@ namespace Elwig.Models.Entities {
|
||||
set => PenaltyNoneValue = value != null ? DecToDb(value.Value) : null;
|
||||
}
|
||||
|
||||
[Column("bs_value")]
|
||||
public long? BusinessShareValueValue { get; set; }
|
||||
[NotMapped]
|
||||
public decimal? BusinessShareValue {
|
||||
get => BusinessShareValueValue != null ? DecFromDb(BusinessShareValueValue.Value) : null;
|
||||
set => BusinessShareValueValue = value != null ? DecToDb(value.Value) : null;
|
||||
}
|
||||
|
||||
[Column("start_date")]
|
||||
public string? StartDateString { get; set; }
|
||||
|
||||
|
@ -382,7 +382,7 @@
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="180"/>
|
||||
<RowDefinition Height="205"/>
|
||||
<RowDefinition Height="*"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
@ -446,6 +446,10 @@
|
||||
<Label Content="Strafe (Nicht-Lieferung):" Margin="10,130,0,10" Grid.Column="2"/>
|
||||
<ctrl:UnitTextBox x:Name="SeasonPenaltyNoneInput" Unit="€" TextChanged="SeasonPenaltyInput_TextChanged"
|
||||
Grid.Column="3" Width="68" Margin="0,130,10,10" HorizontalAlignment="Left" VerticalAlignment="Top"/>
|
||||
|
||||
<Label Content="GA-Wert:" Margin="10,160,0,10" Grid.Column="2"/>
|
||||
<ctrl:UnitTextBox x:Name="SeasonBsValueInput" Unit="€/GA" TextChanged="SeasonPenaltyInput_TextChanged"
|
||||
Grid.Column="3" Width="85" Margin="0,160,10,10" HorizontalAlignment="Left" VerticalAlignment="Top"/>
|
||||
</Grid>
|
||||
|
||||
<GroupBox Grid.Column="1" Grid.Row="1" Header="Zu-/Abschläge" Margin="0,0,10,10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
|
||||
|
@ -43,12 +43,14 @@ namespace Elwig.Windows {
|
||||
SeasonPenaltyPerKgInput.Text = s.PenaltyPerKg?.ToString() ?? "";
|
||||
SeasonPenaltyInput.Text = s.PenaltyAmount?.ToString() ?? "";
|
||||
SeasonPenaltyNoneInput.Text = s.PenaltyNone?.ToString() ?? "";
|
||||
SeasonBsValueInput.Text = s.BusinessShareValue?.ToString() ?? "";
|
||||
|
||||
var sym = s.Currency.Symbol ?? "";
|
||||
SeasonModifierAbsInput.Unit = $"{sym}/kg";
|
||||
SeasonPenaltyPerKgInput.Unit = $"{sym}/kg";
|
||||
SeasonPenaltyInput.Unit = sym;
|
||||
SeasonPenaltyNoneInput.Unit = sym;
|
||||
SeasonBsValueInput.Unit = $"{sym}/GA";
|
||||
AreaCommitmentTypePenaltyPerKgInput.Unit = $"{sym}/kg";
|
||||
AreaCommitmentTypePenaltyInput.Unit = sym;
|
||||
AreaCommitmentTypePenaltyNoneInput.Unit = sym;
|
||||
@ -64,6 +66,7 @@ namespace Elwig.Windows {
|
||||
SeasonPenaltyPerKgInput.Text = "";
|
||||
SeasonPenaltyInput.Text = "";
|
||||
SeasonPenaltyNoneInput.Text = "";
|
||||
SeasonBsValueInput.Text = "";
|
||||
}
|
||||
_seasonUpdate = false;
|
||||
}
|
||||
@ -85,6 +88,7 @@ namespace Elwig.Windows {
|
||||
s.PenaltyPerKg = (SeasonPenaltyPerKgInput.Text.Length > 0) ? decimal.Parse(SeasonPenaltyPerKgInput.Text) : null;
|
||||
s.PenaltyAmount = (SeasonPenaltyInput.Text.Length > 0) ? decimal.Parse(SeasonPenaltyInput.Text) : null;
|
||||
s.PenaltyNone = (SeasonPenaltyNoneInput.Text.Length > 0) ? decimal.Parse(SeasonPenaltyNoneInput.Text) : null;
|
||||
s.BusinessShareValue = (SeasonBsValueInput.Text.Length > 0) ? decimal.Parse(SeasonBsValueInput.Text) : null;
|
||||
|
||||
UpdateButtons();
|
||||
}
|
||||
|
@ -27,7 +27,8 @@ namespace Elwig.Windows {
|
||||
AreaCommitmentTypeMinKgPerHaInput.TextBox, AreaCommitmentTypePenaltyPerKgInput.TextBox,
|
||||
AreaCommitmentTypePenaltyInput.TextBox, AreaCommitmentTypePenaltyNoneInput.TextBox,
|
||||
SeasonMaxKgPerHaInput.TextBox, SeasonVatNormalInput.TextBox, SeasonVatFlatrateInput.TextBox, SeasonStartInput, SeasonEndInput,
|
||||
SeasonMinKgPerBsInput.TextBox, SeasonMaxKgPerBsInput.TextBox, SeasonPenaltyPerKgInput.TextBox, SeasonPenaltyInput.TextBox, SeasonPenaltyNoneInput.TextBox,
|
||||
SeasonMinKgPerBsInput.TextBox, SeasonMaxKgPerBsInput.TextBox, SeasonBsValueInput.TextBox,
|
||||
SeasonPenaltyPerKgInput.TextBox, SeasonPenaltyInput.TextBox, SeasonPenaltyNoneInput.TextBox,
|
||||
SeasonModifierIdInput, SeasonModifierNameInput, SeasonModifierRelInput.TextBox, SeasonModifierAbsInput.TextBox,
|
||||
};
|
||||
WineAttributeFillLowerInput.Visibility = Visibility.Hidden;
|
||||
@ -72,6 +73,7 @@ namespace Elwig.Windows {
|
||||
SeasonPenaltyPerKgInput.TextBox.IsReadOnly = true;
|
||||
SeasonPenaltyInput.TextBox.IsReadOnly = true;
|
||||
SeasonPenaltyNoneInput.TextBox.IsReadOnly = true;
|
||||
SeasonBsValueInput.TextBox.IsReadOnly = true;
|
||||
|
||||
SeasonModifierIdInput.IsReadOnly = true;
|
||||
SeasonModifierNameInput.IsReadOnly = true;
|
||||
@ -117,6 +119,7 @@ namespace Elwig.Windows {
|
||||
SeasonPenaltyPerKgInput.TextBox.IsReadOnly = false;
|
||||
SeasonPenaltyInput.TextBox.IsReadOnly = false;
|
||||
SeasonPenaltyNoneInput.TextBox.IsReadOnly = false;
|
||||
SeasonBsValueInput.TextBox.IsReadOnly = false;
|
||||
|
||||
SeasonModifierIdInput.IsReadOnly = false;
|
||||
SeasonModifierNameInput.IsReadOnly = false;
|
||||
|
@ -37,14 +37,18 @@
|
||||
|
||||
<Button x:Name="DeliveryConfirmationButton" Content="Anlieferungsbestätigungen"
|
||||
Click="DeliveryConfirmationButton_Click"
|
||||
Margin="50,122,0,0"/>
|
||||
Margin="50,120,0,0"/>
|
||||
|
||||
<Button x:Name="OverUnderDeliveryButton" Content="Über-/Unterlieferungen"
|
||||
Click="OverUnderDeliveryButton_Click"
|
||||
Margin="50,164,0,0"/>
|
||||
Margin="50,160,0,0"/>
|
||||
|
||||
<Button x:Name="AutoBusinessSharesButton" Content="Autom. GA nachzeichen"
|
||||
Click="AutoBusinessSharesButton_Click"
|
||||
Margin="50,200,0,0"/>
|
||||
|
||||
<Button x:Name="PaymentButton" Content="Auszahlung"
|
||||
Click="PaymentButton_Click"
|
||||
Margin="50,206,0,0"/>
|
||||
Margin="50,240,0,0"/>
|
||||
</Grid>
|
||||
</local:ContextWindow>
|
||||
|
@ -31,6 +31,7 @@ namespace Elwig.Windows {
|
||||
CalculateBucketsButton.IsEnabled = valid && last;
|
||||
DeliveryConfirmationButton.IsEnabled = valid;
|
||||
OverUnderDeliveryButton.IsEnabled = valid;
|
||||
AutoBusinessSharesButton.IsEnabled = valid;
|
||||
PaymentButton.IsEnabled = valid;
|
||||
}
|
||||
|
||||
@ -82,6 +83,27 @@ namespace Elwig.Windows {
|
||||
Mouse.OverrideCursor = null;
|
||||
}
|
||||
|
||||
private async void AutoBusinessSharesButton_Click(object sender, RoutedEventArgs evt) {
|
||||
if (SeasonInput.Value is not int year)
|
||||
return;
|
||||
if (App.Client.IsMatzen) {
|
||||
AutoBusinessSharesButton.IsEnabled = false;
|
||||
Mouse.OverrideCursor = Cursors.AppStarting;
|
||||
|
||||
var b = new Billing(year);
|
||||
await b.AutoAdjustBusinessShare();
|
||||
|
||||
Mouse.OverrideCursor = null;
|
||||
AutoBusinessSharesButton.IsEnabled = true;
|
||||
} else {
|
||||
MessageBox.Show(
|
||||
"Es ist kein automatisches Nachzeichnen der Geschäftsanteile\n" +
|
||||
"für diese Genossenschaft eingestellt!\n" +
|
||||
"Bitte wenden Sie sich an die Programmierer!", "Fehler",
|
||||
MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
}
|
||||
|
||||
private void PaymentButton_Click(object sender, RoutedEventArgs evt) {
|
||||
if (SeasonInput.Value is not int year)
|
||||
return;
|
||||
|
Reference in New Issue
Block a user