Add regional pricing calculation for products in FalukantService
- Introduced a new function `calcRegionalSellPrice` to compute product prices based on regional worth percentages. - Updated existing methods to utilize the new pricing logic, ensuring revenue calculations reflect regional variations. - Integrated retrieval of `TownProductWorth` data to enhance pricing accuracy across different regions.
This commit is contained in:
@@ -61,6 +61,7 @@ import VehicleType from '../models/falukant/type/vehicle.js';
|
||||
import Vehicle from '../models/falukant/data/vehicle.js';
|
||||
import Transport from '../models/falukant/data/transport.js';
|
||||
import RegionDistance from '../models/falukant/data/region_distance.js';
|
||||
import TownProductWorth from '../models/falukant/data/town_product_worth.js';
|
||||
|
||||
function calcAge(birthdate) {
|
||||
const b = new Date(birthdate); b.setHours(0, 0);
|
||||
@@ -88,6 +89,22 @@ function calcSellPrice(product, knowledgeFactor = 0) {
|
||||
return min + (max - min) * (knowledgeFactor / 100);
|
||||
}
|
||||
|
||||
async function calcRegionalSellPrice(product, knowledgeFactor, regionId) {
|
||||
// Hole TownProductWorth für diese Region und dieses Produkt
|
||||
const townWorth = await TownProductWorth.findOne({
|
||||
where: { productId: product.id, regionId: regionId }
|
||||
});
|
||||
const worthPercent = townWorth?.worthPercent || 50; // Default 50% wenn nicht gefunden
|
||||
|
||||
// Basispreis basierend auf regionalem worthPercent
|
||||
const basePrice = product.sellCost * (worthPercent / 100);
|
||||
|
||||
// Dann Knowledge-Faktor anwenden
|
||||
const min = basePrice * 0.6;
|
||||
const max = basePrice;
|
||||
return min + (max - min) * (knowledgeFactor / 100);
|
||||
}
|
||||
|
||||
function calculateMarriageCost(titleOfNobility, age) {
|
||||
const minTitle = 1;
|
||||
const adjustedTitle = titleOfNobility - minTitle + 1;
|
||||
@@ -1121,7 +1138,8 @@ class FalukantService extends BaseService {
|
||||
if (available < quantity) throw new Error('Not enough inventory available');
|
||||
const item = inventory[0].productType;
|
||||
const knowledgeVal = item.knowledges?.[0]?.knowledge || 0;
|
||||
const revenue = quantity * calcSellPrice(item, knowledgeVal);
|
||||
const pricePerUnit = await calcRegionalSellPrice(item, knowledgeVal, branch.regionId);
|
||||
const revenue = quantity * pricePerUnit;
|
||||
const moneyResult = await updateFalukantUserMoney(user.id, revenue, 'Product sale', user.id);
|
||||
if (!moneyResult.success) throw new Error('Failed to update money');
|
||||
let remaining = quantity;
|
||||
@@ -1189,7 +1207,9 @@ class FalukantService extends BaseService {
|
||||
let total = 0;
|
||||
for (const item of inventory) {
|
||||
const knowledgeVal = item.productType.knowledges[0]?.knowledge || 0;
|
||||
total += item.quantity * calcSellPrice(item.productType, knowledgeVal);
|
||||
const regionId = item.stock.branch.regionId;
|
||||
const pricePerUnit = await calcRegionalSellPrice(item.productType, knowledgeVal, regionId);
|
||||
total += item.quantity * pricePerUnit;
|
||||
await this.addSellItem(item.stock.branch.id, falukantUser.id, item.productType.id, item.quantity);
|
||||
}
|
||||
const moneyResult = await updateFalukantUserMoney(
|
||||
@@ -3564,10 +3584,26 @@ class FalukantService extends BaseService {
|
||||
]
|
||||
});
|
||||
|
||||
// TownProductWorth für alle Städte und dieses Produkt abrufen
|
||||
const townWorths = await TownProductWorth.findAll({
|
||||
where: { productId: productId },
|
||||
attributes: ['regionId', 'worthPercent']
|
||||
});
|
||||
const worthMap = new Map(townWorths.map(tw => [tw.regionId, tw.worthPercent]));
|
||||
|
||||
// Für jede Stadt den Preis berechnen und Branch-Typ bestimmen
|
||||
const results = [];
|
||||
for (const city of cities) {
|
||||
const priceInCity = calcSellPrice(product, knowledgeFactor);
|
||||
// Regionaler Preis-Faktor (worthPercent zwischen 40-60)
|
||||
const worthPercent = worthMap.get(city.id) || 50; // Default 50% wenn nicht gefunden
|
||||
|
||||
// Basispreis basierend auf regionalem worthPercent
|
||||
const basePrice = product.sellCost * (worthPercent / 100);
|
||||
|
||||
// Dann Knowledge-Faktor anwenden (wie in calcSellPrice)
|
||||
const min = basePrice * 0.6;
|
||||
const max = basePrice;
|
||||
const priceInCity = min + (max - min) * (knowledgeFactor / 100);
|
||||
|
||||
// Nur Städte zurückgeben, wo der Preis höher ist
|
||||
if (priceInCity > currentPrice) {
|
||||
|
||||
Reference in New Issue
Block a user