From 1719af2344e250d19d044175e04c1a2e4701f1a5 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Sat, 20 Dec 2025 15:20:03 +0100 Subject: [PATCH] Refactor revenue and tax calculations in DirectorWorker: Adjust the pricing logic to account for cumulative tax inflation, ensuring accurate revenue and payout calculations. Update SQL query for sell logs to change conflict resolution order for better data integrity. --- src/worker/director.rs | 27 +++++++++++++++++++-------- src/worker/sql.rs | 2 +- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/worker/director.rs b/src/worker/director.rs index dd4fc51..9ce2e06 100644 --- a/src/worker/director.rs +++ b/src/worker/director.rs @@ -808,18 +808,29 @@ impl DirectorWorker { // compute piece price and full sell price let piece_price = Self::compute_piece_sell_price(item); - let sell_price = piece_price * item.quantity as f64; - + let one_piece_cost = Self::resolve_one_piece_cost(conn, item.product_id, item.sell_cost)?; let cumulative_tax_percent = Self::get_cumulative_tax_percent(conn, item.branch_id, item.user_id)?; - let revenue_cents = (sell_price * 100.0).round() as i64; - let cost_cents = (one_piece_cost * item.quantity as f64 * 100.0).round() as i64; - let profit_cents = (revenue_cents - cost_cents).max(0); - let tax_cents = ((profit_cents as f64) * cumulative_tax_percent / 100.0).round() as i64; + // Preis-Inflation: Der Preis wird basierend auf der Steuer inflatiert, + // damit der Netto-Betrag für den Verkäufer gleich bleibt (wie im Frontend) + let inflation_factor = if cumulative_tax_percent >= 100.0 { + 1.0 + } else { + 1.0 / (1.0 - cumulative_tax_percent / 100.0) + }; + let adjusted_price_per_unit = (piece_price * inflation_factor * 100.0).round() / 100.0; + let revenue = adjusted_price_per_unit * item.quantity as f64; + + // Steuer wird auf Revenue berechnet (nicht auf Profit), wie im Frontend + let revenue_cents = (revenue * 100.0).round() as i64; + let tax_cents = ((revenue * cumulative_tax_percent / 100.0) * 100.0).round() as i64; let payout_cents = revenue_cents - tax_cents; + + let cost = one_piece_cost * item.quantity as f64; + let profit_cents = revenue_cents - (cost * 100.0).round() as i64; - eprintln!("[DirectorWorker] sell: revenue={:.2}, cost={:.2}, profit_cents={}, tax%={:.2}, tax_cents={}, payout_cents={}", sell_price, one_piece_cost * item.quantity as f64, profit_cents, cumulative_tax_percent, tax_cents, payout_cents); + eprintln!("[DirectorWorker] sell: revenue={:.2}, cost={:.2}, profit_cents={}, tax%={:.2}, tax_cents={}, payout_cents={}", revenue, cost, profit_cents, cumulative_tax_percent, tax_cents, payout_cents); if tax_cents > 0 { let tax_amount = (tax_cents as f64) / 100.0; @@ -837,7 +848,7 @@ impl DirectorWorker { eprintln!( "[DirectorWorker] sell: user_id={}, revenue={:.2}, tax={:.2}, payout={:.2}, product_id={}", item.user_id, - sell_price, + revenue, (tax_cents as f64) / 100.0, payout_amount, item.product_id diff --git a/src/worker/sql.rs b/src/worker/sql.rs index cd325a7..7754ba2 100644 --- a/src/worker/sql.rs +++ b/src/worker/sql.rs @@ -208,7 +208,7 @@ DELETE FROM falukant_data.inventory WHERE id = $1; pub const QUERY_ADD_SELL_LOG: &str = r#" INSERT INTO falukant_log.sell (region_id, product_id, quantity, seller_id, sell_timestamp) VALUES ($1, $2, $3, $4, NOW()) -ON CONFLICT (region_id, product_id, seller_id) +ON CONFLICT (seller_id, product_id, region_id) DO UPDATE SET quantity = falukant_log.sell.quantity + EXCLUDED.quantity, sell_timestamp = COALESCE(EXCLUDED.sell_timestamp, NOW());