2014-07-04 07:22:16 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: sander
|
|
|
|
* Date: 03/07/14
|
|
|
|
* Time: 21:34
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Firefly\Helper\Migration;
|
|
|
|
|
|
|
|
|
|
|
|
class MigrationHelper implements MigrationHelperInterface
|
|
|
|
{
|
|
|
|
protected $path;
|
|
|
|
protected $JSON;
|
2014-07-04 11:39:21 +02:00
|
|
|
protected $map = [];
|
2014-07-04 07:22:16 +02:00
|
|
|
|
|
|
|
public function loadFile($path)
|
|
|
|
{
|
|
|
|
$this->path = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validFile()
|
|
|
|
{
|
|
|
|
// file does not exist:
|
2014-07-04 11:39:21 +02:00
|
|
|
if (!file_exists($this->path)) {
|
2014-07-05 16:19:15 +02:00
|
|
|
\Log::error('Migration file ' . $this->path . ' does not exist!');
|
2014-07-04 07:22:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load the content:
|
|
|
|
$content = file_get_contents($this->path);
|
2014-07-04 11:39:21 +02:00
|
|
|
if ($content === false) {
|
2014-07-04 07:22:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the content
|
|
|
|
$this->JSON = json_decode($content);
|
2014-07-04 11:39:21 +02:00
|
|
|
if (is_null($this->JSON)) {
|
2014-07-04 07:22:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-05 16:19:15 +02:00
|
|
|
\Log::info('Migration file ' . $this->path . ' is valid!');
|
|
|
|
return true;
|
2014-07-04 07:22:16 +02:00
|
|
|
}
|
2014-07-04 11:39:21 +02:00
|
|
|
|
|
|
|
public function migrate()
|
|
|
|
{
|
2014-07-05 16:19:15 +02:00
|
|
|
\Log::info('Start of migration.');
|
|
|
|
\DB::beginTransaction();
|
2014-07-04 11:39:21 +02:00
|
|
|
|
2014-07-05 16:19:15 +02:00
|
|
|
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;
|
2014-07-04 11:39:21 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 16:19:15 +02:00
|
|
|
protected function _importAccounts()
|
2014-07-04 11:39:21 +02:00
|
|
|
{
|
|
|
|
|
2014-07-05 16:19:15 +02:00
|
|
|
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
|
|
|
|
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
|
|
|
|
\Log::info('Going to import ' . count($this->JSON->accounts) . ' accounts.');
|
2014-07-04 11:39:21 +02:00
|
|
|
foreach ($this->JSON->accounts as $entry) {
|
|
|
|
// create account:
|
|
|
|
if ($entry->openingbalance == 0) {
|
|
|
|
$account = $accounts->store(['name' => $entry->name]);
|
|
|
|
} else {
|
|
|
|
$account = $accounts->storeWithInitialBalance(
|
|
|
|
['name' => $entry->name],
|
2014-07-05 16:19:15 +02:00
|
|
|
new \Carbon\Carbon($entry->openingbalancedate),
|
2014-07-04 11:39:21 +02:00
|
|
|
floatval($entry->openingbalance)
|
|
|
|
);
|
|
|
|
}
|
2014-07-05 16:19:15 +02:00
|
|
|
$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;
|
2014-07-04 11:39:21 +02:00
|
|
|
}
|
2014-07-05 16:19:15 +02:00
|
|
|
|
2014-07-04 11:39:21 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-05 16:19:15 +02:00
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
2014-07-04 07:22:16 +02:00
|
|
|
}
|