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

@@ -75,8 +75,8 @@ class AccountDestroyService
}
/** @var JournalDestroyService $service */
$service = app(JournalDestroyService::class);
$user = $account->user;
$service = app(JournalDestroyService::class);
$user = $account->user;
/** @var \stdClass $row */
foreach ($collection as $row) {
@@ -101,7 +101,7 @@ class AccountDestroyService
->get(['transactions.transaction_journal_id'])
;
if ($set->count() > 0) {
$journalId = $set->first()->transaction_journal_id;
$journalId = $set->first()->transaction_journal_id;
app('log')->debug(sprintf('Found opening balance journal with ID #%d', $journalId));
// get transactions with this journal (should be just one):
@@ -120,7 +120,7 @@ class AccountDestroyService
$transaction->delete();
$ibAccount->delete();
}
$journal = TransactionJournal::find($journalId);
$journal = TransactionJournal::find($journalId);
if (null !== $journal) {
/** @var JournalDestroyService $service */
$service = app(JournalDestroyService::class);
@@ -157,7 +157,7 @@ class AccountDestroyService
private function destroyRecurrences(Account $account): void
{
$recurrences = RecurrenceTransaction::where(
$recurrences = RecurrenceTransaction::where(
static function (Builder $q) use ($account): void {
$q->where('source_id', $account->id);
$q->orWhere('destination_id', $account->id);

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();
}
}

View File

@@ -74,7 +74,7 @@ class AccountUpdateService
// find currency, or use default currency instead.
if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {
$currency = $this->getCurrency((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null));
$currency = $this->getCurrency((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null));
unset($data['currency_code'], $data['currency_id']);
$data['currency_id'] = $currency->id;
}
@@ -118,17 +118,17 @@ class AccountUpdateService
return $account;
}
// skip if not of orderable type.
$type = $account->accountType->type;
$type = $account->accountType->type;
if (!in_array($type, [AccountType::ASSET, AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT], true)) {
app('log')->debug('Will not change order of this account.');
return $account;
}
// get account type ID's because a join and an update is hard:
$oldOrder = $account->order;
$newOrder = $data['order'];
$oldOrder = $account->order;
$newOrder = $data['order'];
app('log')->debug(sprintf('Order is set to be updated from %s to %s', $oldOrder, $newOrder));
$list = $this->getTypeIds([AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT]);
$list = $this->getTypeIds([AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT]);
if (AccountType::ASSET === $type) {
$list = $this->getTypeIds([AccountType::ASSET]);
}
@@ -235,7 +235,7 @@ class AccountUpdateService
// otherwise, update or create.
if (!(null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level'])) {
$location = $this->accountRepository->getLocation($account);
$location = $this->accountRepository->getLocation($account);
if (null === $location) {
$location = new Location();
$location->locatable()->associate($account);
@@ -291,11 +291,11 @@ class AccountUpdateService
if (true === $account->active) {
return;
}
$preference = app('preferences')->getForUser($account->user, 'frontpageAccounts');
$preference = app('preferences')->getForUser($account->user, 'frontpageAccounts');
if (null === $preference) {
return;
}
$array = $preference->data;
$array = $preference->data;
if (!is_array($array)) {
$array = [$array];
}

View File

@@ -52,22 +52,22 @@ class BillUpdateService
$this->user = $bill->user;
if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {
$factory = app(TransactionCurrencyFactory::class);
$currency = $factory->find((int) ($data['currency_id'] ?? null), $data['currency_code'] ?? null) ??
$factory = app(TransactionCurrencyFactory::class);
$currency = $factory->find((int) ($data['currency_id'] ?? null), $data['currency_code'] ?? null) ??
app('amount')->getDefaultCurrencyByUserGroup($bill->user->userGroup);
// enable the currency if it isn't.
$currency->enabled = true;
$currency->enabled = true;
$currency->save();
$bill->transaction_currency_id = $currency->id;
$bill->save();
}
// update bill properties:
$bill = $this->updateBillProperties($bill, $data);
$bill = $this->updateBillProperties($bill, $data);
$bill->save();
$bill->refresh();
// old values
$oldData = [
$oldData = [
'name' => $bill->name,
'amount_min' => $bill->amount_min,
'amount_max' => $bill->amount_max,
@@ -197,14 +197,14 @@ class BillUpdateService
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$repository->setUser($bill->user);
$rules = $repository->getRulesForBill($bill);
$rules = $repository->getRulesForBill($bill);
if (0 === $rules->count()) {
app('log')->debug('Found no rules.');
return;
}
app('log')->debug(sprintf('Found %d rules', $rules->count()));
$fields = [
$fields = [
'name' => 'description_contains',
'amount_min' => 'amount_more',
'amount_max' => 'amount_less',

View File

@@ -131,7 +131,7 @@ class CategoryUpdateService
*/
private function updateNotes(Category $category, array $data): void
{
$note = array_key_exists('notes', $data) ? $data['notes'] : null;
$note = array_key_exists('notes', $data) ? $data['notes'] : null;
if (null === $note) {
return;
}
@@ -143,7 +143,7 @@ class CategoryUpdateService
return;
}
$dbNote = $category->notes()->first();
$dbNote = $category->notes()->first();
if (null === $dbNote) {
$dbNote = new Note();
$dbNote->noteable()->associate($category);

View File

@@ -44,7 +44,7 @@ class CurrencyUpdateService
$currency->name = e($data['name']);
}
$currency->enabled = false;
$currency->enabled = false;
if (array_key_exists('decimal_places', $data) && is_int($data['decimal_places'])) {
$currency->decimal_places = $data['decimal_places'];

View File

@@ -97,7 +97,7 @@ class GroupCloneService
// add relation.
// TODO clone ALL linked piggy banks
/** @var null|PiggyBankEvent $event */
$event = $journal->piggyBankEvents()->first();
$event = $journal->piggyBankEvents()->first();
if (null !== $event) {
$piggyBank = $event->piggyBank;
$factory = app(PiggyBankEventFactory::class);

View File

@@ -88,8 +88,8 @@ class GroupUpdateService
app('log')->debug('Going to update split group.');
$existing = $transactionGroup->transactionJournals->pluck('id')->toArray();
$updated = $this->updateTransactions($transactionGroup, $transactions);
$existing = $transactionGroup->transactionJournals->pluck('id')->toArray();
$updated = $this->updateTransactions($transactionGroup, $transactions);
app('log')->debug('Array of updated IDs: ', $updated);
if (0 === count($updated)) {
@@ -101,7 +101,7 @@ class GroupUpdateService
return $transactionGroup;
}
$result = array_diff($existing, $updated);
$result = array_diff($existing, $updated);
app('log')->debug('Result of DIFF: ', $result);
if (count($result) > 0) {
/** @var string $deletedId */
@@ -165,7 +165,7 @@ class GroupUpdateService
$journalId = (int)($transaction['transaction_journal_id'] ?? 0);
/** @var null|TransactionJournal $journal */
$journal = $transactionGroup->transactionJournals()->find($journalId);
$journal = $transactionGroup->transactionJournals()->find($journalId);
if (null === $journal) {
app('log')->debug('This entry has no existing journal: make a new split.');
// force the transaction type on the transaction data.
@@ -216,7 +216,7 @@ class GroupUpdateService
];
/** @var TransactionJournalFactory $factory */
$factory = app(TransactionJournalFactory::class);
$factory = app(TransactionJournalFactory::class);
$factory->setUser($transactionGroup->user);
try {

View File

@@ -109,7 +109,7 @@ class JournalUpdateService
public function setTransactionGroup(TransactionGroup $transactionGroup): void
{
$this->transactionGroup = $transactionGroup;
$this->transactionGroup = $transactionGroup;
$this->billRepository->setUser($transactionGroup->user);
$this->categoryRepository->setUser($transactionGroup->user);
$this->budgetRepository->setUser($transactionGroup->user);
@@ -176,8 +176,8 @@ class JournalUpdateService
private function hasValidSourceAccount(): bool
{
app('log')->debug('Now in hasValidSourceAccount().');
$sourceId = $this->data['source_id'] ?? null;
$sourceName = $this->data['source_name'] ?? null;
$sourceId = $this->data['source_id'] ?? null;
$sourceName = $this->data['source_name'] ?? null;
if (!$this->hasFields(['source_id', 'source_name'])) {
$origSourceAccount = $this->getOriginalSourceAccount();
@@ -191,11 +191,11 @@ class JournalUpdateService
// make a new validator.
/** @var AccountValidator $validator */
$validator = app(AccountValidator::class);
$validator = app(AccountValidator::class);
$validator->setTransactionType($expectedType);
$validator->setUser($this->transactionJournal->user);
$result = $validator->validateSource(['id' => $sourceId]);
$result = $validator->validateSource(['id' => $sourceId]);
app('log')->debug(
sprintf('hasValidSourceAccount(%d, "%s") will return %s', $sourceId, $sourceName, var_export($result, true))
);
@@ -261,8 +261,8 @@ class JournalUpdateService
private function hasValidDestinationAccount(): bool
{
app('log')->debug('Now in hasValidDestinationAccount().');
$destId = $this->data['destination_id'] ?? null;
$destName = $this->data['destination_name'] ?? null;
$destId = $this->data['destination_id'] ?? null;
$destName = $this->data['destination_name'] ?? null;
if (!$this->hasFields(['destination_id', 'destination_name'])) {
app('log')->debug('No destination info submitted, grab the original data.');
@@ -272,12 +272,12 @@ class JournalUpdateService
}
// make new account validator.
$expectedType = $this->getExpectedType();
$expectedType = $this->getExpectedType();
app('log')->debug(sprintf('Expected type (new or unchanged) is %s', $expectedType));
// make a new validator.
/** @var AccountValidator $validator */
$validator = app(AccountValidator::class);
$validator = app(AccountValidator::class);
$validator->setTransactionType($expectedType);
$validator->setUser($this->transactionJournal->user);
$validator->source = $this->getValidSourceAccount();
@@ -330,7 +330,7 @@ class JournalUpdateService
return $this->getOriginalSourceAccount();
}
$sourceInfo = [
$sourceInfo = [
'id' => (int)($this->data['source_id'] ?? null),
'name' => $this->data['source_name'] ?? null,
'iban' => $this->data['source_iban'] ?? null,
@@ -358,8 +358,8 @@ class JournalUpdateService
*/
private function updateAccounts(): void
{
$source = $this->getValidSourceAccount();
$destination = $this->getValidDestinationAccount();
$source = $this->getValidSourceAccount();
$destination = $this->getValidDestinationAccount();
// cowardly refuse to update if both accounts are the same.
if ($source->id === $destination->id) {
@@ -372,7 +372,7 @@ class JournalUpdateService
$origSourceTransaction->account()->associate($source);
$origSourceTransaction->save();
$destTransaction = $this->getDestinationTransaction();
$destTransaction = $this->getDestinationTransaction();
$destTransaction->account()->associate($destination);
$destTransaction->save();
@@ -394,7 +394,7 @@ class JournalUpdateService
return $this->getOriginalDestinationAccount();
}
$destInfo = [
$destInfo = [
'id' => (int)($this->data['destination_id'] ?? null),
'name' => $this->data['destination_name'] ?? null,
'iban' => $this->data['destination_iban'] ?? null,
@@ -423,7 +423,7 @@ class JournalUpdateService
{
app('log')->debug('Now in updateType()');
if ($this->hasFields(['type'])) {
$type = 'opening-balance' === $this->data['type'] ? 'opening balance' : $this->data['type'];
$type = 'opening-balance' === $this->data['type'] ? 'opening balance' : $this->data['type'];
app('log')->debug(
sprintf(
'Trying to change journal #%d from a %s to a %s.',
@@ -475,7 +475,7 @@ class JournalUpdateService
private function updateField(string $fieldName): void
{
if (array_key_exists($fieldName, $this->data) && '' !== (string)$this->data[$fieldName]) {
$value = $this->data[$fieldName];
$value = $this->data[$fieldName];
if ('date' === $fieldName) {
if ($value instanceof Carbon) {
@@ -576,7 +576,7 @@ class JournalUpdateService
if ($this->hasFields([$field])) {
$value = '' === $this->data[$field] ? null : $this->data[$field];
app('log')->debug(sprintf('Field "%s" is present ("%s"), try to update it.', $field, $value));
$set = [
$set = [
'journal' => $this->transactionJournal,
'name' => $field,
'data' => $value,
@@ -617,19 +617,19 @@ class JournalUpdateService
if (!$this->hasFields(['currency_id', 'currency_code'])) {
return;
}
$currencyId = $this->data['currency_id'] ?? null;
$currencyCode = $this->data['currency_code'] ?? null;
$currency = $this->currencyRepository->findCurrency($currencyId, $currencyCode);
$currencyId = $this->data['currency_id'] ?? null;
$currencyCode = $this->data['currency_code'] ?? null;
$currency = $this->currencyRepository->findCurrency($currencyId, $currencyCode);
// update currency everywhere.
$this->transactionJournal->transaction_currency_id = $currency->id;
$this->transactionJournal->save();
$source = $this->getSourceTransaction();
$source->transaction_currency_id = $currency->id;
$source = $this->getSourceTransaction();
$source->transaction_currency_id = $currency->id;
$source->save();
$dest = $this->getDestinationTransaction();
$dest->transaction_currency_id = $currency->id;
$dest = $this->getDestinationTransaction();
$dest->transaction_currency_id = $currency->id;
$dest->save();
// refresh transactions.
@@ -645,7 +645,7 @@ class JournalUpdateService
return;
}
$value = $this->data['amount'] ?? '';
$value = $this->data['amount'] ?? '';
app('log')->debug(sprintf('Amount is now "%s"', $value));
try {
@@ -658,8 +658,8 @@ class JournalUpdateService
$origSourceTransaction = $this->getSourceTransaction();
$origSourceTransaction->amount = app('steam')->negative($amount);
$origSourceTransaction->save();
$destTransaction = $this->getDestinationTransaction();
$destTransaction->amount = app('steam')->positive($amount);
$destTransaction = $this->getDestinationTransaction();
$destTransaction->amount = app('steam')->positive($amount);
$destTransaction->save();
// refresh transactions.
$this->sourceTransaction->refresh();
@@ -698,8 +698,8 @@ class JournalUpdateService
$source->foreign_currency_id = $foreignCurrency->id;
$source->foreign_amount = app('steam')->negative($foreignAmount);
$source->save();
$dest->foreign_currency_id = $foreignCurrency->id;
$dest->foreign_amount = app('steam')->positive($foreignAmount);
$dest->foreign_currency_id = $foreignCurrency->id;
$dest->foreign_amount = app('steam')->positive($foreignAmount);
$dest->save();
app('log')->debug(
@@ -722,8 +722,8 @@ class JournalUpdateService
$source->foreign_amount = null;
$source->save();
$dest->foreign_currency_id = null;
$dest->foreign_amount = null;
$dest->foreign_currency_id = null;
$dest->foreign_amount = null;
$dest->save();
app('log')->debug(sprintf('Foreign amount is "%s" so remove foreign amount info.', $amount));
}

View File

@@ -141,7 +141,7 @@ class RecurrenceUpdateService
// loop all and try to match them:
app('log')->debug('Loop and find');
foreach ($repetitions as $current) {
$match = $this->matchRepetition($recurrence, $current);
$match = $this->matchRepetition($recurrence, $current);
if (null === $match) {
throw new FireflyException('Cannot match recurring repetition to existing repetition. Not sure what to do. Break.');
}
@@ -170,14 +170,14 @@ class RecurrenceUpdateService
return $recurrence->recurrenceRepetitions()->first();
}
// find it:
$fields = [
$fields = [
'id' => 'id',
'type' => 'repetition_type',
'moment' => 'repetition_moment',
'skip' => 'repetition_skip',
'weekend' => 'weekend',
];
$query = $recurrence->recurrenceRepetitions();
$query = $recurrence->recurrenceRepetitions();
foreach ($fields as $field => $column) {
if (array_key_exists($field, $data)) {
$query->where($column, $data[$field]);
@@ -196,7 +196,7 @@ class RecurrenceUpdateService
private function updateTransactions(Recurrence $recurrence, array $transactions): void
{
app('log')->debug('Now in updateTransactions()');
$originalCount = $recurrence->recurrenceTransactions()->count();
$originalCount = $recurrence->recurrenceTransactions()->count();
app('log')->debug(sprintf('Original count is %d', $originalCount));
if (0 === count($transactions)) {
// won't drop transactions, rather avoid.
@@ -221,7 +221,7 @@ class RecurrenceUpdateService
}
// If one left of both we can match those as well and presto.
if (1 === count($originalTransactions) && 1 === count($transactions)) {
$first = array_shift($originalTransactions);
$first = array_shift($originalTransactions);
app('log')->debug(sprintf('One left of each, link them (ID is #%d)', $first['id']));
$combinations[] = [
'original' => $first,
@@ -257,7 +257,7 @@ class RecurrenceUpdateService
$currencyFactory = app(TransactionCurrencyFactory::class);
/** @var RecurrenceTransaction $transaction */
$transaction = $recurrence->recurrenceTransactions()->find($original['id']);
$transaction = $recurrence->recurrenceTransactions()->find($original['id']);
app('log')->debug(sprintf('Now in updateCombination(#%d)', $original['id']));
// loop all and try to match them:
@@ -289,7 +289,7 @@ class RecurrenceUpdateService
}
// update fields that are part of the recurring transaction itself.
$fields = [
$fields = [
'source_id' => 'source_id',
'destination_id' => 'destination_id',
'amount' => 'amount',