calculation
This commit is contained in:
@@ -2092,8 +2092,10 @@ final class Router
|
||||
$resolvedBonusUnit = $resolvedBonusValue !== null ? $resolvedSpeedUnit : null;
|
||||
|
||||
$purchaseCurrency = $this->optionalCurrency($input['currency'] ?? null) ?? (string) ($offer['effective_price_currency'] ?? $offer['price_currency'] ?? $offer['base_price_currency'] ?? '');
|
||||
if ($paymentType === 'crypto' || !$isAutoRenew) {
|
||||
if ($paymentType === 'crypto') {
|
||||
$purchaseCurrency = $cryptoCurrency;
|
||||
} elseif ($purchaseCurrency === '' || $this->isCryptoCurrencyCode($purchaseCurrency)) {
|
||||
$purchaseCurrency = $this->requiredCurrency($settings['report_currency'] ?? 'EUR', 'report_currency');
|
||||
}
|
||||
$purchaseCost = $this->optionalDecimal($input['total_cost_amount'] ?? null);
|
||||
if ($purchaseCost === null) {
|
||||
@@ -2112,6 +2114,21 @@ final class Router
|
||||
$purchasedAt = array_key_exists('purchased_at', $input)
|
||||
? $this->requiredDateTime($input['purchased_at'], 'purchased_at', $this->projectTimezone($projectKey))
|
||||
: $this->currentTimestamp();
|
||||
$settledValueAmount = null;
|
||||
$settledValueCurrency = null;
|
||||
if ($this->isCryptoCurrencyCode($purchaseCurrency)) {
|
||||
$settledValueCurrency = $this->requiredCurrency($settings['report_currency'] ?? 'EUR', 'report_currency');
|
||||
$settledValueAmount = $purchaseCurrency === $settledValueCurrency
|
||||
? $purchaseCost
|
||||
: $this->convertMiningAssetAmount((float) $purchaseCost, $purchaseCurrency, $settledValueCurrency, $purchasedAt);
|
||||
if ($settledValueAmount === null) {
|
||||
throw new ApiException(
|
||||
'Der aktuelle Kurs fuer die Krypto-Zahlung konnte nicht ermittelt werden. Der Miner wird nicht ohne feste Bewertung gespeichert.',
|
||||
422,
|
||||
['currency' => $purchaseCurrency, 'target_currency' => $settledValueCurrency]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->repository()->purchaseMiner($projectKey, null, [
|
||||
'purchased_at' => $purchasedAt,
|
||||
@@ -2126,6 +2143,8 @@ final class Router
|
||||
'usd_reference_amount' => $offer['usd_reference_amount'] ?? null,
|
||||
'reference_price_amount' => $referencePriceAmount,
|
||||
'reference_price_currency' => $referencePriceCurrency,
|
||||
'settled_value_amount' => $settledValueAmount,
|
||||
'settled_value_currency' => $settledValueCurrency,
|
||||
'auto_renew' => $isAutoRenew ? 1 : 0,
|
||||
'note' => $this->optionalString($input['note'] ?? ($offer['note'] ?? null), 1000),
|
||||
'is_active' => 1,
|
||||
|
||||
@@ -355,26 +355,32 @@ final class AnalyticsService
|
||||
|
||||
$price = $latestPriceByCurrency[$currency] ?? $this->convertLatestPrice($latestPriceByCurrency, $currency, $latest);
|
||||
$requiredDoge = ($price && $targetAmount !== null) ? $targetAmount / $price : null;
|
||||
$remainingDoge = $requiredDoge !== null ? $requiredDoge - (float) ($latest['coins_total_effective'] ?? $latest['coins_total']) : null;
|
||||
$remainingDays = (
|
||||
$remainingDoge !== null &&
|
||||
$remainingDogeAtUpload = $requiredDoge !== null ? $requiredDoge - (float) ($latest['coins_total_effective'] ?? $latest['coins_total']) : null;
|
||||
$remainingDaysAtUpload = (
|
||||
$remainingDogeAtUpload !== null &&
|
||||
$latest['doge_per_day_interval'] !== null &&
|
||||
(float) $latest['doge_per_day_interval'] > 0
|
||||
) ? $remainingDoge / (float) $latest['doge_per_day_interval'] : null;
|
||||
) ? $remainingDogeAtUpload / (float) $latest['doge_per_day_interval'] : null;
|
||||
$targetEtaAt = null;
|
||||
if ($remainingDays !== null) {
|
||||
if ($remainingDays <= 0) {
|
||||
if ($remainingDaysAtUpload !== null) {
|
||||
if ($remainingDaysAtUpload <= 0) {
|
||||
$targetEtaAt = (string) ($latest['measured_at'] ?? '');
|
||||
} elseif (!empty($latest['measured_at'])) {
|
||||
try {
|
||||
$targetEtaAt = $this->formatUtcTimestamp(
|
||||
$this->utcTimestamp((string) $latest['measured_at']) + (int) round($remainingDays * 86400)
|
||||
$this->utcTimestamp((string) $latest['measured_at']) + (int) round($remainingDaysAtUpload * 86400)
|
||||
);
|
||||
} catch (\Throwable) {
|
||||
$targetEtaAt = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
$remainingDays = $targetEtaAt !== null
|
||||
? max(0.0, ($this->utcTimestamp($targetEtaAt) - time()) / 86400)
|
||||
: null;
|
||||
$remainingDoge = ($remainingDays !== null && $latest['doge_per_day_interval'] !== null)
|
||||
? max(0.0, $remainingDays * (float) $latest['doge_per_day_interval'])
|
||||
: $remainingDogeAtUpload;
|
||||
|
||||
$targetSummary[] = array_merge($target, [
|
||||
'effective_target_amount_fiat' => $this->roundOrNull($targetAmount, 2),
|
||||
@@ -384,9 +390,11 @@ final class AnalyticsService
|
||||
'latest_price_for_currency' => $price,
|
||||
'required_doge' => $this->roundOrNull($requiredDoge, 6),
|
||||
'remaining_doge' => $this->roundOrNull($remainingDoge, 6),
|
||||
'remaining_doge_at_upload' => $this->roundOrNull($remainingDogeAtUpload, 6),
|
||||
'remaining_days_at_upload' => $this->roundOrNull($remainingDaysAtUpload, 4),
|
||||
'remaining_days' => $this->roundOrNull($remainingDays, 4),
|
||||
'target_eta_at' => $targetEtaAt,
|
||||
'status' => $remainingDoge !== null && $remainingDoge <= 0 ? 'reached' : 'open',
|
||||
'status' => $remainingDays !== null && $remainingDays <= 0 ? 'reached' : 'open',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -452,6 +460,12 @@ final class AnalyticsService
|
||||
$totalHoldingsValue = ($walletValue !== null || $currentVisibleValue !== null)
|
||||
? (float) ($walletValue ?? 0.0) + (float) ($currentVisibleValue ?? 0.0)
|
||||
: null;
|
||||
$settledCryptoSpendValue = $latestCurrency !== ''
|
||||
? $this->settledCryptoSpendValue($purchasedMiners, $latestCurrency, $latest)
|
||||
: null;
|
||||
$earnedValue = ($totalHoldingsValue !== null || $settledCryptoSpendValue !== null)
|
||||
? (float) ($totalHoldingsValue ?? 0.0) + (float) ($settledCryptoSpendValue ?? 0.0)
|
||||
: null;
|
||||
$currentDailyRevenue = is_numeric($latest['theoretical_daily_revenue'] ?? null) ? (float) $latest['theoretical_daily_revenue'] : null;
|
||||
$breakEvenRemainingAmount = ($cashInvestedCapital !== null && $totalHoldingsValue !== null)
|
||||
? max(0.0, $cashInvestedCapital - $totalHoldingsValue)
|
||||
@@ -493,6 +507,8 @@ final class AnalyticsService
|
||||
'wallet_value' => $this->roundOrNull($walletValue, 8),
|
||||
'wallet_snapshot_measured_at' => $useWalletSnapshot ? (string) ($latestWalletSnapshot['measured_at'] ?? '') : null,
|
||||
'total_holdings_value' => $this->roundOrNull($totalHoldingsValue, 8),
|
||||
'settled_crypto_spend_value' => $this->roundOrNull($settledCryptoSpendValue, 8),
|
||||
'earned_value' => $this->roundOrNull($earnedValue, 8),
|
||||
'break_even_remaining_amount' => $this->roundOrNull($breakEvenRemainingAmount, 8),
|
||||
'break_even_days_overall' => $this->roundOrNull($breakEvenDaysOverall, 4),
|
||||
'break_even_eta_at' => $breakEvenProjection['eta'] ?? null,
|
||||
@@ -1646,6 +1662,33 @@ final class AnalyticsService
|
||||
return array_map(fn (float $value): float => round($value, 8), $balances);
|
||||
}
|
||||
|
||||
private function settledCryptoSpendValue(array $purchasedMiners, string $targetCurrency, ?array $fxContext = null): ?float
|
||||
{
|
||||
$total = 0.0;
|
||||
$matched = false;
|
||||
foreach ($purchasedMiners as $miner) {
|
||||
if ($this->entryFundingSource($miner) !== 'reinvest') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$amount = is_numeric($miner['settled_value_amount'] ?? null) ? (float) $miner['settled_value_amount'] : null;
|
||||
$currency = strtoupper(trim((string) ($miner['settled_value_currency'] ?? '')));
|
||||
if ($amount === null || $currency === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$converted = $this->convertAmount($amount, $currency, $targetCurrency, $fxContext);
|
||||
if ($converted === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$matched = true;
|
||||
$total += $converted;
|
||||
}
|
||||
|
||||
return $matched ? $total : null;
|
||||
}
|
||||
|
||||
private function mergeWalletBalances(array $baseBalances, array $deltaBalances): array
|
||||
{
|
||||
$merged = $baseBalances;
|
||||
@@ -1765,6 +1808,13 @@ final class AnalyticsService
|
||||
$balances[$normalizedCode] = round((float) $balance, 8);
|
||||
}
|
||||
|
||||
// A wallet screenshot's primary balance is authoritative even when OCR did not
|
||||
// expose a complete per-asset balance list.
|
||||
$walletCurrency = strtoupper(trim((string) ($snapshot['wallet_currency'] ?? '')));
|
||||
if ($walletCurrency !== '' && is_numeric($snapshot['wallet_balance'] ?? null)) {
|
||||
$balances[$walletCurrency] = round((float) $snapshot['wallet_balance'], 8);
|
||||
}
|
||||
|
||||
ksort($balances);
|
||||
return $balances;
|
||||
}
|
||||
|
||||
@@ -1005,12 +1005,12 @@ final class MiningRepository
|
||||
'INSERT INTO ' . $this->table('purchased_miners') . ' (
|
||||
project_key, owner_sub, miner_offer_id, purchased_at, label, runtime_months,
|
||||
mining_speed_value, mining_speed_unit, bonus_speed_value, bonus_speed_unit,
|
||||
total_cost_amount, currency, usd_reference_amount, reference_price_amount, reference_price_currency,
|
||||
total_cost_amount, currency, usd_reference_amount, reference_price_amount, reference_price_currency, settled_value_amount, settled_value_currency,
|
||||
daily_cost_amount, daily_cost_currency, auto_renew, note, is_active
|
||||
) VALUES (
|
||||
:project_key, :owner_sub, :miner_offer_id, :purchased_at, :label, :runtime_months,
|
||||
:mining_speed_value, :mining_speed_unit, :bonus_speed_value, :bonus_speed_unit,
|
||||
:total_cost_amount, :currency, :usd_reference_amount, :reference_price_amount, :reference_price_currency,
|
||||
:total_cost_amount, :currency, :usd_reference_amount, :reference_price_amount, :reference_price_currency, :settled_value_amount, :settled_value_currency,
|
||||
:daily_cost_amount, :daily_cost_currency, :auto_renew, :note, :is_active
|
||||
)
|
||||
RETURNING *'
|
||||
@@ -1023,12 +1023,12 @@ final class MiningRepository
|
||||
'INSERT INTO ' . $this->table('purchased_miners') . ' (
|
||||
project_key, owner_sub, miner_offer_id, purchased_at, label, runtime_months,
|
||||
mining_speed_value, mining_speed_unit, bonus_speed_value, bonus_speed_unit,
|
||||
total_cost_amount, currency, usd_reference_amount, reference_price_amount, reference_price_currency,
|
||||
total_cost_amount, currency, usd_reference_amount, reference_price_amount, reference_price_currency, settled_value_amount, settled_value_currency,
|
||||
daily_cost_amount, daily_cost_currency, auto_renew, note, is_active
|
||||
) VALUES (
|
||||
:project_key, :owner_sub, :miner_offer_id, :purchased_at, :label, :runtime_months,
|
||||
:mining_speed_value, :mining_speed_unit, :bonus_speed_value, :bonus_speed_unit,
|
||||
:total_cost_amount, :currency, :usd_reference_amount, :reference_price_amount, :reference_price_currency,
|
||||
:total_cost_amount, :currency, :usd_reference_amount, :reference_price_amount, :reference_price_currency, :settled_value_amount, :settled_value_currency,
|
||||
:daily_cost_amount, :daily_cost_currency, :auto_renew, :note, :is_active
|
||||
)'
|
||||
);
|
||||
@@ -1057,6 +1057,8 @@ final class MiningRepository
|
||||
'usd_reference_amount' => $payload['usd_reference_amount'] ?? null,
|
||||
'reference_price_amount' => $payload['reference_price_amount'] ?? null,
|
||||
'reference_price_currency' => $payload['reference_price_currency'] ?? null,
|
||||
'settled_value_amount' => $payload['settled_value_amount'] ?? null,
|
||||
'settled_value_currency' => $payload['settled_value_currency'] ?? null,
|
||||
'daily_cost_amount' => $payload['daily_cost_amount'] ?? $this->deriveDailyCostAmount($payload['total_cost_amount'] ?? null, $payload['runtime_months'] ?? null),
|
||||
'daily_cost_currency' => $payload['daily_cost_currency'] ?? ($payload['currency'] ?? null),
|
||||
'auto_renew' => $payload['auto_renew'] ?? 0,
|
||||
@@ -1069,12 +1071,12 @@ final class MiningRepository
|
||||
'INSERT INTO ' . $this->table('purchased_miners') . ' (
|
||||
project_key, owner_sub, miner_offer_id, purchased_at, label, runtime_months,
|
||||
mining_speed_value, mining_speed_unit, bonus_speed_value, bonus_speed_unit,
|
||||
total_cost_amount, currency, usd_reference_amount, reference_price_amount, reference_price_currency,
|
||||
total_cost_amount, currency, usd_reference_amount, reference_price_amount, reference_price_currency, settled_value_amount, settled_value_currency,
|
||||
daily_cost_amount, daily_cost_currency, auto_renew, note, is_active
|
||||
) VALUES (
|
||||
:project_key, :owner_sub, :miner_offer_id, :purchased_at, :label, :runtime_months,
|
||||
:mining_speed_value, :mining_speed_unit, :bonus_speed_value, :bonus_speed_unit,
|
||||
:total_cost_amount, :currency, :usd_reference_amount, :reference_price_amount, :reference_price_currency,
|
||||
:total_cost_amount, :currency, :usd_reference_amount, :reference_price_amount, :reference_price_currency, :settled_value_amount, :settled_value_currency,
|
||||
:daily_cost_amount, :daily_cost_currency, :auto_renew, :note, :is_active
|
||||
)
|
||||
RETURNING *'
|
||||
@@ -1087,12 +1089,12 @@ final class MiningRepository
|
||||
'INSERT INTO ' . $this->table('purchased_miners') . ' (
|
||||
project_key, owner_sub, miner_offer_id, purchased_at, label, runtime_months,
|
||||
mining_speed_value, mining_speed_unit, bonus_speed_value, bonus_speed_unit,
|
||||
total_cost_amount, currency, usd_reference_amount, reference_price_amount, reference_price_currency,
|
||||
total_cost_amount, currency, usd_reference_amount, reference_price_amount, reference_price_currency, settled_value_amount, settled_value_currency,
|
||||
daily_cost_amount, daily_cost_currency, auto_renew, note, is_active
|
||||
) VALUES (
|
||||
:project_key, :owner_sub, :miner_offer_id, :purchased_at, :label, :runtime_months,
|
||||
:mining_speed_value, :mining_speed_unit, :bonus_speed_value, :bonus_speed_unit,
|
||||
:total_cost_amount, :currency, :usd_reference_amount, :reference_price_amount, :reference_price_currency,
|
||||
:total_cost_amount, :currency, :usd_reference_amount, :reference_price_amount, :reference_price_currency, :settled_value_amount, :settled_value_currency,
|
||||
:daily_cost_amount, :daily_cost_currency, :auto_renew, :note, :is_active
|
||||
)'
|
||||
);
|
||||
@@ -1646,6 +1648,8 @@ final class MiningRepository
|
||||
'usd_reference_amount' => $payload['usd_reference_amount'],
|
||||
'reference_price_amount' => $payload['reference_price_amount'] ?? null,
|
||||
'reference_price_currency' => $payload['reference_price_currency'] ?? null,
|
||||
'settled_value_amount' => $payload['settled_value_amount'] ?? null,
|
||||
'settled_value_currency' => $payload['settled_value_currency'] ?? null,
|
||||
'daily_cost_amount' => $dailyCostAmount,
|
||||
'daily_cost_currency' => $dailyCostCurrency,
|
||||
'auto_renew' => $payload['auto_renew'] ?? 0,
|
||||
|
||||
@@ -259,6 +259,13 @@ final class SchemaManager
|
||||
$this->upgradePurchasedMinerReferenceColumns();
|
||||
$applied[] = 'purchased_miner_reference_columns';
|
||||
}
|
||||
if ($this->tableExists($this->prefix . 'purchased_miners') && (
|
||||
!$this->columnExists($this->prefix . 'purchased_miners', 'settled_value_amount') ||
|
||||
!$this->columnExists($this->prefix . 'purchased_miners', 'settled_value_currency')
|
||||
)) {
|
||||
$this->upgradePurchasedMinerSettlementColumns();
|
||||
$applied[] = 'purchased_miner_settlement_columns';
|
||||
}
|
||||
if ($this->tableExists($this->prefix . 'targets') && $this->tableExists($this->prefix . 'miner_offers')) {
|
||||
$this->ensureTargetOfferForeignKey();
|
||||
$applied[] = 'target_offer_foreign_key';
|
||||
@@ -401,6 +408,13 @@ final class SchemaManager
|
||||
$this->upgradePurchasedMinerReferenceColumns();
|
||||
$applied[] = 'purchased_miner_reference_columns';
|
||||
}
|
||||
if ($this->tableExists($this->prefix . 'purchased_miners') && (
|
||||
!$this->columnExists($this->prefix . 'purchased_miners', 'settled_value_amount') ||
|
||||
!$this->columnExists($this->prefix . 'purchased_miners', 'settled_value_currency')
|
||||
)) {
|
||||
$this->upgradePurchasedMinerSettlementColumns();
|
||||
$applied[] = 'purchased_miner_settlement_columns';
|
||||
}
|
||||
if ($this->tableExists($this->prefix . 'targets') && $this->tableExists($this->prefix . 'miner_offers')) {
|
||||
$this->ensureTargetOfferForeignKey();
|
||||
$applied[] = 'target_offer_foreign_key';
|
||||
@@ -754,6 +768,12 @@ final class SchemaManager
|
||||
if (!$this->tableExists($this->prefix . 'purchased_miners')) {
|
||||
$upgrades[] = 'purchased_miners_table';
|
||||
}
|
||||
if ($this->tableExists($this->prefix . 'purchased_miners') && (
|
||||
!$this->columnExists($this->prefix . 'purchased_miners', 'settled_value_amount') ||
|
||||
!$this->columnExists($this->prefix . 'purchased_miners', 'settled_value_currency')
|
||||
)) {
|
||||
$upgrades[] = 'purchased_miner_settlement_columns';
|
||||
}
|
||||
if ($this->tableExists($this->prefix . 'targets') && !$this->columnExists($this->prefix . 'targets', 'miner_offer_id')) {
|
||||
$upgrades[] = 'target_offer_column';
|
||||
}
|
||||
@@ -1502,6 +1522,33 @@ final class SchemaManager
|
||||
}
|
||||
}
|
||||
|
||||
private function upgradePurchasedMinerSettlementColumns(): void
|
||||
{
|
||||
$table = $this->prefix . 'purchased_miners';
|
||||
$statements = $this->driver === 'pgsql'
|
||||
? [
|
||||
'ALTER TABLE ' . $table . ' ADD COLUMN IF NOT EXISTS settled_value_amount NUMERIC(20,8)',
|
||||
'ALTER TABLE ' . $table . ' ADD COLUMN IF NOT EXISTS settled_value_currency VARCHAR(10)',
|
||||
"UPDATE " . $table . " SET settled_value_amount = CASE WHEN reference_price_amount IS NOT NULL AND NULLIF(BTRIM(reference_price_currency), '') IS NOT NULL AND UPPER(reference_price_currency) <> UPPER(currency) THEN reference_price_amount ELSE total_cost_amount * 0.07 END, settled_value_currency = CASE WHEN reference_price_amount IS NOT NULL AND NULLIF(BTRIM(reference_price_currency), '') IS NOT NULL AND UPPER(reference_price_currency) <> UPPER(currency) THEN reference_price_currency ELSE 'EUR' END WHERE UPPER(currency) IN ('ADA','ARB','BNB','BTC','DAI','DOGE','DOT','ETH','LINK','LTC','SOL','USDC','USDT','XRP') AND settled_value_amount IS NULL",
|
||||
]
|
||||
: [
|
||||
'ALTER TABLE `' . $table . '` ADD COLUMN settled_value_amount DECIMAL(20,8) NULL',
|
||||
'ALTER TABLE `' . $table . '` ADD COLUMN settled_value_currency VARCHAR(10) NULL',
|
||||
"UPDATE `" . $table . "` SET settled_value_amount = CASE WHEN reference_price_amount IS NOT NULL AND NULLIF(TRIM(reference_price_currency), '') IS NOT NULL AND UPPER(reference_price_currency) <> UPPER(currency) THEN reference_price_amount ELSE total_cost_amount * 0.07 END, settled_value_currency = CASE WHEN reference_price_amount IS NOT NULL AND NULLIF(TRIM(reference_price_currency), '') IS NOT NULL AND UPPER(reference_price_currency) <> UPPER(currency) THEN reference_price_currency ELSE 'EUR' END WHERE UPPER(currency) IN ('ADA','ARB','BNB','BTC','DAI','DOGE','DOT','ETH','LINK','LTC','SOL','USDC','USDT','XRP') AND settled_value_amount IS NULL",
|
||||
];
|
||||
|
||||
foreach ($statements as $statement) {
|
||||
try {
|
||||
$this->executeUpgradeStatements([$statement], 'Schema-Upgrade fuer die feste Krypto-Bewertung fehlgeschlagen.');
|
||||
} catch (\Throwable $exception) {
|
||||
if ($this->driver === 'mysql' && str_contains(strtolower($exception->getMessage()), 'duplicate column')) {
|
||||
continue;
|
||||
}
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function upgradeServerDailyCostColumns(): void
|
||||
{
|
||||
foreach ([$this->prefix . 'cost_plans', $this->prefix . 'purchased_miners'] as $table) {
|
||||
|
||||
Reference in New Issue
Block a user