¨4.0.1¨
This commit is contained in:
@@ -4,6 +4,7 @@ namespace Modules\Currency\Admin;
|
||||
|
||||
use Modules\Currency\Currency;
|
||||
use Modules\Admin\Ui\AdminTable;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class CurrencyRateTable extends AdminTable
|
||||
{
|
||||
@@ -12,12 +13,13 @@ class CurrencyRateTable extends AdminTable
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $rawColumns = ['rate', 'updated_at'];
|
||||
protected array $rawColumns = ['rate', 'updated_at'];
|
||||
|
||||
|
||||
/**
|
||||
* Make table response for the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function make()
|
||||
{
|
||||
|
||||
@@ -19,6 +19,7 @@ class CurrencyRateTabs extends Tabs
|
||||
->add($this->general());
|
||||
}
|
||||
|
||||
|
||||
private function general()
|
||||
{
|
||||
return tap(new Tab('general', trans('currency::currency_rates.tabs.general')), function (Tab $tab) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
return [
|
||||
'services' => [
|
||||
'array' => ['latest_rates' => []],
|
||||
'array' => ['latestRates' => []],
|
||||
'fixer' => ['access_key' => env('FIXER_ACCESS_KEY')],
|
||||
'forge' => ['api_key' => env('FORGE_API_KEY')],
|
||||
'currency_data_feed' => ['api_key' => env('CURRENCY_DATA_FEED_API_KEY')],
|
||||
|
||||
@@ -25,14 +25,16 @@ class RefreshCurrencyRatesCommand extends Command
|
||||
/**
|
||||
* The currency rate exchanger instance.
|
||||
*
|
||||
* @var \Modules\Currency\Services\CurrencyRateExchanger
|
||||
* @var CurrencyRateExchanger
|
||||
*/
|
||||
private $exchanger;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @param \Modules\Currency\Services\CurrencyRateExchanger $exchanger
|
||||
* @param CurrencyRateExchanger $exchanger
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(CurrencyRateExchanger $exchanger)
|
||||
@@ -42,6 +44,7 @@ class RefreshCurrencyRatesCommand extends Command
|
||||
$this->exchanger = $exchanger;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
|
||||
@@ -18,6 +18,18 @@ class Currency
|
||||
*/
|
||||
private static $currencies;
|
||||
|
||||
|
||||
/**
|
||||
* Get all currency codes.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function codes()
|
||||
{
|
||||
return array_keys(self::all());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all currencies.
|
||||
*
|
||||
@@ -32,15 +44,6 @@ class Currency
|
||||
return self::$currencies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all currency codes.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function codes()
|
||||
{
|
||||
return array_keys(self::all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all currency names.
|
||||
@@ -54,10 +57,12 @@ class Currency
|
||||
}, self::all());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get name of the give currency code.
|
||||
*
|
||||
* @param string $code
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function name($code)
|
||||
@@ -65,10 +70,12 @@ class Currency
|
||||
return array_get(self::all(), "{$code}.name");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get subunit for the given currency code.
|
||||
*
|
||||
* @param string $code
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function subunit($code)
|
||||
|
||||
@@ -21,6 +21,7 @@ class CreateCurrencyRatesTable extends Migration
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Modules\Currency\Entities;
|
||||
|
||||
use Money\Currency;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Modules\Support\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Modules\Currency\Admin\CurrencyRateTable;
|
||||
@@ -17,6 +17,7 @@ class CurrencyRate extends Model
|
||||
*/
|
||||
protected $fillable = ['currency', 'rate'];
|
||||
|
||||
|
||||
/**
|
||||
* Perform any actions required after the model boots.
|
||||
*
|
||||
@@ -29,10 +30,12 @@ class CurrencyRate extends Model
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Refresh all supported currencies exchange rate.
|
||||
*
|
||||
* @param \Modules\Currency\Services\CurrencyRateExchanger $exchanger
|
||||
* @param CurrencyRateExchanger $exchanger
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function refreshRates(CurrencyRateExchanger $exchanger)
|
||||
@@ -46,10 +49,12 @@ class CurrencyRate extends Model
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get currency rate for the given currency.
|
||||
*
|
||||
* @param string $currency
|
||||
*
|
||||
* @return int|float
|
||||
*/
|
||||
public static function for($currency)
|
||||
@@ -59,10 +64,11 @@ class CurrencyRate extends Model
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get table data for the resource
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function table()
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Modules\Currency\Http\Controllers\Admin;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
use Modules\Admin\Traits\HasCrudActions;
|
||||
use Modules\Currency\Entities\CurrencyRate;
|
||||
use Modules\Currency\Services\CurrencyRateExchanger;
|
||||
@@ -39,11 +40,13 @@ class CurrencyRateController
|
||||
*/
|
||||
protected $validation = UpdateCurrencyRateRequest::class;
|
||||
|
||||
|
||||
/**
|
||||
* Refresh currency rates.
|
||||
*
|
||||
* @param \Modules\Currency\Services\CurrencyRateExchanger $exchanger
|
||||
* @return \Illuminate\Http\Response
|
||||
* @param CurrencyRateExchanger $exchanger
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function refresh(CurrencyRateExchanger $exchanger)
|
||||
{
|
||||
|
||||
@@ -2,17 +2,20 @@
|
||||
|
||||
namespace Modules\Currency\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class CurrentCurrencyController
|
||||
{
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param string $currency
|
||||
* @return \Illuminate\Http\Response
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function store($currency)
|
||||
{
|
||||
if (! in_array($currency, setting('supported_currencies'))) {
|
||||
if (!in_array($currency, setting('supported_currencies'))) {
|
||||
return back();
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ class UpdateCurrencyRateRequest extends Request
|
||||
*/
|
||||
protected $availableAttributes = 'currency::attributes';
|
||||
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
|
||||
@@ -10,7 +10,8 @@ class CreateCurrencyRates
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param \Modules\Setting\Events\SettingSaved $event
|
||||
* @param SettingSaved $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle(SettingSaved $event)
|
||||
@@ -18,6 +19,7 @@ class CreateCurrencyRates
|
||||
CurrencyRate::insert($this->rates());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the currency rates.
|
||||
*
|
||||
|
||||
@@ -15,7 +15,7 @@ class CurrencyExchangeRateServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
if (! config('app.installed')) {
|
||||
if (!config('app.installed')) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ class CurrencyExchangeRateServiceProvider extends ServiceProvider
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Setup currency rate exchange service.
|
||||
*
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Modules\Currency\Providers;
|
||||
|
||||
use Modules\Support\Traits\AddsAsset;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Modules\Admin\Ui\Facades\TabManager;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
@@ -11,8 +10,6 @@ use Modules\Currency\Console\RefreshCurrencyRatesCommand;
|
||||
|
||||
class CurrencyServiceProvider extends ServiceProvider
|
||||
{
|
||||
use AddsAsset;
|
||||
|
||||
/**
|
||||
* Bootstrap the application services.
|
||||
*
|
||||
@@ -20,14 +17,12 @@ class CurrencyServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
if (! config('app.installed')) {
|
||||
if (!config('app.installed')) {
|
||||
return;
|
||||
}
|
||||
|
||||
TabManager::register('currency_rates', CurrencyRateTabs::class);
|
||||
|
||||
$this->addAdminAssets('admin.currency_rates.index', ['admin.currency.js']);
|
||||
|
||||
if ($this->app->runningInConsole() && setting('auto_refresh_currency_rates', false)) {
|
||||
$this->commands(RefreshCurrencyRatesCommand::class);
|
||||
|
||||
@@ -35,6 +30,7 @@ class CurrencyServiceProvider extends ServiceProvider
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function registerScheduler()
|
||||
{
|
||||
$this->app->booted(function ($app) {
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Modules\Currency\Providers;
|
||||
|
||||
use Modules\Setting\Events\SettingSaved;
|
||||
use Modules\Currency\Listeners\CreateCurrencyRates;
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
|
||||
class EventServiceProvider extends ServiceProvider
|
||||
@@ -12,8 +14,8 @@ class EventServiceProvider extends ServiceProvider
|
||||
* @var array
|
||||
*/
|
||||
protected $listen = [
|
||||
\Modules\Setting\Events\SettingSaved::class => [
|
||||
\Modules\Currency\Listeners\CreateCurrencyRates::class,
|
||||
SettingSaved::class => [
|
||||
CreateCurrencyRates::class,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'exchange_service_is_not_configured' => 'Currency rate exchange service is not configured.',
|
||||
'exchange_service_is_not_configured' => 'Currency rate exchange service is not configured',
|
||||
];
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
@endpush
|
||||
|
||||
@push('scripts')
|
||||
<script>
|
||||
<script type="module">
|
||||
keypressAction([
|
||||
{ key: 'b', route: "{{ route('admin.currency_rates.index') }}" }
|
||||
]);
|
||||
|
||||
@@ -31,10 +31,16 @@
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@push('globals')
|
||||
@vite([
|
||||
'Modules/Currency/Resources/assets/admin/js/main.js',
|
||||
])
|
||||
@endpush
|
||||
|
||||
@push('scripts')
|
||||
<script>
|
||||
<script type="module">
|
||||
DataTable.setRoutes('#currency-rates-table .table', {
|
||||
index: 'admin.currency_rates.index',
|
||||
table: 'admin.currency_rates.table',
|
||||
edit: 'admin.currency_rates.edit',
|
||||
});
|
||||
|
||||
|
||||
@@ -20,6 +20,12 @@ Route::put('currency-rates/{id}', [
|
||||
'middleware' => 'can:admin.currency_rates.edit',
|
||||
]);
|
||||
|
||||
Route::get('currency-rates/index/table', [
|
||||
'as' => 'admin.currency_rates.table',
|
||||
'uses' => 'CurrencyRateController@table',
|
||||
'middleware' => 'can:admin.currency_rates.index',
|
||||
]);
|
||||
|
||||
Route::get('currency-rates/refresh', [
|
||||
'as' => 'admin.currency_rates.refresh',
|
||||
'uses' => 'CurrencyRateController@refresh',
|
||||
|
||||
@@ -10,32 +10,35 @@ class CurrencyRateExchanger
|
||||
/**
|
||||
* Instance of swap.
|
||||
*
|
||||
* @var \Swap\Swap
|
||||
* @var Swap
|
||||
*/
|
||||
private $swap;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new CurrencyRateExchanger instance.
|
||||
*
|
||||
* @param \Swap\Swap $swap
|
||||
* @param Swap $swap
|
||||
*/
|
||||
public function __construct(Swap $swap)
|
||||
{
|
||||
$this->swap = $swap;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Exchange to the latest currency rate.
|
||||
*
|
||||
* @param string $fromCurrency
|
||||
* @param string $toCurrency
|
||||
*
|
||||
* @return float|null
|
||||
*/
|
||||
public function exchange($fromCurrency, $toCurrency)
|
||||
{
|
||||
try {
|
||||
return $this->swap->latest("{$fromCurrency}/{$toCurrency}")->getValue();
|
||||
} catch (Exception $e) {
|
||||
} catch (Exception) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user