Files
InvoiceShelf/app/Services/ExchangeRateProviderService.php
Darko Gjorgjijoski b0b7d40c73 Fix exchange rate parity across all document types
- Fix exchange-rate service types to match actual backend response shapes
  (exchangeRate array, activeProvider success/error, used currencies as strings)
- Add ExchangeRateConverter to payments, expenses, and recurring invoices
- Set currency_id from customer currency in invoice/estimate selectCustomer()
- Load globalStore.currencies in ExchangeRateConverter on mount
- Pass driver/key/driver_config params to getSupportedCurrencies in provider modal
- Fix OpenExchangeRateDriver validateConnection to use base=USD (free plan compat)
- Fix checkActiveCurrencies SQLite whereJsonContains with array values
- Remove broken currency/companyCurrency props from ExpenseCreateView, use stores
- Show base currency equivalent in document line items and totals when exchange
  rate is active
2026-04-06 21:07:50 +02:00

121 lines
3.7 KiB
PHP

<?php
namespace App\Services;
use App\Http\Requests\ExchangeRateProviderRequest;
use App\Models\CompanySetting;
use App\Models\ExchangeRateLog;
use App\Models\ExchangeRateProvider;
use App\Services\ExchangeRate\ExchangeRateDriverFactory;
use App\Services\ExchangeRate\ExchangeRateException;
class ExchangeRateProviderService
{
public function create(ExchangeRateProviderRequest $request): ExchangeRateProvider
{
return ExchangeRateProvider::create($request->getExchangeRateProviderPayload());
}
public function update(ExchangeRateProvider $provider, ExchangeRateProviderRequest $request): ExchangeRateProvider
{
$provider->update($request->getExchangeRateProviderPayload());
return $provider;
}
public function checkActiveCurrencies($request)
{
$currencies = $request->currencies;
if (empty($currencies)) {
return collect();
}
$query = ExchangeRateProvider::where('active', true);
foreach ($currencies as $currency) {
$query->orWhere(function ($q) use ($currency) {
$q->where('active', true)
->whereJsonContains('currencies', $currency);
});
}
return $query->get();
}
public function checkUpdateActiveCurrencies(ExchangeRateProvider $provider, $request)
{
$currencies = $request->currencies;
if (empty($currencies)) {
return collect();
}
$query = ExchangeRateProvider::where('id', '<>', $provider->id)
->where('active', true);
$query->where(function ($q) use ($currencies) {
foreach ($currencies as $currency) {
$q->orWhereJsonContains('currencies', $currency);
}
});
return $query->get();
}
public function checkProviderStatus($request)
{
try {
$driver = ExchangeRateDriverFactory::make(
$request['driver'],
$request['key'],
$request['driver_config'] ?? []
);
$rates = $driver->validateConnection();
return response()->json([
'exchangeRate' => $rates,
], 200);
} catch (ExchangeRateException $e) {
return respondJson($e->errorKey, $e->getMessage());
}
}
public function getExchangeRate(string $driver, string $apiKey, array $driverConfig, string $baseCurrency, string $targetCurrency)
{
try {
$driverInstance = ExchangeRateDriverFactory::make($driver, $apiKey, $driverConfig);
return response()->json([
'exchangeRate' => $driverInstance->getExchangeRate($baseCurrency, $targetCurrency),
], 200);
} catch (ExchangeRateException $e) {
return respondJson($e->errorKey, $e->getMessage());
}
}
public function getSupportedCurrencies(string $driver, string $apiKey, array $driverConfig = [])
{
try {
$driverInstance = ExchangeRateDriverFactory::make($driver, $apiKey, $driverConfig);
return response()->json([
'supportedCurrencies' => $driverInstance->getSupportedCurrencies(),
]);
} catch (ExchangeRateException $e) {
return respondJson($e->errorKey, $e->getMessage());
}
}
public function addExchangeRateLog($model): ExchangeRateLog
{
return ExchangeRateLog::create([
'exchange_rate' => $model->exchange_rate,
'company_id' => $model->company_id,
'base_currency_id' => $model->currency_id,
'currency_id' => CompanySetting::getSetting('currency', $model->company_id),
]);
}
}