PaymentVariantsWindow: Impelment BillingVariant
This commit is contained in:
@ -1,3 +1,6 @@
|
||||
using Elwig.Models.Entities;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
@ -5,18 +8,23 @@ using System.Threading.Tasks;
|
||||
namespace Elwig.Helpers.Billing {
|
||||
public class BillingVariant : Billing {
|
||||
|
||||
private readonly int AvNr;
|
||||
protected readonly int AvNr;
|
||||
protected readonly PaymentVar PaymentVariant;
|
||||
|
||||
public BillingVariant(int year, int avnr) : base(year) {
|
||||
AvNr = avnr;
|
||||
PaymentVariant = Context.PaymentVariants.Find(Year, AvNr) ?? throw new ArgumentException("PaymentVar not found");
|
||||
}
|
||||
|
||||
public async Task Calculate() {
|
||||
await DeleteInDb();
|
||||
await SetCalcTime();
|
||||
await CalculatePrices();
|
||||
await CalculateModifiers();
|
||||
await CalculateMemberModifiers();
|
||||
using var cnx = await AppDbContext.ConnectAsync();
|
||||
using var tx = await cnx.BeginTransactionAsync();
|
||||
await DeleteInDb(cnx);
|
||||
await SetCalcTime(cnx);
|
||||
await CalculatePrices(cnx);
|
||||
await CalculateModifiers(cnx);
|
||||
await CalculateMemberModifiers(cnx);
|
||||
await tx.CommitAsync();
|
||||
}
|
||||
|
||||
public async Task Commit() {
|
||||
@ -68,15 +76,13 @@ namespace Elwig.Helpers.Billing {
|
||||
""");
|
||||
}
|
||||
|
||||
protected async Task SetCalcTime() {
|
||||
using var cnx = await AppDbContext.ConnectAsync();
|
||||
protected async Task SetCalcTime(SqliteConnection cnx) {
|
||||
await AppDbContext.ExecuteBatch(cnx, $"""
|
||||
UPDATE payment_variant SET calc_time = UNIXEPOCH() WHERE (year, avnr) = ({Year}, {AvNr})
|
||||
""");
|
||||
}
|
||||
|
||||
protected async Task DeleteInDb() {
|
||||
using var cnx = await AppDbContext.ConnectAsync();
|
||||
protected async Task DeleteInDb(SqliteConnection cnx) {
|
||||
await AppDbContext.ExecuteBatch(cnx, $"""
|
||||
DELETE FROM payment_delivery_part_bucket WHERE (year, avnr) = ({Year}, {AvNr});
|
||||
DELETE FROM payment_delivery_part WHERE (year, avnr) = ({Year}, {AvNr});
|
||||
@ -85,8 +91,7 @@ namespace Elwig.Helpers.Billing {
|
||||
""");
|
||||
}
|
||||
|
||||
protected async Task CalculateMemberModifiers() {
|
||||
using var cnx = await AppDbContext.ConnectAsync();
|
||||
protected async Task CalculateMemberModifiers(SqliteConnection cnx) {
|
||||
if (App.Client.IsMatzen) {
|
||||
var lastYears = 3;
|
||||
var multiplier = 0.50;
|
||||
@ -113,13 +118,22 @@ namespace Elwig.Helpers.Billing {
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task CalculatePrices() {
|
||||
using var cnx = await AppDbContext.ConnectAsync();
|
||||
protected async Task CalculatePrices(SqliteConnection cnx) {
|
||||
var jsonData = PaymentVariant.Data;
|
||||
var attrVariants = Context.DeliveryParts
|
||||
.Where(d => d.Year == Year)
|
||||
.Select(d => $"{d.SortId}{d.AttrId}")
|
||||
.Distinct()
|
||||
.ToList()
|
||||
.Union(Context.WineVarieties.Select(v => v.SortId))
|
||||
.ToList();
|
||||
|
||||
var parts = new List<(int Year, int DId, int DPNr, int BktNr, string SortId, string Discr, int Value, bool MinQuw, double Oe, double Kmw)>();
|
||||
var data = BillingData.FromJson(jsonData, attrVariants);
|
||||
|
||||
var parts = new List<(int Year, int DId, int DPNr, int BktNr, string SortId, string Discr, int Value, double Oe, double Kmw, string QualId)>();
|
||||
using (var cmd = cnx.CreateCommand()) {
|
||||
cmd.CommandText = $"""
|
||||
SELECT d.year, d.did, d.dpnr, b.bktnr, d.sortid, b.discr, b.value, d.min_quw, d.oe, d.kmw
|
||||
SELECT d.year, d.did, d.dpnr, b.bktnr, d.sortid, b.discr, b.value, d.oe, d.kmw, d.qualid
|
||||
FROM delivery_part_bucket b
|
||||
JOIN v_delivery d ON (d.year, d.did, d.dpnr) = (b.year, b.did, b.dpnr)
|
||||
WHERE b.year = {Year}
|
||||
@ -129,18 +143,17 @@ namespace Elwig.Helpers.Billing {
|
||||
parts.Add((
|
||||
reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2), reader.GetInt32(3),
|
||||
reader.GetString(4), reader.GetString(5), reader.GetInt32(6),
|
||||
reader.GetBoolean(7), reader.GetDouble(8), reader.GetDouble(9)
|
||||
reader.GetDouble(7), reader.GetDouble(8), reader.GetString(9)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
var inserts = new List<(int Year, int DId, int DPNr, int BktNr, long Price, long Amount)>();
|
||||
foreach (var part in parts) {
|
||||
var price = !part.MinQuw ? 0.5m : ((part.BktNr == 2 ? 0.8m : (part.BktNr == 1 ? 0.7m : 0.6m)) + ((decimal)(part.Oe - 73) * 0.005m)); // TODO
|
||||
price += (AvNr - 1) * 0.2m;
|
||||
var priceL = Utils.DecToDb(price, 4);
|
||||
var amount = Utils.DecToDb(price * part.Value, 4);
|
||||
inserts.Add((part.Year, part.DId, part.DPNr, part.BktNr, priceL, amount));
|
||||
var attrId = (part.Discr == "_" || part.Discr == "") ? null : part.Discr;
|
||||
var price = data.CalculatePrice(part.SortId, attrId, part.QualId, part.Discr != "_", part.Oe, part.Kmw);
|
||||
var priceL = PaymentVariant.Season.DecToDb(price);
|
||||
inserts.Add((part.Year, part.DId, part.DPNr, part.BktNr, priceL, priceL * part.Value));
|
||||
}
|
||||
|
||||
await AppDbContext.ExecuteBatch(cnx, $"""
|
||||
@ -149,8 +162,7 @@ namespace Elwig.Helpers.Billing {
|
||||
""");
|
||||
}
|
||||
|
||||
protected async Task CalculateModifiers() {
|
||||
using var cnx = await AppDbContext.ConnectAsync();
|
||||
protected async Task CalculateModifiers(SqliteConnection cnx) {
|
||||
await AppDbContext.ExecuteBatch(cnx, $"""
|
||||
INSERT INTO payment_delivery_part (year, did, dpnr, avnr, net_amount, mod_abs, mod_rel)
|
||||
SELECT d.year, d.did, d.dpnr, {AvNr}, 0, COALESCE(m.abs, 0), COALESCE(m.rel, 0)
|
||||
|
Reference in New Issue
Block a user