/** * Gets the sum of all services, converted to user's * preferred currency with current exchange rate. * * It is possible to pass a collection of services to this method, * and it will return the sum of that collection. * * @param Collection|null $services * @return [type] */ public function getSum(Collection $services = null) { if (!$services instanceof Collection) { $services = $this->all(); } $preferredCurrency = strtoupper(auth()->user()->preferred_currency); $sum = 0; foreach ($services as $service) { $currentCurrency = strtoupper($service->currency); $exchange_rate = \Swap::quote("{$currentCurrency}/{$preferredCurrency}")->getValue(); $sum += $service->cost / 100 * $exchange_rate; } //$sum = ceil($sum); return number_format($sum, 2, ',', '.') . ' ' . $preferredCurrency; }