Commit 6db0a10f authored by Jack Stupple's avatar Jack Stupple

add currency console command

parent ab4dc49f
<?php
namespace App\Console\Commands;
use App\Currency;
use App\Language;
use App\Modifier;
use App\Resource;
use App\ResourceCategory;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
/**
* Class ImportResources
* @package App\Console\Commands
* @see https://laravel.com/docs/5.6/artisan
*/
class AddCurrency extends Command
{
protected $signature = 'make:currency
{currency_code : Currency code like; GBP or USD}
{--M|multiplier= : Optionally when copying across all modifiers to this new currency, multiply them by this value.}';
protected $description = 'Inject a new currency with a multiplier of the existing values.';
public function handle()
{
if ($this->anticipate('This is intended for debugging, would you like to continue?', ['Yes', 'No'], 'Yes') !== 'Yes') {
$this->comment('No changes made');
return;
}
$currency_code = $this->argument('currency_code');
$multiplier = floatval($this->option('multiplier'));
$this->line('<info>Currency Code:</info> ' . $currency_code);
$this->line('<info>Currency Multiplier:</info> ' . $multiplier);
DB::beginTransaction();
$currency = new Currency;
$currency->currency_code = $currency_code;
$currency->save();
$default_currency = Currency::where('currency_code', 'EUR')->firstOrFail();
$total_modifiers = Modifier::where('currency_id', $default_currency->id)->count();
$this->output->progressStart($total_modifiers);
$modifiers = Modifier::where('currency_id', $default_currency->id)->get();
foreach ($modifiers as $modifier) {
$new_modifier = new Modifier();
$new_modifier->model = $modifier->model;
$new_modifier->model_id = $modifier->model_id;
$new_modifier->modifier_category_id = $modifier->modifier_category_id;
$new_modifier->currency_id = $currency->id;
$new_modifier->amount = $modifier->amount * $multiplier;
$new_modifier->save();
$this->output->progressAdvance();
}
$this->output->progressFinish();
DB::commit();
$this->info('Added new currency and updated modifiers');
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@
namespace App\Console;
use App\Console\Commands\AddCurrency;
use App\Console\Commands\ImportResources;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
......@@ -14,7 +15,8 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
ImportResources::class
ImportResources::class,
AddCurrency::class
];
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment