Various code cleanup

This commit is contained in:
James Cole
2016-12-27 19:59:56 +01:00
parent c29180a094
commit 4804257fd1
5 changed files with 78 additions and 76 deletions

View File

@@ -57,8 +57,6 @@ class UpgradeDatabase extends Command
public function handle() public function handle()
{ {
$this->setTransactionIdentifier(); $this->setTransactionIdentifier();
} }
/** /**
@@ -85,42 +83,52 @@ class UpgradeDatabase extends Command
$journalIds = array_unique($result->pluck('id')->toArray()); $journalIds = array_unique($result->pluck('id')->toArray());
foreach ($journalIds as $journalId) { foreach ($journalIds as $journalId) {
// grab all positive transactiosn from this journal that are not deleted. $this->updateJournal($journalId);
// for each one, grab the negative opposing one which has 0 as an identifier and give it the same identifier. }
$identifier = 0; }
$processed = [];
$transactions = Transaction::where('transaction_journal_id', $journalId)->where('amount', '>', 0)->get();
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
// find opposing:
$amount = bcmul(strval($transaction->amount), '-1');
try { /**
/** @var Transaction $opposing */ * grab all positive transactiosn from this journal that are not deleted. for each one, grab the negative opposing one
$opposing = Transaction::where('transaction_journal_id', $journalId) * which has 0 as an identifier and give it the same identifier.
->where('amount', $amount)->where('identifier', '=', 0) *
->whereNotIn('id', $processed) * @param int $journalId
->first(); */
} catch (QueryException $e) { private function updateJournal(int $journalId)
Log::error($e->getMessage()); {
$this->error('Firefly III could not find the "identifier" field in the "transactions" table.'); $identifier = 0;
$this->error(sprintf('This field is required for Firefly III version %s to run.', config('firefly.version'))); $processed = [];
$this->error('Please run "php artisan migrate" to add this field to the table.'); $transactions = Transaction::where('transaction_journal_id', $journalId)->where('amount', '>', 0)->get();
$this->info('Then, run "php artisan firefly:upgrade-database" to try again.'); /** @var Transaction $transaction */
break 2; foreach ($transactions as $transaction) {
} // find opposing:
if (!is_null($opposing)) { $amount = bcmul(strval($transaction->amount), '-1');
// give both a new identifier:
$transaction->identifier = $identifier; try {
$transaction->save(); /** @var Transaction $opposing */
$opposing->identifier = $identifier; $opposing = Transaction::where('transaction_journal_id', $journalId)
$opposing->save(); ->where('amount', $amount)->where('identifier', '=', 0)
$processed[] = $transaction->id; ->whereNotIn('id', $processed)
$processed[] = $opposing->id; ->first();
$this->line(sprintf('Database upgrade for journal #%d, transactions #%d and #%d', $journalId, $transaction->id, $opposing->id)); } catch (QueryException $e) {
} Log::error($e->getMessage());
$identifier++; $this->error('Firefly III could not find the "identifier" field in the "transactions" table.');
$this->error(sprintf('This field is required for Firefly III version %s to run.', config('firefly.version')));
$this->error('Please run "php artisan migrate" to add this field to the table.');
$this->info('Then, run "php artisan firefly:upgrade-database" to try again.');
return;
} }
if (!is_null($opposing)) {
// give both a new identifier:
$transaction->identifier = $identifier;
$transaction->save();
$opposing->identifier = $identifier;
$opposing->save();
$processed[] = $transaction->id;
$processed[] = $opposing->id;
$this->line(sprintf('Database upgrade for journal #%d, transactions #%d and #%d', $journalId, $transaction->id, $opposing->id));
}
$identifier++;
} }
} }
} }

View File

@@ -76,15 +76,4 @@ class Kernel extends ConsoleKernel
{ {
require base_path('routes/console.php'); require base_path('routes/console.php');
} }
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
*
* @return void
*/
protected function schedule(Schedule $schedule)
{
}
} }

View File

@@ -72,6 +72,7 @@ class Handler extends ExceptionHandler
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
* *
* @param Exception $exception * @param Exception $exception
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly five.
* *
* @return void * @return void
*/ */
@@ -98,8 +99,8 @@ class Handler extends ExceptionHandler
]; ];
// create job that will mail. // create job that will mail.
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0'; $ipAddress = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
$job = new MailError($userData, env('SITE_OWNER', ''), $ip, $data); $job = new MailError($userData, env('SITE_OWNER', ''), $ipAddress, $data);
dispatch($job); dispatch($job);
} }

View File

@@ -287,7 +287,7 @@ class JournalExportCollector extends BasicCollector implements CollectorInterfac
} }
/** /**
* * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/ */
private function getWorkSet() private function getWorkSet()
{ {

View File

@@ -29,6 +29,7 @@ use Crypt;
* *
* *
* Class Entry * Class Entry
* @SuppressWarnings(PHPMD.LongVariable)
* *
* @package FireflyIII\Export\Entry * @package FireflyIII\Export\Entry
*/ */
@@ -71,33 +72,21 @@ final class Entry
*/ */
public static function fromObject($object): Entry public static function fromObject($object): Entry
{ {
$entry = new self; $entry = new self;
$entry->journal_id = $object->transaction_journal_id;
// journal information: $entry->description = self::decrypt($object->journal_encrypted, $object->journal_description);
$entry->journal_id = $object->transaction_journal_id; $entry->amount = round($object->amount, 2); // always positive
$entry->description = $object->journal_encrypted === 1 ? Crypt::decrypt($object->journal_description) : $object->journal_description; $entry->date = $object->date;
$entry->amount = round($object->amount, 2); // always positive $entry->transaction_type = $object->transaction_type;
$entry->date = $object->date; $entry->currency_code = $object->transaction_currency_code;
$entry->transaction_type = $object->transaction_type; $entry->source_account_id = $object->account_id;
$entry->currency_code = $object->transaction_currency_code; $entry->source_account_name = self::decrypt($object->account_name_encrypted, $object->account_name);
// source information:
$entry->source_account_id = $object->account_id;
$entry->source_account_name = $object->account_name_encrypted === 1 ? Crypt::decrypt($object->account_name) : $object->account_name;
// destination information
$entry->destination_account_id = $object->opposing_account_id; $entry->destination_account_id = $object->opposing_account_id;
$entry->destination_account_name = $object->opposing_account_encrypted === 1 ? Crypt::decrypt($object->opposing_account_name) $entry->destination_account_name = self::decrypt($object->opposing_account_encrypted, $object->opposing_account_name);
: $object->opposing_account_name; $entry->category_id = $object->category_id ?? '';
$entry->category_name = $object->category_name ?? '';
$entry->budget_id = $object->budget_id ?? '';
// category and budget $entry->budget_name = $object->budget_name ?? '';
$entry->category_id = $object->category_id ?? '';
$entry->category_name = $object->category_name ?? '';
$entry->budget_id = $object->budget_id ?? '';
$entry->budget_name = $object->budget_name ?? '';
// update description when transaction description is different: // update description when transaction description is different:
if (!is_null($object->description) && $object->description != $entry->description) { if (!is_null($object->description) && $object->description != $entry->description) {
@@ -107,4 +96,19 @@ final class Entry
return $entry; return $entry;
} }
/**
* @param int $isEncrypted
* @param $value
*
* @return string
*/
protected static function decrypt(int $isEncrypted, $value)
{
if ($isEncrypted === 1) {
return Crypt::decrypt($value);
}
return $value;
}
} }