diff --git a/app/Http/Controllers/Customer/OrderController.php b/app/Http/Controllers/Customer/OrderController.php index d3a89b0..c0c9c59 100644 --- a/app/Http/Controllers/Customer/OrderController.php +++ b/app/Http/Controllers/Customer/OrderController.php @@ -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; diff --git a/app/Models/Customer.php b/app/Models/Customer.php index 1dc6f46..333f2f1 100644 --- a/app/Models/Customer.php +++ b/app/Models/Customer.php @@ -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']); } + + public function orders() + { + return $this->hasMany(Order::class, 'customer_id'); + } + public function user() { return $this->belongsTo(User::class)->select(['id','name']);