Various code cleanup and fixed alignments.

This commit is contained in:
James Cole
2024-01-01 14:43:56 +01:00
parent 1368aafe5f
commit f963ac63f1
443 changed files with 3668 additions and 3672 deletions

View File

@@ -94,14 +94,14 @@ trait AccountServiceTrait
*/
public function updateMetaData(Account $account, array $data): void
{
$fields = $this->validFields;
$fields = $this->validFields;
if (AccountType::ASSET === $account->accountType->type) {
$fields = $this->validAssetFields;
}
// remove currency_id if necessary.
$type = $account->accountType->type;
$list = config('firefly.valid_currency_account_types');
$type = $account->accountType->type;
$list = config('firefly.valid_currency_account_types');
if (!in_array($type, $list, true)) {
$pos = array_search('currency_id', $fields, true);
if (false !== $pos) {
@@ -150,7 +150,7 @@ trait AccountServiceTrait
public function updateNote(Account $account, string $note): bool
{
$dbNote = $account->notes()->first();
$dbNote = $account->notes()->first();
if ('' === $note) {
if (null !== $dbNote) {
$dbNote->delete();
@@ -196,7 +196,7 @@ trait AccountServiceTrait
protected function createOBGroup(Account $account, array $data): TransactionGroup
{
app('log')->debug('Now going to create an OB group.');
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
if (is_array($language)) {
$language = 'en_US';
}
@@ -227,10 +227,10 @@ trait AccountServiceTrait
}
// make amount positive, regardless:
$amount = app('steam')->positive($amount);
$amount = app('steam')->positive($amount);
// get or grab currency:
$currency = $this->accountRepository->getAccountCurrency($account);
$currency = $this->accountRepository->getAccountCurrency($account);
if (null === $currency) {
$currency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
@@ -268,7 +268,7 @@ trait AccountServiceTrait
app('log')->debug('Going for submission in createOBGroup', $submission);
/** @var TransactionGroupFactory $factory */
$factory = app(TransactionGroupFactory::class);
$factory = app(TransactionGroupFactory::class);
$factory->setUser($account->user);
try {
@@ -343,10 +343,10 @@ trait AccountServiceTrait
{
// find currency, or use default currency instead.
/** @var TransactionCurrencyFactory $factory */
$factory = app(TransactionCurrencyFactory::class);
$factory = app(TransactionCurrencyFactory::class);
/** @var null|TransactionCurrency $currency */
$currency = $factory->find($currencyId, $currencyCode);
$currency = $factory->find($currencyId, $currencyCode);
if (null === $currency) {
// use default currency:
@@ -374,7 +374,7 @@ trait AccountServiceTrait
}
// if direction is "debit" (i owe this debt), amount is negative.
// which means the liability will have a negative balance which the user must fill.
$openingBalance = app('steam')->negative($openingBalance);
$openingBalance = app('steam')->negative($openingBalance);
// if direction is "credit" (I am owed this debt), amount is positive.
// which means the liability will have a positive balance which is drained when its paid back into any asset.
@@ -383,21 +383,21 @@ trait AccountServiceTrait
}
// create if not exists:
$clGroup = $this->getCreditTransaction($account);
$clGroup = $this->getCreditTransaction($account);
if (null === $clGroup) {
return $this->createCreditTransaction($account, $openingBalance, $openingBalanceDate);
}
// if exists, update:
$currency = $this->accountRepository->getAccountCurrency($account);
$currency = $this->accountRepository->getAccountCurrency($account);
if (null === $currency) {
$currency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
// simply grab the first journal and change it:
$journal = $this->getObJournal($clGroup);
$clTransaction = $this->getOBTransaction($journal, $account);
$accountTransaction = $this->getNotOBTransaction($journal, $account);
$journal->date = $openingBalanceDate;
$journal = $this->getObJournal($clGroup);
$clTransaction = $this->getOBTransaction($journal, $account);
$accountTransaction = $this->getNotOBTransaction($journal, $account);
$journal->date = $openingBalanceDate;
$journal->transactionCurrency()->associate($currency);
// account always gains money:
@@ -405,8 +405,8 @@ trait AccountServiceTrait
$accountTransaction->transaction_currency_id = $currency->id;
// CL account always loses money:
$clTransaction->amount = app('steam')->negative($openingBalance);
$clTransaction->transaction_currency_id = $currency->id;
$clTransaction->amount = app('steam')->negative($openingBalance);
$clTransaction->transaction_currency_id = $currency->id;
// save both
$accountTransaction->save();
$clTransaction->save();
@@ -429,11 +429,11 @@ trait AccountServiceTrait
throw new FireflyException('Amount for new liability credit was unexpectedly 0.');
}
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
if (is_array($language)) {
$language = 'en_US';
}
$language = (string)$language;
$language = (string)$language;
// set source and/or destination based on whether the amount is positive or negative.
// first, assume the amount is positive and go from there:
@@ -451,10 +451,10 @@ trait AccountServiceTrait
}
// amount must be positive for the transaction to work.
$amount = app('steam')->positive($openingBalance);
$amount = app('steam')->positive($openingBalance);
// get or grab currency:
$currency = $this->accountRepository->getAccountCurrency($account);
$currency = $this->accountRepository->getAccountCurrency($account);
if (null === $currency) {
$currency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
@@ -492,7 +492,7 @@ trait AccountServiceTrait
app('log')->debug('Going for submission in createCreditTransaction', $submission);
/** @var TransactionGroupFactory $factory */
$factory = app(TransactionGroupFactory::class);
$factory = app(TransactionGroupFactory::class);
$factory->setUser($account->user);
try {
@@ -517,14 +517,14 @@ trait AccountServiceTrait
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
// create if not exists:
$obGroup = $this->getOBGroup($account);
$obGroup = $this->getOBGroup($account);
if (null === $obGroup) {
return $this->createOBGroupV2($account, $openingBalance, $openingBalanceDate);
}
app('log')->debug('Update OB group');
// if exists, update:
$currency = $this->accountRepository->getAccountCurrency($account);
$currency = $this->accountRepository->getAccountCurrency($account);
if (null === $currency) {
$currency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
@@ -544,8 +544,8 @@ trait AccountServiceTrait
$accountTransaction->transaction_currency_id = $currency->id;
// OB account transaction gains money
$obTransaction->amount = app('steam')->positive($openingBalance);
$obTransaction->transaction_currency_id = $currency->id;
$obTransaction->amount = app('steam')->positive($openingBalance);
$obTransaction->transaction_currency_id = $currency->id;
}
if (-1 === bccomp('0', $openingBalance)) {
app('log')->debug('Amount is positive.');
@@ -554,8 +554,8 @@ trait AccountServiceTrait
$accountTransaction->transaction_currency_id = $currency->id;
// OB account loses money:
$obTransaction->amount = app('steam')->negative($openingBalance);
$obTransaction->transaction_currency_id = $currency->id;
$obTransaction->amount = app('steam')->negative($openingBalance);
$obTransaction->transaction_currency_id = $currency->id;
}
// save both
$accountTransaction->save();
@@ -572,7 +572,7 @@ trait AccountServiceTrait
protected function createOBGroupV2(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
{
app('log')->debug('Now going to create an OB group.');
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
if (is_array($language)) {
$language = 'en_US';
}
@@ -602,10 +602,10 @@ trait AccountServiceTrait
}
// make amount positive, regardless:
$amount = app('steam')->positive($openingBalance);
$amount = app('steam')->positive($openingBalance);
// get or grab currency:
$currency = $this->accountRepository->getAccountCurrency($account);
$currency = $this->accountRepository->getAccountCurrency($account);
if (null === $currency) {
$currency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
@@ -643,7 +643,7 @@ trait AccountServiceTrait
app('log')->debug('Going for submission in createOBGroupV2', $submission);
/** @var TransactionGroupFactory $factory */
$factory = app(TransactionGroupFactory::class);
$factory = app(TransactionGroupFactory::class);
$factory->setUser($account->user);
try {

View File

@@ -61,7 +61,7 @@ trait BillServiceTrait
return true;
}
$dbNote = $bill->notes()->first();
$dbNote = $bill->notes()->first();
if (null === $dbNote) {
$dbNote = new Note();
$dbNote->noteable()->associate($bill);

View File

@@ -104,7 +104,7 @@ class CreditRecalculateService
$destination = $this->getDestinationAccount($journal);
// destination or source must be liability.
$valid = config('firefly.valid_liabilities');
$valid = config('firefly.valid_liabilities');
if (in_array($destination->accountType->type, $valid, true)) {
$this->work[] = $destination;
}
@@ -127,7 +127,7 @@ class CreditRecalculateService
private function getAccountByDirection(TransactionJournal $journal, string $direction): Account
{
/** @var null|Transaction $transaction */
$transaction = $journal->transactions()->where('amount', $direction, '0')->first();
$transaction = $journal->transactions()->where('amount', $direction, '0')->first();
if (null === $transaction) {
throw new FireflyException(sprintf('Cannot find "%s"-transaction of journal #%d', $direction, $journal->id));
}
@@ -179,12 +179,12 @@ class CreditRecalculateService
$this->validateOpeningBalance($account, $openingBalance);
}
}
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0';
$leftOfDebt = app('steam')->positive($startOfDebt);
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0';
$leftOfDebt = app('steam')->positive($startOfDebt);
app('log')->debug(sprintf('Start of debt is "%s", so initial left of debt is "%s"', app('steam')->bcround($startOfDebt, 2), app('steam')->bcround($leftOfDebt, 2)));
/** @var AccountMetaFactory $factory */
$factory = app(AccountMetaFactory::class);
$factory = app(AccountMetaFactory::class);
// amount is positive or negative, doesn't matter.
$factory->crud($account, 'start_of_debt', $startOfDebt);
@@ -192,12 +192,12 @@ class CreditRecalculateService
app('log')->debug(sprintf('Debt direction is "%s"', $direction));
// now loop all transactions (except opening balance and credit thing)
$transactions = $account->transactions()
$transactions = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->orderBy('transaction_journals.date', 'ASC')
->get(['transactions.*'])
;
$total = $transactions->count();
$total = $transactions->count();
app('log')->debug(sprintf('Found %d transaction(s) to process.', $total));
/** @var Transaction $transaction */
@@ -218,7 +218,7 @@ class CreditRecalculateService
$source = $openingBalance->transactions()->where('amount', '<', 0)->first();
/** @var Transaction $dest */
$dest = $openingBalance->transactions()->where('amount', '>', 0)->first();
$dest = $openingBalance->transactions()->where('amount', '>', 0)->first();
if ($source->account_id !== $account->id) {
app('log')->info(sprintf('Liability #%d has a reversed opening balance. Will fix this now.', $account->id));
app('log')->debug(sprintf('Source amount "%s" is now "%s"', $source->amount, app('steam')->positive($source->amount)));
@@ -269,10 +269,10 @@ class CreditRecalculateService
}
// amount to use depends on the currency:
$usedAmount = $this->getAmountToUse($transaction, $accountCurrency, $foreignCurrency);
$isSameAccount = $account->id === $transaction->account_id;
$isDebit = 'debit' === $direction;
$isCredit = 'credit' === $direction;
$usedAmount = $this->getAmountToUse($transaction, $accountCurrency, $foreignCurrency);
$isSameAccount = $account->id === $transaction->account_id;
$isDebit = 'debit' === $direction;
$isCredit = 'credit' === $direction;
if ($isSameAccount && $isCredit && $this->isWithdrawalIn($usedAmount, $type)) { // case 1
$usedAmount = app('steam')->positive($usedAmount);

View File

@@ -61,23 +61,23 @@ trait JournalServiceTrait
unset($array);
// and now try to find it, based on the type of transaction.
$message = 'Transaction = %s, %s account should be in: %s. Direction is %s.';
$message = 'Transaction = %s, %s account should be in: %s. Direction is %s.';
app('log')->debug(sprintf($message, $transactionType, $direction, implode(', ', $expectedTypes[$transactionType] ?? ['UNKNOWN']), $direction));
$result = $this->findAccountById($data, $expectedTypes[$transactionType]);
$result = $this->findAccountByIban($result, $data, $expectedTypes[$transactionType]);
$ibanResult = $result;
$result = $this->findAccountByNumber($result, $data, $expectedTypes[$transactionType]);
$numberResult = $result;
$result = $this->findAccountByName($result, $data, $expectedTypes[$transactionType]);
$nameResult = $result;
$result = $this->findAccountById($data, $expectedTypes[$transactionType]);
$result = $this->findAccountByIban($result, $data, $expectedTypes[$transactionType]);
$ibanResult = $result;
$result = $this->findAccountByNumber($result, $data, $expectedTypes[$transactionType]);
$numberResult = $result;
$result = $this->findAccountByName($result, $data, $expectedTypes[$transactionType]);
$nameResult = $result;
// if $result (find by name) is NULL, but IBAN is set, any result of the search by NAME can't overrule
// this account. In such a case, the name search must be retried with a new name.
if (null !== $nameResult && null === $numberResult && null === $ibanResult && '' !== (string)$data['iban'] && '' !== (string)$nameResult->iban) {
$data['name'] = sprintf('%s (%s)', $data['name'], $data['iban']);
app('log')->debug(sprintf('Search again using the new name, "%s".', $data['name']));
$result = $this->findAccountByName(null, $data, $expectedTypes[$transactionType]);
$result = $this->findAccountByName(null, $data, $expectedTypes[$transactionType]);
}
// the account that Firefly III creates must be "creatable", aka select the one we can create from the list just in case

View File

@@ -56,7 +56,7 @@ trait RecurringTransactionTrait
return true;
}
$dbNote = $recurrence->notes()->first();
$dbNote = $recurrence->notes()->first();
if (null === $dbNote) {
$dbNote = new Note();
$dbNote->noteable()->associate($recurrence);
@@ -95,10 +95,10 @@ trait RecurringTransactionTrait
app('log')->debug('Now in createTransactions()');
foreach ($transactions as $index => $array) {
app('log')->debug(sprintf('Now at transaction #%d', $index));
$sourceTypes = config(sprintf('firefly.expected_source_types.source.%s', $recurrence->transactionType->type));
$destTypes = config(sprintf('firefly.expected_source_types.destination.%s', $recurrence->transactionType->type));
$source = $this->findAccount($sourceTypes, $array['source_id'], null);
$destination = $this->findAccount($destTypes, $array['destination_id'], null);
$sourceTypes = config(sprintf('firefly.expected_source_types.source.%s', $recurrence->transactionType->type));
$destTypes = config(sprintf('firefly.expected_source_types.destination.%s', $recurrence->transactionType->type));
$source = $this->findAccount($sourceTypes, $array['source_id'], null);
$destination = $this->findAccount($destTypes, $array['destination_id'], null);
/** @var TransactionCurrencyFactory $factory */
$factory = app(TransactionCurrencyFactory::class);
@@ -114,7 +114,7 @@ trait RecurringTransactionTrait
// once the accounts have been determined, we still verify their validity:
/** @var AccountValidator $validator */
$validator = app(AccountValidator::class);
$validator = app(AccountValidator::class);
$validator->setUser($recurrence->user);
$validator->setTransactionType($recurrence->transactionType->type);
@@ -129,7 +129,7 @@ trait RecurringTransactionTrait
unset($array['foreign_amount']);
}
// TODO typeOverrule. The account validator may have a different opinion on the type of the transaction.
$transaction = new RecurrenceTransaction(
$transaction = new RecurrenceTransaction(
[
'recurrence_id' => $recurrence->id,
'transaction_currency_id' => $currency->id,
@@ -166,22 +166,22 @@ trait RecurringTransactionTrait
protected function findAccount(array $expectedTypes, ?int $accountId, ?string $accountName): Account
{
$result = null;
$accountId = (int) $accountId;
$accountName = (string) $accountName;
$result = null;
$accountId = (int) $accountId;
$accountName = (string) $accountName;
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($this->user);
// if user has submitted an account ID, search for it.
$result = $repository->find($accountId);
$result = $repository->find($accountId);
if (null !== $result) {
return $result;
}
// if user has submitted a name, search for it:
$result = $repository->findByName($accountName, $expectedTypes);
$result = $repository->findByName($accountName, $expectedTypes);
if (null !== $result) {
return $result;
}
@@ -190,7 +190,7 @@ trait RecurringTransactionTrait
$cannotCreate = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD];
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
/** @var string $expectedType */
@@ -213,12 +213,12 @@ trait RecurringTransactionTrait
protected function updatePiggyBank(RecurrenceTransaction $transaction, int $piggyId): void
{
/** @var PiggyBankFactory $factory */
$factory = app(PiggyBankFactory::class);
$factory = app(PiggyBankFactory::class);
$factory->setUser($transaction->recurrence->user);
$piggyBank = $factory->find($piggyId, null);
if (null !== $piggyBank) {
/** @var null|RecurrenceMeta $entry */
$entry = $transaction->recurrenceTransactionMeta()->where('name', 'piggy_bank_id')->first();
$entry = $transaction->recurrenceTransactionMeta()->where('name', 'piggy_bank_id')->first();
if (null === $entry) {
$entry = RecurrenceTransactionMeta::create(['rt_id' => $transaction->id, 'name' => 'piggy_bank_id', 'value' => $piggyBank->id]);
}
@@ -235,7 +235,7 @@ trait RecurringTransactionTrait
{
if (0 !== count($tags)) {
/** @var null|RecurrenceMeta $entry */
$entry = $transaction->recurrenceTransactionMeta()->where('name', 'tags')->first();
$entry = $transaction->recurrenceTransactionMeta()->where('name', 'tags')->first();
if (null === $entry) {
$entry = RecurrenceTransactionMeta::create(['rt_id' => $transaction->id, 'name' => 'tags', 'value' => json_encode($tags)]);
}
@@ -269,18 +269,18 @@ trait RecurringTransactionTrait
{
$budgetFactory = app(BudgetFactory::class);
$budgetFactory->setUser($transaction->recurrence->user);
$budget = $budgetFactory->find($budgetId, null);
$budget = $budgetFactory->find($budgetId, null);
if (null === $budget) {
return;
}
$meta = $transaction->recurrenceTransactionMeta()->where('name', 'budget_id')->first();
$meta = $transaction->recurrenceTransactionMeta()->where('name', 'budget_id')->first();
if (null === $meta) {
$meta = new RecurrenceTransactionMeta();
$meta->rt_id = $transaction->id;
$meta->name = 'budget_id';
}
$meta->value = $budget->id;
$meta->value = $budget->id;
$meta->save();
}
@@ -288,12 +288,12 @@ trait RecurringTransactionTrait
{
$billFactory = app(BillFactory::class);
$billFactory->setUser($transaction->recurrence->user);
$bill = $billFactory->find($billId, null);
$bill = $billFactory->find($billId, null);
if (null === $bill) {
return;
}
$meta = $transaction->recurrenceTransactionMeta()->where('name', 'bill_id')->first();
$meta = $transaction->recurrenceTransactionMeta()->where('name', 'bill_id')->first();
if (null === $meta) {
$meta = new RecurrenceTransactionMeta();
$meta->rt_id = $transaction->id;
@@ -307,7 +307,7 @@ trait RecurringTransactionTrait
{
$categoryFactory = app(CategoryFactory::class);
$categoryFactory->setUser($transaction->recurrence->user);
$category = $categoryFactory->findOrCreate($categoryId, null);
$category = $categoryFactory->findOrCreate($categoryId, null);
if (null === $category) {
// remove category:
$transaction->recurrenceTransactionMeta()->where('name', 'category_id')->delete();
@@ -316,13 +316,13 @@ trait RecurringTransactionTrait
return;
}
$transaction->recurrenceTransactionMeta()->where('name', 'category_name')->delete();
$meta = $transaction->recurrenceTransactionMeta()->where('name', 'category_id')->first();
$meta = $transaction->recurrenceTransactionMeta()->where('name', 'category_id')->first();
if (null === $meta) {
$meta = new RecurrenceTransactionMeta();
$meta->rt_id = $transaction->id;
$meta->name = 'category_id';
}
$meta->value = $category->id;
$meta->value = $category->id;
$meta->save();
}
}