Refactor sales processing in DirectorWorker: Aggregate local sales payouts and taxes for improved efficiency. Update sell_single_inventory_item to return payout and tax amounts, ensuring accurate financial updates. Enhance money transfer logic to handle aggregated sales data, streamlining financial interactions.

This commit is contained in:
Torsten Schulz (local)
2026-03-23 12:21:23 +01:00
parent e811ec6264
commit 195bf49d14

View File

@@ -563,16 +563,35 @@ impl DirectorWorker {
// item.quantity wurde dort bereits aktualisiert
}
// Anschließend lokale Verkäufe für die verbleibenden Mengen durchführen.
// Lokale Verkäufe: Geld-Updates gebündelt (eine money_history-Zeile pro Nutzer/Steuer
// statt einer Zeile pro Inventarzeile).
let mut total_payout: f64 = 0.0;
let mut total_tax: f64 = 0.0;
for item in items.drain(..) {
if item.quantity > 0 {
self.sell_single_inventory_item(&mut conn, &item)?;
let (payout, tax) = self.sell_single_inventory_item(&mut conn, &item)?;
total_payout += payout;
total_tax += tax;
} else {
// Falls die Menge auf 0 gesetzt wurde, das Inventar ggf. aufräumen.
conn.execute("remove_inventory", &[&item.id])?;
}
}
total_payout = (total_payout * 100.0).round() / 100.0;
total_tax = (total_tax * 100.0).round() / 100.0;
if total_tax > 0.0 {
let _ = self.base.change_falukant_user_money(
DEFAULT_TREASURY_USER_ID,
total_tax,
&format!("tax from sales branch {}", director.branch_id),
);
}
if total_payout != 0.0 {
let _ = self.base.change_falukant_user_money(falukant_user_id, total_payout, "sell products");
}
Ok(())
}
@@ -676,14 +695,16 @@ impl DirectorWorker {
Ok(cumulative_tax_percent)
}
/// Verkauft eine Inventarzeile; Geld fließt erst nach Aggregation in [`start_sellings`].
/// Rückgabe: `(Auszahlung an Spieler, Steuer ans Finanzamt)` in gleicher Währung wie die DB.
fn sell_single_inventory_item(
&mut self,
conn: &mut DbConnection,
item: &InventoryItem,
) -> Result<(), DbError> {
) -> Result<(f64, f64), DbError> {
if item.quantity <= 0 {
conn.execute("remove_inventory", &[&item.id])?;
return Ok(());
return Ok((0.0, 0.0));
}
// compute piece price and full sell price
@@ -699,15 +720,17 @@ impl DirectorWorker {
let tax_cents = ((profit_cents as f64) * cumulative_tax_percent / 100.0).round() as i64;
let payout_cents = revenue_cents - tax_cents;
if tax_cents > 0 {
let tax_amount = (tax_cents as f64) / 100.0;
let _ = self.base.change_falukant_user_money(DEFAULT_TREASURY_USER_ID, tax_amount, &format!("tax from sale product {}", item.product_id));
}
let tax_amount = if tax_cents > 0 {
(tax_cents as f64) / 100.0
} else {
0.0
};
let payout_amount = (payout_cents as f64) / 100.0;
if payout_cents != 0 {
let _ = self.base.change_falukant_user_money(item.user_id, payout_amount, "sell products");
}
let payout_amount = if payout_cents != 0 {
(payout_cents as f64) / 100.0
} else {
0.0
};
eprintln!(
"[DirectorWorker] Verkauf: branch_id={}, product_id={}, quantity={}, revenue={:.2}, tax%={:.2}, payout={:.2}, user_id={}",
@@ -738,7 +761,7 @@ impl DirectorWorker {
);
self.base.broker.publish(message);
Ok(())
Ok((payout_amount, tax_amount))
}
/// Plant ggf. Transporte für ein einzelnes Inventar-Item und gibt die