From 1134be0f8d6898e037233fc2c316845d68ad5ec3 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Wed, 10 Dec 2025 12:57:08 +0100 Subject: [PATCH] Enhance tax calculation in DirectorWorker: Integrate DEFAULT_TAX_PERCENT and DEFAULT_TREASURY_USER_ID for improved financial operations during product sales. --- src/worker/base.rs | 5 ++++ src/worker/director.rs | 53 +++++++++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/worker/base.rs b/src/worker/base.rs index cb2dba1..419af42 100644 --- a/src/worker/base.rs +++ b/src/worker/base.rs @@ -17,6 +17,11 @@ pub(crate) struct WorkerState { pub(crate) current_step: Mutex, } +// 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 { diff --git a/src/worker/director.rs b/src/worker/director.rs index faa5db8..8b5f507 100644 --- a/src/worker/director.rs +++ b/src/worker/director.rs @@ -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; - if let Err(err) = self.base.change_falukant_user_money( - item.user_id, - sell_price, - "sell products", - ) { - eprintln!( - "[DirectorWorker] Fehler bei change_falukant_user_money (sell products): {err}" - ); + // 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, + 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(