mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-19 10:53:31 +00:00
Clean up complicated method.
This commit is contained in:
@@ -24,9 +24,11 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Transformers;
|
namespace FireflyIII\Transformers;
|
||||||
|
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
|
use FireflyIII\Models\Bill;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Models\Category;
|
use FireflyIII\Models\Category;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
use FireflyIII\Models\TransactionGroup;
|
use FireflyIII\Models\TransactionGroup;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
@@ -124,11 +126,114 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
Log::error($e->getTraceAsString());
|
Log::error($e->getTraceAsString());
|
||||||
throw new FireflyException(sprintf('Transaction group #%d is broken. Please check out your log files.', $group->id));
|
throw new FireflyException(sprintf('Transaction group #%d is broken. Please check out your log files.', $group->id));
|
||||||
}
|
}
|
||||||
|
|
||||||
// do something else.
|
// do something else.
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $type
|
||||||
|
* @param string $amount
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function getAmount(string $type, string $amount): string
|
||||||
|
{
|
||||||
|
$amount = app('steam')->positive($amount);
|
||||||
|
if (TransactionType::WITHDRAWAL !== $type) {
|
||||||
|
$amount = app('steam')->negative($amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Bill|null $bill
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getBill(?Bill $bill): array
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
'id' => null,
|
||||||
|
'name' => null,
|
||||||
|
];
|
||||||
|
if (null === $bill) {
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
$array['id'] = $bill->id;
|
||||||
|
$array['name'] = $bill->name;
|
||||||
|
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Budget|null $budget
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getBudget(?Budget $budget): array
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
'id' => null,
|
||||||
|
'name' => null,
|
||||||
|
];
|
||||||
|
if (null === $budget) {
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
$array['id'] = $budget->id;
|
||||||
|
$array['name'] = $budget->name;
|
||||||
|
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Category|null $category
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getCategory(?Category $category): array
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
'id' => null,
|
||||||
|
'name' => null,
|
||||||
|
];
|
||||||
|
if (null === $category) {
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
$array['id'] = $category->id;
|
||||||
|
$array['name'] = $category->name;
|
||||||
|
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param NullArrayObject $dates
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getDates(NullArrayObject $dates): array
|
||||||
|
{
|
||||||
|
$fields = [
|
||||||
|
'interest_date',
|
||||||
|
'book_date',
|
||||||
|
'process_date',
|
||||||
|
'due_date',
|
||||||
|
'payment_date',
|
||||||
|
'invoice_date',
|
||||||
|
];
|
||||||
|
$return = [];
|
||||||
|
foreach ($fields as $field) {
|
||||||
|
$return[$field] = null;
|
||||||
|
if (null !== $dates[$field]) {
|
||||||
|
$return[$field] = $dates[$field]->toAtomString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
*
|
*
|
||||||
@@ -149,6 +254,46 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $type
|
||||||
|
* @param string|null $foreignAmount
|
||||||
|
*
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
private function getForeignAmount(string $type, ?string $foreignAmount): ?string
|
||||||
|
{
|
||||||
|
$result = null;
|
||||||
|
if (null !== $foreignAmount) {
|
||||||
|
$result = TransactionType::WITHDRAWAL !== $type ? app('steam')->negative($foreignAmount) : app('steam')->positive($foreignAmount);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param TransactionCurrency|null $currency
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getForeignCurrency(?TransactionCurrency $currency): array
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
'id' => null,
|
||||||
|
'code' => null,
|
||||||
|
'symbol' => null,
|
||||||
|
'decimal_places' => null,
|
||||||
|
];
|
||||||
|
if (null === $currency) {
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
$array['id'] = $currency->id;
|
||||||
|
$array['code'] = $currency->code;
|
||||||
|
$array['symbol'] = $currency->symbol;
|
||||||
|
$array['decimal_places'] = $currency->decimal_places;
|
||||||
|
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
*
|
*
|
||||||
@@ -170,44 +315,27 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $transactionJournals
|
* @param TransactionJournal $journal
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
private function transformJournals(Collection $transactionJournals): array
|
private function transformJournal(TransactionJournal $journal): array
|
||||||
{
|
{
|
||||||
$result = [];
|
|
||||||
/** @var TransactionJournal $journal */
|
|
||||||
foreach ($transactionJournals as $journal) {
|
|
||||||
$source = $this->getSourceTransaction($journal);
|
$source = $this->getSourceTransaction($journal);
|
||||||
$destination = $this->getDestinationTransaction($journal);
|
$destination = $this->getDestinationTransaction($journal);
|
||||||
$type = $journal->transactionType->type;
|
$type = $journal->transactionType->type;
|
||||||
|
$amount = $this->getAmount($type, $source->amount);
|
||||||
// get amount
|
$foreignAmount = $this->getForeignAmount($type, $source->foreign_amount);
|
||||||
$amount = app('steam')->positive($source->amount);
|
|
||||||
if (TransactionType::WITHDRAWAL !== $type) {
|
|
||||||
$amount = app('steam')->negative($source->amount);
|
|
||||||
}
|
|
||||||
|
|
||||||
// get foreign amount:
|
|
||||||
$foreignAmount = null;
|
|
||||||
// @codeCoverageIgnoreStart
|
|
||||||
if (null !== $source->foreign_amount) {
|
|
||||||
$foreignAmount = TransactionType::WITHDRAWAL !== $type
|
|
||||||
? app('steam')->negative($source->foreign_amount)
|
|
||||||
: app('steam')->positive($source->foreign_amount);
|
|
||||||
}
|
|
||||||
// @codeCoverageIgnoreEnd
|
|
||||||
|
|
||||||
$metaFieldData = $this->groupRepos->getMetaFields($journal->id, $this->metaFields);
|
$metaFieldData = $this->groupRepos->getMetaFields($journal->id, $this->metaFields);
|
||||||
$metaDateData = $this->groupRepos->getMetaDateFields($journal->id, $this->metaDateFields);
|
$metaDates = $this->getDates($this->groupRepos->getMetaDateFields($journal->id, $this->metaDateFields));
|
||||||
/** @var Budget $budget */
|
|
||||||
$budget = $journal->budgets->first();
|
|
||||||
/** @var Category $category */
|
|
||||||
$category = $journal->categories->first();
|
|
||||||
$currency = $source->transactionCurrency;
|
$currency = $source->transactionCurrency;
|
||||||
$result[] = [
|
$foreignCurrency = $this->getForeignCurrency($source->foreignCurrency);
|
||||||
|
$budget = $this->getBudget($journal->budgets->first());
|
||||||
|
$category = $this->getCategory($journal->categories->first());
|
||||||
|
$bill = $this->getBill($journal->bill);
|
||||||
|
|
||||||
|
return [
|
||||||
'user' => (int)$journal->user_id,
|
'user' => (int)$journal->user_id,
|
||||||
'transaction_journal_id' => $journal->id,
|
'transaction_journal_id' => $journal->id,
|
||||||
'type' => strtolower($type),
|
'type' => strtolower($type),
|
||||||
@@ -219,10 +347,10 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
'currency_symbol' => $currency->symbol,
|
'currency_symbol' => $currency->symbol,
|
||||||
'currency_decimal_places' => $currency->decimal_places,
|
'currency_decimal_places' => $currency->decimal_places,
|
||||||
|
|
||||||
'foreign_currency_id' => $source->foreignCurrency ? $source->foreignCurrency->id : null,
|
'foreign_currency_id' => $foreignCurrency['id'],
|
||||||
'foreign_currency_code' => $source->foreignCurrency ? $source->foreignCurrency->code : null,
|
'foreign_currency_code' => $foreignCurrency['code'],
|
||||||
'foreign_currency_symbol' => $source->foreignCurrency ? $source->foreignCurrency->symbol : null,
|
'foreign_currency_symbol' => $foreignCurrency['symbol'],
|
||||||
'foreign_currency_decimal_places' => $source->foreignCurrency ? $source->foreignCurrency->decimal_places : null,
|
'foreign_currency_decimal_places' => $foreignCurrency['decimal_places'],
|
||||||
|
|
||||||
'amount' => $amount,
|
'amount' => $amount,
|
||||||
'foreign_amount' => $foreignAmount,
|
'foreign_amount' => $foreignAmount,
|
||||||
@@ -239,14 +367,14 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
'destination_iban' => $destination->account->iban,
|
'destination_iban' => $destination->account->iban,
|
||||||
'destination_type' => $destination->account->accountType->type,
|
'destination_type' => $destination->account->accountType->type,
|
||||||
|
|
||||||
'budget_id' => $budget ? $budget->id : null,
|
'budget_id' => $budget['id'],
|
||||||
'budget_name' => $budget ? $budget->name : null,
|
'budget_name' => $budget['name'],
|
||||||
|
|
||||||
'category_id' => $category ? $category->id : null,
|
'category_id' => $category['id'],
|
||||||
'category_name' => $category ? $category->name : null,
|
'category_name' => $category['name'],
|
||||||
|
|
||||||
'bill_id' => $journal->bill_id ?: null,
|
'bill_id' => $bill['id'],
|
||||||
'bill_name' => $journal->bill ? $journal->bill->name : null,
|
'bill_name' => $bill['name'],
|
||||||
|
|
||||||
'reconciled' => $source->reconciled,
|
'reconciled' => $source->reconciled,
|
||||||
'notes' => $this->groupRepos->getNoteText($journal->id),
|
'notes' => $this->groupRepos->getNoteText($journal->id),
|
||||||
@@ -268,15 +396,29 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
'sepa_ci' => $metaFieldData['sepa_ci'],
|
'sepa_ci' => $metaFieldData['sepa_ci'],
|
||||||
'sepa_batch_id' => $metaFieldData['sepa_batch_id'],
|
'sepa_batch_id' => $metaFieldData['sepa_batch_id'],
|
||||||
|
|
||||||
'interest_date' => $metaDateData['interest_date'] ? $metaDateData['interest_date']->toAtomString() : null,
|
'interest_date' => $metaDates['interest_date'],
|
||||||
'book_date' => $metaDateData['book_date'] ? $metaDateData['book_date']->toAtomString() : null,
|
'book_date' => $metaDates['book_date'],
|
||||||
'process_date' => $metaDateData['process_date'] ? $metaDateData['process_date']->toAtomString() : null,
|
'process_date' => $metaDates['process_date'],
|
||||||
'due_date' => $metaDateData['due_date'] ? $metaDateData['due_date']->toAtomString() : null,
|
'due_date' => $metaDates['due_date'],
|
||||||
'payment_date' => $metaDateData['payment_date'] ? $metaDateData['payment_date']->toAtomString() : null,
|
'payment_date' => $metaDates['payment_date'],
|
||||||
'invoice_date' => $metaDateData['invoice_date'] ? $metaDateData['invoice_date']->toAtomString() : null,
|
'invoice_date' => $metaDates['invoice_date'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection $transactionJournals
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
private function transformJournals(Collection $transactionJournals): array
|
||||||
|
{
|
||||||
|
$result = [];
|
||||||
|
/** @var TransactionJournal $journal */
|
||||||
|
foreach ($transactionJournals as $journal) {
|
||||||
|
$result[] = $this->transformJournal($journal);
|
||||||
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user