mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-29 20:51:09 -04:00
v5.0.0 update
This commit is contained in:
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Http\Controllers\V1\Admin\ExchangeRate;
|
||||
|
||||
use Crater\Http\Controllers\Controller;
|
||||
use Crater\Http\Requests\ExchangeRateProviderRequest;
|
||||
use Crater\Http\Resources\ExchangeRateProviderResource;
|
||||
use Crater\Models\ExchangeRateProvider;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ExchangeRateProviderController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->authorize('viewAny', ExchangeRateProvider::class);
|
||||
|
||||
$limit = $request->has('limit') ? $request->limit : 5;
|
||||
|
||||
$exchangeRateProviders = ExchangeRateProvider::whereCompany()->paginate($limit);
|
||||
|
||||
return ExchangeRateProviderResource::collection($exchangeRateProviders);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(ExchangeRateProviderRequest $request)
|
||||
{
|
||||
$this->authorize('create', ExchangeRateProvider::class);
|
||||
|
||||
$query = ExchangeRateProvider::checkActiveCurrencies($request);
|
||||
|
||||
if (count($query) !== 0) {
|
||||
return respondJson('currency_used', 'Currency used.');
|
||||
}
|
||||
|
||||
$checkConverterApi = ExchangeRateProvider::checkExchangeRateProviderStatus($request);
|
||||
|
||||
if ($checkConverterApi->status() == 200) {
|
||||
$exchangeRateProvider = ExchangeRateProvider::createFromRequest($request);
|
||||
|
||||
return new ExchangeRateProviderResource($exchangeRateProvider);
|
||||
}
|
||||
|
||||
return $checkConverterApi;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param \Crater\Models\ExchangeRateProvider $exchangeRateProvider
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show(ExchangeRateProvider $exchangeRateProvider)
|
||||
{
|
||||
$this->authorize('view', $exchangeRateProvider);
|
||||
|
||||
return new ExchangeRateProviderResource($exchangeRateProvider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Crater\Models\ExchangeRateProvider $exchangeRateProvider
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(ExchangeRateProviderRequest $request, ExchangeRateProvider $exchangeRateProvider)
|
||||
{
|
||||
$this->authorize('update', $exchangeRateProvider);
|
||||
|
||||
$query = $exchangeRateProvider->checkUpdateActiveCurrencies($request);
|
||||
|
||||
if (count($query) !== 0) {
|
||||
return respondJson('currency_used', 'Currency used.');
|
||||
}
|
||||
|
||||
$checkConverterApi = ExchangeRateProvider::checkExchangeRateProviderStatus($request);
|
||||
|
||||
if ($checkConverterApi->status() == 200) {
|
||||
$exchangeRateProvider->updateFromRequest($request);
|
||||
|
||||
return new ExchangeRateProviderResource($exchangeRateProvider);
|
||||
}
|
||||
|
||||
return $checkConverterApi;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param \Crater\Models\ExchangeRateProvider $exchangeRateProvider
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy(ExchangeRateProvider $exchangeRateProvider)
|
||||
{
|
||||
$this->authorize('delete', $exchangeRateProvider);
|
||||
|
||||
if ($exchangeRateProvider->active == true) {
|
||||
return respondJson('provider_active', 'Provider Active.');
|
||||
}
|
||||
|
||||
$exchangeRateProvider->delete();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Http\Controllers\V1\Admin\ExchangeRate;
|
||||
|
||||
use Crater\Http\Controllers\Controller;
|
||||
use Crater\Models\Currency;
|
||||
use Crater\Models\ExchangeRateProvider;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class GetActiveProviderController extends Controller
|
||||
{
|
||||
/**
|
||||
* Handle the incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\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);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Http\Controllers\V1\Admin\ExchangeRate;
|
||||
|
||||
use Crater\Http\Controllers\Controller;
|
||||
use Crater\Models\CompanySetting;
|
||||
use Crater\Models\Currency;
|
||||
use Crater\Models\ExchangeRateLog;
|
||||
use Crater\Models\ExchangeRateProvider;
|
||||
use Crater\Traits\ExchangeRateProvidersTrait;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class GetExchangeRateController extends Controller
|
||||
{
|
||||
use ExchangeRateProvidersTrait;
|
||||
|
||||
/**
|
||||
* Handle the incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\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);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Http\Controllers\V1\Admin\ExchangeRate;
|
||||
|
||||
use Crater\Http\Controllers\Controller;
|
||||
use Crater\Models\ExchangeRateProvider;
|
||||
use Crater\Traits\ExchangeRateProvidersTrait;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class GetSupportedCurrenciesController extends Controller
|
||||
{
|
||||
use ExchangeRateProvidersTrait;
|
||||
|
||||
/**
|
||||
* Handle the incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$this->authorize('viewAny', ExchangeRateProvider::class);
|
||||
|
||||
return $this->getSupportedCurrencies($request);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Http\Controllers\V1\Admin\ExchangeRate;
|
||||
|
||||
use Crater\Http\Controllers\Controller;
|
||||
use Crater\Models\ExchangeRateProvider;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class GetUsedCurrenciesController extends Controller
|
||||
{
|
||||
/**
|
||||
* Handle the incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\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 : [],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user