fix some bug
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Order;
|
||||
use App\Models\Price;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -60,6 +61,13 @@ class AssetSummaryService
|
||||
$mainWalletProductIds
|
||||
);
|
||||
$totalRialValue = $cashWallet + $assetsRialValue;
|
||||
$portfolio = $this->buildPortfolioInsight(
|
||||
$customerId,
|
||||
$metalTotals,
|
||||
$cashWallet,
|
||||
$assetsRialValue,
|
||||
$totalRialValue
|
||||
);
|
||||
|
||||
return [
|
||||
'meta' => [
|
||||
@@ -78,6 +86,78 @@ class AssetSummaryService
|
||||
'metal_totals' => $metalTotals,
|
||||
'assets_rial_value' => round($assetsRialValue, 0),
|
||||
'total_rial_value' => round($totalRialValue, 0),
|
||||
'profit_loss' => $portfolio['unrealized_profit_rial'],
|
||||
'portfolio' => $portfolio,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* خلاصه دارایی برای داشبورد — ارزش کل، تفکیک نقد/فلز، سود/زیان تحققنیافته طلا.
|
||||
*
|
||||
* @return array{
|
||||
* total_rial_value: float,
|
||||
* cash_rial: float,
|
||||
* metal_rial_value: float,
|
||||
* gold_weight: float,
|
||||
* gold_rial_value: float,
|
||||
* average_buy_price: float|null,
|
||||
* cost_basis_rial: float|null,
|
||||
* unrealized_profit_rial: float|null,
|
||||
* unrealized_profit_percent: float|null,
|
||||
* live_gold_price: float
|
||||
* }
|
||||
*/
|
||||
private function buildPortfolioInsight(
|
||||
int $customerId,
|
||||
array $metalTotals,
|
||||
float $cashWallet,
|
||||
float $assetsRialValue,
|
||||
float $totalRialValue
|
||||
): array {
|
||||
$goldMetal = collect($metalTotals)->first(fn (array $row) => (int) ($row['metal_id'] ?? 0) === 1);
|
||||
$goldWeight = (float) ($goldMetal['total_weight'] ?? 0);
|
||||
$liveGoldPrice = $this->resolveLivePricePerUnit(1);
|
||||
$goldRialValue = $goldWeight > 0 && $liveGoldPrice > 0
|
||||
? round($goldWeight * $liveGoldPrice, 0)
|
||||
: 0.0;
|
||||
|
||||
$averageBuyPrice = null;
|
||||
$costBasisRial = null;
|
||||
$unrealizedProfitRial = null;
|
||||
$unrealizedProfitPercent = null;
|
||||
|
||||
if ($goldWeight > 0) {
|
||||
$buyOrders = Order::query()
|
||||
->where('customer_id', $customerId)
|
||||
->where('direction', 1)
|
||||
->where('sts', 2)
|
||||
->where('amount', '>', 0)
|
||||
->get(['amount', 'price']);
|
||||
|
||||
$totalBought = (float) $buyOrders->sum('amount');
|
||||
$weightedSum = (float) $buyOrders->sum(fn (Order $order) => (float) $order->price * (float) $order->amount);
|
||||
|
||||
if ($totalBought > 0 && $weightedSum > 0) {
|
||||
$averageBuyPrice = round($weightedSum / $totalBought, 0);
|
||||
$costBasisRial = round($averageBuyPrice * $goldWeight, 0);
|
||||
$unrealizedProfitRial = round($goldRialValue - $costBasisRial, 0);
|
||||
if ($costBasisRial > 0) {
|
||||
$unrealizedProfitPercent = round(($unrealizedProfitRial / $costBasisRial) * 100, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'total_rial_value' => round($totalRialValue, 0),
|
||||
'cash_rial' => round($cashWallet, 0),
|
||||
'metal_rial_value' => round($assetsRialValue, 0),
|
||||
'gold_weight' => round($goldWeight, 3),
|
||||
'gold_rial_value' => $goldRialValue,
|
||||
'average_buy_price' => $averageBuyPrice,
|
||||
'cost_basis_rial' => $costBasisRial,
|
||||
'unrealized_profit_rial' => $unrealizedProfitRial,
|
||||
'unrealized_profit_percent' => $unrealizedProfitPercent,
|
||||
'live_gold_price' => round($liveGoldPrice, 0),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user