Delete old code.

This commit is contained in:
James Cole
2018-07-13 15:52:27 +02:00
parent 2b4088c5f7
commit cede11ecea
6 changed files with 0 additions and 2012 deletions

View File

@@ -1,479 +0,0 @@
<?php
/**
* ImportAccount.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Object;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
use Log;
/**
* @deprecated
* @codeCoverageIgnore
* Class ImportAccount.
*/
class ImportAccount
{
/** @var Account */
private $account;
/** @var array */
private $accountBic = [];
/** @var array */
private $accountIban = [];
/** @var array */
private $accountId = [];
/** @var array */
private $accountName = [];
/** @var array */
private $accountNumber = [];
/** @var int */
private $defaultAccountId = 0;
/** @var string */
private $expectedType;
/**
* This value is used to indicate the other account ID (the opposing transaction's account),
* if it is know. If so, this particular import account may never return an Account with this ID.
* If it would, this would result in a transaction from-to the same account.
*
* @var int
*/
private $forbiddenAccountId = 0;
/** @var AccountRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* ImportAccount constructor.
*/
public function __construct()
{
$this->expectedType = AccountType::ASSET;
$this->repository = app(AccountRepositoryInterface::class);
Log::debug('Created ImportAccount.');
}
/**
* @return Account
*
* @throws FireflyException
*/
public function getAccount(): Account
{
if (null === $this->account) {
$this->store();
}
return $this->account;
}
/**
* @codeCoverageIgnore
*
* @return string
*/
public function getExpectedType(): string
{
return $this->expectedType;
}
/**
* @codeCoverageIgnore
*
* @param string $expectedType
*/
public function setExpectedType(string $expectedType)
{
$this->expectedType = $expectedType;
}
/**
* @param array $accountBic
*/
public function setAccountBic(array $accountBic): void
{
$this->accountBic = $accountBic;
}
/**
* @codeCoverageIgnore
*
* @param array $accountIban
*/
public function setAccountIban(array $accountIban)
{
$this->accountIban = $accountIban;
}
/**
* @codeCoverageIgnore
*
* @param array $value
*/
public function setAccountId(array $value)
{
$this->accountId = $value;
}
/**
* @codeCoverageIgnore
*
* @param array $accountName
*/
public function setAccountName(array $accountName)
{
$this->accountName = $accountName;
}
/**
* @codeCoverageIgnore
*
* @param array $accountNumber
*/
public function setAccountNumber(array $accountNumber)
{
$this->accountNumber = $accountNumber;
}
/**
* @codeCoverageIgnore
*
* @param int $defaultAccountId
*/
public function setDefaultAccountId(int $defaultAccountId)
{
$this->defaultAccountId = $defaultAccountId;
}
/**
* @codeCoverageIgnore
*
* @param int $forbiddenAccountId
*/
public function setForbiddenAccountId(int $forbiddenAccountId)
{
$this->forbiddenAccountId = $forbiddenAccountId;
}
/**
* @codeCoverageIgnore
*
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
/**
* Find account by IBAN and type.
*
* @param AccountType $type
*
* @return Account|null
*/
private function findByIBAN(AccountType $type): ?Account
{
if (3 === \count($this->accountIban)) {
$accounts = $this->repository->getAccountsByType([$type->type]);
$iban = $this->accountIban['value'];
Log::debug(sprintf('Finding account of type %d and IBAN %s', $type->id, $iban));
$filtered = $accounts->filter(
function (Account $account) use ($iban) {
if ($account->iban === $iban && $account->id !== $this->forbiddenAccountId) {
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 (1 === $filtered->count()) {
return $filtered->first();
}
Log::debug('Found nothing.');
}
return null;
}
/**
* Find account of type X by its ID.
*
* @param AccountType $type
*
* @return Account|null
*/
private function findById(AccountType $type): ?Account
{
if (3 === \count($this->accountId)) {
Log::debug(sprintf('Finding account of type %d and ID %d', $type->id, $this->accountId['value']));
/** @var Account $account */
$account = $this->user->accounts()
->where('id', '!=', $this->forbiddenAccountId)
->where('account_type_id', $type->id)
->where('id', $this->accountId['value'])
->first();
if (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.');
}
return null;
}
/**
* Find account by account type and name.
*
* @param AccountType $type
*
* @return Account|null
*/
private function findByName(AccountType $type): ?Account
{
// Three: find by name (and type):
if (3 === \count($this->accountName)) {
$accounts = $this->repository->getAccountsByType([$type->type]);
$name = $this->accountName['value'];
Log::debug(sprintf('Finding account of type %d and name %s', $type->id, $name));
$filtered = $accounts->filter(
function (Account $account) use ($name) {
if ($account->name === $name && $account->id !== $this->forbiddenAccountId) {
Log::debug(sprintf('Found unmapped %s account by name (#%d): %s', $this->expectedType, $account->id, $account->name));
return $account;
}
return null;
}
);
if (1 === $filtered->count()) {
return $filtered->first();
}
Log::debug('Found nothing.');
}
return null;
}
/**
* Determin account type to find, then use fields in object to try and find it.
*
* @return Account|null
*/
private function findExistingObject(): ?Account
{
Log::debug('In findExistingObject() for Account');
/** @var AccountType $accountType */
$accountType = $this->repository->getAccountType($this->expectedType);
$result = $this->findById($accountType);
if (null !== $result) {
return $result;
}
$result = $this->findByIBAN($accountType);
if (null !== $result) {
return $result;
}
$result = $this->findByName($accountType);
if (null !== $result) {
return $result;
}
Log::debug('Found NO existing accounts.');
return null;
}
/**
* @return Account|null
*/
private function findMappedObject(): ?Account
{
Log::debug('In findMappedObject() for Account');
$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 (null !== $mapped) {
Log::debug(sprintf('Found account #%d!', $mapped->id));
return $mapped;
}
}
Log::debug('Found no account on mapped data or no map present.');
return null;
}
/**
* @param array $array
*
* @return Account|null
*/
private function getMappedObject(array $array): ?Account
{
Log::debug('In getMappedObject() for Account');
if (0 === \count($array)) {
Log::debug('Array is empty, nothing will come of this.');
return null;
}
if (array_key_exists('mapped', $array) && null === $array['mapped']) {
Log::debug(sprintf('No map present for value "%s". Return NULL.', $array['value']));
return null;
}
Log::debug('Finding a mapped account based on', $array);
$search = (int)($array['mapped'] ?? 0.0);
$account = $this->repository->findNull($search);
if (null === $account) {
Log::error(sprintf('There is no account with id #%d. Invalid mapping will be ignored!', $search));
return null;
}
// must be of the same type
// except when mapped is an asset, then it's fair game.
// which only shows that user must map very carefully.
if ($account->accountType->type !== $this->expectedType && AccountType::ASSET !== $account->accountType->type) {
Log::error(
sprintf(
'Mapped account #%d is of type "%s" but we expect a "%s"-account. Mapping will be ignored.',
$account->id,
$account->accountType->type,
$this->expectedType
)
);
return null;
}
Log::debug(sprintf('Found account! #%d ("%s"). Return it', $account->id, $account->name));
return $account;
}
/**
* @return bool
*
* @throws FireflyException
*/
private function store(): bool
{
if (null === $this->user) {
throw new FireflyException('ImportAccount cannot continue without user.');
}
if ((null === $this->defaultAccountId || 0 === (int)$this->defaultAccountId) && AccountType::ASSET === $this->expectedType) {
throw new FireflyException('ImportAccount cannot continue without a default account to fall back on.');
}
// 1: find mapped object:
$mapped = $this->findMappedObject();
if (null !== $mapped) {
$this->account = $mapped;
return true;
}
// 2: find existing by given values:
$found = $this->findExistingObject();
if (null !== $found) {
$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 (null !== $found) {
Log::debug('Found asset account!');
$this->account = $found;
return true;
}
$this->expectedType = $oldExpectedType;
// 4: if search for an asset account, fall back to given "default account" (mandatory)
if (AccountType::ASSET === $this->expectedType) {
$this->account = $this->repository->findNull($this->defaultAccountId);
Log::debug(sprintf('Fall back to default account #%d "%s"', $this->account->id, $this->account->name));
return true;
}
// 5: then maybe, create one:
Log::debug(sprintf('Found no account of type %s so must create one ourselves.', $this->expectedType));
// make sure name field is sensible.
$name = '(no name)';
if (isset($this->accountNumber['value'])) {
$name = $this->accountNumber['value'];
}
if (isset($this->accountIban['value'])) {
$name = $this->accountIban['value'];
}
if (isset($this->accountName['value'])) {
$name = $this->accountName['value'];
}
$data = [
'accountType' => config('firefly.shortNamesByFullName.' . $this->expectedType),
'name' => $name,
'iban' => $this->accountIban['value'] ?? null,
'active' => true,
'accountNumber' => $this->accountNumber['value'] ?? null,
'virtualBalance' => '0',
'account_type_id' => null,
'BIC' => $this->accountBic['value'] ?? 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

@@ -1,290 +0,0 @@
<?php
/**
* ImportBill.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Object;
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\User;
use Log;
use Steam;
/**
* @deprecated
* @codeCoverageIgnore
* Class ImportBill.
*/
class ImportBill
{
/** @var string */
private $amount = '1';
/** @var Bill */
private $bill;
/** @var array */
private $id = [];
/** @var array */
private $name = [];
/** @var BillRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* ImportBill constructor.
*/
public function __construct()
{
$this->repository = app(BillRepositoryInterface::class);
Log::debug('Created ImportBill.');
}
/**
* @return Bill|null
*/
public function getBill(): ?Bill
{
if (null === $this->bill) {
$this->store();
}
return $this->bill;
}
/**
* @param string $amount
*/
public function setAmount(string $amount)
{
$this->amount = Steam::positive($amount);
}
/**
* @param array $id
*/
public function setId(array $id)
{
$this->id = $id;
}
/**
* @param array $name
*/
public function setName(array $name)
{
$this->name = $name;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
/**
* @return Bill|null
*/
private function findById(): ?Bill
{
if (3 === \count($this->id)) {
Log::debug(sprintf('Finding bill with ID #%d', $this->id['value']));
/** @var Bill $bill */
$bill = $this->repository->find((int)$this->id['value']);
if (null !== $bill) {
Log::debug(sprintf('Found unmapped bill by ID (#%d): %s', $bill->id, $bill->name));
return $bill;
}
Log::debug('Found nothing.');
}
return null;
}
/**
* @return Bill|null
*/
private function findByName(): ?Bill
{
if (3 === \count($this->name)) {
$bills = $this->repository->getBills();
$name = $this->name['value'];
Log::debug(sprintf('Finding bill with name %s', $name));
$filtered = $bills->filter(
function (Bill $bill) use ($name) {
if ($bill->name === $name) {
Log::debug(sprintf('Found unmapped bill by name (#%d): %s', $bill->id, $bill->name));
return $bill;
}
return null;
}
);
if (1 === $filtered->count()) {
return $filtered->first();
}
Log::debug('Found nothing.');
}
return null;
}
/**
* @return Bill|null
*/
private function findExistingObject(): ?Bill
{
Log::debug('In findExistingObject() for Bill');
$result = $this->findById();
if (null !== $result) {
return $result;
}
$result = $this->findByName();
if (null !== $result) {
return $result;
}
Log::debug('Found NO existing bills.');
return null;
}
/**
* @return Bill|null
*/
private function findMappedObject(): ?Bill
{
Log::debug('In findMappedObject() for Bill');
$fields = ['id', 'name'];
foreach ($fields as $field) {
$array = $this->$field;
Log::debug(sprintf('Find mapped bill based on field "%s" with value', $field), $array);
// check if a pre-mapped object exists.
$mapped = $this->getMappedObject($array);
if (null !== $mapped) {
Log::debug(sprintf('Found bill #%d!', $mapped->id));
return $mapped;
}
}
Log::debug('Found no bill on mapped data or no map present.');
return null;
}
/**
* @param array $array
*
* @return Bill
*/
private function getMappedObject(array $array): ?Bill
{
Log::debug('In getMappedObject() for Bill');
if (0 === \count($array)) {
Log::debug('Array is empty, nothing will come of this.');
return null;
}
if (array_key_exists('mapped', $array) && null === $array['mapped']) {
Log::debug(sprintf('No map present for value "%s". Return NULL.', $array['value']));
return null;
}
Log::debug('Finding a mapped bill based on', $array);
$search = (int)$array['mapped'];
$bill = $this->repository->find($search);
if (null === $bill) {
Log::error(sprintf('There is no bill with id #%d. Invalid mapping will be ignored!', $search));
return null;
}
Log::debug(sprintf('Found bill! #%d ("%s"). Return it', $bill->id, $bill->name));
return $bill;
}
/**
* @return bool
*/
private function store(): bool
{
// 1: find mapped object:
$mapped = $this->findMappedObject();
if (null !== $mapped) {
$this->bill = $mapped;
return true;
}
// 2: find existing by given values:
$found = $this->findExistingObject();
if (null !== $found) {
$this->bill = $found;
return true;
}
$name = $this->name['value'] ?? '';
if (0 === \strlen($name)) {
return true;
}
$data = [
'name' => $name,
'match' => $name,
'amount_min' => bcmul($this->amount, '0.9'),
'amount_max' => bcmul($this->amount, '1.1'),
'user_id' => $this->user->id,
'date' => date('Y-m-d'),
'repeat_freq' => 'monthly',
'skip' => '0',
'transaction_currency_id' => 1,
'automatch' => '0',
'active' => '1',
];
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
if (null !== $currency) {
$data['transaction_currency_id'] = $currency->id;
}
Log::debug('Found no bill so must create one ourselves. Assume default values.', $data);
$result = $this->repository->store($data);
if (null !== $result) {
$this->bill = $result;
Log::debug(sprintf('Successfully stored new bill #%d: %s', $this->bill->id, $this->bill->name));
}
if (null === $result) {
Log::error('Could not store new bill.');
}
return true;
}
}

View File

@@ -1,259 +0,0 @@
<?php
/**
* ImportBudget.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Object;
use FireflyIII\Models\Budget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\User;
use Log;
/**
* @deprecated
* @codeCoverageIgnore
* Class ImportBudget.
*/
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->repository = app(BudgetRepositoryInterface::class);
Log::debug('Created ImportBudget.');
}
/**
* @return Budget|null
*/
public function getBudget(): ?Budget
{
if (null === $this->budget) {
$this->store();
}
return $this->budget;
}
/**
* @param array $id
*/
public function setId(array $id)
{
$this->id = $id;
}
/**
* @param array $name
*/
public function setName(array $name)
{
$this->name = $name;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
/**
* @return Budget|null
*/
private function findById(): ?Budget
{
if (3 === \count($this->id)) {
Log::debug(sprintf('Finding budget with ID #%d', $this->id['value']));
/** @var Budget $budget */
$budget = $this->repository->findNull((int)$this->id['value']);
if (null !== $budget) {
Log::debug(sprintf('Found unmapped budget by ID (#%d): %s', $budget->id, $budget->name));
return $budget;
}
Log::debug('Found nothing.');
}
return null;
}
/**
* @return Budget|null
*/
private function findByName(): ?Budget
{
if (3 === \count($this->name)) {
$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 (1 === $filtered->count()) {
return $filtered->first();
}
Log::debug('Found nothing.');
}
return null;
}
/**
* @return Budget
*/
private function findExistingObject(): ?Budget
{
Log::debug('In findExistingObject() for Budget');
$result = $this->findById();
if (null !== $result) {
return $result;
}
$result = $this->findByName();
if (null !== $result) {
return $result;
}
Log::debug('Found NO existing budgets.');
return null;
}
/**
* @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 (null !== $mapped) {
Log::debug(sprintf('Found budget #%d!', $mapped->id));
return $mapped;
}
}
Log::debug('Found no budget on mapped data or no map present.');
return null;
}
/**
* @param array $array
*
* @return Budget
*/
private function getMappedObject(array $array): ?Budget
{
Log::debug('In getMappedObject() for Budget');
if (0 === \count($array)) {
Log::debug('Array is empty, nothing will come of this.');
return null;
}
if (array_key_exists('mapped', $array) && null === $array['mapped']) {
Log::debug(sprintf('No map present for value "%s". Return NULL.', $array['value']));
return null;
}
Log::debug('Finding a mapped budget based on', $array);
$search = (int)$array['mapped'];
$budget = $this->repository->find($search);
if (null === $budget->id) {
Log::error(sprintf('There is no budget with id #%d. Invalid mapping will be ignored!', $search));
return null;
}
Log::debug(sprintf('Found budget! #%d ("%s"). Return it', $budget->id, $budget->name));
return $budget;
}
/**
* @return bool
*/
private function store(): bool
{
// 1: find mapped object:
$mapped = $this->findMappedObject();
if (null !== $mapped) {
$this->budget = $mapped;
return true;
}
// 2: find existing by given values:
$found = $this->findExistingObject();
if (null !== $found) {
$this->budget = $found;
return true;
}
$name = $this->name['value'] ?? '';
if (0 === \strlen($name)) {
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

@@ -1,260 +0,0 @@
<?php
/**
* ImportCategory.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Object;
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\User;
use Log;
/**
* @deprecated
* @codeCoverageIgnore
* Class ImportCategory
*/
class ImportCategory
{
/** @var Category */
private $category;
/** @var array */
private $id = [];
/** @var array */
private $name = [];
/** @var CategoryRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* ImportCategory constructor.
*/
public function __construct()
{
$this->repository = app(CategoryRepositoryInterface::class);
Log::debug('Created ImportCategory.');
}
/**
* @return null|Category
*/
public function getCategory(): ?Category
{
if (null === $this->category) {
$this->store();
}
return $this->category;
}
/**
* @param array $id
*/
public function setId(array $id)
{
$this->id = $id;
}
/**
* @param array $name
*/
public function setName(array $name)
{
$this->name = $name;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
/**
* Find category by ID.
*
* @return Category|null
*/
private function findById(): ?Category
{
if (3 === \count($this->id)) {
Log::debug(sprintf('Finding category with ID #%d', $this->id['value']));
/** @var Category $category */
$category = $this->repository->findNull((int)$this->id['value']);
if (null !== $category) {
Log::debug(sprintf('Found unmapped category by ID (#%d): %s', $category->id, $category->name));
return $category;
}
Log::debug('Found nothing.');
}
return null;
}
/**
* Find category by name.
*
* @return Category|null
*/
private function findByName(): ?Category
{
if (3 === \count($this->name)) {
$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 (1 === $filtered->count()) {
return $filtered->first();
}
Log::debug('Found nothing.');
}
return null;
}
/**
* @return Category
*/
private function findExistingObject(): ?Category
{
Log::debug('In findExistingObject() for Category');
$result = $this->findById();
if (null !== $result) {
return $result;
}
$result = $this->findByName();
if (null !== $result) {
return $result;
}
Log::debug('Found NO existing categories.');
return null;
}
/**
* @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 (null !== $mapped) {
Log::debug(sprintf('Found category #%d!', $mapped->id));
return $mapped;
}
}
Log::debug('Found no category on mapped data or no map present.');
return null;
}
/**
* @param array $array
*
* @return Category
*/
private function getMappedObject(array $array): ?Category
{
Log::debug('In getMappedObject() for Category');
if (0 === \count($array)) {
Log::debug('Array is empty, nothing will come of this.');
return null;
}
if (array_key_exists('mapped', $array) && null === $array['mapped']) {
Log::debug(sprintf('No map present for value "%s". Return NULL.', $array['value']));
return null;
}
Log::debug('Finding a mapped category based on', $array);
$search = (int)$array['mapped'];
$category = $this->repository->findNull($search);
if (null === $category) {
Log::error(sprintf('There is no category with id #%d. Invalid mapping will be ignored!', $search));
return null;
}
Log::debug(sprintf('Found category! #%d ("%s"). Return it', $category->id, $category->name));
return $category;
}
/**
* @return bool
*/
private function store(): bool
{
// 1: find mapped object:
$mapped = $this->findMappedObject();
if (null !== $mapped) {
$this->category = $mapped;
return true;
}
// 2: find existing by given values:
$found = $this->findExistingObject();
if (null !== $found) {
$this->category = $found;
return true;
}
$name = $this->name['value'] ?? '';
if (0 === \strlen($name)) {
return true;
}
Log::debug('Found no category so must create one ourselves.');
$this->category = $this->repository->store(['name' => $name]);
Log::debug(sprintf('Successfully stored new category #%d: %s', $this->category->id, $this->category->name));
return true;
}
}

View File

@@ -1,233 +0,0 @@
<?php
/**
* ImportCurrency.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Object;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\User;
use Log;
/**
* @deprecated
* @codeCoverageIgnore
* Class ImportCurrency
*/
class ImportCurrency
{
/** @var array */
private $code = [];
/** @var TransactionCurrency */
private $currency;
/** @var array */
private $id = [];
/** @var array */
private $name = [];
/** @var CurrencyRepositoryInterface */
private $repository;
/** @var array */
private $symbol = [];
/** @var User */
private $user;
/**
* ImportCurrency constructor.
*/
public function __construct()
{
$this->repository = app(CurrencyRepositoryInterface::class);
}
/**
* @return TransactionCurrency
*/
public function getTransactionCurrency(): ?TransactionCurrency
{
if (null !== $this->currency) {
return $this->currency;
}
Log::debug('In createCurrency()');
// check if any of them is mapped:
$mapped = $this->findMappedObject();
if (null !== $mapped) {
Log::debug('Mapped existing currency.', ['new' => $mapped->toArray()]);
$this->currency = $mapped;
return $mapped;
}
$searched = $this->findExistingObject();
if (null !== $searched) {
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,
];
if (null === $data['code']) {
Log::debug('Need at least a code to create currency, return nothing.');
return null;
}
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
*/
public function setCode(array $code)
{
$this->code = $code;
}
/**
* @param array $id
*/
public function setId(array $id)
{
$id['value'] = (int)$id['value'];
$this->id = $id;
}
/**
* @param array $name
*/
public function setName(array $name)
{
$this->name = $name;
}
/**
* @param array $symbol
*/
public function setSymbol(array $symbol)
{
$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' => 'findNull',
'code' => 'findByCodeNull',
'symbol' => 'findBySymbolNull',
'name' => 'findByNameNull',
];
foreach ($search as $field => $function) {
$value = $this->$field['value'] ?? null;
if (null !== $value) {
Log::debug(sprintf('Searching for %s using function %s and value %s', $field, $function, $value));
/** @var TransactionCurrency|null $currency */
$currency = $this->repository->$function($value);
if (null !== $currency) {
return $currency;
}
}
}
return null;
}
/**
* @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 (null !== $mapped) {
Log::debug(sprintf('Found currency #%d!', $mapped->id));
return $mapped;
}
}
Log::debug('Found no currency on mapped data or no map present.');
return null;
}
/**
* @param array $array
*
* @return TransactionCurrency
*/
private function getMappedObject(array $array): ?TransactionCurrency
{
Log::debug('In getMappedObject()');
if (0 === \count($array)) {
Log::debug('Array is empty, nothing will come of this.');
return null;
}
if (array_key_exists('mapped', $array) && null === $array['mapped']) {
Log::debug(sprintf('No map present for value "%s". Return NULL.', $array['value']));
return null;
}
Log::debug('Finding a mapped object based on', $array);
$search = (int)$array['mapped'];
$currency = $this->repository->findNull($search);
if (null === $currency) {
Log::error(sprintf('There is no currency with id #%d. Invalid mapping will be ignored!', $search));
return null;
}
Log::debug(sprintf('Found currency! #%d ("%s"). Return it', $currency->id, $currency->name));
return $currency;
}
}

View File

@@ -1,491 +0,0 @@
<?php
/**
* ImportJournal.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Object;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Converter\Amount;
use FireflyIII\Import\Converter\ConverterInterface;
use FireflyIII\Import\MapperPreProcess\PreProcessorInterface;
use FireflyIII\User;
use InvalidArgumentException;
use Log;
use Steam;
/**
* @deprecated
* @codeCoverageIgnore
* Class ImportJournal.
*/
class ImportJournal
{
/** @var ImportAccount */
public $asset;
/** @var ImportBill */
public $bill;
/** @var ImportBudget */
public $budget;
/** @var ImportCategory */
public $category;
/** @var ImportCurrency */
public $currency;
/** @var string */
public $description = '';
/** @var ImportCurrency */
public $foreignCurrency;
/** @var string */
public $hash;
/** @var array */
public $metaDates = [];
/** @var array */
public $metaFields = [];
/** @var string */
public $notes = '';
/** @var ImportAccount */
public $opposing;
/** @var array */
public $tags = [];
/** @var array */
private $amount;
/** @var array */
private $amountCredit;
/** @var array */
private $amountDebit;
/** @var string */
private $convertedAmount;
/** @var string */
private $date = '';
/** @var string */
private $externalId = '';
/** @var array */
private $foreignAmount;
/** @var array */
private $modifiers = [];
/** @var User */
private $user;
/**
* ImportEntry constructor.
*/
public function __construct()
{
$this->asset = new ImportAccount;
$this->opposing = new ImportAccount;
$this->bill = new ImportBill;
$this->category = new ImportCategory;
$this->budget = new ImportBudget;
$this->currency = new ImportCurrency;
$this->foreignCurrency = new ImportCurrency;
}
/**
* @param array $modifier
*/
public function addToModifier(array $modifier)
{
$this->modifiers[] = $modifier;
}
/**
* @return string
*
* @throws FireflyException
*/
public function getAmount(): string
{
Log::debug('Now in getAmount()');
Log::debug(sprintf('amount is %s', var_export($this->amount, true)));
Log::debug(sprintf('debit amount is %s', var_export($this->amountDebit, true)));
Log::debug(sprintf('credit amount is %s', var_export($this->amountCredit, true)));
if (null === $this->convertedAmount) {
$this->calculateAmount();
}
Log::debug(sprintf('convertedAmount is: "%s"', $this->convertedAmount));
if (0 === bccomp($this->convertedAmount, '0')) {
throw new FireflyException('Amount is zero.');
}
return $this->convertedAmount;
}
/**
* @param string $format
*
* @return Carbon
*/
public function getDate(string $format): Carbon
{
$date = new Carbon;
try {
$date = Carbon::createFromFormat($format, $this->date);
} catch (InvalidArgumentException $e) {
// don't care, just log.
Log::error(sprintf('Import journal cannot parse date "%s" from value "%s" so will return current date instead.', $format, $this->date));
}
return $date;
}
/**
* @return string
*/
public function getDescription(): string
{
if ('' === $this->description) {
return '(no description)';
}
return $this->description;
}
/**
* @return string
*/
public function getExternalId(): string
{
return $this->externalId;
}
/**
* @return string|null
*/
public function getForeignAmount(): ?string
{
Log::debug('Now in getForeignAmount()');
Log::debug(sprintf('foreign amount is %s', var_export($this->foreignAmount, true)));
// no foreign amount? return null
if (null === $this->foreignAmount) {
Log::debug('Return NULL for foreign amount');
return null;
}
// converter is default amount converter: no special stuff
$converter = app(Amount::class);
$amount = $converter->convert($this->foreignAmount['value']);
Log::debug(sprintf('First attempt to convert foreign gives "%s"', $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);
Log::debug(sprintf('Now launching converter %s', $class));
if ($converter->convert($modifier['value']) === -1) {
$amount = Steam::negative($amount);
}
Log::debug(sprintf('Foreign amount after conversion is %s', $amount));
}
Log::debug(sprintf('After modifiers the result is: "%s"', $amount));
Log::debug(sprintf('converted foreign amount is: "%s"', $amount));
if (0 === bccomp($amount, '0')) {
return null;
}
return $amount;
}
/**
* Get date field or NULL
*
* @param string $field
*
* @return Carbon|null
*/
public function getMetaDate(string $field): ?Carbon
{
if (isset($this->metaDates[$field])) {
return new Carbon($this->metaDates[$field]);
}
return null;
}
/**
* Get string field or NULL
*
* @param string $field
*
* @return string|null
*/
public function getMetaString(string $field): ?string
{
if (isset($this->metaFields[$field]) && \strlen($this->metaFields[$field]) > 0) {
return (string)$this->metaFields[$field];
}
return null;
}
/**
* @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);
$this->bill->setUser($user);
}
/**
* @param array $array
*
* @throws FireflyException
*/
public function setValue(array $array)
{
$array['mapped'] = $array['mapped'] ?? null;
$array['value'] = $array['value'] ?? null;
switch ($array['role']) {
default:
throw new FireflyException(sprintf('ImportJournal cannot handle "%s" with value "%s".', $array['role'], $array['value']));
case 'account-id':
$this->asset->setAccountId($array);
break;
case 'sepa-cc':
case 'sepa-ct-op':
case 'sepa-ct-id':
case 'sepa-db':
case 'sepa-country':
case 'sepa-ep':
case 'sepa-ci':
$value = trim((string)$array['value']);
if (\strlen($value) > 0) {
$this->metaFields[$array['role']] = $value;
}
break;
case 'amount':
$this->amount = $array;
break;
case 'amount_foreign':
$this->foreignAmount = $array;
break;
case 'foreign-currency-code':
$this->foreignCurrency->setCode($array);
break;
case 'amount_debit':
$this->amountDebit = $array;
break;
case 'amount_credit':
$this->amountCredit = $array;
break;
case 'account-iban':
$this->asset->setAccountIban($array);
break;
case 'account-name':
$this->asset->setAccountName($array);
break;
case 'account-number':
$this->asset->setAccountNumber($array);
break;
case 'bill-id':
$this->bill->setId($array);
break;
case 'bill-name':
$this->bill->setName($array);
break;
case 'budget-id':
$this->budget->setId($array);
break;
case 'budget-name':
$this->budget->setName($array);
break;
case 'category-id':
$this->category->setId($array);
break;
case 'category-name':
$this->category->setName($array);
break;
case 'currency-code':
$this->currency->setCode($array);
break;
case 'currency-id':
$this->currency->setId($array);
break;
case 'currency-name':
$this->currency->setName($array);
break;
case 'currency-symbol':
$this->currency->setSymbol($array);
break;
case 'date-transaction':
$this->date = $array['value'];
break;
case 'description':
$this->description .= $array['value'];
break;
case 'note':
$this->notes .= ' ' . $array['value'];
$this->notes = trim($this->notes);
break;
case 'external-id':
$this->externalId = $array['value'];
break;
case 'internal-reference':
$this->metaFields['internal_reference'] = $array['value'];
break;
case '_ignore':
break;
case 'ing-debit-credit':
case 'rabo-debit-credit':
$this->addToModifier($array);
break;
case 'opposing-iban':
$this->opposing->setAccountIban($array);
break;
case 'opposing-name':
$this->opposing->setAccountName($array);
break;
case 'opposing-number':
$this->opposing->setAccountNumber($array);
break;
case 'opposing-id':
$this->opposing->setAccountId($array);
break;
case 'opposing-bic':
$this->opposing->setAccountBic($array);
break;
case 'tags-comma':
case 'tags-space':
$this->setTags($array);
break;
case 'date-interest':
$this->metaDates['interest_date'] = $array['value'];
break;
case 'date-book':
$this->metaDates['book_date'] = $array['value'];
break;
case 'date-process':
$this->metaDates['process_date'] = $array['value'];
break;
case 'date-due':
$this->metaDates['due_date'] = $array['value'];
break;
case 'date-payment':
$this->metaDates['payment_date'] = $array['value'];
break;
case 'date-invoice':
$this->metaDates['invoice_date'] = $array['value'];
break;
}
}
/**
* If convertedAmount is NULL, this method will try to calculate the correct amount.
* It starts with amount, but can be overruled by debit and credit amounts.
*
* @throws FireflyException
*/
private function calculateAmount()
{
// first check if the amount is set:
Log::debug('convertedAmount is NULL');
$info = $this->selectAmountInput();
if (0 === \count($info)) {
throw new FireflyException('No amount information for this row.');
}
$class = $info['class'] ?? '';
if (0 === \strlen($class)) {
throw new FireflyException('No amount information (conversion class) for this row.');
}
Log::debug(sprintf('Converter class is %s', $info['class']));
/** @var ConverterInterface $amountConverter */
$amountConverter = app($info['class']);
$this->convertedAmount = $amountConverter->convert($info['value']);
Log::debug(sprintf('First attempt to convert gives "%s"', $this->convertedAmount));
// 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);
Log::debug(sprintf('Now launching converter %s', $class));
if ($converter->convert($modifier['value']) === -1) {
$this->convertedAmount = Steam::negative($this->convertedAmount);
}
Log::debug(sprintf('convertedAmount after conversion is %s', $this->convertedAmount));
}
Log::debug(sprintf('After modifiers the result is: "%s"', $this->convertedAmount));
}
/**
* This methods decides which input to use for the amount calculation.
*
* @return array
*/
private function selectAmountInput()
{
$info = [];
$converterClass = '';
if (null !== $this->amount) {
Log::debug('Amount value is not NULL, assume this is the correct value.');
$converterClass = sprintf('FireflyIII\Import\Converter\%s', config(sprintf('csv.import_roles.%s.converter', $this->amount['role'])));
$info = $this->amount;
}
if (null !== $this->amountDebit) {
Log::debug('Amount DEBIT value is not NULL, assume this is the correct value (overrules Amount).');
$converterClass = sprintf('FireflyIII\Import\Converter\%s', config(sprintf('csv.import_roles.%s.converter', $this->amountDebit['role'])));
$info = $this->amountDebit;
}
if (null !== $this->amountCredit) {
Log::debug('Amount CREDIT value is not NULL, assume this is the correct value (overrules Amount and AmountDebit).');
$converterClass = sprintf('FireflyIII\Import\Converter\%s', config(sprintf('csv.import_roles.%s.converter', $this->amountCredit['role'])));
$info = $this->amountCredit;
}
$info['class'] = $converterClass;
return $info;
}
/**
* @param array $array
*/
private function setTags(array $array): void
{
$preProcessorClass = config(sprintf('csv.import_roles.%s.pre-process-mapper', $array['role']));
/** @var PreProcessorInterface $preProcessor */
$preProcessor = app(sprintf('\FireflyIII\Import\MapperPreProcess\%s', $preProcessorClass));
$tags = $preProcessor->run($array['value']);
$this->tags = array_merge($this->tags, $tags);
}
}