Enhance tax calculation in DirectorWorker: Integrate DEFAULT_TAX_PERCENT and DEFAULT_TREASURY_USER_ID for improved financial operations during product sales.

This commit is contained in:
Torsten Schulz (local)
2025-12-10 12:57:08 +01:00
parent 433259da74
commit 1134be0f8d
2 changed files with 47 additions and 11 deletions

View File

@@ -17,6 +17,11 @@ pub(crate) struct WorkerState {
pub(crate) current_step: Mutex<String>,
}
// Default tax percent and treasury user id used if no external config is available.
// Percent, e.g. 10.0 => 10%.
pub const DEFAULT_TAX_PERCENT: f64 = 10.0;
pub const DEFAULT_TREASURY_USER_ID: i32 = 1;
impl WorkerState {
pub(crate) fn new(name: &str) -> Self {
Self {

View File

@@ -6,7 +6,7 @@ use std::sync::Arc;
use std::time::{Duration, Instant};
use crate::db::ConnectionPool;
use super::base::{BaseWorker, Worker, WorkerState};
use super::base::{BaseWorker, Worker, WorkerState, DEFAULT_TAX_PERCENT, DEFAULT_TREASURY_USER_ID};
#[derive(Debug, Clone)]
struct Director {
@@ -1001,20 +1001,51 @@ impl DirectorWorker {
let sell_price = piece_sell_price * item.quantity as f64;
// Steuerberechnung und Auszahlung: Berechne in Cent und runde wie JS Math.round
let revenue_cents = (sell_price * 100.0).round() as i64;
// Produktionskosten nicht direkt verfügbar hier, wir approximieren profit als revenue
// (wenn präzisere Kosten nötig sind, kann man production_cost übergeben)
let cost_cents = 0i64;
let tax_percent = DEFAULT_TAX_PERCENT;
let profit_cents = (revenue_cents - cost_cents).max(0);
let tax_cents = ((profit_cents as f64) * (tax_percent / 100.0)).round() as i64;
let payout_cents = revenue_cents - tax_cents;
// Credit tax to treasury
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),
) {
eprintln!("[DirectorWorker] Fehler bei change_falukant_user_money (tax): {err}");
}
}
// Payout to seller
let payout_amount = (payout_cents as f64) / 100.0;
if payout_cents != 0 {
if let Err(err) = self.base.change_falukant_user_money(
item.user_id,
sell_price,
payout_amount,
"sell products",
) {
eprintln!(
"[DirectorWorker] Fehler bei change_falukant_user_money (sell products): {err}"
);
}
}
// Debug: Log vor dem DB-Aufruf
eprintln!(
"[DirectorWorker] calling change_falukant_user_money for sell products: user_id={}, money_change={}, product_id={}",
item.user_id, sell_price, item.product_id
"[DirectorWorker] sell: user_id={}, revenue={:.2}, tax={:.2}, payout={:.2}, product_id={}",
item.user_id,
sell_price,
(tax_cents as f64) / 100.0,
payout_amount,
item.product_id
);
conn.execute(