mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 04:03:26 +00:00
Simplify transformer.
This commit is contained in:
@@ -42,12 +42,9 @@ use Log;
|
|||||||
*/
|
*/
|
||||||
class TransactionGroupTransformer extends AbstractTransformer
|
class TransactionGroupTransformer extends AbstractTransformer
|
||||||
{
|
{
|
||||||
/** @var TransactionGroupRepositoryInterface */
|
private TransactionGroupRepositoryInterface $groupRepos;
|
||||||
private $groupRepos;
|
private array $metaDateFields;
|
||||||
/** @var array Array with meta date fields. */
|
private array $metaFields;
|
||||||
private $metaDateFields;
|
|
||||||
/** @var array Array with meta fields. */
|
|
||||||
private $metaFields;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
@@ -80,10 +77,10 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
$data = new NullArrayObject($group);
|
$data = new NullArrayObject($group);
|
||||||
$first = new NullArrayObject(reset($group['transactions']));
|
$first = new NullArrayObject(reset($group['transactions']));
|
||||||
$result = [
|
$result = [
|
||||||
'id' => (int)$first['transaction_group_id'],
|
'id' => (int) $first['transaction_group_id'],
|
||||||
'created_at' => $first['created_at']->toAtomString(),
|
'created_at' => $first['created_at']->toAtomString(),
|
||||||
'updated_at' => $first['updated_at']->toAtomString(),
|
'updated_at' => $first['updated_at']->toAtomString(),
|
||||||
'user' => (int)$data['user_id'],
|
'user' => (int) $data['user_id'],
|
||||||
'group_title' => $data['title'],
|
'group_title' => $data['title'],
|
||||||
'transactions' => $this->transformTransactions($data),
|
'transactions' => $this->transformTransactions($data),
|
||||||
'links' => [
|
'links' => [
|
||||||
@@ -108,20 +105,20 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
public function transformObject(TransactionGroup $group): array
|
public function transformObject(TransactionGroup $group): array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$result = [
|
$result = [
|
||||||
'id' => (int)$group->id,
|
'id' => (int) $group->id,
|
||||||
'created_at' => $group->created_at->toAtomString(),
|
'created_at' => $group->created_at->toAtomString(),
|
||||||
'updated_at' => $group->updated_at->toAtomString(),
|
'updated_at' => $group->updated_at->toAtomString(),
|
||||||
'user' => (int)$group->user_id,
|
'user' => (int) $group->user_id,
|
||||||
'group_title' => $group->title,
|
'group_title' => $group->title,
|
||||||
'transactions' => $this->transformJournals($group->transactionJournals),
|
'transactions' => $this->transformJournals($group->transactionJournals),
|
||||||
'links' => [
|
'links' => [
|
||||||
[
|
[
|
||||||
'rel' => 'self',
|
'rel' => 'self',
|
||||||
'uri' => '/transactions/' . $group->id,
|
'uri' => '/transactions/' . $group->id,
|
||||||
|
],
|
||||||
],
|
],
|
||||||
],
|
];
|
||||||
];
|
|
||||||
} catch (FireflyException $e) {
|
} catch (FireflyException $e) {
|
||||||
Log::error($e->getMessage());
|
Log::error($e->getMessage());
|
||||||
Log::error($e->getTraceAsString());
|
Log::error($e->getTraceAsString());
|
||||||
@@ -245,7 +242,7 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
{
|
{
|
||||||
$result = $journal->transactions->first(
|
$result = $journal->transactions->first(
|
||||||
static function (Transaction $transaction) {
|
static function (Transaction $transaction) {
|
||||||
return (float)$transaction->amount > 0;
|
return (float) $transaction->amount > 0;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (null === $result) {
|
if (null === $result) {
|
||||||
@@ -305,7 +302,7 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
{
|
{
|
||||||
$result = $journal->transactions->first(
|
$result = $journal->transactions->first(
|
||||||
static function (Transaction $transaction) {
|
static function (Transaction $transaction) {
|
||||||
return (float)$transaction->amount < 0;
|
return (float) $transaction->amount < 0;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (null === $result) {
|
if (null === $result) {
|
||||||
@@ -437,92 +434,146 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
$result = [];
|
$result = [];
|
||||||
$transactions = $data['transactions'] ?? [];
|
$transactions = $data['transactions'] ?? [];
|
||||||
foreach ($transactions as $transaction) {
|
foreach ($transactions as $transaction) {
|
||||||
$row = new NullArrayObject($transaction);
|
$result[] = $this->transformTransaction($transaction);
|
||||||
|
|
||||||
// amount:
|
|
||||||
$type = $row['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
|
|
||||||
$amount = app('steam')->positive($row['amount'] ?? '0');
|
|
||||||
$foreignAmount = null;
|
|
||||||
if (null !== $row['foreign_amount']) {
|
|
||||||
$foreignAmount = app('steam')->positive($row['foreign_amount']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$metaFieldData = $this->groupRepos->getMetaFields((int)$row['transaction_journal_id'], $this->metaFields);
|
|
||||||
$metaDateData = $this->groupRepos->getMetaDateFields((int)$row['transaction_journal_id'], $this->metaDateFields);
|
|
||||||
|
|
||||||
$result[] = [
|
|
||||||
'user' => (int) $row['user_id'],
|
|
||||||
'transaction_journal_id' => (int) $row['transaction_journal_id'],
|
|
||||||
'type' => strtolower($type),
|
|
||||||
'date' => $row['date']->toAtomString(),
|
|
||||||
'order' => $row['order'],
|
|
||||||
|
|
||||||
'currency_id' => (int) $row['currency_id'],
|
|
||||||
'currency_code' => $row['currency_code'],
|
|
||||||
'currency_name' => $row['currency_name'],
|
|
||||||
'currency_symbol' => $row['currency_symbol'],
|
|
||||||
'currency_decimal_places' => (int) $row['currency_decimal_places'],
|
|
||||||
|
|
||||||
'foreign_currency_id' => $row['foreign_currency_id'] ? (int) $row['foreign_currency_id'] : null,
|
|
||||||
'foreign_currency_code' => $row['foreign_currency_code'],
|
|
||||||
'foreign_currency_symbol' => $row['foreign_currency_symbol'],
|
|
||||||
'foreign_currency_decimal_places' => $row['foreign_currency_decimal_places'],
|
|
||||||
|
|
||||||
'amount' => $amount,
|
|
||||||
'foreign_amount' => $foreignAmount,
|
|
||||||
|
|
||||||
'description' => $row['description'],
|
|
||||||
|
|
||||||
'source_id' => (int) $row['source_account_id'],
|
|
||||||
'source_name' => $row['source_account_name'],
|
|
||||||
'source_iban' => $row['source_account_iban'],
|
|
||||||
'source_type' => $row['source_account_type'],
|
|
||||||
|
|
||||||
'destination_id' => (int) $row['destination_account_id'],
|
|
||||||
'destination_name' => $row['destination_account_name'],
|
|
||||||
'destination_iban' => $row['destination_account_iban'],
|
|
||||||
'destination_type' => $row['destination_account_type'],
|
|
||||||
|
|
||||||
'budget_id' => $row['budget_id'] ? (int) $row['budget_id'] : null,
|
|
||||||
'budget_name' => $row['budget_name'],
|
|
||||||
|
|
||||||
'category_id' => $row['category_id'] ? (int) $row['category_id'] : null,
|
|
||||||
'category_name' => $row['category_name'],
|
|
||||||
|
|
||||||
'bill_id' => $row['bill_id'] ? (int) $row['bill_id'] : null,
|
|
||||||
'bill_name' => $row['bill_name'],
|
|
||||||
|
|
||||||
'reconciled' => $row['reconciled'],
|
|
||||||
'notes' => $this->groupRepos->getNoteText((int) $row['transaction_journal_id']),
|
|
||||||
'tags' => $this->groupRepos->getTags((int) $row['transaction_journal_id']),
|
|
||||||
|
|
||||||
'internal_reference' => $metaFieldData['internal_reference'],
|
|
||||||
'external_id' => $metaFieldData['external_id'],
|
|
||||||
'original_source' => $metaFieldData['original_source'],
|
|
||||||
'recurrence_id' => null !== $metaFieldData['recurrence_id'] ? (int) $metaFieldData['recurrence_id'] : null,
|
|
||||||
'recurrence_total' => null !== $metaFieldData['recurrence_total'] ? (int) $metaFieldData['recurrence_total'] : null,
|
|
||||||
'recurrence_count' => null !== $metaFieldData['recurrence_count'] ? (int) $metaFieldData['recurrence_count'] : null,
|
|
||||||
'bunq_payment_id' => $metaFieldData['bunq_payment_id'],
|
|
||||||
'external_uri' => $metaFieldData['external_uri'],
|
|
||||||
'import_hash_v2' => $metaFieldData['import_hash_v2'],
|
|
||||||
|
|
||||||
'sepa_cc' => $metaFieldData['sepa_cc'],
|
|
||||||
'sepa_ct_op' => $metaFieldData['sepa_ct_op'],
|
|
||||||
'sepa_ct_id' => $metaFieldData['sepa_ct_id'],
|
|
||||||
'sepa_db' => $metaFieldData['sepa_ddb'],
|
|
||||||
'sepa_country' => $metaFieldData['sepa_country'],
|
|
||||||
'sepa_ep' => $metaFieldData['sepa_ep'],
|
|
||||||
'sepa_ci' => $metaFieldData['sepa_ci'],
|
|
||||||
'sepa_batch_id' => $metaFieldData['sepa_batch_id'],
|
|
||||||
|
|
||||||
'interest_date' => $metaDateData['interest_date'] ? $metaDateData['interest_date']->toAtomString() : null,
|
|
||||||
'book_date' => $metaDateData['book_date'] ? $metaDateData['book_date']->toAtomString() : null,
|
|
||||||
'process_date' => $metaDateData['process_date'] ? $metaDateData['process_date']->toAtomString() : null,
|
|
||||||
'due_date' => $metaDateData['due_date'] ? $metaDateData['due_date']->toAtomString() : null,
|
|
||||||
'payment_date' => $metaDateData['payment_date'] ? $metaDateData['payment_date']->toAtomString() : null,
|
|
||||||
'invoice_date' => $metaDateData['invoice_date'] ? $metaDateData['invoice_date']->toAtomString() : null,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $transaction
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function transformTransaction(array $transaction): array
|
||||||
|
{
|
||||||
|
$row = new NullArrayObject($transaction);
|
||||||
|
|
||||||
|
// amount:
|
||||||
|
$type = $this->stringFromArray($transaction, 'transaction_type_type', TransactionType::WITHDRAWAL);
|
||||||
|
$amount = app('steam')->positive($row['amount'] ?? '0');
|
||||||
|
$foreignAmount = null;
|
||||||
|
if (null !== $row['foreign_amount']) {
|
||||||
|
$foreignAmount = app('steam')->positive($row['foreign_amount']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$metaFieldData = $this->groupRepos->getMetaFields((int) $row['transaction_journal_id'], $this->metaFields);
|
||||||
|
$metaDateData = $this->groupRepos->getMetaDateFields((int) $row['transaction_journal_id'], $this->metaDateFields);
|
||||||
|
|
||||||
|
$result[] = [
|
||||||
|
'user' => (int) $row['user_id'],
|
||||||
|
'transaction_journal_id' => (int) $row['transaction_journal_id'],
|
||||||
|
'type' => strtolower($type),
|
||||||
|
'date' => $row['date']->toAtomString(),
|
||||||
|
'order' => $row['order'],
|
||||||
|
|
||||||
|
'currency_id' => (int) $row['currency_id'],
|
||||||
|
'currency_code' => $row['currency_code'],
|
||||||
|
'currency_name' => $row['currency_name'],
|
||||||
|
'currency_symbol' => $row['currency_symbol'],
|
||||||
|
'currency_decimal_places' => (int) $row['currency_decimal_places'],
|
||||||
|
|
||||||
|
'foreign_currency_id' => $this->integerFromArray($transaction, 'foreign_currency_id'),
|
||||||
|
'foreign_currency_code' => $row['foreign_currency_code'],
|
||||||
|
'foreign_currency_symbol' => $row['foreign_currency_symbol'],
|
||||||
|
'foreign_currency_decimal_places' => $row['foreign_currency_decimal_places'],
|
||||||
|
|
||||||
|
'amount' => $amount,
|
||||||
|
'foreign_amount' => $foreignAmount,
|
||||||
|
|
||||||
|
'description' => $row['description'],
|
||||||
|
|
||||||
|
'source_id' => (int) $row['source_account_id'],
|
||||||
|
'source_name' => $row['source_account_name'],
|
||||||
|
'source_iban' => $row['source_account_iban'],
|
||||||
|
'source_type' => $row['source_account_type'],
|
||||||
|
|
||||||
|
'destination_id' => (int) $row['destination_account_id'],
|
||||||
|
'destination_name' => $row['destination_account_name'],
|
||||||
|
'destination_iban' => $row['destination_account_iban'],
|
||||||
|
'destination_type' => $row['destination_account_type'],
|
||||||
|
|
||||||
|
'budget_id' => $this->integerFromArray($transaction, 'budget_id'),
|
||||||
|
'budget_name' => $row['budget_name'],
|
||||||
|
|
||||||
|
'category_id' => $this->integerFromArray($transaction, 'category_id'),
|
||||||
|
'category_name' => $row['category_name'],
|
||||||
|
|
||||||
|
'bill_id' => $this->integerFromArray($transaction, 'bill_id'),
|
||||||
|
'bill_name' => $row['bill_name'],
|
||||||
|
|
||||||
|
'reconciled' => $row['reconciled'],
|
||||||
|
'notes' => $this->groupRepos->getNoteText((int) $row['transaction_journal_id']),
|
||||||
|
'tags' => $this->groupRepos->getTags((int) $row['transaction_journal_id']),
|
||||||
|
|
||||||
|
'internal_reference' => $metaFieldData['internal_reference'],
|
||||||
|
'external_id' => $metaFieldData['external_id'],
|
||||||
|
'original_source' => $metaFieldData['original_source'],
|
||||||
|
'recurrence_id' => $this->integerFromArray($metaFieldData->getArrayCopy(), 'recurrence_id'),
|
||||||
|
'recurrence_total' => $this->integerFromArray($metaFieldData->getArrayCopy(), 'recurrence_total'),
|
||||||
|
'recurrence_count' => $this->integerFromArray($metaFieldData->getArrayCopy(), 'recurrence_count'),
|
||||||
|
'bunq_payment_id' => $metaFieldData['bunq_payment_id'],
|
||||||
|
'external_uri' => $metaFieldData['external_uri'],
|
||||||
|
'import_hash_v2' => $metaFieldData['import_hash_v2'],
|
||||||
|
|
||||||
|
'sepa_cc' => $metaFieldData['sepa_cc'],
|
||||||
|
'sepa_ct_op' => $metaFieldData['sepa_ct_op'],
|
||||||
|
'sepa_ct_id' => $metaFieldData['sepa_ct_id'],
|
||||||
|
'sepa_db' => $metaFieldData['sepa_ddb'],
|
||||||
|
'sepa_country' => $metaFieldData['sepa_country'],
|
||||||
|
'sepa_ep' => $metaFieldData['sepa_ep'],
|
||||||
|
'sepa_ci' => $metaFieldData['sepa_ci'],
|
||||||
|
'sepa_batch_id' => $metaFieldData['sepa_batch_id'],
|
||||||
|
|
||||||
|
'interest_date' => $this->dateFromArray($metaDateData, 'interest_date'),
|
||||||
|
'book_date' => $this->dateFromArray($metaDateData, 'book_date'),
|
||||||
|
'process_date' => $this->dateFromArray($metaDateData, 'process_date'),
|
||||||
|
'due_date' => $this->dateFromArray($metaDateData, 'due_date'),
|
||||||
|
'payment_date' => $this->dateFromArray($metaDateData, 'payment_date'),
|
||||||
|
'invoice_date' => $this->dateFromArray($metaDateData, 'invoice_date'),
|
||||||
|
];
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $array
|
||||||
|
* @param string $key
|
||||||
|
* @param string|null $default
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
private function stringFromArray(array $array, string $key, ?string $default): ?string
|
||||||
|
{
|
||||||
|
if (array_key_exists($key, $array)) {
|
||||||
|
return $array[$key];
|
||||||
|
}
|
||||||
|
if (null !== $default) {
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $array
|
||||||
|
* @param string $key
|
||||||
|
* @return int|null
|
||||||
|
*/
|
||||||
|
private function integerFromArray(array $array, string $key): ?int
|
||||||
|
{
|
||||||
|
if (array_key_exists($key, $array)) {
|
||||||
|
return (int) $array[$key];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param NullArrayObject $object
|
||||||
|
* @param string $key
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
private function dateFromArray(NullArrayObject $object, string $key): ?string
|
||||||
|
{
|
||||||
|
if (null === $object[$key]) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $object['interest_date']->toAtomString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user