Clean up various code.

This commit is contained in:
James Cole
2023-12-22 17:28:42 +01:00
parent e8890ada7c
commit 067d160c13
33 changed files with 331 additions and 699 deletions

View File

@@ -47,6 +47,11 @@ class BillTransformer extends AbstractTransformer
private array $notes;
private array $paidDates;
/**
* @throws \FireflyIII\Exceptions\FireflyException
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function collectMetaData(Collection $objects): void
{
$currencies = [];
@@ -55,7 +60,6 @@ class BillTransformer extends AbstractTransformer
$this->groups = [];
$this->paidDates = [];
// start with currencies:
/** @var Bill $object */
foreach ($objects as $object) {
$id = $object->transaction_currency_id;
@@ -63,9 +67,7 @@ class BillTransformer extends AbstractTransformer
$currencies[$id] ??= TransactionCurrency::find($id);
}
$this->currencies = $currencies;
// continue with notes
$notes = Note::whereNoteableType(Bill::class)->whereIn('noteable_id', array_keys($bills))->get();
$notes = Note::whereNoteableType(Bill::class)->whereIn('noteable_id', array_keys($bills))->get();
/** @var Note $note */
foreach ($notes as $note) {
@@ -81,8 +83,8 @@ class BillTransformer extends AbstractTransformer
/** @var ObjectGroup $entry */
foreach ($set as $entry) {
$billId = (int)$entry->object_groupable_id;
$id = (int)$entry->object_group_id;
$billId = (int) $entry->object_groupable_id;
$id = (int) $entry->object_group_id;
$order = $entry->order;
$this->groups[$billId] = [
'object_group_id' => $id,
@@ -103,11 +105,10 @@ class BillTransformer extends AbstractTransformer
$journalIds = $journals->pluck('id')->toArray();
// grab transactions for amount:
$set = Transaction::whereIn('transaction_journal_id', $journalIds)
$set = Transaction::whereIn('transaction_journal_id', $journalIds)
->where('transactions.amount', '<', 0)
->get(['transactions.id', 'transactions.transaction_journal_id', 'transactions.amount', 'transactions.foreign_amount', 'transactions.transaction_currency_id', 'transactions.foreign_currency_id'])
;
// convert to array for easy finding:
$transactions = [];
/** @var Transaction $transaction */
@@ -120,8 +121,8 @@ class BillTransformer extends AbstractTransformer
foreach ($journals as $journal) {
app('log')->debug(sprintf('Processing journal #%d', $journal->id));
$transaction = $transactions[$journal->id] ?? [];
$billId = (int)$journal->bill_id;
$currencyId = (int)($transaction['transaction_currency_id'] ?? 0);
$billId = (int) $journal->bill_id;
$currencyId = (int) ($transaction['transaction_currency_id'] ?? 0);
$currencies[$currencyId] ??= TransactionCurrency::find($currencyId);
// foreign currency
@@ -133,7 +134,7 @@ class BillTransformer extends AbstractTransformer
app('log')->debug('Foreign currency is NULL');
if (null !== $transaction['foreign_currency_id']) {
app('log')->debug(sprintf('Foreign currency is #%d', $transaction['foreign_currency_id']));
$foreignCurrencyId = (int)$transaction['foreign_currency_id'];
$foreignCurrencyId = (int) $transaction['foreign_currency_id'];
$currencies[$foreignCurrencyId] ??= TransactionCurrency::find($foreignCurrencyId);
$foreignCurrencyCode = $currencies[$foreignCurrencyId]->code;
$foreignCurrencyName = $currencies[$foreignCurrencyId]->name;
@@ -142,8 +143,8 @@ class BillTransformer extends AbstractTransformer
}
$this->paidDates[$billId][] = [
'transaction_group_id' => (string)$journal->id,
'transaction_journal_id' => (string)$journal->transaction_group_id,
'transaction_group_id' => (string) $journal->id,
'transaction_journal_id' => (string) $journal->transaction_group_id,
'date' => $journal->date->toAtomString(),
'currency_id' => $currencies[$currencyId]->id,
'currency_code' => $currencies[$currencyId]->code,
@@ -162,7 +163,7 @@ class BillTransformer extends AbstractTransformer
'amount' => $transaction['amount'],
'foreign_amount' => $transaction['foreign_amount'],
'native_amount' => $this->converter->convert($currencies[$currencyId], $this->default, $journal->date, $transaction['amount']),
'foreign_native_amount' => '' === (string)$transaction['foreign_amount'] ? null : $this->converter->convert(
'foreign_native_amount' => '' === (string) $transaction['foreign_amount'] ? null : $this->converter->convert(
$currencies[$foreignCurrencyId],
$this->default,
$journal->date,
@@ -203,7 +204,7 @@ class BillTransformer extends AbstractTransformer
'amount_max' => app('steam')->bcround($bill->amount_max, $currency->decimal_places),
'native_amount_min' => $this->converter->convert($currency, $this->default, $date, $bill->amount_min),
'native_amount_max' => $this->converter->convert($currency, $this->default, $date, $bill->amount_max),
'currency_id' => (string)$bill->transaction_currency_id,
'currency_id' => (string) $bill->transaction_currency_id,
'currency_code' => $currency->code,
'currency_name' => $currency->name,
'currency_symbol' => $currency->symbol,

View File

@@ -57,13 +57,13 @@ class TransactionGroupTransformer extends AbstractTransformer
/** @var array $object */
foreach ($objects as $object) {
foreach ($object['sums'] as $sum) {
$id = (int)$sum['currency_id'];
$id = (int) $sum['currency_id'];
$currencies[$id] ??= TransactionCurrency::find($sum['currency_id']);
}
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
$id = (int)$transaction['transaction_journal_id'];
$id = (int) $transaction['transaction_journal_id'];
$journals[$id] = [];
}
}
@@ -97,7 +97,7 @@ class TransactionGroupTransformer extends AbstractTransformer
/** @var \stdClass $tag */
foreach ($tags as $tag) {
$id = (int)$tag->transaction_journal_id;
$id = (int) $tag->transaction_journal_id;
$this->tags[$id][] = $tag->tag;
}
@@ -110,11 +110,11 @@ class TransactionGroupTransformer extends AbstractTransformer
$first = reset($group['transactions']);
return [
'id' => (string)$group['id'],
'id' => (string) $group['id'],
'created_at' => $first['created_at']->toAtomString(),
'updated_at' => $first['updated_at']->toAtomString(),
'user' => (string)$first['user_id'],
'user_group' => (string)$first['user_group_id'],
'user' => (string) $first['user_id'],
'user_group' => (string) $first['user_group_id'],
'group_title' => $group['title'] ?? null,
'transactions' => $this->transformTransactions($group['transactions'] ?? []),
'links' => [
@@ -140,33 +140,35 @@ class TransactionGroupTransformer extends AbstractTransformer
/**
* @throws FireflyException
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function transformTransaction(array $transaction): array
{
$transaction = new NullArrayObject($transaction);
$type = $this->stringFromArray($transaction, 'transaction_type_type', TransactionType::WITHDRAWAL);
$journalId = (int)$transaction['transaction_journal_id'];
$journalId = (int) $transaction['transaction_journal_id'];
$meta = new NullArrayObject($this->meta[$journalId] ?? []);
/**
* Convert and use amount:
*/
$amount = app('steam')->positive((string)($transaction['amount'] ?? '0'));
$currencyId = (int)$transaction['currency_id'];
$amount = app('steam')->positive((string) ($transaction['amount'] ?? '0'));
$currencyId = (int) $transaction['currency_id'];
$nativeAmount = $this->converter->convert($this->default, $this->currencies[$currencyId], $transaction['date'], $amount);
$foreignAmount = null;
$nativeForeignAmount = null;
if (null !== $transaction['foreign_amount']) {
$foreignCurrencyId = (int)$transaction['foreign_currency_id'];
$foreignCurrencyId = (int) $transaction['foreign_currency_id'];
$foreignAmount = app('steam')->positive($transaction['foreign_amount']);
$nativeForeignAmount = $this->converter->convert($this->default, $this->currencies[$foreignCurrencyId], $transaction['date'], $foreignAmount);
}
$this->converter->summarize();
return [
'user' => (string)$transaction['user_id'],
'user_group' => (string)$transaction['user_group_id'],
'transaction_journal_id' => (string)$transaction['transaction_journal_id'],
'user' => (string) $transaction['user_id'],
'user_group' => (string) $transaction['user_group_id'],
'transaction_journal_id' => (string) $transaction['transaction_journal_id'],
'type' => strtolower($type),
'date' => $transaction['date']->toAtomString(),
'order' => $transaction['order'],
@@ -174,14 +176,14 @@ class TransactionGroupTransformer extends AbstractTransformer
'native_amount' => $nativeAmount,
'foreign_amount' => $foreignAmount,
'native_foreign_amount' => $nativeForeignAmount,
'currency_id' => (string)$transaction['currency_id'],
'currency_id' => (string) $transaction['currency_id'],
'currency_code' => $transaction['currency_code'],
'currency_name' => $transaction['currency_name'],
'currency_symbol' => $transaction['currency_symbol'],
'currency_decimal_places' => (int)$transaction['currency_decimal_places'],
'currency_decimal_places' => (int) $transaction['currency_decimal_places'],
// converted to native currency
'native_currency_id' => (string)$this->default->id,
'native_currency_id' => (string) $this->default->id,
'native_currency_code' => $this->default->code,
'native_currency_name' => $this->default->name,
'native_currency_symbol' => $this->default->symbol,
@@ -196,11 +198,11 @@ class TransactionGroupTransformer extends AbstractTransformer
// foreign converted to native:
'description' => $transaction['description'],
'source_id' => (string)$transaction['source_account_id'],
'source_id' => (string) $transaction['source_account_id'],
'source_name' => $transaction['source_account_name'],
'source_iban' => $transaction['source_account_iban'],
'source_type' => $transaction['source_account_type'],
'destination_id' => (string)$transaction['destination_account_id'],
'destination_id' => (string) $transaction['destination_account_id'],
'destination_name' => $transaction['destination_account_name'],
'destination_iban' => $transaction['destination_account_iban'],
'destination_type' => $transaction['destination_account_type'],
@@ -265,7 +267,7 @@ class TransactionGroupTransformer extends AbstractTransformer
return $default;
}
if (null !== $array[$key]) {
return (string)$array[$key];
return (string) $array[$key];
}
if (null !== $default) {