Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Learning and Work
managing-money-api
Commits
6db0a10f
Commit
6db0a10f
authored
Jun 27, 2018
by
Jack Stupple
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add currency console command
parent
ab4dc49f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
1 deletion
+72
-1
app/Console/Commands/AddCurrency.php
app/Console/Commands/AddCurrency.php
+69
-0
app/Console/Kernel.php
app/Console/Kernel.php
+3
-1
No files found.
app/Console/Commands/AddCurrency.php
0 → 100644
View file @
6db0a10f
<?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
app/Console/Kernel.php
View file @
6db0a10f
...
...
@@ -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
];
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment