New importers.

This commit is contained in:
James Cole 2016-07-16 07:58:25 +02:00
parent 5130ba7ea4
commit 697566fe42
17 changed files with 611 additions and 11 deletions

View File

@ -84,12 +84,20 @@ class AccountCrud implements AccountCrudInterface
/**
* @param string $iban
* @param array $types
*
* @return Account
*/
public function findByIban(string $iban): Account
public function findByIban(string $iban, array $types): Account
{
$accounts = $this->user->accounts()->where('iban', '!=', "")->get();
$query = $this->user->accounts()->where('iban', '!=', "");
if (count($types) > 0) {
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
$query->whereIn('account_types.type', $types);
}
$accounts = $query->get();
/** @var Account $account */
foreach ($accounts as $account) {
if ($account->iban === $iban) {
@ -100,6 +108,32 @@ class AccountCrud implements AccountCrudInterface
return new Account;
}
/**
* @param string $name
* @param array $types
*
* @return Account
*/
public function findByName(string $name, array $types): Account
{
$query = $this->user->accounts()->where('iban', '!=', "");
if (count($types) > 0) {
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
$query->whereIn('account_types.type', $types);
}
$accounts = $query->get();
/** @var Account $account */
foreach ($accounts as $account) {
if ($account->name === $name) {
return $account;
}
}
return new Account;
}
/**
* @param array $accountIds
*

View File

@ -39,10 +39,19 @@ interface AccountCrudInterface
/**
* @param string $iban
* @param array $types
*
* @return Account
*/
public function findByIban(string $iban): Account;
public function findByIban(string $iban, array $types): Account;
/**
* @param string $name
* @param array $types
*
* @return Account
*/
public function findByName(string $name, array $types): Account;
/**
* @param array $accountIds

View File

@ -0,0 +1,63 @@
<?php
/**
* Amount.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.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
/**
* Class RabobankDebetCredit
*
* @package FireflyIII\Import\Converter
*/
class Amount extends BasicConverter implements ConverterInterface
{
/**
* Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
* - Jamie Zawinski
*
* @param $value
*
* @return float
*/
public function convert($value): float
{
$len = strlen($value);
$decimalPosition = $len - 3;
$decimal = null;
if ($len > 2 && $value{$decimalPosition} == '.') {
$decimal = '.';
}
if ($len > 2 && $value{$decimalPosition} == ',') {
$decimal = ',';
}
// if decimal is dot, replace all comma's and spaces with nothing. then parse as float (round to 4 pos)
if ($decimal === '.') {
$search = [',', ' '];
$value = str_replace($search, '', $value);
}
if ($decimal === ',') {
$search = ['.', ' '];
$value = str_replace($search, '', $value);
$value = str_replace(',', '.', $value);
}
if (is_null($decimal)) {
// replace all:
$search = ['.', ' ', ','];
$value = str_replace($search, '', $value);
}
return round(floatval($value), 4);
}
}

View File

@ -13,6 +13,7 @@ namespace FireflyIII\Import\Converter;
use FireflyIII\Crud\Account\AccountCrudInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use Log;
/**
@ -30,7 +31,12 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface
*/
public function convert($value): Account
{
Log::debug('Going to convert value ' . $value);
$value = trim($value);
Log::debug('Going to convert ', ['value' => $value]);
if (strlen($value) === 0) {
return new Account;
}
/** @var AccountCrudInterface $repository */
$repository = app(AccountCrudInterface::class, [$this->user]);
@ -47,7 +53,7 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface
}
// not mapped? Still try to find it first:
$account = $repository->findByIban($value);
$account = $repository->findByIban($value, [AccountType::ASSET]);
if (!is_null($account->id)) {
Log::debug('Found account by IBAN', ['id' => $account->id]);

View File

@ -0,0 +1,34 @@
<?php
/**
* AssetAccountId.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.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Exceptions\FireflyException;
/**
* Class AssetAccountId
*
* @package FireflyIII\Import\Converter
*/
class AssetAccountId extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @throws FireflyException
*/
public function convert($value)
{
throw new FireflyException('Importer with name AssetAccountId has not yet been configured.');
}
}

View File

@ -0,0 +1,34 @@
<?php
/**
* AssetAccountName.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.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Exceptions\FireflyException;
/**
* Class AssetAccountName
*
* @package FireflyIII\Import\Converter
*/
class AssetAccountName extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @throws FireflyException
*/
public function convert($value)
{
throw new FireflyException('Importer with name AssetAccountId has not yet been configured.');
}
}

View File

@ -21,15 +21,23 @@ use FireflyIII\User;
*/
class BasicConverter
{
/** @var array */
public $config;
/** @var bool */
public $doMap;
/** @var array */
public $mapping = [];
/** @var User */
public $user;
/**
* @param array $config
*/
public function setConfig(array $config)
{
$this->config = $config;
}
/**
* @param mixed $doMap
*/

View File

@ -10,6 +10,7 @@
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\User;
/**
@ -25,6 +26,11 @@ interface ConverterInterface
*/
public function convert($value);
/**
* @param array $config
*/
public function setConfig(array $config);
/**
* @param bool $doMap
*/

View File

@ -0,0 +1,65 @@
<?php
/**
* CurrencyCode.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.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Log;
/**
* Class CurrencyCode
*
* @package FireflyIII\Import\Converter
*/
class CurrencyCode extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @return TransactionCurrency
*/
public function convert($value): TransactionCurrency
{
Log::debug('Going to convert ', ['value' => $value]);
/** @var CurrencyRepositoryInterface $repository */
$repository = app(CurrencyRepositoryInterface::class);
if (isset($this->mapping[$value])) {
Log::debug('Found currency in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]);
$currency = $repository->find(intval($this->mapping[$value]));
if (!is_null($currency->id)) {
Log::debug('Found currency by ID', ['id' => $currency->id]);
return $currency;
}
}
// not mapped? Still try to find it first:
$currency = $repository->findByCode($value);
if (!is_null($currency->id)) {
Log::debug('Found currency by code', ['id' => $currency->id]);
return $currency;
}
$currency = $repository->store(
[
'name' => $value,
'code' => $value,
'symbol' => $value,
]
);
return $currency;
}
}

View File

@ -0,0 +1,48 @@
<?php
/**
* Date.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.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use InvalidArgumentException;
use Log;
/**
* Class Date
*
* @package FireflyIII\Import\Converter
*/
class Date extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @return Carbon
* @throws FireflyException
*/
public function convert($value): Carbon
{
Log::debug('Going to convert ', ['value' => $value]);
Log::debug('Format: ', ['format' => $this->config['date-format']]);
try {
$date = Carbon::createFromFormat($this->config['date-format'], $value);
} catch (InvalidArgumentException $e) {
Log::critical($e->getMessage());
Log::critical('Cannot convert this string using the given format.', ['value' => $value, 'format' => $this->config['date-format']]);
throw new FireflyException(sprintf('Cannot convert "%s" to a valid date using format "%s".', $value, $this->config['date-format']));
}
Log::debug('Converted date', ['converted' => $date->toAtomString()]);
return $date;
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* Description.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.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
/**
* Class Description
*
* @package FireflyIII\Import\Converter
*/
class Description extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @return string
*/
public function convert($value): string
{
// this should replace all control characters
// but leave utf8 intact:
$value = preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', $value);
return strval($value);
}
}

View File

@ -0,0 +1,32 @@
<?php
/**
* Ignore.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.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
/**
* Class Ignore
*
* @package FireflyIII\Import\Converter
*/
class Ignore extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @return null
*/
public function convert($value)
{
return null;
}
}

View File

@ -0,0 +1,106 @@
<?php
/**
* OpposingAccountIban.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.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Crud\Account\AccountCrudInterface;
use FireflyIII\Models\Account;
use Log;
/**
* Class OpposingAccountIban
*
* @package FireflyIII\Import\Converter
*/
class OpposingAccountIban extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @return Account
*/
public function convert($value): Account
{
$value = trim($value);
Log::debug('Going to convert ', ['value' => $value]);
if (strlen($value) === 0) {
return new Account;
}
/** @var AccountCrudInterface $repository */
$repository = app(AccountCrudInterface::class, [$this->user]);
if (isset($this->mapping[$value])) {
Log::debug('Found account in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]);
$account = $repository->find(intval($this->mapping[$value]));
if (!is_null($account->id)) {
Log::debug('Found account by ID', ['id' => $account->id]);
return $account;
}
}
// not mapped? Still try to find it first:
$account = $repository->findByIban($value, []);
if (!is_null($account->id)) {
Log::debug('Found account by IBAN', ['id' => $account->id]);
Log::warning(
'The match between IBAN and account is uncertain because the type of transactions may not have been determined.',
['id' => $account->id, 'iban' => $value]
);
return $account;
}
$account = $repository->store(
['name' => $value, 'iban' => $value, 'user' => $this->user->id, 'accountType' => 'import', 'virtualBalance' => 0, 'active' => true,
'openingBalance' => 0<?php
/**
* AssetAccountId.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.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Exceptions\FireflyException;
/**
* Class AssetAccountId
*
* @package FireflyIII\Import\Converter
*/
class AssetAccountId extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @throws FireflyException
*/
public function convert($value)
{
throw new FireflyException('Importer with name AssetAccountId has not yet been configured.');
}
}]
);
return $account;
}
}

