mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-20 19:54:08 +00:00
Consolidate ExchangeRate single-action controllers into ExchangeRateProviderController
Merge 4 invocable controllers (GetActiveProvider, GetExchangeRate, GetSupportedCurrencies, GetUsedCurrencies) as methods on the existing resource controller: activeProvider(), getRate(), supportedCurrencies(), usedCurrencies().
This commit is contained in:
@@ -5,12 +5,19 @@ namespace App\Http\Controllers\V1\Admin\ExchangeRate;
|
|||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Requests\ExchangeRateProviderRequest;
|
use App\Http\Requests\ExchangeRateProviderRequest;
|
||||||
use App\Http\Resources\ExchangeRateProviderResource;
|
use App\Http\Resources\ExchangeRateProviderResource;
|
||||||
|
use App\Models\CompanySetting;
|
||||||
|
use App\Models\Currency;
|
||||||
|
use App\Models\ExchangeRateLog;
|
||||||
use App\Models\ExchangeRateProvider;
|
use App\Models\ExchangeRateProvider;
|
||||||
|
use App\Traits\ExchangeRateProvidersTrait;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Response;
|
use Illuminate\Http\Response;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
class ExchangeRateProviderController extends Controller
|
class ExchangeRateProviderController extends Controller
|
||||||
{
|
{
|
||||||
|
use ExchangeRateProvidersTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*
|
*
|
||||||
@@ -112,4 +119,102 @@ class ExchangeRateProviderController extends Controller
|
|||||||
'success' => true,
|
'success' => true,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function activeProvider(Request $request, Currency $currency)
|
||||||
|
{
|
||||||
|
$query = ExchangeRateProvider::whereCompany()->whereJsonContains('currencies', $currency->code)
|
||||||
|
->where('active', true)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
if (count($query) !== 0) {
|
||||||
|
return response()->json([
|
||||||
|
'success' => true,
|
||||||
|
'message' => 'provider_active',
|
||||||
|
], 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'error' => 'no_active_provider',
|
||||||
|
], 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRate(Request $request, Currency $currency)
|
||||||
|
{
|
||||||
|
$settings = CompanySetting::getSettings(['currency'], $request->header('company'));
|
||||||
|
$baseCurrency = Currency::findOrFail($settings['currency']);
|
||||||
|
|
||||||
|
$query = ExchangeRateProvider::whereJsonContains('currencies', $currency->code)
|
||||||
|
->where('active', true)
|
||||||
|
->get()
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
$exchange_rate = ExchangeRateLog::where('base_currency_id', $currency->id)
|
||||||
|
->where('currency_id', $baseCurrency->id)
|
||||||
|
->orderBy('created_at', 'desc')
|
||||||
|
->value('exchange_rate');
|
||||||
|
|
||||||
|
if ($query) {
|
||||||
|
$filter = Arr::only($query[0], ['key', 'driver', 'driver_config']);
|
||||||
|
$exchange_rate_value = $this->getExchangeRate($filter, $currency->code, $baseCurrency->code);
|
||||||
|
|
||||||
|
if ($exchange_rate_value->status() == 200) {
|
||||||
|
return $exchange_rate_value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($exchange_rate) {
|
||||||
|
return response()->json([
|
||||||
|
'exchangeRate' => [$exchange_rate],
|
||||||
|
], 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'error' => 'no_exchange_rate_available',
|
||||||
|
], 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportedCurrencies(Request $request)
|
||||||
|
{
|
||||||
|
$this->authorize('viewAny', ExchangeRateProvider::class);
|
||||||
|
|
||||||
|
return $this->getSupportedCurrencies($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function usedCurrencies(Request $request)
|
||||||
|
{
|
||||||
|
$this->authorize('viewAny', ExchangeRateProvider::class);
|
||||||
|
|
||||||
|
$providerId = $request->provider_id;
|
||||||
|
|
||||||
|
$activeExchangeRateProviders = ExchangeRateProvider::where('active', true)
|
||||||
|
->whereCompany()
|
||||||
|
->when($providerId, function ($query) use ($providerId) {
|
||||||
|
return $query->where('id', '<>', $providerId);
|
||||||
|
})
|
||||||
|
->pluck('currencies');
|
||||||
|
$activeExchangeRateProvider = [];
|
||||||
|
|
||||||
|
foreach ($activeExchangeRateProviders as $data) {
|
||||||
|
if (is_array($data)) {
|
||||||
|
for ($limit = 0; $limit < count($data); $limit++) {
|
||||||
|
$activeExchangeRateProvider[] = $data[$limit];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$allExchangeRateProviders = ExchangeRateProvider::whereCompany()->pluck('currencies');
|
||||||
|
$allExchangeRateProvider = [];
|
||||||
|
|
||||||
|
foreach ($allExchangeRateProviders as $data) {
|
||||||
|
if (is_array($data)) {
|
||||||
|
for ($limit = 0; $limit < count($data); $limit++) {
|
||||||
|
$allExchangeRateProvider[] = $data[$limit];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'allUsedCurrencies' => $allExchangeRateProvider ? $allExchangeRateProvider : [],
|
||||||
|
'activeUsedCurrencies' => $activeExchangeRateProvider ? $activeExchangeRateProvider : [],
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,35 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Controllers\V1\Admin\ExchangeRate;
|
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
|
||||||
use App\Models\Currency;
|
|
||||||
use App\Models\ExchangeRateProvider;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Illuminate\Http\Response;
|
|
||||||
|
|
||||||
class GetActiveProviderController extends Controller
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Handle the incoming request.
|
|
||||||
*
|
|
||||||
* @return Response
|
|
||||||
*/
|
|
||||||
public function __invoke(Request $request, Currency $currency)
|
|
||||||
{
|
|
||||||
$query = ExchangeRateProvider::whereCompany()->whereJsonContains('currencies', $currency->code)
|
|
||||||
->where('active', true)
|
|
||||||
->get();
|
|
||||||
|
|
||||||
if (count($query) !== 0) {
|
|
||||||
return response()->json([
|
|
||||||
'success' => true,
|
|
||||||
'message' => 'provider_active',
|
|
||||||
], 200);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json([
|
|
||||||
'error' => 'no_active_provider',
|
|
||||||
], 200);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Controllers\V1\Admin\ExchangeRate;
|
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
|
||||||
use App\Models\CompanySetting;
|
|
||||||
use App\Models\Currency;
|
|
||||||
use App\Models\ExchangeRateLog;
|
|
||||||
use App\Models\ExchangeRateProvider;
|
|
||||||
use App\Traits\ExchangeRateProvidersTrait;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Illuminate\Http\Response;
|
|
||||||
use Illuminate\Support\Arr;
|
|
||||||
|
|
||||||
class GetExchangeRateController extends Controller
|
|
||||||
{
|
|
||||||
use ExchangeRateProvidersTrait;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle the incoming request.
|
|
||||||
*
|
|
||||||
* @return Response
|
|
||||||
*/
|
|
||||||
public function __invoke(Request $request, Currency $currency)
|
|
||||||
{
|
|
||||||
$settings = CompanySetting::getSettings(['currency'], $request->header('company'));
|
|
||||||
$baseCurrency = Currency::findOrFail($settings['currency']);
|
|
||||||
|
|
||||||
$query = ExchangeRateProvider::whereJsonContains('currencies', $currency->code)
|
|
||||||
->where('active', true)
|
|
||||||
->get()
|
|
||||||
->toArray();
|
|
||||||
|
|
||||||
$exchange_rate = ExchangeRateLog::where('base_currency_id', $currency->id)
|
|
||||||
->where('currency_id', $baseCurrency->id)
|
|
||||||
->orderBy('created_at', 'desc')
|
|
||||||
->value('exchange_rate');
|
|
||||||
|
|
||||||
if ($query) {
|
|
||||||
$filter = Arr::only($query[0], ['key', 'driver', 'driver_config']);
|
|
||||||
$exchange_rate_value = $this->getExchangeRate($filter, $currency->code, $baseCurrency->code);
|
|
||||||
|
|
||||||
if ($exchange_rate_value->status() == 200) {
|
|
||||||
return $exchange_rate_value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($exchange_rate) {
|
|
||||||
return response()->json([
|
|
||||||
'exchangeRate' => [$exchange_rate],
|
|
||||||
], 200);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json([
|
|
||||||
'error' => 'no_exchange_rate_available',
|
|
||||||
], 200);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Controllers\V1\Admin\ExchangeRate;
|
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
|
||||||
use App\Models\ExchangeRateProvider;
|
|
||||||
use App\Traits\ExchangeRateProvidersTrait;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Illuminate\Http\Response;
|
|
||||||
|
|
||||||
class GetSupportedCurrenciesController extends Controller
|
|
||||||
{
|
|
||||||
use ExchangeRateProvidersTrait;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle the incoming request.
|
|
||||||
*
|
|
||||||
* @return Response
|
|
||||||
*/
|
|
||||||
public function __invoke(Request $request)
|
|
||||||
{
|
|
||||||
$this->authorize('viewAny', ExchangeRateProvider::class);
|
|
||||||
|
|
||||||
return $this->getSupportedCurrencies($request);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Controllers\V1\Admin\ExchangeRate;
|
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
|
||||||
use App\Models\ExchangeRateProvider;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Illuminate\Http\Response;
|
|
||||||
|
|
||||||
class GetUsedCurrenciesController extends Controller
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Handle the incoming request.
|
|
||||||
*
|
|
||||||
* @return Response
|
|
||||||
*/
|
|
||||||
public function __invoke(Request $request)
|
|
||||||
{
|
|
||||||
$this->authorize('viewAny', ExchangeRateProvider::class);
|
|
||||||
|
|
||||||
$providerId = $request->provider_id;
|
|
||||||
|
|
||||||
$activeExchangeRateProviders = ExchangeRateProvider::where('active', true)
|
|
||||||
->whereCompany()
|
|
||||||
->when($providerId, function ($query) use ($providerId) {
|
|
||||||
return $query->where('id', '<>', $providerId);
|
|
||||||
})
|
|
||||||
->pluck('currencies');
|
|
||||||
$activeExchangeRateProvider = [];
|
|
||||||
|
|
||||||
foreach ($activeExchangeRateProviders as $data) {
|
|
||||||
if (is_array($data)) {
|
|
||||||
for ($limit = 0; $limit < count($data); $limit++) {
|
|
||||||
$activeExchangeRateProvider[] = $data[$limit];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$allExchangeRateProviders = ExchangeRateProvider::whereCompany()->pluck('currencies');
|
|
||||||
$allExchangeRateProvider = [];
|
|
||||||
|
|
||||||
foreach ($allExchangeRateProviders as $data) {
|
|
||||||
if (is_array($data)) {
|
|
||||||
for ($limit = 0; $limit < count($data); $limit++) {
|
|
||||||
$allExchangeRateProvider[] = $data[$limit];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json([
|
|
||||||
'allUsedCurrencies' => $allExchangeRateProvider ? $allExchangeRateProvider : [],
|
|
||||||
'activeUsedCurrencies' => $activeExchangeRateProvider ? $activeExchangeRateProvider : [],
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,10 +11,6 @@ use App\Http\Controllers\V1\Admin\Dashboard\DashboardController;
|
|||||||
use App\Http\Controllers\V1\Admin\Estimate\EstimatesController;
|
use App\Http\Controllers\V1\Admin\Estimate\EstimatesController;
|
||||||
use App\Http\Controllers\V1\Admin\Estimate\EstimateTemplatesController;
|
use App\Http\Controllers\V1\Admin\Estimate\EstimateTemplatesController;
|
||||||
use App\Http\Controllers\V1\Admin\ExchangeRate\ExchangeRateProviderController;
|
use App\Http\Controllers\V1\Admin\ExchangeRate\ExchangeRateProviderController;
|
||||||
use App\Http\Controllers\V1\Admin\ExchangeRate\GetActiveProviderController;
|
|
||||||
use App\Http\Controllers\V1\Admin\ExchangeRate\GetExchangeRateController;
|
|
||||||
use App\Http\Controllers\V1\Admin\ExchangeRate\GetSupportedCurrenciesController;
|
|
||||||
use App\Http\Controllers\V1\Admin\ExchangeRate\GetUsedCurrenciesController;
|
|
||||||
use App\Http\Controllers\V1\Admin\Expense\ExpenseCategoriesController;
|
use App\Http\Controllers\V1\Admin\Expense\ExpenseCategoriesController;
|
||||||
use App\Http\Controllers\V1\Admin\Expense\ExpensesController;
|
use App\Http\Controllers\V1\Admin\Expense\ExpensesController;
|
||||||
use App\Http\Controllers\V1\Admin\Expense\ShowReceiptController;
|
use App\Http\Controllers\V1\Admin\Expense\ShowReceiptController;
|
||||||
@@ -354,13 +350,13 @@ Route::prefix('/v1')->group(function () {
|
|||||||
// Exchange Rate
|
// Exchange Rate
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
|
|
||||||
Route::get('/currencies/{currency}/exchange-rate', GetExchangeRateController::class);
|
Route::get('/currencies/{currency}/exchange-rate', [ExchangeRateProviderController::class, 'getRate']);
|
||||||
|
|
||||||
Route::get('/currencies/{currency}/active-provider', GetActiveProviderController::class);
|
Route::get('/currencies/{currency}/active-provider', [ExchangeRateProviderController::class, 'activeProvider']);
|
||||||
|
|
||||||
Route::get('/used-currencies', GetUsedCurrenciesController::class);
|
Route::get('/used-currencies', [ExchangeRateProviderController::class, 'usedCurrencies']);
|
||||||
|
|
||||||
Route::get('/supported-currencies', GetSupportedCurrenciesController::class);
|
Route::get('/supported-currencies', [ExchangeRateProviderController::class, 'supportedCurrencies']);
|
||||||
|
|
||||||
Route::apiResource('exchange-rate-providers', ExchangeRateProviderController::class);
|
Route::apiResource('exchange-rate-providers', ExchangeRateProviderController::class);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user