Expand import routine.

This commit is contained in:
James Cole
2017-06-20 21:04:25 +02:00
parent b9f110ac2b
commit a905cce2c9
16 changed files with 762 additions and 117 deletions

View File

@@ -16,6 +16,7 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
/**
@@ -36,6 +37,8 @@ class ImportAccount
private $accountName = [];
/** @var array */
private $accountNumber = [];
/** @var string */
private $expectedType = '';
/** @var AccountRepositoryInterface */
private $repository;
/** @var User */
@@ -46,77 +49,10 @@ class ImportAccount
*/
public function __construct()
{
$this->account = new Account;
$this->repository = app(AccountRepositoryInterface::class);
$this->expectedType = AccountType::ASSET;
$this->account = new Account;
$this->repository = app(AccountRepositoryInterface::class);
Log::debug('Created ImportAccount.');
}
/**
* @return bool
*/
public function convertToExpense(): bool
{
if ($this->getAccount()->accountType->type === AccountType::EXPENSE) {
return true;
}
// maybe that an account of expense account type already exists?
$expenseType = AccountType::whereType(AccountType::EXPENSE)->first();
$this->account->account_type_id = $expenseType->id;
$this->account->save();
return true;
}
public function convertToRevenue(): bool
{
if ($this->getAccount()->accountType->type === AccountType::REVENUE) {
return true;
}
// maybe that an account of revenue account type already exists?
$revenueType = AccountType::whereType(AccountType::REVENUE)->first();
$this->account->account_type_id = $revenueType->id;
$this->account->save();
return true;
}
/**
* @return Account
*/
public function createAccount(): Account
{
if (!is_null($this->account->id)) {
return $this->account;
}
Log::debug('In createAccount()');
// check if any of them is mapped:
$mapped = $this->findMappedObject();
if (is_null($mapped->id)) {
// none are, create new object!
$data = [
'accountType' => 'import',
'name' => $this->accountName['value'] ?? '(no name)',
'iban' => $this->accountIban['value'] ?? null,
'active' => true,
'virtualBalance' => null,
];
if (!is_null($data['iban']) && $data['name'] === '(no name)') {
$data['name'] = $data['iban'];
}
Log::debug('Search for maps resulted in nothing, create new one based on', $data);
$account = $this->repository->store($data);
$this->account = $account;
Log::info('Made new account.', ['input' => $data, 'new' => $account->toArray()]);
return $account;
}
Log::debug('Mapped existing account.', ['new' => $mapped->toArray()]);
$this->account = $mapped;
return $mapped;
}
/**
@@ -124,6 +60,10 @@ class ImportAccount
*/
public function getAccount(): Account
{
if (is_null($this->account->id)) {
$this->store();
}
return $this->account;
}
@@ -159,6 +99,14 @@ class ImportAccount
$this->accountNumber = $accountNumber;
}
/**
* @param string $expectedType
*/
public function setExpectedType(string $expectedType)
{
$this->expectedType = $expectedType;
}
/**
* @param User $user
*/
@@ -168,12 +116,88 @@ class ImportAccount
$this->repository->setUser($user);
}
/**
* @return Account
*/
private function findExistingObject(): Account
{
Log::debug('In findExistingObject() for Account');
// 0: determin account type:
/** @var AccountType $accountType */
$accountType = AccountType::whereType($this->expectedType)->first();
// 1: find by ID, iban or name (and type)
if (count($this->accountId) === 3) {
Log::debug(sprintf('Finding account of type %d and ID %d', $accountType->id, $this->accountId['value']));
/** @var Account $account */
$account = $this->user->accounts()->where('account_type_id', $accountType->id)->where('id', $this->accountId['value'])->first();
if (!is_null($account)) {
Log::debug(sprintf('Found unmapped %s account by ID (#%d): %s', $this->expectedType, $account->id, $account->name));
return $account;
}
Log::debug('Found nothing.');
}
/** @var Collection $accounts */
$accounts = $this->repository->getAccountsByType([$accountType->type]);
// 2: find by IBAN (and type):
if (count($this->accountIban) === 3) {
$iban = $this->accountIban['value'];
Log::debug(sprintf('Finding account of type %d and IBAN %s', $accountType->id, $iban));
$filtered = $accounts->filter(
function (Account $account) use ($iban) {
if ($account->iban === $iban) {
Log::debug(
sprintf('Found unmapped %s account by IBAN (#%d): %s (%s)', $this->expectedType, $account->id, $account->name, $account->iban)
);
return $account;
}
return null;
}
);
if ($filtered->count() === 1) {
return $filtered->first();
}
Log::debug('Found nothing.');
}
// 3: find by name (and type):
if (count($this->accountName) === 3) {
$name = $this->accountName['value'];
Log::debug(sprintf('Finding account of type %d and name %s', $accountType->id, $name));
$filtered = $accounts->filter(
function (Account $account) use ($name) {
if ($account->name === $name) {
Log::debug(sprintf('Found unmapped %s account by name (#%d): %s', $this->expectedType, $account->id, $account->name));
return $account;
}
return null;
}
);
if ($filtered->count() === 1) {
return $filtered->first();
}
Log::debug('Found nothing.');
}
// 4: do not search by account number.
Log::debug('Found NO existing accounts.');
return new Account;
}
/**
* @return Account
*/
private function findMappedObject(): Account
{
Log::debug('In findMappedObject()');
Log::debug('In findMappedObject() for Account');
$fields = ['accountId', 'accountIban', 'accountNumber', 'accountName'];
foreach ($fields as $field) {
$array = $this->$field;
@@ -199,7 +223,7 @@ class ImportAccount
*/
private function getMappedObject(array $array): Account
{
Log::debug('In getMappedObject()');
Log::debug('In getMappedObject() for Account');
if (count($array) === 0) {
Log::debug('Array is empty, nothing will come of this.');
@@ -212,7 +236,7 @@ class ImportAccount
return new Account;
}
Log::debug('Finding a mapped object based on', $array);
Log::debug('Finding a mapped account based on', $array);
$search = intval($array['mapped']);
$account = $this->repository->find($search);
@@ -222,5 +246,54 @@ class ImportAccount
return $account;
}
/**
* @return bool
*/
private function store(): bool
{
// 1: find mapped object:
$mapped = $this->findMappedObject();
if (!is_null($mapped->id)) {
$this->account = $mapped;
return true;
}
// 2: find existing by given values:
$found = $this->findExistingObject();
if (!is_null($found->id)) {
$this->account = $found;
return true;
}
// 3: if found nothing, retry the search with an asset account:
Log::debug('Will try to find an asset account just in case.');
$oldExpectedType = $this->expectedType;
$this->expectedType = AccountType::ASSET;
$found = $this->findExistingObject();
if (!is_null($found->id)) {
Log::debug('Found asset account!');
$this->account = $found;
return true;
}
$this->expectedType = $oldExpectedType;
Log::debug(sprintf('Found no account of type %s so must create one ourselves.', $this->expectedType));
$data = [
'accountType' => config('firefly.shortNamesByFullName.' . $this->expectedType),
'name' => $this->accountName['value'] ?? '(no name)',
'iban' => $this->accountIban['value'] ?? null,
'active' => true,
'virtualBalance' => null,
];
$this->account = $this->repository->store($data);
Log::debug(sprintf('Successfully stored new account #%d: %s', $this->account->id, $this->account->name));
return true;
}
}

View File

@@ -12,13 +12,52 @@ declare(strict_types=1);
namespace FireflyIII\Import\Object;
use FireflyIII\Models\Budget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
/**
* Class ImportBudget
*
* @package FireflyIII\Import\Object
*/
class ImportBudget
{
/** @var Budget */
private $budget;
/** @var array */
private $id = [];
/** @var array */
private $name = [];
/** @var BudgetRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* ImportBudget constructor.
*/
public function __construct()
{
$this->budget = new Budget;
$this->repository = app(BudgetRepositoryInterface::class);
Log::debug('Created ImportBudget.');
}
/**
* @return Budget
*/
public function getBudget(): Budget
{
if (is_null($this->budget->id)) {
$this->store();
}
return $this->budget;
}
/**
* @param array $id
@@ -36,5 +75,155 @@ class ImportBudget
$this->name = $name;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
/**
* @return Budget
*/
private function findExistingObject(): Budget
{
Log::debug('In findExistingObject() for Budget');
// 1: find by ID, or name
if (count($this->id) === 3) {
Log::debug(sprintf('Finding budget with ID #%d', $this->id['value']));
/** @var Budget $budget */
$budget = $this->repository->find(intval($this->id['value']));
if (!is_null($budget->id)) {
Log::debug(sprintf('Found unmapped budget by ID (#%d): %s', $budget->id, $budget->name));
return $budget;
}
Log::debug('Found nothing.');
}
// 2: find by name
if (count($this->name) === 3) {
/** @var Collection $budgets */
$budgets = $this->repository->getBudgets();
$name = $this->name['value'];
Log::debug(sprintf('Finding budget with name %s', $name));
$filtered = $budgets->filter(
function (Budget $budget) use ($name) {
if ($budget->name === $name) {
Log::debug(sprintf('Found unmapped budget by name (#%d): %s', $budget->id, $budget->name));
return $budget;
}
return null;
}
);
if ($filtered->count() === 1) {
return $filtered->first();
}
Log::debug('Found nothing.');
}
// 4: do not search by account number.
Log::debug('Found NO existing budgets.');
return new Budget;
}
/**
* @return Budget
*/
private function findMappedObject(): Budget
{
Log::debug('In findMappedObject() for Budget');
$fields = ['id', 'name'];
foreach ($fields as $field) {
$array = $this->$field;
Log::debug(sprintf('Find mapped budget based on field "%s" with value', $field), $array);
// check if a pre-mapped object exists.
$mapped = $this->getMappedObject($array);
if (!is_null($mapped->id)) {
Log::debug(sprintf('Found budget #%d!', $mapped->id));
return $mapped;
}
}
Log::debug('Found no budget on mapped data or no map present.');
return new Budget;
}
/**
* @param array $array
*
* @return Budget
*/
private function getMappedObject(array $array): Budget
{
Log::debug('In getMappedObject() for Budget');
if (count($array) === 0) {
Log::debug('Array is empty, nothing will come of this.');
return new Budget;
}
if (array_key_exists('mapped', $array) && is_null($array['mapped'])) {
Log::debug(sprintf('No map present for value "%s". Return NULL.', $array['value']));
return new Budget;
}
Log::debug('Finding a mapped budget based on', $array);
$search = intval($array['mapped']);
$account = $this->repository->find($search);
Log::debug(sprintf('Found budget! #%d ("%s"). Return it', $account->id, $account->name));
return $account;
}
/**
* @return bool
*/
private function store(): bool
{
// 1: find mapped object:
$mapped = $this->findMappedObject();
if (!is_null($mapped->id)) {
$this->budget = $mapped;
return true;
}
// 2: find existing by given values:
$found = $this->findExistingObject();
if (!is_null($found->id)) {
$this->budget = $found;
return true;
}
$name = $this->name['value'] ?? '';
if (strlen($name) === 0) {
return true;
}
Log::debug('Found no budget so must create one ourselves.');
$data = [
'name' => $name,
];
$this->budget = $this->repository->store($data);
Log::debug(sprintf('Successfully stored new budget #%d: %s', $this->budget->id, $this->budget->name));
return true;
}
}

View File

@@ -12,14 +12,24 @@ declare(strict_types=1);
namespace FireflyIII\Import\Object;
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
class ImportCategory
{
/** @var Category */
private $category;
/** @var array */
private $id = [];
/** @var array */
private $name = [];
/** @var CategoryRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* @param array $id
*/
@@ -28,6 +38,16 @@ class ImportCategory
$this->id = $id;
}
/**
* ImportCategory constructor.
*/
public function __construct()
{
$this->category = new Category();
$this->repository = app(CategoryRepositoryInterface::class);
Log::debug('Created ImportCategory.');
}
/**
* @param array $name
*/
@@ -37,4 +57,166 @@ class ImportCategory
}
/**
* @return Category
*/
public function getCategory(): Category
{
if (is_null($this->category->id)) {
$this->store();
}
return $this->category;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
/**
* @return Category
*/
private function findExistingObject(): Category
{
Log::debug('In findExistingObject() for Category');
// 1: find by ID, or name
if (count($this->id) === 3) {
Log::debug(sprintf('Finding category with ID #%d', $this->id['value']));
/** @var Category $category */
$category = $this->repository->find(intval($this->id['value']));
if (!is_null($category->id)) {
Log::debug(sprintf('Found unmapped category by ID (#%d): %s', $category->id, $category->name));
return $category;
}
Log::debug('Found nothing.');
}
// 2: find by name
if (count($this->name) === 3) {
/** @var Collection $categories */
$categories = $this->repository->getCategories();
$name = $this->name['value'];
Log::debug(sprintf('Finding category with name %s', $name));
$filtered = $categories->filter(
function (Category $category) use ($name) {
if ($category->name === $name) {
Log::debug(sprintf('Found unmapped category by name (#%d): %s', $category->id, $category->name));
return $category;
}
return null;
}
);
if ($filtered->count() === 1) {
return $filtered->first();
}
Log::debug('Found nothing.');
}
// 4: do not search by account number.
Log::debug('Found NO existing categories.');
return new Category;
}
/**
* @return Category
*/
private function findMappedObject(): Category
{
Log::debug('In findMappedObject() for Category');
$fields = ['id', 'name'];
foreach ($fields as $field) {
$array = $this->$field;
Log::debug(sprintf('Find mapped category based on field "%s" with value', $field), $array);
// check if a pre-mapped object exists.
$mapped = $this->getMappedObject($array);
if (!is_null($mapped->id)) {
Log::debug(sprintf('Found category #%d!', $mapped->id));
return $mapped;
}
}
Log::debug('Found no category on mapped data or no map present.');
return new Category;
}
/**
* @param array $array
*
* @return Category
*/
private function getMappedObject(array $array): Category
{
Log::debug('In getMappedObject() for Category');
if (count($array) === 0) {
Log::debug('Array is empty, nothing will come of this.');
return new Category;
}
if (array_key_exists('mapped', $array) && is_null($array['mapped'])) {
Log::debug(sprintf('No map present for value "%s". Return NULL.', $array['value']));
return new Category;
}
Log::debug('Finding a mapped category based on', $array);
$search = intval($array['mapped']);
$account = $this->repository->find($search);
Log::debug(sprintf('Found category! #%d ("%s"). Return it', $account->id, $account->name));
return $account;
}
/**
* @return bool
*/
private function store(): bool
{
// 1: find mapped object:
$mapped = $this->findMappedObject();
if (!is_null($mapped->id)) {
$this->category = $mapped;
return true;
}
// 2: find existing by given values:
$found = $this->findExistingObject();
if (!is_null($found->id)) {
$this->category = $found;
return true;
}
$name = $this->name['value'] ?? '';
if (strlen($name) === 0) {
return true;
}
Log::debug('Found no category so must create one ourselves.');
$data = [
'name' => $name,
];
$this->category = $this->repository->store($data);
Log::debug(sprintf('Successfully stored new category #%d: %s', $this->category->id, $this->category->name));
return true;
}
}

