BillingVariant: Assume price to be 0 for undefined vaributes
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Test / Run tests (push) Successful in 1m56s
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Test / Run tests (push) Successful in 1m56s
				
			This commit is contained in:
		@@ -20,13 +20,19 @@ namespace Elwig.Helpers.Billing {
 | 
			
		||||
            Data = PaymentBillingData.FromJson(PaymentVariant.Data, Utils.GetVaributes(ctx, Year, onlyDelivered: false));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Calculate(bool? honorGebunden = null, bool? allowAttrsIntoLower = null, bool? avoidUnderDeliveries = null) {
 | 
			
		||||
        public async Task Calculate(bool strictPrices = true, bool? honorGebunden = null, bool? allowAttrsIntoLower = null, bool? avoidUnderDeliveries = null) {
 | 
			
		||||
            using var cnx = await AppDbContext.ConnectAsync();
 | 
			
		||||
            using var tx = await cnx.BeginTransactionAsync();
 | 
			
		||||
            await CalculateBuckets(honorGebunden, allowAttrsIntoLower, avoidUnderDeliveries, cnx);
 | 
			
		||||
            await DeleteInDb(cnx);
 | 
			
		||||
            await SetCalcTime(cnx);
 | 
			
		||||
            await CalculatePrices(cnx);
 | 
			
		||||
            KeyNotFoundException? exception = null;
 | 
			
		||||
            try {
 | 
			
		||||
                await CalculatePrices(cnx, strictPrices);
 | 
			
		||||
            } catch (KeyNotFoundException e) {
 | 
			
		||||
                if (strictPrices) throw;
 | 
			
		||||
                exception = e;
 | 
			
		||||
            }
 | 
			
		||||
            if (Data.ConsiderDelieryModifiers) {
 | 
			
		||||
                await CalculateDeliveryModifiers(cnx);
 | 
			
		||||
            }
 | 
			
		||||
@@ -34,6 +40,8 @@ namespace Elwig.Helpers.Billing {
 | 
			
		||||
                await CalculateMemberModifiers(cnx);
 | 
			
		||||
            }
 | 
			
		||||
            await tx.CommitAsync();
 | 
			
		||||
            if (exception != null)
 | 
			
		||||
                throw exception;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Commit() {
 | 
			
		||||
@@ -142,7 +150,8 @@ namespace Elwig.Helpers.Billing {
 | 
			
		||||
                """);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected async Task CalculatePrices(SqliteConnection cnx) {
 | 
			
		||||
        protected async Task CalculatePrices(SqliteConnection cnx, bool strict = true) {
 | 
			
		||||
            var invalid = new HashSet<string>();
 | 
			
		||||
            var parts = new List<(int Year, int DId, int DPNr, int BktNr, string SortId, string? AttrId, string? CultId, string Discr, int Value, double Oe, double Kmw, string QualId, bool AttrAreaCom)>();
 | 
			
		||||
            using (var cmd = cnx.CreateCommand()) {
 | 
			
		||||
                cmd.CommandText = $"""
 | 
			
		||||
@@ -172,15 +181,25 @@ namespace Elwig.Helpers.Billing {
 | 
			
		||||
                var payAttrId = (part.Discr is "" or "_") ? null : part.Discr;
 | 
			
		||||
                var attrId = part.AttrAreaCom ? payAttrId : part.AttrId;
 | 
			
		||||
                var geb = !ungeb && (payAttrId == attrId || !part.AttrAreaCom);
 | 
			
		||||
                var price = Data.CalculatePrice(part.SortId, attrId, part.CultId, part.QualId, geb, part.Oe, part.Kmw);
 | 
			
		||||
                decimal price = 0;
 | 
			
		||||
                try {
 | 
			
		||||
                    price = Data.CalculatePrice(part.SortId, attrId, part.CultId, part.QualId, geb, part.Oe, part.Kmw);
 | 
			
		||||
                } catch (KeyNotFoundException e) {
 | 
			
		||||
                    invalid.Add(e.Message.Split('\'')[1]);
 | 
			
		||||
                }
 | 
			
		||||
                var priceL = PaymentVariant.Season.DecToDb(price);
 | 
			
		||||
                inserts.Add((part.Year, part.DId, part.DPNr, part.BktNr, priceL, priceL * part.Value));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var msg = invalid.Count == 0 ? null : "Für folgende Sorten wurde noch keine Preiskurve festgelegt: " + string.Join(", ", invalid);
 | 
			
		||||
            if (msg != null && strict)
 | 
			
		||||
                throw new KeyNotFoundException(msg);
 | 
			
		||||
            await AppDbContext.ExecuteBatch(cnx, $"""
 | 
			
		||||
                INSERT INTO payment_delivery_part_bucket (year, did, dpnr, bktnr, avnr, price, amount)
 | 
			
		||||
                VALUES {string.Join(",\n       ", inserts.Select(i => $"({i.Year}, {i.DId}, {i.DPNr}, {i.BktNr}, {AvNr}, {i.Price}, {i.Amount})"))};
 | 
			
		||||
                """);
 | 
			
		||||
            if (msg != null)
 | 
			
		||||
                throw new KeyNotFoundException(msg);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected async Task CalculateDeliveryModifiers(SqliteConnection cnx) {
 | 
			
		||||
 
 | 
			
		||||
@@ -664,8 +664,10 @@ namespace Elwig.Windows {
 | 
			
		||||
            try {
 | 
			
		||||
                await Task.Run(async () => {
 | 
			
		||||
                    var b = new BillingVariant(PaymentVar.Year, PaymentVar.AvNr);
 | 
			
		||||
                    await b.Calculate();
 | 
			
		||||
                    await b.Calculate(false);
 | 
			
		||||
                });
 | 
			
		||||
            } catch (KeyNotFoundException exc) {
 | 
			
		||||
                MessageBox.Show(exc.Message, "Noch nicht alle Preise festgelegt", MessageBoxButton.OK, MessageBoxImage.Information);
 | 
			
		||||
            } catch (Exception exc) {
 | 
			
		||||
                MessageBox.Show(exc.Message, "Berechnungsfehler", MessageBoxButton.OK, MessageBoxImage.Error);
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
@@ -188,7 +188,7 @@ namespace Tests.UnitTests.HelperTests {
 | 
			
		||||
                Assert.That(payment["GV"],  Is.EqualTo(10_000));
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            await b.Calculate(false, false, false);
 | 
			
		||||
            await b.Calculate(true, false, false, false);
 | 
			
		||||
            var prices = await GetMemberDeliveryPrices(year, mgnr);
 | 
			
		||||
            Assert.Multiple(() => {
 | 
			
		||||
                Assert.That(prices, Has.Count.EqualTo(7));
 | 
			
		||||
@@ -234,7 +234,7 @@ namespace Tests.UnitTests.HelperTests {
 | 
			
		||||
                Assert.That(payment["GV"],  Is.EqualTo(8_000));
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            await b.Calculate(true, false, false);
 | 
			
		||||
            await b.Calculate(true, true, false, false);
 | 
			
		||||
            var prices = await GetMemberDeliveryPrices(year, mgnr);
 | 
			
		||||
            Assert.Multiple(() => {
 | 
			
		||||
                Assert.That(prices, Has.Count.EqualTo(6));
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user