mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-05-04 05:06:37 +00:00
new language strings and updated code
This commit is contained in:
@@ -23,13 +23,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Services\Internal\Support;
|
||||
|
||||
use Exception;
|
||||
use FireflyIII\Factory\AccountFactory;
|
||||
use FireflyIII\Factory\AccountMetaFactory;
|
||||
use FireflyIII\Factory\TransactionFactory;
|
||||
use FireflyIII\Factory\TransactionJournalFactory;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountMeta;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\Transaction;
|
||||
@@ -128,6 +126,7 @@ trait AccountServiceTrait
|
||||
*
|
||||
* @return TransactionJournal|null
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function storeIBJournal(Account $account, array $data): ?TransactionJournal
|
||||
{
|
||||
@@ -181,7 +180,7 @@ trait AccountServiceTrait
|
||||
/** @var TransactionFactory $factory */
|
||||
$factory = app(TransactionFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
$one = $factory->create(
|
||||
$factory->create(
|
||||
[
|
||||
'account' => $firstAccount,
|
||||
'transaction_journal' => $journal,
|
||||
@@ -193,7 +192,7 @@ trait AccountServiceTrait
|
||||
'reconciled' => false,
|
||||
]
|
||||
);
|
||||
$two = $factory->create(
|
||||
$factory->create(
|
||||
[
|
||||
'account' => $secondAccount,
|
||||
'transaction_journal' => $journal,
|
||||
@@ -205,9 +204,6 @@ trait AccountServiceTrait
|
||||
'reconciled' => false,
|
||||
]
|
||||
);
|
||||
if (null !== $one && null !== $two) {
|
||||
Log::notice(sprintf('Stored two transactions for new account, #%d and #%d', $one->id, $two->id));
|
||||
}
|
||||
|
||||
return $journal;
|
||||
}
|
||||
@@ -267,6 +263,7 @@ trait AccountServiceTrait
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function updateIBJournal(Account $account, TransactionJournal $journal, array $data): bool
|
||||
{
|
||||
@@ -274,7 +271,6 @@ trait AccountServiceTrait
|
||||
$amount = (string)$data['openingBalance'];
|
||||
$negativeAmount = bcmul($amount, '-1');
|
||||
$currencyId = (int)$data['currency_id'];
|
||||
|
||||
Log::debug(sprintf('Submitted amount for opening balance to update is "%s"', $amount));
|
||||
if (0 === bccomp($amount, '0')) {
|
||||
Log::notice(sprintf('Amount "%s" is zero, delete opening balance.', $amount));
|
||||
@@ -282,16 +278,11 @@ trait AccountServiceTrait
|
||||
$service = app(JournalDestroyService::class);
|
||||
$service->destroy($journal);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// update date:
|
||||
$journal->date = $date;
|
||||
$journal->transaction_currency_id = $currencyId;
|
||||
$journal->save();
|
||||
|
||||
// update transactions:
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($journal->transactions()->get() as $transaction) {
|
||||
if ((int)$account->id === (int)$transaction->account_id) {
|
||||
@@ -317,6 +308,7 @@ trait AccountServiceTrait
|
||||
*
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function updateMetaData(Account $account, array $data): void
|
||||
{
|
||||
@@ -331,33 +323,7 @@ trait AccountServiceTrait
|
||||
/** @var AccountMetaFactory $factory */
|
||||
$factory = app(AccountMetaFactory::class);
|
||||
foreach ($fields as $field) {
|
||||
/** @var AccountMeta $entry */
|
||||
$entry = $account->accountMeta()->where('name', $field)->first();
|
||||
|
||||
// must not be an empty string:
|
||||
if (isset($data[$field]) && \strlen((string)$data[$field]) > 0) {
|
||||
|
||||
// if $data has field and $entry is null, create new one:
|
||||
if (null === $entry) {
|
||||
Log::debug(sprintf('Created meta-field "%s":"%s" for account #%d ("%s") ', $field, $data[$field], $account->id, $account->name));
|
||||
$factory->create(['account_id' => $account->id, 'name' => $field, 'data' => $data[$field],]);
|
||||
}
|
||||
|
||||
// if $data has field and $entry is not null, update $entry:
|
||||
// let's not bother with a service.
|
||||
if (null !== $entry) {
|
||||
$entry->data = $data[$field];
|
||||
$entry->save();
|
||||
Log::debug(sprintf('Updated meta-field "%s":"%s" for #%d ("%s") ', $field, $data[$field], $account->id, $account->name));
|
||||
}
|
||||
}
|
||||
if (null !== $entry && isset($data[$field]) && '' === (string)$data[$field]) {
|
||||
try {
|
||||
$entry->delete();
|
||||
} catch (Exception $e) {
|
||||
Log::debug(sprintf('Could not delete entry: %s', $e->getMessage()));
|
||||
}
|
||||
}
|
||||
$factory->crud($account, $field, $data[$field] ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,8 +38,11 @@ trait JournalServiceTrait
|
||||
{
|
||||
|
||||
/**
|
||||
* Link tags to journal.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $data
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function connectTags(TransactionJournal $journal, array $data): void
|
||||
{
|
||||
|
||||
@@ -45,15 +45,6 @@ use Log;
|
||||
*/
|
||||
trait RecurringTransactionTrait
|
||||
{
|
||||
/**
|
||||
* @param null|string $expectedType
|
||||
* @param int|null $accountId
|
||||
* @param null|string $accountName
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
abstract public function findAccount(?string $expectedType, ?int $accountId, ?string $accountName): ?Account;
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $repetitions
|
||||
@@ -76,8 +67,12 @@ trait RecurringTransactionTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* Store transactions of a recurring transactions. It's complex but readable.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $transactions
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function createTransactions(Recurrence $recurrence, array $transactions): void
|
||||
{
|
||||
@@ -103,9 +98,8 @@ trait RecurringTransactionTrait
|
||||
$factory = app(TransactionCurrencyFactory::class);
|
||||
$currency = $factory->find($array['currency_id'], $array['currency_code']);
|
||||
$foreignCurrency = $factory->find($array['foreign_currency_id'], $array['foreign_currency_code']);
|
||||
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($recurrence->user);
|
||||
if (null === $currency) {
|
||||
$currency = $defaultCurrency;
|
||||
$currency = app('amount')->getDefaultCurrencyByUser($recurrence->user);
|
||||
}
|
||||
$transaction = new RecurrenceTransaction(
|
||||
[
|
||||
@@ -178,6 +172,17 @@ trait RecurringTransactionTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null|string $expectedType
|
||||
* @param int|null $accountId
|
||||
* @param null|string $accountName
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
abstract public function findAccount(?string $expectedType, ?int $accountId, ?string $accountName): ?Account;
|
||||
|
||||
/**
|
||||
* Update meta data for recurring transaction.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $data
|
||||
*/
|
||||
@@ -186,6 +191,22 @@ trait RecurringTransactionTrait
|
||||
// only two special meta fields right now. Let's just hard code them.
|
||||
$piggyId = (int)($data['meta']['piggy_bank_id'] ?? 0.0);
|
||||
$piggyName = $data['meta']['piggy_bank_name'] ?? '';
|
||||
$this->updatePiggyBank($recurrence, $piggyId, $piggyName);
|
||||
|
||||
|
||||
$tags = $data['meta']['tags'] ?? [];
|
||||
$this->updateTags($recurrence, $tags);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param int $piggyId
|
||||
* @param string $piggyName
|
||||
*/
|
||||
protected function updatePiggyBank(Recurrence $recurrence, int $piggyId, string $piggyName): void
|
||||
{
|
||||
|
||||
/** @var PiggyBankFactory $factory */
|
||||
$factory = app(PiggyBankFactory::class);
|
||||
$factory->setUser($recurrence->user);
|
||||
@@ -203,9 +224,14 @@ trait RecurringTransactionTrait
|
||||
// delete if present
|
||||
$recurrence->recurrenceMeta()->where('name', 'piggy_bank_id')->delete();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$tags = $data['meta']['tags'] ?? [];
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $tagst
|
||||
*/
|
||||
protected function updateTags(Recurrence $recurrence, array $tags): void
|
||||
{
|
||||
if (\count($tags) > 0) {
|
||||
/** @var RecurrenceMeta $entry */
|
||||
$entry = $recurrence->recurrenceMeta()->where('name', 'tags')->first();
|
||||
|
||||
@@ -51,47 +51,30 @@ trait TransactionServiceTrait
|
||||
* @param string $direction
|
||||
*
|
||||
* @return string|null
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function accountType(TransactionJournal $journal, string $direction): ?string
|
||||
{
|
||||
$types = [];
|
||||
$type = $journal->transactionType->type;
|
||||
switch ($type) {
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
Log::error(sprintf('Cannot handle type "%s" in accountType()', $type));
|
||||
|
||||
return null;
|
||||
// @codeCoverageIgnoreEnd
|
||||
case TransactionType::WITHDRAWAL:
|
||||
$types['source'] = AccountType::ASSET;
|
||||
$types['destination'] = AccountType::EXPENSE;
|
||||
break;
|
||||
case TransactionType::DEPOSIT:
|
||||
$types['source'] = AccountType::REVENUE;
|
||||
$types['destination'] = AccountType::ASSET;
|
||||
break;
|
||||
case TransactionType::TRANSFER:
|
||||
$types['source'] = AccountType::ASSET;
|
||||
$types['destination'] = AccountType::ASSET;
|
||||
break;
|
||||
case TransactionType::RECONCILIATION:
|
||||
// always NULL, since this is handled by the reconciliation.
|
||||
$types['source'] = null;
|
||||
$types['destination'] = null;
|
||||
|
||||
// return here:
|
||||
return $types[$direction];
|
||||
if (TransactionType::WITHDRAWAL === $type) {
|
||||
$types['source'] = AccountType::ASSET;
|
||||
$types['destination'] = AccountType::EXPENSE;
|
||||
}
|
||||
if (!isset($types[$direction])) {
|
||||
// @codeCoverageIgnoreStart
|
||||
Log::error(sprintf('No type set for direction "%s" and type "%s"', $type, $direction));
|
||||
|
||||
return null;
|
||||
// @codeCoverageIgnoreEnd
|
||||
if (TransactionType::DEPOSIT === $type) {
|
||||
$types['source'] = AccountType::REVENUE;
|
||||
$types['destination'] = AccountType::ASSET;
|
||||
}
|
||||
if (TransactionType::TRANSFER === $type) {
|
||||
$types['source'] = AccountType::ASSET;
|
||||
$types['destination'] = AccountType::ASSET;
|
||||
}
|
||||
if (TransactionType::RECONCILIATION === $type) {
|
||||
$types['source'] = null;
|
||||
$types['destination'] = null;
|
||||
}
|
||||
|
||||
return $types[$direction];
|
||||
return $types[$direction] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,6 +84,7 @@ trait TransactionServiceTrait
|
||||
*
|
||||
* @return Account|null
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function findAccount(?string $expectedType, ?int $accountId, ?string $accountName): ?Account
|
||||
{
|
||||
@@ -115,55 +99,23 @@ trait TransactionServiceTrait
|
||||
return $repository->findNull($accountId);
|
||||
}
|
||||
|
||||
switch ($expectedType) {
|
||||
case AccountType::ASSET:
|
||||
if ($accountId > 0) {
|
||||
// must be able to find it based on ID. Validator should catch invalid ID's.
|
||||
return $repository->findNull($accountId);
|
||||
}
|
||||
|
||||
// alternatively, return by name. Validator should catch invalid names.
|
||||
return $repository->findByName($accountName, [AccountType::ASSET]);
|
||||
case AccountType::EXPENSE:
|
||||
if ($accountId > 0) {
|
||||
// must be able to find it based on ID. Validator should catch invalid ID's.
|
||||
return $repository->findNull($accountId);
|
||||
}
|
||||
if (\strlen($accountName) > 0) {
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($this->user);
|
||||
|
||||
return $factory->findOrCreate($accountName, AccountType::EXPENSE);
|
||||
}
|
||||
|
||||
// return cash account:
|
||||
return $repository->getCashAccount();
|
||||
case AccountType::REVENUE:
|
||||
if ($accountId > 0) {
|
||||
// must be able to find it based on ID. Validator should catch invalid ID's.
|
||||
return $repository->findNull($accountId);
|
||||
}
|
||||
if (\strlen($accountName) > 0) {
|
||||
// alternatively, return by name.
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($this->user);
|
||||
|
||||
return $factory->findOrCreate($accountName, AccountType::REVENUE);
|
||||
}
|
||||
|
||||
// return cash account:
|
||||
return $repository->getCashAccount();
|
||||
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
Log::error(sprintf('Cannot find account of type "%s".', $expectedType));
|
||||
|
||||
return null;
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
if ($accountId > 0) {
|
||||
// must be able to find it based on ID. Validator should catch invalid ID's.
|
||||
return $repository->findNull($accountId);
|
||||
}
|
||||
if (AccountType::ASSET === $expectedType) {
|
||||
return $repository->findByName($accountName, [AccountType::ASSET]);
|
||||
}
|
||||
// for revenue and expense:
|
||||
if (\strlen($accountName) > 0) {
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($this->user);
|
||||
|
||||
return $factory->findOrCreate($accountName, $expectedType);
|
||||
}
|
||||
|
||||
return $repository->getCashAccount();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user