ysdsd
This commit is contained in:
@@ -700,24 +700,7 @@ final class AnalyticsService
|
||||
continue;
|
||||
}
|
||||
|
||||
$storedDailyCost = is_numeric($entry['daily_cost_amount'] ?? null) ? (float) $entry['daily_cost_amount'] : null;
|
||||
$entryCurrency = strtoupper(trim((string) ($entry['daily_cost_currency'] ?? $entry['currency'] ?? '')));
|
||||
if ($entryCurrency === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$planDailyCost = $storedDailyCost;
|
||||
if ($planDailyCost === null || $planDailyCost <= 0) {
|
||||
$amount = is_numeric($entry['total_cost_amount'] ?? null) ? (float) $entry['total_cost_amount'] : null;
|
||||
if ($amount === null || $amount <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$runtimeDays = $runtimeMonths * 30.4375;
|
||||
$planDailyCost = $amount / $runtimeDays;
|
||||
}
|
||||
|
||||
$convertedDailyCost = $this->convertAmount($planDailyCost, $entryCurrency, $currency, $fxContext);
|
||||
$convertedDailyCost = $this->entryDailyCostInCurrency($entry, $runtimeMonths, $currency, $fxContext);
|
||||
if ($convertedDailyCost === null) {
|
||||
continue;
|
||||
}
|
||||
@@ -729,6 +712,74 @@ final class AnalyticsService
|
||||
return $matched ? $dailyTotal : null;
|
||||
}
|
||||
|
||||
private function entryDailyCostInCurrency(array $entry, int $runtimeMonths, string $targetCurrency, ?array $fxContext = null): ?float
|
||||
{
|
||||
$runtimeDays = $runtimeMonths * 30.4375;
|
||||
if ($runtimeDays <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->entryIsCryptoPaid($entry)) {
|
||||
$settledAmount = is_numeric($entry['settled_value_amount'] ?? null) ? (float) $entry['settled_value_amount'] : null;
|
||||
$settledCurrency = strtoupper(trim((string) ($entry['settled_value_currency'] ?? '')));
|
||||
$spentAmount = is_numeric($entry['total_cost_amount'] ?? null) ? (float) $entry['total_cost_amount'] : null;
|
||||
$usesLegacyFallback = false;
|
||||
|
||||
if ($settledAmount === null || $settledAmount <= 0 || $settledCurrency === '') {
|
||||
if ($spentAmount === null || $spentAmount <= 0) {
|
||||
return null;
|
||||
}
|
||||
$settledAmount = $spentAmount * 0.07;
|
||||
$settledCurrency = 'USD';
|
||||
$usesLegacyFallback = true;
|
||||
}
|
||||
|
||||
$dailyAmount = $settledAmount / $runtimeDays;
|
||||
$target = strtoupper(trim($targetCurrency));
|
||||
if ($settledCurrency === $target) {
|
||||
return $dailyAmount;
|
||||
}
|
||||
|
||||
// The legacy fallback has a separately agreed fixed EUR value and must not use current FX.
|
||||
if ($usesLegacyFallback && $target === 'EUR' && $spentAmount !== null) {
|
||||
return ($spentAmount * 0.068) / $runtimeDays;
|
||||
}
|
||||
|
||||
$historicalFxContext = [
|
||||
'fx_fetch_id' => $entry['settled_fx_fetch_id'] ?? null,
|
||||
'measured_at' => $entry['purchased_at'] ?? null,
|
||||
];
|
||||
$converted = $this->convertAmount($dailyAmount, $settledCurrency, $target, $historicalFxContext);
|
||||
if ($converted !== null) {
|
||||
return $converted;
|
||||
}
|
||||
|
||||
// Older records without a snapshot retain the best available legacy conversion.
|
||||
if (!is_numeric($entry['settled_fx_fetch_id'] ?? null)) {
|
||||
return $this->convertAmount($dailyAmount, $settledCurrency, $target, $fxContext);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$storedDailyCost = is_numeric($entry['daily_cost_amount'] ?? null) ? (float) $entry['daily_cost_amount'] : null;
|
||||
$entryCurrency = strtoupper(trim((string) ($entry['daily_cost_currency'] ?? $entry['currency'] ?? '')));
|
||||
if ($entryCurrency === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$dailyAmount = $storedDailyCost;
|
||||
if ($dailyAmount === null || $dailyAmount <= 0) {
|
||||
$amount = is_numeric($entry['total_cost_amount'] ?? null) ? (float) $entry['total_cost_amount'] : null;
|
||||
if ($amount === null || $amount <= 0) {
|
||||
return null;
|
||||
}
|
||||
$dailyAmount = $amount / $runtimeDays;
|
||||
}
|
||||
|
||||
return $this->convertAmount($dailyAmount, $entryCurrency, $targetCurrency, $fxContext);
|
||||
}
|
||||
|
||||
private function convertLatestPrice(array $latestPriceByCurrency, string $targetCurrency, ?array $fxContext = null): ?float
|
||||
{
|
||||
foreach ($latestPriceByCurrency as $sourceCurrency => $price) {
|
||||
|
||||
Reference in New Issue
Block a user