Refactor revenue and tax calculations in DirectorWorker: Update pricing logic to compute revenue based on piece price and quantity, adjust profit calculations, and implement optional treasury user ID from environment variables for tax handling. This enhances accuracy in financial computations and improves code clarity.

This commit is contained in:
Torsten Schulz (local)
2025-12-23 12:16:58 +01:00
parent 431a6aff08
commit ade3bff5cf

View File

@@ -910,35 +910,38 @@ impl DirectorWorker {
return Ok(());
}
// compute piece price and full sell price
let piece_price = Self::compute_piece_sell_price(item);
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)?;
// compute piece price and full sell price
let piece_price = Self::compute_piece_sell_price(item);
let revenue = 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)?;
// 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;
let cost_cents = (cost * 100.0).round() as i64;
let profit_cents = (revenue_cents - cost_cents).max(0);
// Steuer wird vom Gewinn abgezogen (nicht „zugerechnet“)
let tax_cents = ((profit_cents as f64) * cumulative_tax_percent / 100.0).round() as i64;
let payout_cents = revenue_cents - tax_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);
// Treasury-User-ID optional per ENV, fallback auf DEFAULT_TREASURY_USER_ID
let treasury_user_id: i32 = std::env::var("TREASURY_FALUKANT_USER_ID")
.ok()
.and_then(|v| v.parse::<i32>().ok())
.unwrap_or(DEFAULT_TREASURY_USER_ID);
if tax_cents > 0 {
let tax_amount = (tax_cents as f64) / 100.0;
if let Err(err) = self.base.change_falukant_user_money(DEFAULT_TREASURY_USER_ID, tax_amount, &format!("tax from sale product {}", item.product_id)) {
if let Err(err) = self.base.change_falukant_user_money(
treasury_user_id,
tax_amount,
&format!("tax from sale product {}", item.product_id),
) {
eprintln!("[DirectorWorker] Fehler bei change_falukant_user_money (tax): {err}");
}
}