Fix profit_gold API for gold apps without orders relation eager load.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -116,18 +116,30 @@ class OrderController extends Controller
|
||||
|
||||
public function profit_gold()
|
||||
{
|
||||
$customer = Customer::with(['wallet', 'orders'])
|
||||
->findOrFail(auth('customer')->id());
|
||||
$customerId = auth('customer')->id();
|
||||
$customer = Customer::with(['wallet'])
|
||||
->findOrFail($customerId);
|
||||
|
||||
$lastPrice = Price::latest('created_at')->value('price') ?? 0;
|
||||
$goldAmount = $customer->wallet->first()->gold ?? 0;
|
||||
$orders = $customer->orders;
|
||||
$lastPrice = (float) (Price::latest('created_at')->value('price') ?? 0);
|
||||
|
||||
$averagePrice = $orders->avg('price') ?? 0;
|
||||
$averageFee = $orders->avg('fee') ?? 0;
|
||||
$totalProfit = $orders->sum(fn ($order) => $order->direction === 1
|
||||
? $order->profit_per_moment
|
||||
: -$order->profit_per_moment);
|
||||
$mainWallet = $customer->wallet->first(
|
||||
fn ($wallet) => is_null($wallet->product_id) && is_null($wallet->fund_id)
|
||||
) ?? $customer->wallet->first();
|
||||
$goldAmount = (float) ($mainWallet->gold ?? 0);
|
||||
|
||||
$orders = Order::query()
|
||||
->where('customer_id', $customerId)
|
||||
->where('type', 1)
|
||||
->where('sts', 2)
|
||||
->get();
|
||||
|
||||
$averagePrice = (float) ($orders->avg('price') ?? 0);
|
||||
$averageFee = (float) ($orders->avg('fee') ?? 0);
|
||||
$totalProfit = $orders->sum(function (Order $order) use ($lastPrice) {
|
||||
$profitPerMoment = ($lastPrice - (float) $order->price) * (float) $order->amount;
|
||||
|
||||
return (int) $order->direction === 1 ? $profitPerMoment : -$profitPerMoment;
|
||||
});
|
||||
|
||||
$feeSingle = (float) (Setting::where('key', 'fee_single')->value('value') ?? config('fee.single', 0));
|
||||
$currentPriceWithFee = $lastPrice + $feeSingle;
|
||||
|
||||
Reference in New Issue
Block a user