mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-19 10:53:37 +00:00
Expand import routine.
This commit is contained in:
@@ -88,7 +88,7 @@ class CreateImport extends Command
|
|||||||
$this->line('Stored import data...');
|
$this->line('Stored import data...');
|
||||||
|
|
||||||
$job->configuration = $configurationData;
|
$job->configuration = $configurationData;
|
||||||
$job->status = 'settings_complete';
|
$job->status = 'configured';
|
||||||
$job->save();
|
$job->save();
|
||||||
$this->line('Stored configuration...');
|
$this->line('Stored configuration...');
|
||||||
|
|
||||||
|
@@ -13,8 +13,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Console\Commands;
|
namespace FireflyIII\Console\Commands;
|
||||||
|
|
||||||
use FireflyIII\Import\ImportProcedure;
|
|
||||||
use FireflyIII\Import\Logging\CommandHandler;
|
use FireflyIII\Import\Logging\CommandHandler;
|
||||||
|
use FireflyIII\Import\Routine\ImportRoutine;
|
||||||
use FireflyIII\Models\ImportJob;
|
use FireflyIII\Models\ImportJob;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
@@ -75,15 +75,16 @@ class Import extends Command
|
|||||||
$monolog = Log::getMonolog();
|
$monolog = Log::getMonolog();
|
||||||
$handler = new CommandHandler($this);
|
$handler = new CommandHandler($this);
|
||||||
$monolog->pushHandler($handler);
|
$monolog->pushHandler($handler);
|
||||||
$importProcedure = new ImportProcedure;
|
|
||||||
$result = $importProcedure->runImport($job);
|
$routine = new ImportRoutine($job);
|
||||||
|
$routine->run();
|
||||||
|
|
||||||
// display result to user:
|
// display result to user:
|
||||||
$this->presentResults($result);
|
//$this->presentResults($result);
|
||||||
$this->line('The import has completed.');
|
$this->line('The import has completed.');
|
||||||
|
|
||||||
// get any errors from the importer:
|
// get any errors from the importer:
|
||||||
$this->presentErrors($job);
|
//$this->presentErrors($job);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -96,12 +97,14 @@ class Import extends Command
|
|||||||
private function isValid(ImportJob $job): bool
|
private function isValid(ImportJob $job): bool
|
||||||
{
|
{
|
||||||
if (is_null($job)) {
|
if (is_null($job)) {
|
||||||
|
Log::error('This job does not seem to exist.');
|
||||||
$this->error('This job does not seem to exist.');
|
$this->error('This job does not seem to exist.');
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($job->status != 'settings_complete') {
|
if ($job->status !== 'configured') {
|
||||||
|
Log::error(sprintf('This job is not ready to be imported (status is %s).', $job->status));
|
||||||
$this->error('This job is not ready to be imported.');
|
$this->error('This job is not ready to be imported.');
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@@ -78,6 +78,7 @@ class TransactionController extends Controller
|
|||||||
$start = null;
|
$start = null;
|
||||||
$end = null;
|
$end = null;
|
||||||
$periods = new Collection;
|
$periods = new Collection;
|
||||||
|
$path = '/transactions/' . $what;
|
||||||
|
|
||||||
// prep for "all" view.
|
// prep for "all" view.
|
||||||
if ($moment === 'all') {
|
if ($moment === 'all') {
|
||||||
@@ -85,6 +86,7 @@ class TransactionController extends Controller
|
|||||||
$first = $repository->first();
|
$first = $repository->first();
|
||||||
$start = $first->date ?? new Carbon;
|
$start = $first->date ?? new Carbon;
|
||||||
$end = new Carbon;
|
$end = new Carbon;
|
||||||
|
$path = '/transactions/'.$what.'/all/';
|
||||||
}
|
}
|
||||||
|
|
||||||
// prep for "specific date" view.
|
// prep for "specific date" view.
|
||||||
@@ -118,7 +120,7 @@ class TransactionController extends Controller
|
|||||||
$collector->setAllAssetAccounts()->setRange($start, $end)->setTypes($types)->setLimit($pageSize)->setPage($page)->withOpposingAccount();
|
$collector->setAllAssetAccounts()->setRange($start, $end)->setTypes($types)->setLimit($pageSize)->setPage($page)->withOpposingAccount();
|
||||||
$collector->removeFilter(InternalTransferFilter::class);
|
$collector->removeFilter(InternalTransferFilter::class);
|
||||||
$journals = $collector->getPaginatedJournals();
|
$journals = $collector->getPaginatedJournals();
|
||||||
$journals->setPath('/transactions/' . $what);
|
$journals->setPath($path);
|
||||||
$count = $journals->getCollection()->count();
|
$count = $journals->getCollection()->count();
|
||||||
if ($count === 0) {
|
if ($count === 0) {
|
||||||
$start->subDay();
|
$start->subDay();
|
||||||
|
@@ -12,7 +12,6 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Import\FileProcessor;
|
namespace FireflyIII\Import\FileProcessor;
|
||||||
|
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Import\Converter\ConverterInterface;
|
|
||||||
use FireflyIII\Import\Object\ImportJournal;
|
use FireflyIII\Import\Object\ImportJournal;
|
||||||
use FireflyIII\Import\Specifics\SpecificInterface;
|
use FireflyIII\Import\Specifics\SpecificInterface;
|
||||||
use FireflyIII\Models\ImportJob;
|
use FireflyIII\Models\ImportJob;
|
||||||
@@ -184,6 +183,7 @@ class CsvProcessor implements FileProcessorInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Take a row, build import journal by annotating each value and storing it in the import journal.
|
* Take a row, build import journal by annotating each value and storing it in the import journal.
|
||||||
|
*
|
||||||
* @param int $index
|
* @param int $index
|
||||||
* @param array $row
|
* @param array $row
|
||||||
*
|
*
|
||||||
@@ -198,10 +198,13 @@ class CsvProcessor implements FileProcessorInterface
|
|||||||
$journal->setHash(hash('sha256', json_encode($row)));
|
$journal->setHash(hash('sha256', json_encode($row)));
|
||||||
|
|
||||||
foreach ($row as $rowIndex => $value) {
|
foreach ($row as $rowIndex => $value) {
|
||||||
|
$value = trim($value);
|
||||||
|
if (strlen($value) > 0) {
|
||||||
$annotated = $this->annotateValue($rowIndex, $value);
|
$annotated = $this->annotateValue($rowIndex, $value);
|
||||||
Log::debug('Annotated value', $annotated);
|
Log::debug('Annotated value', $annotated);
|
||||||
$journal->setValue($annotated);
|
$journal->setValue($annotated);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Log::debug('ImportJournal complete, returning.');
|
Log::debug('ImportJournal complete, returning.');
|
||||||
|
|
||||||
return $journal;
|
return $journal;
|
||||||
|
@@ -16,6 +16,7 @@ use FireflyIII\Models\Account;
|
|||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
use Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -36,6 +37,8 @@ class ImportAccount
|
|||||||
private $accountName = [];
|
private $accountName = [];
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private $accountNumber = [];
|
private $accountNumber = [];
|
||||||
|
/** @var string */
|
||||||
|
private $expectedType = '';
|
||||||
/** @var AccountRepositoryInterface */
|
/** @var AccountRepositoryInterface */
|
||||||
private $repository;
|
private $repository;
|
||||||
/** @var User */
|
/** @var User */
|
||||||
@@ -46,77 +49,10 @@ class ImportAccount
|
|||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
$this->expectedType = AccountType::ASSET;
|
||||||
$this->account = new Account;
|
$this->account = new Account;
|
||||||
$this->repository = app(AccountRepositoryInterface::class);
|
$this->repository = app(AccountRepositoryInterface::class);
|
||||||
Log::debug('Created ImportAccount.');
|
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
|
public function getAccount(): Account
|
||||||
{
|
{
|
||||||
|
if (is_null($this->account->id)) {
|
||||||
|
$this->store();
|
||||||
|
}
|
||||||
|
|
||||||
return $this->account;
|
return $this->account;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,6 +99,14 @@ class ImportAccount
|
|||||||
$this->accountNumber = $accountNumber;
|
$this->accountNumber = $accountNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $expectedType
|
||||||
|
*/
|
||||||
|
public function setExpectedType(string $expectedType)
|
||||||
|
{
|
||||||
|
$this->expectedType = $expectedType;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param User $user
|
* @param User $user
|
||||||
*/
|
*/
|
||||||
@@ -168,12 +116,88 @@ class ImportAccount
|
|||||||
$this->repository->setUser($user);
|
$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
|
* @return Account
|
||||||
*/
|
*/
|
||||||
private function findMappedObject(): Account
|
private function findMappedObject(): Account
|
||||||
{
|
{
|
||||||
Log::debug('In findMappedObject()');
|
Log::debug('In findMappedObject() for Account');
|
||||||
$fields = ['accountId', 'accountIban', 'accountNumber', 'accountName'];
|
$fields = ['accountId', 'accountIban', 'accountNumber', 'accountName'];
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
$array = $this->$field;
|
$array = $this->$field;
|
||||||
@@ -199,7 +223,7 @@ class ImportAccount
|
|||||||
*/
|
*/
|
||||||
private function getMappedObject(array $array): Account
|
private function getMappedObject(array $array): Account
|
||||||
{
|
{
|
||||||
Log::debug('In getMappedObject()');
|
Log::debug('In getMappedObject() for Account');
|
||||||
if (count($array) === 0) {
|
if (count($array) === 0) {
|
||||||
Log::debug('Array is empty, nothing will come of this.');
|
Log::debug('Array is empty, nothing will come of this.');
|
||||||
|
|
||||||
@@ -212,7 +236,7 @@ class ImportAccount
|
|||||||
return new Account;
|
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']);
|
$search = intval($array['mapped']);
|
||||||
$account = $this->repository->find($search);
|
$account = $this->repository->find($search);
|
||||||
@@ -222,5 +246,54 @@ class ImportAccount
|
|||||||
return $account;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@@ -12,13 +12,52 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Import\Object;
|
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
|
class ImportBudget
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/** @var Budget */
|
||||||
|
private $budget;
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private $id = [];
|
private $id = [];
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private $name = [];
|
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
|
* @param array $id
|
||||||
@@ -36,5 +75,155 @@ class ImportBudget
|
|||||||
$this->name = $name;
|
$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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@@ -12,14 +12,24 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Import\Object;
|
namespace FireflyIII\Import\Object;
|
||||||
|
|
||||||
|
|
||||||
|
use FireflyIII\Models\Category;
|
||||||
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||||
|
use FireflyIII\User;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Log;
|
||||||
|
|
||||||
class ImportCategory
|
class ImportCategory
|
||||||
{
|
{
|
||||||
|
/** @var Category */
|
||||||
|
private $category;
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private $id = [];
|
private $id = [];
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private $name = [];
|
private $name = [];
|
||||||
|
/** @var CategoryRepositoryInterface */
|
||||||
|
private $repository;
|
||||||
|
/** @var User */
|
||||||
|
private $user;
|
||||||
/**
|
/**
|
||||||
* @param array $id
|
* @param array $id
|
||||||
*/
|
*/
|
||||||
@@ -28,6 +38,16 @@ class ImportCategory
|
|||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ImportCategory constructor.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->category = new Category();
|
||||||
|
$this->repository = app(CategoryRepositoryInterface::class);
|
||||||
|
Log::debug('Created ImportCategory.');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $name
|
* @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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@@ -46,7 +46,7 @@ class ImportCurrency
|
|||||||
/**
|
/**
|
||||||
* @return TransactionCurrency
|
* @return TransactionCurrency
|
||||||
*/
|
*/
|
||||||
public function createCurrency(): TransactionCurrency
|
public function getTransactionCurrency(): TransactionCurrency
|
||||||
{
|
{
|
||||||
if (!is_null($this->currency->id)) {
|
if (!is_null($this->currency->id)) {
|
||||||
return $this->currency;
|
return $this->currency;
|
||||||
@@ -77,6 +77,12 @@ class ImportCurrency
|
|||||||
'name' => $this->name['value'] ?? null,
|
'name' => $this->name['value'] ?? null,
|
||||||
'decimal_places' => 2,
|
'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);
|
Log::debug('Search for maps resulted in nothing, create new one based on', $data);
|
||||||
$currency = $this->repository->store($data);
|
$currency = $this->repository->store($data);
|
||||||
$this->currency = $currency;
|
$this->currency = $currency;
|
||||||
|
@@ -14,11 +14,12 @@ namespace FireflyIII\Import\Object;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
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\TransactionJournal;
|
||||||
use FireflyIII\Models\TransactionType;
|
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Steam;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ImportJournal
|
* Class ImportJournal
|
||||||
@@ -27,34 +28,32 @@ use Illuminate\Support\Collection;
|
|||||||
*/
|
*/
|
||||||
class ImportJournal
|
class ImportJournal
|
||||||
{
|
{
|
||||||
|
/** @var ImportAccount */
|
||||||
|
public $asset;
|
||||||
|
/** @var ImportBudget */
|
||||||
|
public $budget;
|
||||||
|
/** @var string */
|
||||||
|
public $description;
|
||||||
/** @var Collection */
|
/** @var Collection */
|
||||||
public $errors;
|
public $errors;
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $amount = '0';
|
public $hash;
|
||||||
/** @var ImportAccount */
|
/** @var ImportAccount */
|
||||||
public $asset;
|
public $opposing;
|
||||||
|
/** @var string */
|
||||||
|
private $amount = '0';
|
||||||
/** @var ImportBill */
|
/** @var ImportBill */
|
||||||
private $bill;
|
private $bill;
|
||||||
/** @var ImportBudget */
|
|
||||||
private $budget;
|
|
||||||
/** @var ImportCategory */
|
/** @var ImportCategory */
|
||||||
private $category;
|
public $category;
|
||||||
/** @var ImportCurrency */
|
/** @var ImportCurrency */
|
||||||
private $currency;
|
private $currency;
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $date = '';
|
private $date = '';
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $dateFormat = 'Ymd';
|
|
||||||
/** @var string */
|
|
||||||
private $description;
|
|
||||||
/** @var string */
|
|
||||||
private $externalId = '';
|
private $externalId = '';
|
||||||
/** @var string */
|
|
||||||
private $hash;
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private $modifiers = [];
|
private $modifiers = [];
|
||||||
/** @var ImportAccount */
|
|
||||||
private $opposing;
|
|
||||||
private $tags = [];
|
private $tags = [];
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $transactionType = '';
|
private $transactionType = '';
|
||||||
@@ -72,6 +71,7 @@ class ImportJournal
|
|||||||
$this->bill = new ImportBill;
|
$this->bill = new ImportBill;
|
||||||
$this->category = new ImportCategory;
|
$this->category = new ImportCategory;
|
||||||
$this->budget = new ImportBudget;
|
$this->budget = new ImportBudget;
|
||||||
|
$this->currency = new ImportCurrency;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,15 +91,66 @@ class ImportJournal
|
|||||||
exit('does not work yet');
|
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
|
* @param User $user
|
||||||
*/
|
*/
|
||||||
public function setUser(User $user)
|
public function setUser(User $user)
|
||||||
{
|
{
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
|
|
||||||
// set user for related objects:
|
// set user for related objects:
|
||||||
$this->asset->setUser($user);
|
$this->asset->setUser($user);
|
||||||
$this->opposing->setUser($user);
|
$this->opposing->setUser($user);
|
||||||
|
$this->budget->setUser($user);
|
||||||
|
$this->category->setUser($user);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -60,6 +60,7 @@ class ImportRoutine
|
|||||||
}
|
}
|
||||||
|
|
||||||
$storage = new ImportStorage;
|
$storage = new ImportStorage;
|
||||||
|
$storage->setJob($this->job);
|
||||||
$storage->setDateFormat($this->job->configuration['date-format']);
|
$storage->setDateFormat($this->job->configuration['date-format']);
|
||||||
$storage->setObjects($objects);
|
$storage->setObjects($objects);
|
||||||
$storage->store();
|
$storage->store();
|
||||||
|
@@ -11,11 +11,18 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Import\Storage;
|
namespace FireflyIII\Import\Storage;
|
||||||
|
|
||||||
|
use Amount;
|
||||||
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Import\Object\ImportJournal;
|
use FireflyIII\Import\Object\ImportJournal;
|
||||||
use FireflyIII\Import\Object\ImportObject;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\TransactionJournalMeta;
|
use FireflyIII\Models\AccountType;
|
||||||
|
use FireflyIII\Models\ImportJob;
|
||||||
|
use FireflyIII\Models\Transaction;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
|
use FireflyIII\Models\TransactionType;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
use Log;
|
||||||
|
use Steam;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is capable of storing individual ImportJournal objects.
|
* Is capable of storing individual ImportJournal objects.
|
||||||
@@ -27,6 +34,9 @@ class ImportStorage
|
|||||||
{
|
{
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $dateFormat = 'Ymd';
|
private $dateFormat = 'Ymd';
|
||||||
|
private $defaultCurrency;
|
||||||
|
/** @var ImportJob */
|
||||||
|
private $job;
|
||||||
/** @var Collection */
|
/** @var Collection */
|
||||||
private $objects;
|
private $objects;
|
||||||
|
|
||||||
@@ -46,6 +56,13 @@ class ImportStorage
|
|||||||
$this->dateFormat = $dateFormat;
|
$this->dateFormat = $dateFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ImportJob $job
|
||||||
|
*/
|
||||||
|
public function setJob(ImportJob $job)
|
||||||
|
{
|
||||||
|
$this->job = $job;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $objects
|
* @param Collection $objects
|
||||||
@@ -61,15 +78,102 @@ class ImportStorage
|
|||||||
*/
|
*/
|
||||||
public function store()
|
public function store()
|
||||||
{
|
{
|
||||||
|
$this->defaultCurrency = Amount::getDefaultCurrencyByUser($this->job->user);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int $index
|
* @var int $index
|
||||||
* @var ImportJournal $object
|
* @var ImportJournal $object
|
||||||
*/
|
*/
|
||||||
foreach ($this->objects as $index => $object) {
|
foreach ($this->objects as $index => $object) {
|
||||||
Log::debug(sprintf('Going to store object #%d', $index));
|
Log::debug(sprintf('Going to store object #%d with description "%s"', $index, $object->description));
|
||||||
|
|
||||||
|
// create the asset account
|
||||||
|
$asset = $object->asset->getAccount();
|
||||||
|
$opposing = new Account;
|
||||||
|
$amount = $object->getAmount();
|
||||||
|
$currency = $object->getCurrency()->getTransactionCurrency();
|
||||||
|
$date = $object->getDate($this->dateFormat);
|
||||||
|
$transactionType = new TransactionType;
|
||||||
|
|
||||||
|
if (is_null($currency->id)) {
|
||||||
|
$currency = $this->defaultCurrency;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bccomp($amount, '0') === -1) {
|
||||||
|
// amount is negative, it's a withdrawal, opposing is an expense:
|
||||||
|
Log::debug(sprintf('%s is negative, create opposing expense account.', $amount));
|
||||||
|
$object->opposing->setExpectedType(AccountType::EXPENSE);
|
||||||
|
$opposing = $object->opposing->getAccount();
|
||||||
|
$transactionType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
||||||
|
}
|
||||||
|
if (bccomp($amount, '0') === 1) {
|
||||||
|
Log::debug(sprintf('%s is positive, create opposing revenue account.', $amount));
|
||||||
|
// amount is positive, it's a deposit, opposing is an revenue:
|
||||||
|
$object->opposing->setExpectedType(AccountType::REVENUE);
|
||||||
|
$opposing = $object->opposing->getAccount();
|
||||||
|
$transactionType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
// if opposing is an asset account, it's a transfer:
|
||||||
|
if ($opposing->accountType->type === AccountType::ASSET) {
|
||||||
|
Log::debug(sprintf('Opposing account #%d %s is an asset account, make transfer.', $opposing->id, $opposing->name));
|
||||||
|
$transactionType = TransactionType::whereType(TransactionType::TRANSFER)->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
$journal = new TransactionJournal;
|
||||||
|
$journal->user_id = $this->job->user_id;
|
||||||
|
$journal->transaction_type_id = $transactionType->id;
|
||||||
|
$journal->transaction_currency_id = $currency->id;
|
||||||
|
$journal->description = $object->description;
|
||||||
|
$journal->date = $date->format('Y-m-d');
|
||||||
|
$journal->order = 0;
|
||||||
|
$journal->tag_count = 0;
|
||||||
|
$journal->encrypted = 0;
|
||||||
|
$journal->completed = 0;
|
||||||
|
if (!$journal->save()) {
|
||||||
|
throw new FireflyException($journal->getErrors()->first());
|
||||||
|
}
|
||||||
|
$journal->setMeta('importHash', $object->hash);
|
||||||
|
Log::debug(sprintf('Created journal with ID #%d', $journal->id));
|
||||||
|
|
||||||
|
// create transactions:
|
||||||
|
$one = new Transaction;
|
||||||
|
$one->account_id = $asset->id;
|
||||||
|
$one->transaction_journal_id = $journal->id;
|
||||||
|
$one->transaction_currency_id = $currency->id;
|
||||||
|
$one->amount = $amount;
|
||||||
|
$one->save();
|
||||||
|
Log::debug(sprintf('Created transaction with ID #%d and account #%d', $one->id, $asset->id));
|
||||||
|
|
||||||
|
$two = new Transaction;
|
||||||
|
$two->account_id = $opposing->id;
|
||||||
|
$two->transaction_journal_id = $journal->id;
|
||||||
|
$two->transaction_currency_id = $currency->id;
|
||||||
|
$two->amount = Steam::opposite($amount);
|
||||||
|
$two->save();
|
||||||
|
Log::debug(sprintf('Created transaction with ID #%d and account #%d', $two->id, $opposing->id));
|
||||||
|
|
||||||
|
// category
|
||||||
|
$category = $object->category->getCategory();
|
||||||
|
if (!is_null($category->id)) {
|
||||||
|
Log::debug(sprintf('Linked category #%d to journal #%d', $category->id, $journal->id));
|
||||||
|
$journal->categories()->save($category);
|
||||||
|
}
|
||||||
|
|
||||||
|
// budget
|
||||||
|
$budget = $object->budget->getBudget();
|
||||||
|
if (!is_null($budget->id)) {
|
||||||
|
Log::debug(sprintf('Linked budget #%d to journal #%d', $budget->id, $journal->id));
|
||||||
|
$journal->budgets()->save($budget);
|
||||||
|
}
|
||||||
|
// bill
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
die('Cannot actually store yet.');
|
die('Cannot actually store yet.');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@@ -243,9 +243,11 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// account may exist already:
|
// account may exist already:
|
||||||
$existingAccount = $this->findByName($data['name'], [$data['accountType']]);
|
$existingAccount = $this->findByName($data['name'], [$type]);
|
||||||
if (!is_null($existingAccount->id)) {
|
if (!is_null($existingAccount->id)) {
|
||||||
throw new FireflyException(sprintf('There already is an account named "%s" of type "%s".', $data['name'], $data['accountType']));
|
Log::warning(sprintf('There already is an account named "%s" of type "%s".', $data['name'], $type));
|
||||||
|
|
||||||
|
return $existingAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create it:
|
// create it:
|
||||||
@@ -267,6 +269,7 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
);
|
);
|
||||||
throw new FireflyException(sprintf('Tried to create account named "%s" but failed. The logs have more details.', $data['name']));
|
throw new FireflyException(sprintf('Tried to create account named "%s" but failed. The logs have more details.', $data['name']));
|
||||||
}
|
}
|
||||||
|
Log::debug(sprintf('Created new account #%d named "%s" of type %s.', $newAccount->id, $newAccount->name, $accountType->type));
|
||||||
|
|
||||||
return $newAccount;
|
return $newAccount;
|
||||||
}
|
}
|
||||||
|
@@ -107,7 +107,7 @@ trait FindAccountsTrait
|
|||||||
$query->whereIn('account_types.type', $types);
|
$query->whereIn('account_types.type', $types);
|
||||||
|
|
||||||
}
|
}
|
||||||
Log::debug(sprintf('Searching for account named %s of the following type(s)', $name), ['types' => $types]);
|
Log::debug(sprintf('Searching for account named "%s" (of user #%d) of the following type(s)', $name, $this->user->id), ['types' => $types]);
|
||||||
|
|
||||||
$accounts = $query->get(['accounts.*']);
|
$accounts = $query->get(['accounts.*']);
|
||||||
/** @var Account $account */
|
/** @var Account $account */
|
||||||
@@ -118,7 +118,7 @@ trait FindAccountsTrait
|
|||||||
return $account;
|
return $account;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log::debug('Found nothing.');
|
Log::debug(sprintf('There is no account with name "%s" or types', $name), $types);
|
||||||
|
|
||||||
return new Account;
|
return new Account;
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,7 @@ use FireflyIII\Models\Transaction as TransactionModel;
|
|||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
|
use FireflyIII\User;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Preferences as Prefs;
|
use Preferences as Prefs;
|
||||||
|
|
||||||
@@ -208,13 +209,27 @@ class Amount
|
|||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
public function getDefaultCurrency(): TransactionCurrency
|
public function getDefaultCurrency(): TransactionCurrency
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
|
||||||
|
return $this->getDefaultCurrencyByUser($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
*
|
||||||
|
* @return \FireflyIII\Models\TransactionCurrency
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
public function getDefaultCurrencyByUser(User $user): TransactionCurrency
|
||||||
{
|
{
|
||||||
$cache = new CacheProperties;
|
$cache = new CacheProperties;
|
||||||
$cache->addProperty('getDefaultCurrency');
|
$cache->addProperty('getDefaultCurrency');
|
||||||
|
$cache->addProperty($user->id);
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return $cache->get(); // @codeCoverageIgnore
|
return $cache->get(); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
$currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR'));
|
$currencyPreference = Prefs::getForUser($user, 'currencyPreference', config('firefly.default_currency', 'EUR'));
|
||||||
$currency = TransactionCurrency::where('code', $currencyPreference->data)->first();
|
$currency = TransactionCurrency::where('code', $currencyPreference->data)->first();
|
||||||
if (is_null($currency)) {
|
if (is_null($currency)) {
|
||||||
throw new FireflyException(sprintf('No currency found with code "%s"', $currencyPreference->data));
|
throw new FireflyException(sprintf('No currency found with code "%s"', $currencyPreference->data));
|
||||||
|
@@ -284,6 +284,18 @@ class Steam
|
|||||||
return $amount;
|
return $amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $amount
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function opposite(string $amount): string
|
||||||
|
{
|
||||||
|
$amount = bcmul($amount, '-1');
|
||||||
|
|
||||||
|
return $amount;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $string
|
* @param $string
|
||||||
*
|
*
|
||||||
|
@@ -28,6 +28,7 @@ class TransactionCurrencySeeder extends Seeder
|
|||||||
TransactionCurrency::create(['code' => 'GBP', 'name' => 'British Pound', 'symbol' => '£', 'decimal_places' => 2]);
|
TransactionCurrency::create(['code' => 'GBP', 'name' => 'British Pound', 'symbol' => '£', 'decimal_places' => 2]);
|
||||||
TransactionCurrency::create(['code' => 'IDR', 'name' => 'Indonesian rupiah', 'symbol' => 'Rp', 'decimal_places' => 2]);
|
TransactionCurrency::create(['code' => 'IDR', 'name' => 'Indonesian rupiah', 'symbol' => 'Rp', 'decimal_places' => 2]);
|
||||||
TransactionCurrency::create(['code' => 'XBT', 'name' => 'Bitcoin', 'symbol' => 'B', 'decimal_places' => 8]);
|
TransactionCurrency::create(['code' => 'XBT', 'name' => 'Bitcoin', 'symbol' => 'B', 'decimal_places' => 8]);
|
||||||
|
TransactionCurrency::create(['code' => 'JPY', 'name' => 'Japanese yen', 'symbol' => '¥', 'decimal_places' => 2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user