Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Code cleanup in export routine
James Cole1d2a4e707efa
diff --git a/app/Export/ConfigurationFile.php b/app/Export/ConfigurationFile.phpindex 46a22c6e8d..e934f48736 100644--- a/app/Export/ConfigurationFile.php+++ b/app/Export/ConfigurationFile.php@@ -43,8 +43,8 @@ class ConfigurationFile*/public function make(): string{- $fields = array_keys(get_class_vars(Entry::class));- $types = Entry::getTypes();+ $fields = array_keys(Entry::getFieldsAndTypes());+ $types = Entry::getFieldsAndTypes();$configuration = ['date-format' => 'Y-m-d', // unfortunately, this is hard-coded.diff --git a/app/Export/Entry/Entry.php b/app/Export/Entry/Entry.phpindex d8e1b2203b..a7387f13db 100644--- a/app/Export/Entry/Entry.php+++ b/app/Export/Entry/Entry.php@@ -11,15 +11,13 @@ declare(strict_types = 1);namespace FireflyIII\Export\Entry;use FireflyIII\Models\Account;-use FireflyIII\Models\Budget;-use FireflyIII\Models\Category;use FireflyIII\Models\TransactionJournal;/*** To extend the exported object, in case of new features in Firefly III for example,* do the following:*- * - Add the field(s) to this class+ * - Add the field(s) to this class. If you add more than one related field, add a new object.* - Make sure the "fromJournal"-routine fills these fields.* - Add them to the static function that returns its type (key=value. Remember that the only* valid types can be found in config/csv.php (under "roles").@@ -36,41 +34,20 @@ class Entry{/** @var string */public $amount;- /** @var int */- public $billId;- /** @var string */- public $billName;- /** @var int */- public $budgetId;- /** @var string */- public $budgetName;- /** @var int */- public $categoryId;- /** @var string */- public $categoryName;+ /** @var EntryBill */+ public $bill;+ /** @var EntryBudget */+ public $budget;+ /** @var EntryCategory */+ public $category;/** @var string */public $date;/** @var string */public $description;- /** @var string */- public $fromAccountIban;- /** @var int */- public $fromAccountId;- /** @var string */- public $fromAccountName;- /** @var int */- public $fromAccountNumber;- /** @var string */- public $fromAccountType;- /** @var string */- public $toAccountIban;- /** @var int */- public $toAccountId;- /** @var string */- public $toAccountName;- public $toAccountNumber;- /** @var string */- public $toAccountType;+ /** @var EntryAccount */+ public $destinationAccount;+ /** @var EntryAccount */+ public $sourceAccount;/*** @param TransactionJournal $journal@@ -80,46 +57,22 @@ class Entrypublic static function fromJournal(TransactionJournal $journal){- $entry = new self;- $entry->setDescription($journal->description);- $entry->setDate($journal->date->format('Y-m-d'));- $entry->setAmount(TransactionJournal::amount($journal));-- /** @var Budget $budget */- $budget = $journal->budgets->first();- if (!is_null($budget)) {- $entry->setBudgetId($budget->id);- $entry->setBudgetName($budget->name);- }+ $entry = new self;+ $entry->description = $journal->description;+ $entry->date = $journal->date->format('Y-m-d');+ $entry->amount = TransactionJournal::amount($journal);- /** @var Category $category */- $category = $journal->categories->first();- if (!is_null($category)) {- $entry->setCategoryId($category->id);- $entry->setCategoryName($category->name);- }-- if (!is_null($journal->bill_id)) {- $entry->setBillId($journal->bill_id);- $entry->setBillName($journal->bill->name);- }+ $entry->budget = new EntryBudget($journal->budgets->first());+ $entry->category = new EntryCategory($journal->categories->first());+ $entry->bill = new EntryBill($journal->bill);/** @var Account $sourceAccount */- $sourceAccount = TransactionJournal::sourceAccount($journal);- $entry->setFromAccountId($sourceAccount->id);- $entry->setFromAccountName($sourceAccount->name);- $entry->setFromAccountIban($sourceAccount->iban);- $entry->setFromAccountType($sourceAccount->accountType->type);- $entry->setFromAccountNumber($sourceAccount->getMeta('accountNumber'));-+ $sourceAccount = TransactionJournal::sourceAccount($journal);+ $entry->sourceAccount = new EntryAccount($sourceAccount);/** @var Account $destination */- $destination = TransactionJournal::destinationAccount($journal);- $entry->setToAccountId($destination->id);- $entry->setToAccountName($destination->name);- $entry->setToAccountIban($destination->iban);- $entry->setToAccountType($destination->accountType->type);- $entry->setToAccountNumber($destination->getMeta('accountNumber'));+ $destination = TransactionJournal::destinationAccount($journal);+ $entry->destinationAccount = new EntryAccount($destination);return $entry;@@ -128,336 +81,31 @@ class Entry/*** @return array*/- public static function getTypes(): array+ public static function getFieldsAndTypes(): array{// key = field name (see top of class)// value = field type (see csv.php under 'roles')return [- 'amount' => 'amount',- 'date' => 'date-transaction',- 'description' => 'description',- 'billId' => 'bill-id',- 'billName' => 'bill-name',- 'budgetId' => 'budget-id',- 'budgetName' => 'budget-name',- 'categoryId' => 'category-id',- 'categoryName' => 'category-name',- 'fromAccountId' => 'account-id',- 'fromAccountNumber' => 'account-number',- 'fromAccountName' => 'account-name',- 'fromAccountIban' => 'account-iban',- 'fromAccountType' => '_ignore', // no, Firefly cannot import what it exports. I know :D- 'toAccountId' => 'opposing-id',- 'toAccountNumber' => 'account-number',- 'toAccountName' => 'opposing-name',- 'toAccountIban' => 'opposing-iban',- 'toAccountType' => '_ignore',+ 'description' => 'description',+ 'amount' => 'amount',+ 'date' => 'date-transaction',+ 'source_account_id' => 'account-id',+ 'source_account_name' => 'account-name',+ 'source_account_iban' => 'account-iban',+ 'source_account_type' => '_ignore',+ 'source_account_number' => 'account-number',+ 'destination_account_id' => 'opposing-id',+ 'destination_account_name' => 'opposing-name',+ 'destination_account_iban' => 'opposing-iban',+ 'destination_account_type' => '_ignore',+ 'destination_account_number' => 'account-number',+ 'budget_id' => 'budget-id',+ 'budget_name' => 'budget-name',+ 'category_id' => 'category-id',+ 'category_name' => 'category-name',+ 'bill_id' => 'bill-id',+ 'bill_name' => 'bill-name',];}- /**- * @return string- */- public function getAmount(): string- {- return $this->amount;- }-- /**- * @param string $amount- */- public function setAmount(string $amount)- {- $this->amount = $amount;- }-- /**- * @return int- */- public function getBillId(): int- {- return $this->billId;- }-- /**- * @param int $billId- */- public function setBillId(int $billId)- {- $this->billId = $billId;- }-- /**- * @return string- */- public function getBillName(): string- {- return $this->billName;- }-- /**- * @param string $billName- */- public function setBillName(string $billName)- {- $this->billName = $billName;- }-- /**- * @return int- */- public function getBudgetId(): int- {- return $this->budgetId;- }-- /**- * @param int $budgetId- */- public function setBudgetId(int $budgetId)- {- $this->budgetId = $budgetId;- }-- /**- * @return string- */- public function getBudgetName(): string- {- return $this->budgetName;- }-- /**- * @param string $budgetName- */- public function setBudgetName(string $budgetName)- {- $this->budgetName = $budgetName;- }-- /**- * @return int- */- public function getCategoryId(): int- {- return $this->categoryId;- }-- /**- * @param int $categoryId- */- public function setCategoryId(int $categoryId)- {- $this->categoryId = $categoryId;- }-- /**- * @return string- */- public function getCategoryName(): string- {- return $this->categoryName;- }-- /**- * @param string $categoryName- */- public function setCategoryName(string $categoryName)- {- $this->categoryName = $categoryName;- }-- /**- * @return string- */- public function getDate(): string- {- return $this->date;- }-- /**- * @param string $date- */- public function setDate(string $date)- {- $this->date = $date;- }-- /**- * @return string- */- public function getDescription(): string- {- return $this->description;- }-- /**- * @param string $description- */- public function setDescription(string $description)- {- $this->description = $description;- }-- /**- * @return string- */- public function getFromAccountIban(): string- {- return $this->fromAccountIban;- }-- /**- * @param string $fromAccountIban- */- public function setFromAccountIban(string $fromAccountIban)- {- $this->fromAccountIban = $fromAccountIban;- }-- /**- * @return int- */- public function getFromAccountId():int- {- return $this->fromAccountId;- }-- /**- * @param int $fromAccountId- */- public function setFromAccountId(int $fromAccountId)- {- $this->fromAccountId = $fromAccountId;- }-- /**- * @return string- */- public function getFromAccountName(): string- {- return $this->fromAccountName;- }-- /**- * @param string $fromAccountName- */- public function setFromAccountName(string $fromAccountName)- {- $this->fromAccountName = $fromAccountName;- }-- /**- * @return string- */- public function getFromAccountNumber(): string- {- return $this->fromAccountNumber;- }-- /**- * @param string $fromAccountNumber- */- public function setFromAccountNumber(string $fromAccountNumber)- {- $this->fromAccountNumber = $fromAccountNumber;- }-- /**- * @return string- */- public function getFromAccountType(): string- {- return $this->fromAccountType;- }-- /**- * @param string $fromAccountType- */- public function setFromAccountType(string $fromAccountType)- {- $this->fromAccountType = $fromAccountType;- }-- /**- * @return string- */- public function getToAccountIban(): string- {- return $this->toAccountIban;- }-- /**- * @param string $toAccountIban- */- public function setToAccountIban(string $toAccountIban)- {- $this->toAccountIban = $toAccountIban;- }-- /**- * @return int- */- public function getToAccountId(): int- {- return $this->toAccountId;- }-- /**- * @param int $toAccountId- */- public function setToAccountId(int $toAccountId)- {- $this->toAccountId = $toAccountId;- }-- /**- * @return string- */- public function getToAccountName(): string- {- return $this->toAccountName;- }-- /**- * @param string $toAccountName- */- public function setToAccountName(string $toAccountName)- {- $this->toAccountName = $toAccountName;- }-- /**- * @return string- */- public function getToAccountNumber(): string- {- return $this->toAccountNumber;- }-- /**- * @param string $toAccountNumber- */- public function setToAccountNumber(string $toAccountNumber)- {- $this->toAccountNumber = $toAccountNumber;- }-- /**- * @return string- */- public function getToAccountType(): string- {- return $this->toAccountType;- }-- /**- * @param string $toAccountType- */- public function setToAccountType(string $toAccountType)- {- $this->toAccountType = $toAccountType;- }--}diff --git a/app/Export/Entry/EntryAccount.php b/app/Export/Entry/EntryAccount.phpnew file mode 100644index 0000000000..f12bc6fd43--- /dev/null+++ b/app/Export/Entry/EntryAccount.php@@ -0,0 +1,45 @@+<?php+/**+ * EntryAccount.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++namespace FireflyIII\Export\Entry;++use FireflyIII\Models\Account;++/**+ * Class EntryAccount+ *+ * @package FireflyIII\Export\Entry+ */+class EntryAccount+{+ /** @var string */+ public $iban;+ /** @var int */+ public $id;+ /** @var string */+ public $name;+ /** @var int */+ public $number;+ /** @var string */+ public $type;++ /**+ * EntryAccount constructor.+ *+ * @param Account $account+ */+ public function __construct(Account $account)+ {+ $this->id = $account->id;+ $this->name = $account->name;+ $this->iban = $account->iban;+ $this->type = $account->accountType->type;+ $this->number = $account->getMeta('accountNumber');+ }+}\ No newline at end of filediff --git a/app/Export/Entry/EntryBill.php b/app/Export/Entry/EntryBill.phpnew file mode 100644index 0000000000..2612ec081b--- /dev/null+++ b/app/Export/Entry/EntryBill.php@@ -0,0 +1,39 @@+<?php+/**+ * EntryBill.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++namespace FireflyIII\Export\Entry;++use FireflyIII\Models\Bill;++/**+ * Class EntryBill+ *+ * @package FireflyIII\Export\Entry+ */+class EntryBill+{+ /** @var string */+ public $id = '';+ /** @var string */+ public $name = '';++ /**+ * EntryBill constructor.+ *+ * @param Bill $bill+ */+ public function __construct(Bill $bill = null)+ {+ if (!is_null($bill)) {+ $this->id = $bill->id;+ $this->name = $bill->name;+ }+ }++}\ No newline at end of filediff --git a/app/Export/Entry/EntryBudget.php b/app/Export/Entry/EntryBudget.phpnew file mode 100644index 0000000000..7558537431--- /dev/null+++ b/app/Export/Entry/EntryBudget.php@@ -0,0 +1,39 @@+<?php+/**+ * EntryBudget.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++namespace FireflyIII\Export\Entry;++use FireflyIII\Models\Budget;++/**+ * Class EntryBudget+ *+ * @package FireflyIII\Export\Entry+ */+class EntryBudget+{+ /** @var string */+ public $id = '';+ /** @var string */+ public $name = '';++ /**+ * EntryBudget constructor.+ *+ * @param Budget $budget+ */+ public function __construct(Budget $budget = null)+ {+ if (!is_null($budget)) {+ $this->id = $budget->id;+ $this->name = $budget->name;+ }+ }++}\ No newline at end of filediff --git a/app/Export/Entry/EntryCategory.php b/app/Export/Entry/EntryCategory.phpnew file mode 100644index 0000000000..dae5791af6--- /dev/null+++ b/app/Export/Entry/EntryCategory.php@@ -0,0 +1,38 @@+<?php+/**+ * EntryCategory.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++namespace FireflyIII\Export\Entry;++use FireflyIII\Models\Category;++/**+ * Class EntryCategory+ *+ * @package FireflyIII\Export\Entry+ */+class EntryCategory+{+ /** @var string */+ public $id = '';+ /** @var string */+ public $name = '';++ /**+ * EntryCategory constructor.+ *+ * @param Category $category+ */+ public function __construct(Category $category = null)+ {+ if (!is_null($category)) {+ $this->id = $category->id;+ $this->name = $category->name;+ }+ }+}\ No newline at end of filediff --git a/app/Export/Exporter/CsvExporter.php b/app/Export/Exporter/CsvExporter.phpindex 1919a4d447..df7d0d5b44 100644--- a/app/Export/Exporter/CsvExporter.php+++ b/app/Export/Exporter/CsvExporter.php@@ -62,14 +62,18 @@ class CsvExporter extends BasicExporter implements ExporterInterface$rows = [];// add header:- /** @var Entry $first */- $first = $this->getEntries()->first();- $rows[] = array_keys(get_object_vars($first));+ $rows[] = array_keys(Entry::getFieldsAndTypes());// then the rest:/** @var Entry $entry */foreach ($this->getEntries() as $entry) {- $rows[] = array_values(get_object_vars($entry));+ // order is defined in Entry::getFieldsAndTypes.+ $rows[] = [+ $entry->description, $entry->amount, $entry->date, $entry->sourceAccount->id, $entry->sourceAccount->name, $entry->sourceAccount->iban,+ $entry->sourceAccount->type, $entry->sourceAccount->number, $entry->destinationAccount->id, $entry->destinationAccount->name,+ $entry->destinationAccount->iban, $entry->destinationAccount->type, $entry->destinationAccount->number, $entry->budget->id,+ $entry->budget->name, $entry->category->id, $entry->category->name, $entry->bill->id, $entry->bill->name,+ ];}$writer->insertAll($rows);