View File

@@ -46,7 +46,7 @@ class ImportCurrency
/**
* @return TransactionCurrency
*/
public function createCurrency(): TransactionCurrency
public function getTransactionCurrency(): TransactionCurrency
{
if (!is_null($this->currency->id)) {
return $this->currency;
@@ -77,6 +77,12 @@ class ImportCurrency
'name' => $this->name['value'] ?? null,
'decimal_places' => 2,
];
if (is_null($data['code'])) {
Log::info('Need at least a code to create currency, return nothing.');
return new TransactionCurrency();
}
Log::debug('Search for maps resulted in nothing, create new one based on', $data);
$currency = $this->repository->store($data);
$this->currency = $currency;

View File

@@ -14,11 +14,12 @@ namespace FireflyIII\Import\Object;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\AccountType;
use FireflyIII\Import\Converter\Amount;
use FireflyIII\Import\Converter\ConverterInterface;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Steam;
/**
* Class ImportJournal
@@ -27,35 +28,33 @@ use Illuminate\Support\Collection;
*/
class ImportJournal
{
/** @var Collection */
public $errors;
/** @var string */
private $amount = '0';
/** @var ImportAccount */
public $asset;
/** @var ImportBudget */
public $budget;
/** @var string */
public $description;
/** @var Collection */
public $errors;
/** @var string */
public $hash;
/** @var ImportAccount */
public $opposing;
/** @var string */
private $amount = '0';
/** @var ImportBill */
private $bill;
/** @var ImportBudget */
private $budget;
/** @var ImportCategory */
private $category;
public $category;
/** @var ImportCurrency */
private $currency;
/** @var string */
private $date = '';
/** @var string */
private $dateFormat = 'Ymd';
/** @var string */
private $description;
/** @var string */
private $externalId = '';
/** @var string */
private $hash;
/** @var array */
private $modifiers = [];
/** @var ImportAccount */
private $opposing;
private $tags = [];
private $tags = [];
/** @var string */
private $transactionType = '';
/** @var User */
@@ -72,6 +71,7 @@ class ImportJournal
$this->bill = new ImportBill;
$this->category = new ImportCategory;
$this->budget = new ImportBudget;
$this->currency = new ImportCurrency;
}
/**
@@ -91,15 +91,66 @@ class ImportJournal
exit('does not work yet');
}
/**
* @return string
*/
public function getAmount(): string
{
/** @var ConverterInterface $amountConverter */
$amountConverter = app(Amount::class);
$this->amount = strval($amountConverter->convert($this->amount));
// modify
foreach ($this->modifiers as $modifier) {
$class = sprintf('FireflyIII\Import\Converter\%s', config(sprintf('csv.import_roles.%s.converter', $modifier['role'])));
/** @var ConverterInterface $converter */
$converter = app($class);
if ($converter->convert($modifier['value']) === -1) {
$this->amount = Steam::negative($this->amount);
}
}
return $this->amount;
}
/**
* @return ImportCurrency
*/
public function getCurrency(): ImportCurrency
{
return $this->currency;
}
/**
* @param string $format
*
* @return Carbon
*/
public function getDate(string $format): Carbon
{
return Carbon::createFromFormat($format, $this->date);
}
/**
* @param string $hash
*/
public function setHash(string $hash)
{
$this->hash = $hash;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
// set user for related objects:
$this->asset->setUser($user);
$this->opposing->setUser($user);
$this->budget->setUser($user);
$this->category->setUser($user);
}
/**