mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-26 05:26:17 +00:00
Some cleanup in migration controller.
This commit is contained in:
@@ -20,13 +20,10 @@ class MigrateController extends BaseController
|
|||||||
public function upload()
|
public function upload()
|
||||||
{
|
{
|
||||||
if (Input::hasFile('file') && Input::file('file')->isValid()) {
|
if (Input::hasFile('file') && Input::file('file')->isValid()) {
|
||||||
// move file to storage:
|
|
||||||
// ->move($destinationPath, $fileName);
|
|
||||||
$path = storage_path();
|
$path = storage_path();
|
||||||
$fileName = 'firefly-iii-import-' . date('Y-m-d-H-i') . '.json';
|
$fileName = 'firefly-iii-import-' . date('Y-m-d-H-i') . '.json';
|
||||||
$fullName = $path . DIRECTORY_SEPARATOR . $fileName;
|
$fullName = $path . DIRECTORY_SEPARATOR . $fileName;
|
||||||
if (is_writable($path)) {
|
if (Input::file('file')->move($path, $fileName)) {
|
||||||
Input::file('file')->move($path, $fileName);
|
|
||||||
// so now Firefly pushes something in a queue and does something with it! Yay!
|
// so now Firefly pushes something in a queue and does something with it! Yay!
|
||||||
\Log::debug('Pushed a job to start the import.');
|
\Log::debug('Pushed a job to start the import.');
|
||||||
Queue::push('Firefly\Queue\Import@start', ['file' => $fullName, 'user' => \Auth::user()->id]);
|
Queue::push('Firefly\Queue\Import@start', ['file' => $fullName, 'user' => \Auth::user()->id]);
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ use Illuminate\Queue\Jobs\Job;
|
|||||||
* Class Import
|
* Class Import
|
||||||
*
|
*
|
||||||
* @package Firefly\Queue
|
* @package Firefly\Queue
|
||||||
|
*
|
||||||
|
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
|
||||||
*/
|
*/
|
||||||
class Import
|
class Import
|
||||||
{
|
{
|
||||||
@@ -37,7 +39,7 @@ class Import
|
|||||||
protected $_recurring;
|
protected $_recurring;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* This constructs the import handler and initiates all the relevant interfaces / classes.
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
@@ -54,27 +56,69 @@ class Import
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* The final step in the import routine is to get all transactions which have one of their accounts
|
||||||
|
* still set to "import", which means it is a cash transaction. This routine will set them all to cash instead.
|
||||||
|
*
|
||||||
* @param Job $job
|
* @param Job $job
|
||||||
* @param array $payload
|
* @param array $payload
|
||||||
*/
|
*/
|
||||||
public function cleanImportAccount(Job $job, array $payload)
|
public function cleanImportAccount(Job $job, array $payload)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/** @var \Importmap $importMap */
|
||||||
|
$importMap = $this->_repository->findImportmap($payload['mapID']);
|
||||||
|
$user = $importMap->user;
|
||||||
|
$this->overruleUser($user);
|
||||||
|
|
||||||
|
// two import account types.
|
||||||
$importAccountType = $this->_accounts->findAccountType('Import account');
|
$importAccountType = $this->_accounts->findAccountType('Import account');
|
||||||
$importAccounts = $this->_accounts->getByAccountType($importAccountType);
|
$cashAccountType = $this->_accounts->findAccountType('Cash account');
|
||||||
if (count($importAccounts) == 0) {
|
|
||||||
|
// find or create import account:
|
||||||
|
$importAccount = $this->_accounts->firstOrCreate(
|
||||||
|
[
|
||||||
|
'name' => 'Import account',
|
||||||
|
'account_type_id' => $importAccountType->id,
|
||||||
|
'active' => 1,
|
||||||
|
'user_id' => $user->id,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// find or create cash account:
|
||||||
|
$cashAccount = $this->_accounts->firstOrCreate(
|
||||||
|
[
|
||||||
|
'name' => 'Cash account',
|
||||||
|
'account_type_id' => $cashAccountType->id,
|
||||||
|
'active' => 1,
|
||||||
|
'user_id' => $user->id,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// update all users transactions:
|
||||||
|
$count = \DB::table('transactions')
|
||||||
|
->where('account_id', $importAccount->id)->count();
|
||||||
|
|
||||||
|
\DB::table('transactions')
|
||||||
|
->where('account_id', $importAccount->id)
|
||||||
|
->update(['account_id' => $cashAccount->id]);
|
||||||
|
|
||||||
|
\Log::debug('Updated ' . $count . ' transactions from Import Account to cash.');
|
||||||
$job->delete();
|
$job->delete();
|
||||||
} else if (count($importAccounts) == 1) {
|
|
||||||
/** @var \Account $importAccount */
|
|
||||||
$importAccount = $importAccounts[0];
|
|
||||||
$transactions = $importAccount->transactions()->get();
|
|
||||||
/** @var \Transaction $transaction */
|
|
||||||
foreach ($transactions as $transaction) {
|
|
||||||
$transaction->account()->associate($importAccount);
|
|
||||||
$transaction->save();
|
|
||||||
}
|
}
|
||||||
\Log::debug('Updated ' . count($transactions) . ' transactions from Import Account to cash.');
|
|
||||||
}
|
/**
|
||||||
$job->delete();
|
* @param \User $user
|
||||||
|
*/
|
||||||
|
protected function overruleUser(\User $user)
|
||||||
|
{
|
||||||
|
$this->_accounts->overruleUser($user);
|
||||||
|
$this->_budgets->overruleUser($user);
|
||||||
|
$this->_categories->overruleUser($user);
|
||||||
|
$this->_journals->overruleUser($user);
|
||||||
|
$this->_limits->overruleUser($user);
|
||||||
|
$this->_repository->overruleUser($user);
|
||||||
|
$this->_piggybanks->overruleUser($user);
|
||||||
|
$this->_recurring->overruleUser($user);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -123,7 +167,6 @@ class Import
|
|||||||
$user = $importMap->user;
|
$user = $importMap->user;
|
||||||
$this->overruleUser($user);
|
$this->overruleUser($user);
|
||||||
|
|
||||||
|
|
||||||
// maybe we've already imported this account:
|
// maybe we've already imported this account:
|
||||||
$importEntry = $this->_repository->findImportEntry($importMap, 'Account', intval($payload['data']['id']));
|
$importEntry = $this->_repository->findImportEntry($importMap, 'Account', intval($payload['data']['id']));
|
||||||
|
|
||||||
@@ -140,7 +183,8 @@ class Import
|
|||||||
// unset some data to make firstOrCreate work:
|
// unset some data to make firstOrCreate work:
|
||||||
$oldPayloadId = $payload['data']['id'];
|
$oldPayloadId = $payload['data']['id'];
|
||||||
unset($payload['data']['type_id'], $payload['data']['parent_component_id'],
|
unset($payload['data']['type_id'], $payload['data']['parent_component_id'],
|
||||||
$payload['data']['reporting'], $payload['data']['type'], $payload['data']['id'], $payload['data']['account_type']);
|
$payload['data']['reporting'],
|
||||||
|
$payload['data']['type'], $payload['data']['id'], $payload['data']['account_type']);
|
||||||
// set other data to make it work:
|
// set other data to make it work:
|
||||||
$expenseAccountType = $this->_accounts->findAccountType('Expense account');
|
$expenseAccountType = $this->_accounts->findAccountType('Expense account');
|
||||||
$payload['data']['account_type_id'] = $expenseAccountType->id;
|
$payload['data']['account_type_id'] = $expenseAccountType->id;
|
||||||
@@ -178,21 +222,6 @@ class Import
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param \User $user
|
|
||||||
*/
|
|
||||||
protected function overruleUser(\User $user)
|
|
||||||
{
|
|
||||||
$this->_accounts->overruleUser($user);
|
|
||||||
$this->_budgets->overruleUser($user);
|
|
||||||
$this->_categories->overruleUser($user);
|
|
||||||
$this->_journals->overruleUser($user);
|
|
||||||
$this->_limits->overruleUser($user);
|
|
||||||
$this->_repository->overruleUser($user);
|
|
||||||
$this->_piggybanks->overruleUser($user);
|
|
||||||
$this->_recurring->overruleUser($user);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Import a budget into Firefly.
|
* Import a budget into Firefly.
|
||||||
*
|
*
|
||||||
@@ -639,8 +668,12 @@ class Import
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Yet to import: component_predictables, , component_transfers
|
||||||
|
*
|
||||||
* @param Job $job
|
* @param Job $job
|
||||||
* @param $payload
|
* @param $payload
|
||||||
|
*
|
||||||
|
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
|
||||||
*/
|
*/
|
||||||
public function start(Job $job, array $payload)
|
public function start(Job $job, array $payload)
|
||||||
{
|
{
|
||||||
@@ -660,30 +693,29 @@ class Import
|
|||||||
$raw = file_get_contents($filename);
|
$raw = file_get_contents($filename);
|
||||||
$JSON = json_decode($raw);
|
$JSON = json_decode($raw);
|
||||||
|
|
||||||
$classes = ['accounts', 'components', 'limits', 'piggybanks',
|
$classes
|
||||||
'predictables', 'settings', 'transactions', 'transfers'];
|
= [
|
||||||
|
'accounts', 'components', 'limits',
|
||||||
|
'piggybanks', 'predictables', 'settings',
|
||||||
|
'transactions', 'transfers'
|
||||||
|
];
|
||||||
|
|
||||||
foreach ($classes as $classes_plural) {
|
foreach ($classes as $classesPlural) {
|
||||||
$class = ucfirst(\Str::singular($classes_plural));
|
$class = ucfirst(\Str::singular($classesPlural));
|
||||||
\Log::debug('Create job to import all ' . $classes_plural);
|
\Log::debug('Create job to import all ' . $classesPlural);
|
||||||
foreach ($JSON->$classes_plural as $entry) {
|
foreach ($JSON->$classesPlural as $entry) {
|
||||||
\Log::debug('Create job to import single ' . $class);
|
\Log::debug('Create job to import single ' . $class);
|
||||||
$fn = 'import' . $class;
|
$jobFunction = 'Firefly\Queue\Import@import' . $class;
|
||||||
$jobFunction = 'Firefly\Queue\Import@' . $fn;
|
|
||||||
\Queue::push($jobFunction, ['data' => $entry, 'class' => $class, 'mapID' => $importMap->id]);
|
\Queue::push($jobFunction, ['data' => $entry, 'class' => $class, 'mapID' => $importMap->id]);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// , components, limits, piggybanks, predictables, settings, transactions, transfers
|
|
||||||
// component_predictables, component_transactions, component_transfers
|
|
||||||
|
|
||||||
$count = count($JSON->component_transaction);
|
$count = count($JSON->component_transaction);
|
||||||
foreach ($JSON->component_transaction as $index => $entry) {
|
foreach ($JSON->component_transaction as $index => $entry) {
|
||||||
\Log::debug('Create job to import components_transaction! Yay! (' . $index . '/' . $count . ') ');
|
\Log::debug('Create job to import components_transaction! Yay! (' . $index . '/' . $count . ') ');
|
||||||
$fn = 'importComponentTransaction';
|
\Queue::push(
|
||||||
$jobFunction = 'Firefly\Queue\Import@' . $fn;
|
'Firefly\Queue\Import@importComponentTransaction', ['data' => $entry, 'mapID' => $importMap->id]
|
||||||
\Queue::push($jobFunction, ['data' => $entry, 'mapID' => $importMap->id]);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// queue a job to clean up the "import account", it should properly fall back
|
// queue a job to clean up the "import account", it should properly fall back
|
||||||
|
|||||||
Reference in New Issue
Block a user