View File

@ -0,0 +1,74 @@
<?php
/**
* OpposingAccountName.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.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Crud\Account\AccountCrudInterface;
use FireflyIII\Models\Account;
use Log;
/**
* Class OpposingAccountName
*
* @package FireflyIII\Import\Converter
*/
class OpposingAccountName extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @return Account
*/
public function convert($value): Account
{
$value = trim($value);
Log::debug('Going to convert ', ['value' => $value]);
if (strlen($value) === 0) {
$value = '(empty account name)';
}
/** @var AccountCrudInterface $repository */
$repository = app(AccountCrudInterface::class, [$this->user]);
if (isset($this->mapping[$value])) {
Log::debug('Found account in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]);
$account = $repository->find(intval($this->mapping[$value]));
if (!is_null($account->id)) {
Log::debug('Found account by ID', ['id' => $account->id]);
return $account;
}
}
// not mapped? Still try to find it first:
$account = $repository->findByName($value, []);
if (!is_null($account->id)) {
Log::debug('Found account by name', ['id' => $account->id]);
Log::warning(
'The match between name and account is uncertain because the type of transactions may not have been determined.',
['id' => $account->id, 'name' => $value]
);
return $account;
}
$account = $repository->store(
['name' => $value, 'iban' => null, 'user' => $this->user->id, 'accountType' => 'import', 'virtualBalance' => 0, 'active' => true,
'openingBalance' => 0,
]
);
return $account;
}
}

