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:
@@ -563,16 +563,35 @@ impl DirectorWorker {
|
|||||||
// item.quantity wurde dort bereits aktualisiert
|
// 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(..) {
|
for item in items.drain(..) {
|
||||||
if item.quantity > 0 {
|
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 {
|
} else {
|
||||||
// Falls die Menge auf 0 gesetzt wurde, das Inventar ggf. aufräumen.
|
// Falls die Menge auf 0 gesetzt wurde, das Inventar ggf. aufräumen.
|
||||||
conn.execute("remove_inventory", &[&item.id])?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -676,14 +695,16 @@ impl DirectorWorker {
|
|||||||
Ok(cumulative_tax_percent)
|
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(
|
fn sell_single_inventory_item(
|
||||||
&mut self,
|
&mut self,
|
||||||
conn: &mut DbConnection,
|
conn: &mut DbConnection,
|
||||||
item: &InventoryItem,
|
item: &InventoryItem,
|
||||||
) -> Result<(), DbError> {
|
) -> Result<(f64, f64), DbError> {
|
||||||
if item.quantity <= 0 {
|
if item.quantity <= 0 {
|
||||||
conn.execute("remove_inventory", &[&item.id])?;
|
conn.execute("remove_inventory", &[&item.id])?;
|
||||||
return Ok(());
|
return Ok((0.0, 0.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// compute piece price and full sell price
|
// 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 tax_cents = ((profit_cents as f64) * cumulative_tax_percent / 100.0).round() as i64;
|
||||||
let payout_cents = revenue_cents - tax_cents;
|
let payout_cents = revenue_cents - tax_cents;
|
||||||
|
|
||||||
if tax_cents > 0 {
|
let tax_amount = if tax_cents > 0 {
|
||||||
let tax_amount = (tax_cents as f64) / 100.0;
|
(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));
|
} else {
|
||||||
}
|
0.0
|
||||||
|
};
|
||||||
|
|
||||||
let payout_amount = (payout_cents as f64) / 100.0;
|
let payout_amount = if payout_cents != 0 {
|
||||||
if payout_cents != 0 {
|
(payout_cents as f64) / 100.0
|
||||||
let _ = self.base.change_falukant_user_money(item.user_id, payout_amount, "sell products");
|
} else {
|
||||||
}
|
0.0
|
||||||
|
};
|
||||||
|
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"[DirectorWorker] Verkauf: branch_id={}, product_id={}, quantity={}, revenue={:.2}, tax%={:.2}, payout={:.2}, user_id={}",
|
"[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);
|
self.base.broker.publish(message);
|
||||||
|
|
||||||
Ok(())
|
Ok((payout_amount, tax_amount))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Plant ggf. Transporte für ein einzelnes Inventar-Item und gibt die
|
/// Plant ggf. Transporte für ein einzelnes Inventar-Item und gibt die
|
||||||
|
|||||||
Reference in New Issue
Block a user