Code cleanup.

This commit is contained in:
James Cole
2015-07-06 20:56:20 +02:00
parent c555e28988
commit 001d72a484

View File

@@ -27,6 +27,10 @@ class Importer
protected $data; protected $data;
/** @var array */ /** @var array */
protected $errors; protected $errors;
/** @var array */
protected $importData;
/** @var array */
protected $importRow;
/** @var int */ /** @var int */
protected $imported = 0; protected $imported = 0;
/** @var array */ /** @var array */
@@ -41,6 +45,8 @@ class Importer
protected $specifix; protected $specifix;
/** /**
* Used by CsvController.
*
* @return array * @return array
*/ */
public function getErrors() public function getErrors()
@@ -49,6 +55,8 @@ class Importer
} }
/** /**
* Used by CsvController
*
* @return int * @return int
*/ */
public function getImported() public function getImported()
@@ -57,6 +65,8 @@ class Importer
} }
/** /**
* Used by CsvController
*
* @return int * @return int
*/ */
public function getRows() public function getRows()
@@ -108,10 +118,7 @@ class Importer
*/ */
protected function importRow($row) protected function importRow($row)
{ {
/* $data = $this->getFiller(); // These fields are necessary to create a new transaction journal. Some are optional
* These fields are necessary to create a new transaction journal. Some are optional:
*/
$data = $this->getFiller();
foreach ($row as $index => $value) { foreach ($row as $index => $value) {
$role = isset($this->roles[$index]) ? $this->roles[$index] : '_ignore'; $role = isset($this->roles[$index]) ? $this->roles[$index] : '_ignore';
$class = Config::get('csv.roles.' . $role . '.converter'); $class = Config::get('csv.roles.' . $role . '.converter');
@@ -128,20 +135,23 @@ class Importer
$data[$field] = $converter->convert(); $data[$field] = $converter->convert();
} }
// move to class vars.
$this->importData = $data;
$this->importRow = $row;
unset($data, $row);
// post processing and validating. // post processing and validating.
$data = $this->postProcess($data, $row); $this->postProcess();
$result = $this->validateData($data); $result = $this->validateData();
if ($result === true) {
$journal = $this->createTransactionJournal($data); if (!($result === true)) {
} else { return $result; // return error.
return $result;
} }
$journal = $this->createTransactionJournal();
if ($journal instanceof TransactionJournal) { if ($journal instanceof TransactionJournal) {
return true; return true;
} }
return $journal; return false;
} }
/** /**
@@ -168,21 +178,18 @@ class Importer
/** /**
* Row denotes the original data. * Row denotes the original data.
* *
* @param array $data * @return void
* @param array $row
*
* @return array
*/ */
protected function postProcess(array $data, array $row) protected function postProcess()
{ {
// do bank specific fixes (must be enabled but now all of them. // do bank specific fixes (must be enabled but now all of them.
foreach ($this->getSpecifix() as $className) { foreach ($this->getSpecifix() as $className) {
/** @var SpecifixInterface $specifix */ /** @var SpecifixInterface $specifix */
$specifix = App::make('FireflyIII\Helpers\Csv\Specifix\\' . $className); $specifix = App::make('FireflyIII\Helpers\Csv\Specifix\\' . $className);
$specifix->setData($data); $specifix->setData($this->importData);
$specifix->setRow($row); $specifix->setRow($this->importRow);
$data = $specifix->fix(); $this->importData = $specifix->fix();
} }
@@ -190,11 +197,10 @@ class Importer
foreach ($set as $className) { foreach ($set as $className) {
/** @var PostProcessorInterface $postProcessor */ /** @var PostProcessorInterface $postProcessor */
$postProcessor = App::make('FireflyIII\Helpers\Csv\PostProcessing\\' . $className); $postProcessor = App::make('FireflyIII\Helpers\Csv\PostProcessing\\' . $className);
$postProcessor->setData($data); $postProcessor->setData($this->importData);
$data = $postProcessor->process(); $this->importData = $postProcessor->process();
} }
return $data;
} }
/** /**
@@ -206,16 +212,15 @@ class Importer
} }
/** /**
* @param $data
* *
* @return bool|string * @return bool|string
*/ */
protected function validateData($data) protected function validateData()
{ {
if (is_null($data['date']) && is_null($data['date-rent'])) { if (is_null($this->importData['date']) && is_null($this->importData['date-rent'])) {
return 'No date value for this row.'; return 'No date value for this row.';
} }
if (is_null($data['opposing-account-object'])) { if (is_null($this->importData['opposing-account-object'])) {
return 'Opposing account is null'; return 'Opposing account is null';
} }
@@ -223,59 +228,30 @@ class Importer
} }
/** /**
* @param array $data
* *
* @return TransactionJournal|string * @return TransactionJournal|string
*/ */
protected function createTransactionJournal(array $data) protected function createTransactionJournal()
{ {
bcscale(2); bcscale(2);
$date = $data['date']; $date = $this->importData['date'];
if (is_null($data['date'])) { if (is_null($this->importData['date'])) {
$date = $data['date-rent']; $date = $this->importData['date-rent'];
} }
$transactionType = $this->getTransactionType(); // defaults to deposit
// defaults to deposit $errors = new MessageBag;
$transactionType = TransactionType::where('type', 'Deposit')->first(); $journal = TransactionJournal::create(
if ($data['amount'] < 0) { ['user_id' => Auth::user()->id, 'transaction_type_id' => $transactionType->id, 'transaction_currency_id' => $this->importData['currency']->id,
$transactionType = TransactionType::where('type', 'Withdrawal')->first(); 'description' => $this->importData['description'], 'completed' => 0, 'date' => $date, 'bill_id' => $this->importData['bill-id'],]
}
if (in_array($data['opposing-account-object']->accountType->type, ['Asset account', 'Default account'])) {
$transactionType = TransactionType::where('type', 'Transfer')->first();
}
$errors = new MessageBag;
$journal = TransactionJournal::create(
[
'user_id' => Auth::user()->id,
'transaction_type_id' => $transactionType->id,
'transaction_currency_id' => $data['currency']->id,
'description' => $data['description'],
'completed' => 0,
'date' => $date,
'bill_id' => $data['bill-id'],
]
); );
$errors = $journal->getErrors()->merge($errors);
if ($journal->getErrors()->count() == 0) { if ($journal->getErrors()->count() == 0) {
// create both transactions: $accountId = $this->importData['asset-account']->id; // create first transaction:
$transaction = Transaction::create( $amount = $this->importData['amount'];
[ $transaction = Transaction::create(['transaction_journal_id' => $journal->id, 'account_id' => $accountId, 'amount' => $amount]);
'transaction_journal_id' => $journal->id, $errors = $transaction->getErrors();
'account_id' => $data['asset-account']->id, $accountId = $this->importData['opposing-account-object']->id; // create second transaction:
'amount' => $data['amount'] $amount = bcmul($this->importData['amount'], -1);
] $transaction = Transaction::create(['transaction_journal_id' => $journal->id, 'account_id' => $accountId, 'amount' => $amount]);
);
$errors = $transaction->getErrors()->merge($errors);
$transaction = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $data['opposing-account-object']->id,
'amount' => bcmul($data['amount'], -1)
]
);
$errors = $transaction->getErrors()->merge($errors); $errors = $transaction->getErrors()->merge($errors);
} }
if ($errors->count() == 0) { if ($errors->count() == 0) {
@@ -286,25 +262,62 @@ class Importer
return $text; return $text;
} }
$this->saveBudget($journal);
$this->saveCategory($journal);
$this->saveTags($journal);
return $journal;
}
/**
* @return TransactionType
*/
protected function getTransactionType()
{
$transactionType = TransactionType::where('type', 'Deposit')->first();
if ($this->importData['amount'] < 0) {
$transactionType = TransactionType::where('type', 'Withdrawal')->first();
}
if (in_array($this->importData['opposing-account-object']->accountType->type, ['Asset account', 'Default account'])) {
$transactionType = TransactionType::where('type', 'Transfer')->first();
}
return $transactionType;
}
/**
* @param TransactionJournal $journal
*/
protected function saveBudget(TransactionJournal $journal)
{
// add budget: // add budget:
if (!is_null($data['budget'])) { if (!is_null($this->importData['budget'])) {
$journal->budgets()->save($data['budget']); $journal->budgets()->save($this->importData['budget']);
} }
}
/**
* @param TransactionJournal $journal
*/
protected function saveCategory(TransactionJournal $journal)
{
// add category: // add category:
if (!is_null($data['category'])) { if (!is_null($this->importData['category'])) {
$journal->categories()->save($data['category']); $journal->categories()->save($this->importData['category']);
} }
if (!is_null($data['tags'])) { }
foreach ($data['tags'] as $tag) {
/**
* @param TransactionJournal $journal
*/
protected function saveTags(TransactionJournal $journal)
{
if (!is_null($this->importData['tags'])) {
foreach ($this->importData['tags'] as $tag) {
$journal->tags()->save($tag); $journal->tags()->save($tag);
} }
} }
return $journal;
} }
/** /**