listsTransactions = $listsTransactions; } /** * @return array */ protected function notification():array { return [ 'title' => 'Retrieved Wallet Transactions', 'message' => 'You have successfully retrieved a list of transactions' ]; } /** @var ListsTransactions */ private $listsTransactions; public function logic(Request $request) : JsonResponse { $query = $this->listsTransactions->execute($this->listsTransactions->deserializeFilters($request->input('filters'))); if (str_contains($request->input('filters'), "owner_id") && $query->count() > 0) { $wallet_total_incoming = Transaction::where('owner_type', $query->first()->owner_type) ->where('owner_id', $query->first()->owner_id) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->whereIn('type', [TransactionType::TOP_UP, TransactionType::CREDIT_NOTE]) ->sum('amount'); $wallet_total_outgoing = Transaction::where('owner_type', $query->first()->owner_type) ->where('owner_id', $query->first()->owner_id) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->whereIn('type', [TransactionType::PAYMENT, TransactionType::DEBIT_NOTE]) ->sum('amount'); $currentWalletBalance = $wallet_total_incoming - $wallet_total_outgoing; $incoming = Transaction::where('owner_type', $query->first()->owner_type) ->where('owner_id', $query->first()->owner_id) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->whereIn('type', [TransactionType::TOP_UP, TransactionType::CREDIT_NOTE]) ->where('id', '>', $query->first()->id) ->sum('amount'); $outgoing = Transaction::where('owner_type', $query->first()->owner_type) ->where('owner_id', $query->first()->owner_id) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->whereIn('type', [TransactionType::PAYMENT, TransactionType::DEBIT_NOTE]) ->where('id', '>', $query->first()->id) ->sum('amount'); $runningBalanceInReverse = $currentWalletBalance - $incoming + $outgoing; $request['running_balance'] = $runningBalanceInReverse; } return $this->collectionResponse(WalletTransactionResource::collection($query)); } }