View File

@ -0,0 +1,39 @@
<?php
/**
* RabobankDebetCredit.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.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use Log;
/**
* Class RabobankDebetCredit
*
* @package FireflyIII\Import\Converter
*/
class RabobankDebetCredit extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @return int
*/
public function convert($value): int
{
Log::debug('Going to convert ', ['value' => $value]);
if ($value === 'D') {
return -1;
}
return 1;
}
}

View File

@ -438,6 +438,7 @@ class CsvImporter implements ImporterInterface
$converter->setMapping($mapping);
$converter->setDoMap($doMap);
$converter->setUser($this->job->user);
$converter->setConfig($config);
// run the converter for this value:
$convertedValue = $converter->convert($value);

View File

@ -60,12 +60,15 @@ return [
'Beneficiary account' => 'fa-shopping-cart',
'revenue' => 'fa-download',
'Revenue account' => 'fa-download',
'import' => 'fa-download',
'Import account' => 'fa-download',
],
'accountTypesByIdentifier' =>
[
'asset' => ['Default account', 'Asset account'],
'expense' => ['Expense account', 'Beneficiary account'],
'revenue' => ['Revenue account'],
'import' => ['Import account'],
],
'accountTypeByIdentifier' =>
[
@ -74,11 +77,13 @@ return [
'revenue' => 'Revenue account',
'opening' => 'Initial balance account',
'initial' => 'Initial balance account',
'import' => 'Import account',
],
'shortNamesByFullName' =>
[
'Default account' => 'asset',
'Asset account' => 'asset',
'Import account' => 'import',
'Expense account' => 'expense',
'Beneficiary account' => 'expense',
'Revenue account' => 'revenue',
@ -136,7 +141,7 @@ return [
'end_date' => 'FireflyIII\Support\Binder\Date',
],
'rule-triggers' => [
'rule-triggers' => [
'user_action' => 'FireflyIII\Rules\Triggers\UserAction',
'from_account_starts' => 'FireflyIII\Rules\Triggers\FromAccountStarts',
'from_account_ends' => 'FireflyIII\Rules\Triggers\FromAccountEnds',
@ -155,7 +160,7 @@ return [
'description_contains' => 'FireflyIII\Rules\Triggers\DescriptionContains',
'description_is' => 'FireflyIII\Rules\Triggers\DescriptionIs',
],
'rule-actions' => [
'rule-actions' => [
'set_category' => 'FireflyIII\Rules\Actions\SetCategory',
'clear_category' => 'FireflyIII\Rules\Actions\ClearCategory',
'set_budget' => 'FireflyIII\Rules\Actions\SetBudget',
@ -168,7 +173,7 @@ return [
'prepend_description' => 'FireflyIII\Rules\Actions\PrependDescription',
],
// all rule actions that require text input:
'rule-actions-text' => [
'rule-actions-text' => [
'set_category',
'set_budget',
'add_tag',
@ -177,7 +182,7 @@ return [
'append_description',
'prepend_description',
],
'test-triggers' => [
'test-triggers' => [
// The maximum number of transactions shown when testing a list of triggers
'limit' => 10,