Refactor production cost calculations and SQL query: Updated the QUERY_GET_BEST_PRODUCTION to align profit calculations with the new logic for selling prices based on knowledge levels. Enhanced documentation to clarify the ranking criteria and the relationship between production costs and pricing strategies. Adjusted related functions in director.rs to ensure consistency across the application.
All checks were successful
Deploy yourpart (blue-green) / deploy (push) Successful in 2m54s

This commit is contained in:
Torsten Schulz (local)
2026-04-09 09:08:53 +02:00
parent dda559cbe7
commit bb1c1c4133
3 changed files with 39 additions and 17 deletions

View File

@@ -73,9 +73,14 @@ JOIN falukant_data.branch b ON b.region_id = c.region_id AND b.falukant_user_id
WHERE current_time BETWEEN '08:00:00' AND '17:00:00';
"#;
/// Bester Produktstart: **Gewinn pro Minute** (wie UI „Gewinn/Minute“) ≈ `(Erlös/Stück Stückkosten) / Produktionszeit`.
/// Stückkosten = gleiche Formel wie `Director::piece_production_cost` (Basis 6 + Kategorie, Headroom-Rabatt).
/// Marktpreis: mit Fahrzeug `MAX(worth_percent)` über Filialregionen, sonst nur Direktor-Region. **Ohne** Steuer im Ranking.
/// Bester Produktstart: **Gewinn pro Minute** ≈ `(Verkaufspreis/Stück Stückkosten) / Produktionszeit` — an
/// `DirectorWorker::compute_piece_price_for_percent` angeglichen: regionaler Basispreis
/// `sell_cost × worth/100`, dann linear **60%100%** dieser Basis je nach Wissen (0100), gleiche Idee wie
/// `min_price + (maxmin)×k/100` in `director.rs`. Wissensblend `(2×Charakter + Direktor)/3`. UI soll dieselbe
/// Logik nutzen; weicht das Node-Frontend ab (z.B. 70% statt 60%), dort angleichen.
///
/// Stückkosten wie `Director::piece_production_cost`. Mit Fahrzeug: `MAX(worth_percent)` über Filialregionen;
/// ohne Fahrzeug: nur Direktor-Region. **Ohne** Steuer im Ranking.
pub const QUERY_GET_BEST_PRODUCTION: &str = r#"
SELECT fdu.id falukant_user_id, CAST(fdu.money AS text) AS money, fdu.certificate, ftp.id product_id, ftp.label_tr,
COALESCE(ftp.category, 1)::int AS product_category, fdb.region_id,
@@ -83,15 +88,28 @@ COALESCE(ftp.category, 1)::int AS product_category, fdb.region_id,
COALESCE((SELECT SUM(COALESCE(fdi.quantity, 0)) FROM falukant_data.stock fds JOIN falukant_data.inventory fdi ON fdi.stock_id = fds.id WHERE fds.branch_id = fdb.id), 0) AS used_in_stock,
(
(
ftp.sell_cost * (
CASE
WHEN EXISTS (
SELECT 1 FROM falukant_data.vehicle v
WHERE v.falukant_user_id = fdu.id
(
ftp.sell_cost * (
CASE
WHEN EXISTS (
SELECT 1 FROM falukant_data.vehicle v
WHERE v.falukant_user_id = fdu.id
)
THEN bw.max_worth_pct
ELSE COALESCE(fdtpw_local.worth_percent::float8, 0.0)
END / 100.0
)
) * (
0.6 + 0.4 * LEAST(
100.0::float8,
GREATEST(
0.0::float8,
(
2.0 * COALESCE(fdk_character.knowledge, 0)::float8
+ COALESCE(fdk_director.knowledge, 0)::float8
) / 3.0
)
THEN bw.max_worth_pct
ELSE COALESCE(fdtpw_local.worth_percent::float8, 0.0)
END / 100.0
) / 100.0
)
- (
(6.0 + (GREATEST(COALESCE(ftp.category, 1), 1))::float8)