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.

This commit is contained in:
Torsten Schulz (local)
2025-12-20 15:20:03 +01:00
parent 398e0ba677
commit 1719af2344
2 changed files with 20 additions and 9 deletions

View File

@@ -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

View File

@@ -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());