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

@@ -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',