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()
|
public function profit_gold()
|
||||||
{
|
{
|
||||||
$customer = Customer::with(['wallet', 'orders'])
|
$customerId = auth('customer')->id();
|
||||||
->findOrFail(auth('customer')->id());
|
$customer = Customer::with(['wallet'])
|
||||||
|
->findOrFail($customerId);
|
||||||
|
|
||||||
$lastPrice = Price::latest('created_at')->value('price') ?? 0;
|
$lastPrice = (float) (Price::latest('created_at')->value('price') ?? 0);
|
||||||
$goldAmount = $customer->wallet->first()->gold ?? 0;
|
|
||||||
$orders = $customer->orders;
|
|
||||||
|
|
||||||
$averagePrice = $orders->avg('price') ?? 0;
|
$mainWallet = $customer->wallet->first(
|
||||||
$averageFee = $orders->avg('fee') ?? 0;
|
fn ($wallet) => is_null($wallet->product_id) && is_null($wallet->fund_id)
|
||||||
$totalProfit = $orders->sum(fn ($order) => $order->direction === 1
|
) ?? $customer->wallet->first();
|
||||||
? $order->profit_per_moment
|
$goldAmount = (float) ($mainWallet->gold ?? 0);
|
||||||
: -$order->profit_per_moment);
|
|
||||||
|
$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));
|
$feeSingle = (float) (Setting::where('key', 'fee_single')->value('value') ?? config('fee.single', 0));
|
||||||
$currentPriceWithFee = $lastPrice + $feeSingle;
|
$currentPriceWithFee = $lastPrice + $feeSingle;
|
||||||
|
|||||||
@@ -91,6 +91,12 @@ class Customer extends Authenticatable implements JWTSubject
|
|||||||
{
|
{
|
||||||
return $this->hasMany(Wallet::class ,'owner')->with(['fund','product'])->select(['cash', 'fund_id','product_id','gold', 'owner']);
|
return $this->hasMany(Wallet::class ,'owner')->with(['fund','product'])->select(['cash', 'fund_id','product_id','gold', 'owner']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function orders()
|
||||||
|
{
|
||||||
|
return $this->hasMany(Order::class, 'customer_id');
|
||||||
|
}
|
||||||
|
|
||||||
public function user()
|
public function user()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(User::class)->select(['id','name']);
|
return $this->belongsTo(User::class)->select(['id','name']);
|
||||||
|
|||||||
Reference in New Issue
Block a user