Commit 34d0e013 authored by Jack Stupple's avatar Jack Stupple

massive update to imports to allow for multple currencies

parent 2c09c5a6
......@@ -187,8 +187,8 @@ class Initial extends Migration
]);
DB::table('languages')->insert([
'name' => 'Français',
'slug' => 'fr',
'name' => 'Nederlands',
'slug' => 'nl',
'enabled' => $this->created_at,
'created_at' => $this->created_at
]);
......
......@@ -24,6 +24,14 @@ class CreateCurrenciesTable extends Migration
$currency = new Currency();
$currency->currency_code = 'EUR';
$currency->save();
$currency = new Currency();
$currency->currency_code = 'GBP';
$currency->save();
$currency = new Currency();
$currency->currency_code = 'NOK';
$currency->save();
}
/**
......
......@@ -15,13 +15,13 @@ class AddNewSupportedLanguages extends Migration
{
$now = date('Y-m-d H:i:s');
// remove the old one that was spelled wrong
$de = \App\Language::where('name', 'Deutch')->first();
$de = \App\Language::where('name', 'Deutsch')->first();
if ($de) {
$de->delete();
}
$de = \App\Language::firstOrCreate([
'name' => 'Nederlands',
'name' => 'Deutsch',
'slug' => 'de'
]);
......
......@@ -22,6 +22,7 @@ class ImportJakeQuestions extends CharacterMigration
*/
public function up()
{
return;
$this->created_at = date('Y-m-d H:i:s');
$this->character = new \App\Character();
......
......@@ -22,6 +22,8 @@ class ImportJamalsQuestions extends CharacterMigration
*/
public function up()
{
return;
$this->created_at = date('Y-m-d H:i:s');
$this->character = new \App\Character();
......
......@@ -22,6 +22,8 @@ class ImportAnnQuestions extends CharacterMigration
*/
public function up()
{
return;
$this->created_at = date('Y-m-d H:i:s');
$this->character = new \App\Character();
......
......@@ -14,46 +14,57 @@ use Illuminate\Database\Migrations\Migration;
class CharacterMigration extends Migration
{
static $cache = [];
protected $questions = [];
protected $season_roundups = [];
protected $profile_pictures = [];
protected $base_modifiers = [];
protected $modifiers = [];
protected $languages = [];
protected $currencies = [];
protected $character;
protected function import()
{
$questions = $season_roundups = $profile_pictures = $base_modifiers = [];
if (isset($this->questions) && file_exists(APP_ROOT . $this->questions)) {
$questions = json_decode(file_get_contents(APP_ROOT . $this->questions), 1);
$this->questions = json_decode(file_get_contents(APP_ROOT . $this->questions), 1);
}
if (isset($this->season_roundups) && file_exists(APP_ROOT . $this->season_roundups)) {
$season_roundups = json_decode(file_get_contents(APP_ROOT . $this->season_roundups), 1);
$this->season_roundups = json_decode(file_get_contents(APP_ROOT . $this->season_roundups), 1);
}
if (isset($this->profile_pictures) && file_exists(APP_ROOT . $this->profile_pictures)) {
$profile_pictures = json_decode(file_get_contents(APP_ROOT . $this->profile_pictures), 1);
$this->profilePictures($profile_pictures);
$this->profile_pictures = json_decode(file_get_contents(APP_ROOT . $this->profile_pictures), 1);
$this->profilePictures();
}
if (isset($this->base_modifiers) && file_exists(APP_ROOT . $this->base_modifiers)) {
$base_modifiers = json_decode(file_get_contents(APP_ROOT . $this->base_modifiers), 1);
$this->modifiers($base_modifiers);
$this->base_modifiers = json_decode(file_get_contents(APP_ROOT . $this->base_modifiers), 1);
}
$languages = \App\Language::whereRaw('enabled <= NOW()')->get();
foreach ($languages as $language) {
$this->language = $language;
$this->languages[$language->slug] = $language;
}
if (isset($questions[$this->language->slug])) {
$this->questions($questions[$this->language->slug]);
}
$currencies = \App\Currency::all();
foreach ($currencies as $currency) {
$this->currencies[$currency->currency_code] = $currency;
}
if (isset($season_roundups[$this->language->slug])) {
$this->seasonRoundups($season_roundups[$this->language->slug]);
}
foreach ($languages as $language_code => $language) {
$this->questions($language);
$this->seasonRoundups($language);
}
foreach ($currencies as $currency_code => $currency) {
$this->baseModifiers($currency);
}
}
protected function profilePictures($profile_pictures)
protected function profilePictures()
{
foreach ($profile_pictures as $_profile_picture) {
foreach ($this->profile_pictures as $_profile_picture) {
$profile_picture = new \App\CharacterPicture();
$profile_picture->character_id = $this->character->id;
$profile_picture->uri = '/characters/' . strtolower($this->character->name) . '/profile-pictures/' . $_profile_picture['uri'];
......@@ -62,9 +73,9 @@ class CharacterMigration extends Migration
}
}
protected function modifiers($modifiers)
protected function baseModifiers($currency)
{
foreach ($modifiers as $_modifier) {
foreach ($this->base_modifiers[strtolower($currency->currency_code)] as $_modifier) {
$_category = $_modifier['category'];
$amount = $_modifier['amount'];
$income_outgoing = 'outgoing';
......@@ -88,102 +99,138 @@ class CharacterMigration extends Migration
$base_modifier->model_id = $this->character->id;
$base_modifier->amount = $amount;
$base_modifier->modifier_category_id = $category ? $category->id : null;
$base_modifier->currency_id = $currency->id;
$base_modifier->save();
}
}
protected function questions($questions)
protected function _question($_question, $language)
{
if (empty($_question['question'][strtolower($language->slug)])) {
return false;
}
$question = new \App\Question;
$question->title = $_question['question'][strtolower($language->slug)];
$question->question_category_id = $_question['question_category']->id;
$question->language_id = $language->id;
$question->character_id = $this->character->id;
$question->save();
return $question;
}
protected function _answer($_answer, $question, $language)
{
$answer = new \App\Answer();
$answer->question_id = $question->id;
$answer->title = $_answer['title'][strtolower($language->slug)];
$answer->body = $_answer['body'][strtolower($language->slug)];
$answer->weight = $_answer['weight'];
$answer->save();
return $answer;
}
protected function _modifier($_modifier, $answer, $currency)
{
$modifier = new \App\Modifier();
$modifier->model = 'answer';
$modifier->model_id = $answer->id;
$income_outgoing = 'outgoing';
if ($_modifier['category'] == 'Salary') {
$income_outgoing = 'income';
} else if (strpos($_modifier['category'], 'Other') !== false) {
if ($_modifier['category'] === 'Other-Income') {
$income_outgoing = 'income';
} else {
$income_outgoing = 'outgoing';
}
$_modifier['category'] = 'Other';
}
$category = $this->getModifierCategory($_modifier['category'], $income_outgoing);
$modifier->modifier_category_id = $category ? $category->id : null;
$modifier->currency_id = $currency->id;
$modifier->amount = $_modifier['amount'];
$modifier->save();
return $modifier;
}
protected function questions($language)
{
foreach ($questions as $_question) {
foreach ($this->questions as $_question) {
$question_category = \App\QuestionCategory::where('name', 'LIKE', $_question['question_category'])->first();
if (!$question_category) {
dd('question category does not exist: ' . $_question['question_category']);
}
$question = new \App\Question;
$question->title = $_question['question'];
$question->question_category_id = $question_category ? $question_category->id : 0;
$question->language_id = $this->language->id;
$question->character_id = $this->character->id;
$question->save();
foreach ($_question['answers'] as $_answer) {
$option = new \App\Answer();
$option->question_id = $question->id;
$option->title = $_answer['title'];
$option->body = $_answer['body'];
$option->weight = $_answer['weight'];
$option->save();
foreach ($_answer['modifiers'] as $_modifier) {
$modifier = new \App\Modifier();
$modifier->model = 'answer';
$modifier->model_id = $option->id;
$_question['question_category'] = $question_category;
$income_outgoing = 'outgoing';
if ($_modifier['category'] == 'salary') {
$income_outgoing = 'income';
} else if (strpos($_modifier['category'], 'other_') !== false) {
if ($_modifier['category'] == 'other_in') {
$income_outgoing = 'income';
} else {
$income_outgoing = 'outgoing';
}
$question = $this->_question($_question, $language);
if ($question === false) {
return false;
}
$_modifier['category'] = 'other';
foreach ($_question['answers'] as $_answer) {
$answer = $this->_answer($_answer, $question, $language);
foreach ($this->currencies as $currency) {
foreach ($_answer['modifiers'][strtolower($currency->currency_code)] as $_modifier) {
$this->_modifier($_modifier, $answer, $currency);
}
$category = $this->getModifierCategory($_modifier['category'], $income_outgoing);
$modifier->modifier_category_id = $category ? $category->id : null;
$modifier->amount = $_modifier['amount'];
$modifier->save();
}
foreach ($_answer['resource_categories'] as $_category) {
$category = \App\ResourceCategory::where('name', 'LIKE', $_category)->first();
foreach ($_answer['resource_categories'] as $_resource_category) {
$category = \App\ResourceCategory::where('name', 'LIKE', $_resource_category)->first();
if ($category) {
$option->resourceCategories()->syncWithoutDetaching($category);
$answer->resourceCategories()->syncWithoutDetaching($category);
}
}
}
}
}
protected function seasonRoundups($season_roundups)
protected function seasonRoundups($language)
{
foreach ($season_roundups as $_roundup) {
foreach ($this->season_roundups as $_roundup) {
$roundup = new \App\SeasonRoundup;
$roundup->language_id = $this->language->id;
$roundup->roundup_text = $_roundup['roundup_text'];
$roundup->language_id = $language->id;
$roundup->roundup_text = $_roundup['roundup_text'][strtolower($language->slug)];
$roundup->positive_negative = $_roundup['positive_negative'];
$roundup->character_id = $this->character->id;
$roundup->save();
foreach ($_roundup['modifiers'] as $_modifier) {
$modifier = new \App\Modifier();
$modifier->model = 'season_roundup';
$modifier->model_id = $roundup->id;
foreach ($this->currencies as $currency) {
foreach ($_roundup['modifiers'][strtolower($currency->currency_code)] as $_modifier) {
$modifier = new \App\Modifier();
$modifier->model = 'season_roundup';
$modifier->model_id = $roundup->id;
$income_outgoing = 'outgoing';
if ($_modifier['category'] == 'salary') {
$income_outgoing = 'income';
} else if (strpos($_modifier['category'], 'other_') !== false) {
if ($_modifier['category'] == 'other_in') {
$income_outgoing = 'outgoing';
if ($_modifier['category'] == 'salary') {
$income_outgoing = 'income';
} else {
$income_outgoing = 'outgoing';
}
} else if (strpos($_modifier['category'], 'other_') !== false) {
if ($_modifier['category'] == 'other_in') {
$income_outgoing = 'income';
} else {
$income_outgoing = 'outgoing';
}
$_modifier['category'] = 'other';
}
$_modifier['category'] = 'other';
}
$category = $this->getModifierCategory($_modifier['category'], $income_outgoing);
$category = $this->getModifierCategory($_modifier['category'], $income_outgoing);
$modifier->modifier_category_id = $category ? $category->id : null;
$modifier->amount = $_modifier['amount'];
$modifier->save();
$modifier->modifier_category_id = $category ? $category->id : null;
$modifier->currency_id = $currency->id;
$modifier->amount = $_modifier['amount'];
$modifier->save();
}
}
}
}
......
This diff is collapsed.
{
"en": [
{
"roundup_text": "Ann qualifies for a government scheme to get subsidies on her electricity and heating costs, so her utility bills have gone down.",
"positive_negative": 1,
"modifiers": [
{
"category": "bills",
"amount": -30
}
]
},
{
"roundup_text": "Ann's dog Billy has fallen ill and will require surgery.",
"positive_negative": -1,
"modifiers": [
{
"category": "other_out",
"amount": 100
}
]
},
{
"roundup_text": "Ann's local bus has stopped running at weekends so she will need to find alternative transport.",
"positive_negative": -1,
"modifiers": [
{
"category": "transport",
"amount": 80
}
]
},
{
"roundup_text": "Ann wins money in a radio competition.",
"positive_negative": 1,
"modifiers": [
{
"category": "other_in",
"amount": 50
}
]
},
{
"roundup_text": "Ann decides to adopt a cat from her friend who can longer look after it.",
"positive_negative": -1,
"modifiers": [
{
"category": "bills",
"amount": 30
}
]
},
{
"roundup_text": "Ann joins her friends in taking up a fitness class.",
"positive_negative": -1,
"modifiers": [
{
"category": "entertainment",
"amount": 20
}
]
},
{
"roundup_text": "Ann helps her friend out at her allotment and get free fruit and vegetables.",
"positive_negative": 1,
"modifiers": [
{
"category": "groceries",
"amount": -60
}
]
},
{
"roundup_text": "Ann's son treats her to a weekend spa break for her birthday.",
"positive_negative": 1,
"modifiers": []
},
{
"roundup_text": "Ann subscribes to a film streaming service.",
"positive_negative": -1,
"modifiers": [
{
"category": "entertainment",
"amount": 9.99
}
]
}
],
"fr": []
}
[
{
"roundup_text": "Ann qualifies for a government scheme to get subsidies on her electricity and heating costs, so her utility bills have gone down.",
"positive_negative": 1,
"modifiers": [
{
"category": "bills",
"amount": -30
}
]
},
{
"roundup_text": "Ann's dog Billy has fallen ill and will require surgery.",
"positive_negative": -1,
"modifiers": [
{
"category": "other_out",
"amount": 100
}
]
},
{
"roundup_text": "Ann's local bus has stopped running at weekends so she will need to find alternative transport.",
"positive_negative": -1,
"modifiers": [
{
"category": "transport",
"amount": 80
}
]
},
{
"roundup_text": "Ann wins money in a radio competition.",
"positive_negative": 1,
"modifiers": [
{
"category": "other_in",
"amount": 50
}
]
},
{
"roundup_text": "Ann decides to adopt a cat from her friend who can longer look after it.",
"positive_negative": -1,
"modifiers": [
{
"category": "bills",
"amount": 30
}
]
},
{
"roundup_text": "Ann joins her friends in taking up a fitness class.",
"positive_negative": -1,
"modifiers": [
{
"category": "entertainment",
"amount": 20
}
]
},
{
"roundup_text": "Ann helps her friend out at her allotment and get free fruit and vegetables.",
"positive_negative": 1,
"modifiers": [
{
"category": "groceries",
"amount": -60
}
]
},
{
"roundup_text": "Ann's son treats her to a weekend spa break for her birthday.",
"positive_negative": 1,
"modifiers": []
},
{
"roundup_text": "Ann subscribes to a film streaming service.",
"positive_negative": -1,
"modifiers": [
{
"category": "entertainment",
"amount": 9.99
}
]
}
]
[
{
"category": "salary",
"amount": 1131
},
{
"category": "rent",
"amount": 450
},
{
"category": "transport",
"amount": 55
},
{
"category": "groceries",
"amount": 66.55
},
{
"category": "bills",
"amount": 60
},
{
"category": "shopping",
"amount": 119.50
},
{
"category": "entertainment",
"amount": 160
},
{
"category": "credit cards \/ loans",
"amount": 50
},
{
"category": "investments",
"amount": 0
},
{
"category": "other_out",
"amount": 0
},
{
"category": "other_in",
"amount": 0
},
{
"category": "savings",
"amount":