New stuff pertaining to the import procedure and user registration.

This commit is contained in:
James Cole
2014-07-05 16:19:15 +02:00
parent c3254c2351
commit a0c0dc288d
23 changed files with 305 additions and 63 deletions

View File

@@ -24,6 +24,7 @@ class MigrationHelper implements MigrationHelperInterface
{
// file does not exist:
if (!file_exists($this->path)) {
\Log::error('Migration file ' . $this->path . ' does not exist!');
return false;
}
@@ -38,20 +39,39 @@ class MigrationHelper implements MigrationHelperInterface
if (is_null($this->JSON)) {
return false;
}
\Log::info('Migration file ' . $this->path . ' is valid!');
return true;
}
public function migrate()
{
\Log::info('Start of migration.');
\DB::beginTransaction();
// create the accounts:
$this->_createAccounts();
try {
$this->_importAccounts();
$this->_importComponents();
$this->_importPiggybanks();
} catch (\Firefly\Exception\FireflyException $e) {
\DB::rollBack();
\Log::error('Rollback because of error!');
\Log::error($e->getMessage());
return false;
}
\DB::commit();
\Log::info('Done!');
return true;
}
protected function _createAccounts()
protected function _importAccounts()
{
$accounts = App::make('Firefly\Storage\Account\AccountRepositoryInterface');
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
\Log::info('Going to import ' . count($this->JSON->accounts) . ' accounts.');
foreach ($this->JSON->accounts as $entry) {
// create account:
if ($entry->openingbalance == 0) {
@@ -59,13 +79,66 @@ class MigrationHelper implements MigrationHelperInterface
} else {
$account = $accounts->storeWithInitialBalance(
['name' => $entry->name],
new Carbon($entry->openingbalancedate),
new \Carbon\Carbon($entry->openingbalancedate),
floatval($entry->openingbalance)
);
}
if ($account) {
$this->map['accounts'][$entry->id] = $account->id;
}
$this->map['accounts'][$entry->id] = $account->id;
\Log::info('Imported account "' . $entry->name . '" with balance ' . $entry->openingbalance);
}
}
protected function _importComponents()
{
$beneficiaryAT = \AccountType::where('description', 'Beneficiary account')->first();
$budgetType = \ComponentType::where('type', 'budget')->first();
$categoryType = \ComponentType::where('type', 'category')->first();
foreach ($this->JSON->components as $entry) {
switch ($entry->type->type) {
case 'beneficiary':
$beneficiary = $this->_importBeneficiary($entry, $beneficiaryAT);
$this->map['accounts'][$entry->id] = $beneficiary->id;
break;
case 'category':
$component = $this->_importComponent($entry, $categoryType);
$this->map['components'][$entry->id] = $component->id;
break;
case 'budget':
$component = $this->_importComponent($entry, $budgetType);
$this->map['components'][$entry->id] = $component->id;
break;
}
}
}
protected function _importPiggybanks() {
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
// get type for piggy:
$piggyAT = \AccountType::where('description', 'Piggy bank')->first();
foreach($this->JSON->piggybanks as $piggyBank) {
}
}
protected function _importBeneficiary($component, \AccountType $beneficiaryAT)
{
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
return $accounts->store(
[
'name' => $component->name,
'account_type' => $beneficiaryAT
]
);
}
protected function _importComponent($component, \ComponentType $type)
{
/** @var \Firefly\Storage\Component\ComponentRepositoryInterface $components */
$components = \App::make('Firefly\Storage\Component\ComponentRepositoryInterface');
return $components->store(['name' => $component->name, 'component_type' => $type]);
}
}