Code cleanup in export routine

This commit is contained in:
James Cole 2016-04-26 20:49:22 +02:00
parent edf9dbc6e8
commit 1d2a4e707e
7 changed files with 213 additions and 400 deletions

View File

@ -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.

View File

@ -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
@ -81,45 +58,21 @@ class Entry
{
$entry = new self;
$entry->setDescription($journal->description);
$entry->setDate($journal->date->format('Y-m-d'));
$entry->setAmount(TransactionJournal::amount($journal));
$entry->description = $journal->description;
$entry->date = $journal->date->format('Y-m-d');
$entry->amount = TransactionJournal::amount($journal);
/** @var Budget $budget */
$budget = $journal->budgets->first();
if (!is_null($budget)) {
$entry->setBudgetId($budget->id);
$entry->setBudgetName($budget->name);
}
/** @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'));
$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'));
$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 [
'description' => 'description',
'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',
'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;
}
}

View File

@ -0,0 +1,45 @@
<?php
/**
* EntryAccount.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* 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');
}
}

View File

@ -0,0 +1,39 @@
<?php
/**
* EntryBill.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* 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;
}
}
}

View File

@ -0,0 +1,39 @@
<?php
/**
* EntryBudget.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* 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;
}
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
* EntryCategory.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* 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;
}
}
}

View File

@ -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);