mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-06 06:01:21 +00:00
Refactor and rename some import things.
This commit is contained in:
@@ -12,43 +12,119 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Import\Object;
|
||||
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class ImportAccount
|
||||
*
|
||||
* @package FireflyIII\Import\Object
|
||||
*/
|
||||
class ImportAccount
|
||||
{
|
||||
|
||||
/** @var array */
|
||||
private $accountId = [];
|
||||
|
||||
/** @var Account */
|
||||
private $account;
|
||||
/** @var array */
|
||||
private $accountIban = [];
|
||||
/** @var array */
|
||||
private $accountId = [];
|
||||
/** @var array */
|
||||
private $accountName = [];
|
||||
|
||||
/** @var array */
|
||||
private $accountNumber = [];
|
||||
/** @var AccountRepositoryInterface */
|
||||
private $repository;
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* @param array $accountNumber
|
||||
* ImportAccount constructor.
|
||||
*/
|
||||
public function setAccountNumber(array $accountNumber)
|
||||
public function __construct()
|
||||
{
|
||||
$this->accountNumber = $accountNumber;
|
||||
$this->account = new Account;
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
Log::debug('Created ImportAccount.');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $accountName
|
||||
* @return bool
|
||||
*/
|
||||
public function setAccountName(array $accountName)
|
||||
public function convertToExpense(): bool
|
||||
{
|
||||
$this->accountName = $accountName;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $value
|
||||
* @return Account
|
||||
*/
|
||||
public function setAccountId(array $value)
|
||||
public function createAccount(): Account
|
||||
{
|
||||
$this->accountId = $value;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
public function getAccount(): Account
|
||||
{
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,5 +135,92 @@ class ImportAccount
|
||||
$this->accountIban = $accountIban;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $value
|
||||
*/
|
||||
public function setAccountId(array $value)
|
||||
{
|
||||
$this->accountId = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $accountName
|
||||
*/
|
||||
public function setAccountName(array $accountName)
|
||||
{
|
||||
$this->accountName = $accountName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $accountNumber
|
||||
*/
|
||||
public function setAccountNumber(array $accountNumber)
|
||||
{
|
||||
$this->accountNumber = $accountNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->repository->setUser($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
private function findMappedObject(): Account
|
||||
{
|
||||
Log::debug('In findMappedObject()');
|
||||
$fields = ['accountId', 'accountIban', 'accountNumber', 'accountName'];
|
||||
foreach ($fields as $field) {
|
||||
$array = $this->$field;
|
||||
Log::debug(sprintf('Find mapped account 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 account #%d!', $mapped->id));
|
||||
|
||||
return $mapped;
|
||||
}
|
||||
|
||||
}
|
||||
Log::debug('Found no account on mapped data or no map present.');
|
||||
|
||||
return new Account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
*
|
||||
* @return Account
|
||||
*/
|
||||
private function getMappedObject(array $array): Account
|
||||
{
|
||||
Log::debug('In getMappedObject()');
|
||||
if (count($array) === 0) {
|
||||
Log::debug('Array is empty, nothing will come of this.');
|
||||
|
||||
return new Account;
|
||||
}
|
||||
|
||||
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 Account;
|
||||
}
|
||||
|
||||
Log::debug('Finding a mapped object based on', $array);
|
||||
|
||||
$search = intval($array['mapped']);
|
||||
$account = $this->repository->find($search);
|
||||
|
||||
Log::debug(sprintf('Found account! #%d ("%s"). Return it', $account->id, $account->name));
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -12,12 +12,80 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Import\Object;
|
||||
|
||||
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Log;
|
||||
|
||||
class ImportCurrency
|
||||
{
|
||||
private $code = [];
|
||||
/** @var array */
|
||||
private $code = [];
|
||||
/** @var TransactionCurrency */
|
||||
private $currency;
|
||||
/** @var array */
|
||||
private $id = [];
|
||||
/** @var array */
|
||||
private $name = [];
|
||||
/** @var CurrencyRepositoryInterface */
|
||||
private $repository;
|
||||
/** @var array */
|
||||
private $symbol = [];
|
||||
private $name = [];
|
||||
private $id = [];
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* ImportCurrency constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->currency = new TransactionCurrency;
|
||||
$this->repository = app(CurrencyRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
public function createCurrency(): TransactionCurrency
|
||||
{
|
||||
if (!is_null($this->currency->id)) {
|
||||
return $this->currency;
|
||||
}
|
||||
Log::debug('In createCurrency()');
|
||||
// check if any of them is mapped:
|
||||
$mapped = $this->findMappedObject();
|
||||
$searched = null;
|
||||
|
||||
if (!is_null($mapped->id)) {
|
||||
|
||||
Log::debug('Mapped existing currency.', ['new' => $mapped->toArray()]);
|
||||
$this->currency = $mapped;
|
||||
|
||||
return $mapped;
|
||||
}
|
||||
|
||||
$searched = $this->findExistingObject();
|
||||
if (!is_null($searched->id)) {
|
||||
Log::debug('Found existing currency.', ['found' => $searched->toArray()]);
|
||||
$this->currency = $searched;
|
||||
|
||||
return $searched;
|
||||
}
|
||||
$data = [
|
||||
'code' => $this->code['value'] ?? null,
|
||||
'symbol' => $this->symbol['value'] ?? null,
|
||||
'name' => $this->name['value'] ?? null,
|
||||
'decimal_places' => 2,
|
||||
];
|
||||
Log::debug('Search for maps resulted in nothing, create new one based on', $data);
|
||||
$currency = $this->repository->store($data);
|
||||
$this->currency = $currency;
|
||||
Log::info('Made new currency.', ['input' => $data, 'new' => $currency->toArray()]);
|
||||
|
||||
|
||||
return $currency;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $code
|
||||
@@ -28,11 +96,12 @@ class ImportCurrency
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $symbol
|
||||
* @param array $id
|
||||
*/
|
||||
public function setSymbol(array $symbol)
|
||||
public function setId(array $id)
|
||||
{
|
||||
$this->symbol = $symbol;
|
||||
$id['value'] = intval($id['value']);
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,11 +113,100 @@ class ImportCurrency
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $id
|
||||
* @param array $symbol
|
||||
*/
|
||||
public function setId(array $id)
|
||||
public function setSymbol(array $symbol)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->symbol = $symbol;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->repository->setUser($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
private function findExistingObject(): TransactionCurrency
|
||||
{
|
||||
$search = [
|
||||
'id' => 'find',
|
||||
'code' => 'findByCode',
|
||||
'symbol' => 'findBySymbol',
|
||||
'name' => 'findByName',
|
||||
];
|
||||
foreach ($search as $field => $function) {
|
||||
$value = $this->$field['value'] ?? null;
|
||||
if (!is_null($value)) {
|
||||
Log::debug(sprintf('Searching for %s using function %s and value %s', $field, $function, $value));
|
||||
$currency = $this->repository->$function($value);
|
||||
|
||||
if (!is_null($currency->id)) {
|
||||
return $currency;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new TransactionCurrency();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
private function findMappedObject(): TransactionCurrency
|
||||
{
|
||||
Log::debug('In findMappedObject()');
|
||||
$fields = ['id', 'code', 'name', 'symbol'];
|
||||
foreach ($fields as $field) {
|
||||
$array = $this->$field;
|
||||
Log::debug(sprintf('Find mapped currency 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 currency #%d!', $mapped->id));
|
||||
|
||||
return $mapped;
|
||||
}
|
||||
|
||||
}
|
||||
Log::debug('Found no currency on mapped data or no map present.');
|
||||
|
||||
return new TransactionCurrency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
private function getMappedObject(array $array): TransactionCurrency
|
||||
{
|
||||
Log::debug('In getMappedObject()');
|
||||
if (count($array) === 0) {
|
||||
Log::debug('Array is empty, nothing will come of this.');
|
||||
|
||||
return new TransactionCurrency;
|
||||
}
|
||||
|
||||
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 TransactionCurrency;
|
||||
}
|
||||
|
||||
Log::debug('Finding a mapped object based on', $array);
|
||||
|
||||
$search = intval($array['mapped']);
|
||||
$currency = $this->repository->find($search);
|
||||
|
||||
Log::debug(sprintf('Found currency! #%d ("%s"). Return it', $currency->id, $currency->name));
|
||||
|
||||
return $currency;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* ImportObject.php
|
||||
* ImportJournal.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
|
||||
*
|
||||
@@ -12,32 +12,52 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Import\Object;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class ImportObject
|
||||
/**
|
||||
* Class ImportJournal
|
||||
*
|
||||
* @package FireflyIII\Import\Object
|
||||
*/
|
||||
class ImportJournal
|
||||
{
|
||||
/** @var Collection */
|
||||
public $errors;
|
||||
/** @var string */
|
||||
private $amount = '0';
|
||||
/** @var ImportAccount */
|
||||
private $asset;
|
||||
public $asset;
|
||||
/** @var ImportBill */
|
||||
private $bill;
|
||||
/** @var ImportBudget */
|
||||
private $budget;
|
||||
/** @var ImportCategory */
|
||||
private $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 = [];
|
||||
/** @var ImportTransaction */
|
||||
private $transaction;
|
||||
/** @var string */
|
||||
private $transactionType = '';
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
@@ -46,18 +66,29 @@ class ImportObject
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->errors = new Collection;
|
||||
$this->transaction = new ImportTransaction;
|
||||
$this->asset = new ImportAccount;
|
||||
$this->opposing = new ImportAccount;
|
||||
$this->bill = new ImportBill;
|
||||
$this->category = new ImportCategory;
|
||||
$this->budget = new ImportBudget;
|
||||
$this->errors = new Collection;
|
||||
$this->asset = new ImportAccount;
|
||||
$this->opposing = new ImportAccount;
|
||||
$this->bill = new ImportBill;
|
||||
$this->category = new ImportCategory;
|
||||
$this->budget = new ImportBudget;
|
||||
}
|
||||
|
||||
public function setHash(string $hash)
|
||||
/**
|
||||
* @param array $modifier
|
||||
*/
|
||||
public function addToModifier(array $modifier)
|
||||
{
|
||||
$this->hash = $hash;
|
||||
$this->modifiers[] = $modifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TransactionJournal
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function createTransactionJournal(): TransactionJournal
|
||||
{
|
||||
exit('does not work yet');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,6 +97,9 @@ class ImportObject
|
||||
public function setUser(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
// set user for related objects:
|
||||
$this->asset->setUser($user);
|
||||
$this->opposing->setUser($user);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,12 +111,12 @@ class ImportObject
|
||||
{
|
||||
switch ($array['role']) {
|
||||
default:
|
||||
throw new FireflyException(sprintf('ImportObject cannot handle "%s" with value "%s".', $array['role'], $array['value']));
|
||||
throw new FireflyException(sprintf('ImportJournal cannot handle "%s" with value "%s".', $array['role'], $array['value']));
|
||||
case 'account-id':
|
||||
$this->asset->setAccountId($array);
|
||||
break;
|
||||
case 'amount':
|
||||
$this->transaction->setAmount($array['value']);
|
||||
$this->amount = $array['value'];
|
||||
break;
|
||||
case 'account-iban':
|
||||
$this->asset->setAccountIban($array);
|
||||
@@ -112,23 +146,22 @@ class ImportObject
|
||||
$this->category->setName($array);
|
||||
break;
|
||||
case 'currency-code':
|
||||
$this->transaction->getCurrency()->setCode($array);
|
||||
$this->currency->setCode($array);
|
||||
break;
|
||||
case 'currency-id':
|
||||
$this->transaction->getCurrency()->setId($array);
|
||||
$this->currency->setId($array);
|
||||
break;
|
||||
case 'currency-name':
|
||||
$this->transaction->getCurrency()->setName($array);
|
||||
$this->currency->setName($array);
|
||||
break;
|
||||
case 'currency-symbol':
|
||||
$this->transaction->getCurrency()->setSymbol($array);
|
||||
$this->currency->setSymbol($array);
|
||||
break;
|
||||
case 'date-transaction':
|
||||
$this->transaction->setDate($array['value']);
|
||||
$this->date = $array['value'];
|
||||
break;
|
||||
case 'description':
|
||||
$this->description = $array['value'];
|
||||
$this->transaction->setDescription($array['value']);
|
||||
break;
|
||||
case 'external-id':
|
||||
$this->externalId = $array['value'];
|
||||
@@ -137,7 +170,7 @@ class ImportObject
|
||||
break;
|
||||
case 'ing-debet-credit':
|
||||
case 'rabo-debet-credit':
|
||||
$this->transaction->addToModifier($array);
|
||||
$this->addToModifier($array);
|
||||
break;
|
||||
case 'opposing-iban':
|
||||
$this->opposing->setAccountIban($array);
|
||||
@@ -157,5 +190,4 @@ class ImportObject
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,6 +12,9 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Import\Object;
|
||||
|
||||
|
||||
use FireflyIII\Import\Converter\ConverterInterface;
|
||||
use Steam;
|
||||
|
||||
class ImportTransaction
|
||||
{
|
||||
/** @var string */
|
||||
@@ -39,6 +42,39 @@ class ImportTransaction
|
||||
$this->modifiers[] = $modifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAmount(): string
|
||||
{
|
||||
// use converter:
|
||||
$this->amount = strval($this->parseAmount());
|
||||
|
||||
|
||||
// also apply modifiers:
|
||||
$this->amount = Steam::positive($this->amount);
|
||||
|
||||
// Can handle ING
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $amount
|
||||
*/
|
||||
public function setAmount(string $amount)
|
||||
{
|
||||
$this->amount = $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ImportCurrency
|
||||
*/
|
||||
@@ -55,14 +91,6 @@ class ImportTransaction
|
||||
$this->currency = $currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $amount
|
||||
*/
|
||||
public function setAmount(string $amount)
|
||||
{
|
||||
$this->amount = $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
*/
|
||||
@@ -87,4 +115,43 @@ class ImportTransaction
|
||||
$this->positive = $positive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
|
||||
* - Jamie Zawinski
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
private function parseAmount()
|
||||
{
|
||||
$value = $this->amount;
|
||||
$len = strlen($value);
|
||||
$decimalPosition = $len - 3;
|
||||
$decimal = null;
|
||||
|
||||
if (($len > 2 && $value{$decimalPosition} == '.') || ($len > 2 && strpos($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), 12);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user