mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Expand code.
This commit is contained in:
@@ -116,7 +116,6 @@ class TransactionGroupTransformer extends AbstractTransformer
|
||||
$row = new NullArrayObject($transaction);
|
||||
|
||||
// amount:
|
||||
$type = $this->stringFromArray($transaction, 'transaction_type_type', TransactionType::WITHDRAWAL);
|
||||
$amount = app('steam')->positive((string)($row['amount'] ?? '0'));
|
||||
$foreignAmount = null;
|
||||
if (null !== $row['foreign_amount']) {
|
||||
|
32
app/Transformers/V2/AbstractTransformer.php
Normal file
32
app/Transformers/V2/AbstractTransformer.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* AbstractTransformer.php
|
||||
* Copyright (c) 2022 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Transformers\V2;
|
||||
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
/**
|
||||
* Class AbstractTransformer
|
||||
*/
|
||||
abstract class AbstractTransformer extends TransformerAbstract
|
||||
{
|
||||
|
||||
}
|
85
app/Transformers/V2/AccountTransformer.php
Normal file
85
app/Transformers/V2/AccountTransformer.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
/*
|
||||
* AccountTransformer.php
|
||||
* Copyright (c) 2022 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Transformers\V2;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
|
||||
/**
|
||||
* Class AccountTransformer
|
||||
*/
|
||||
class AccountTransformer extends AbstractTransformer
|
||||
{
|
||||
/**
|
||||
* Transform the account.
|
||||
*
|
||||
* @param Account $account
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Account $account): array
|
||||
{
|
||||
$fullType = $account->accountType->type;
|
||||
$accountType = (string) config(sprintf('firefly.shortNamesByFullName.%s', $fullType));
|
||||
|
||||
return [
|
||||
'id' => (string) $account->id,
|
||||
'created_at' => $account->created_at->toAtomString(),
|
||||
'updated_at' => $account->updated_at->toAtomString(),
|
||||
'active' => $account->active,
|
||||
//'order' => $order,
|
||||
'name' => $account->name,
|
||||
'type' => strtolower($accountType),
|
||||
// 'account_role' => $accountRole,
|
||||
// 'currency_id' => $currencyId,
|
||||
// 'currency_code' => $currencyCode,
|
||||
// 'currency_symbol' => $currencySymbol,
|
||||
// 'currency_decimal_places' => $decimalPlaces,
|
||||
// 'current_balance' => number_format((float) app('steam')->balance($account, $date), $decimalPlaces, '.', ''),
|
||||
// 'current_balance_date' => $date->toAtomString(),
|
||||
// 'notes' => $this->repository->getNoteText($account),
|
||||
// 'monthly_payment_date' => $monthlyPaymentDate,
|
||||
// 'credit_card_type' => $creditCardType,
|
||||
// 'account_number' => $this->repository->getMetaValue($account, 'account_number'),
|
||||
'iban' => '' === $account->iban ? null : $account->iban,
|
||||
// 'bic' => $this->repository->getMetaValue($account, 'BIC'),
|
||||
// 'virtual_balance' => number_format((float) $account->virtual_balance, $decimalPlaces, '.', ''),
|
||||
// 'opening_balance' => $openingBalance,
|
||||
// 'opening_balance_date' => $openingBalanceDate,
|
||||
// 'liability_type' => $liabilityType,
|
||||
// 'liability_direction' => $liabilityDirection,
|
||||
// 'interest' => $interest,
|
||||
// 'interest_period' => $interestPeriod,
|
||||
// 'current_debt' => $this->repository->getMetaValue($account, 'current_debt'),
|
||||
// 'include_net_worth' => $includeNetWorth,
|
||||
// 'longitude' => $longitude,
|
||||
// 'latitude' => $latitude,
|
||||
// 'zoom_level' => $zoomLevel,
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/accounts/' . $account->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
52
app/Transformers/V2/PreferenceTransformer.php
Normal file
52
app/Transformers/V2/PreferenceTransformer.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* PreferenceTransformer.php
|
||||
* Copyright (c) 2019 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Transformers\V2;
|
||||
|
||||
use FireflyIII\Models\Preference;
|
||||
|
||||
/**
|
||||
* Class PreferenceTransformer
|
||||
*/
|
||||
class PreferenceTransformer extends AbstractTransformer
|
||||
{
|
||||
/**
|
||||
* Transform the preference
|
||||
*
|
||||
* @param Preference $preference
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Preference $preference): array
|
||||
{
|
||||
return [
|
||||
'id' => (int) $preference->id,
|
||||
'created_at' => $preference->created_at->toAtomString(),
|
||||
'updated_at' => $preference->updated_at->toAtomString(),
|
||||
'name' => $preference->name,
|
||||
'data' => $preference->data,
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
}
|
179
app/Transformers/V2/TransactionGroupTransformer.php
Normal file
179
app/Transformers/V2/TransactionGroupTransformer.php
Normal file
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
/*
|
||||
* TransactionGroupTransformer.php
|
||||
* Copyright (c) 2022 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Transformers\V2;
|
||||
|
||||
use FireflyIII\Models\TransactionType;
|
||||
|
||||
/**
|
||||
* Class TransactionGroupTransformer
|
||||
*/
|
||||
class TransactionGroupTransformer extends AbstractTransformer
|
||||
{
|
||||
/**
|
||||
* @param array $group
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(array $group): array
|
||||
{
|
||||
//$data = new NullArrayObject($group);
|
||||
$first = reset($group['transactions']);
|
||||
|
||||
return [
|
||||
'id' => (string) $group['id'],
|
||||
'created_at' => $first['created_at']->toAtomString(),
|
||||
'updated_at' => $first['updated_at']->toAtomString(),
|
||||
'user' => (string) $first['user_id'],
|
||||
'group_title' => $group['group_title'] ?? null,
|
||||
'transactions' => $this->transformTransactions($group['transactions'] ?? []),
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/transactions/' . 1,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $transactions
|
||||
* @return array
|
||||
*/
|
||||
private function transformTransactions(array $transactions): array
|
||||
{
|
||||
$return = [];
|
||||
/** @var array $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$return[] = $this->transformTransaction($transaction);
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
private function transformTransaction(array $transaction): array
|
||||
{
|
||||
$type = $this->stringFromArray($transaction, 'transaction_type_type', TransactionType::WITHDRAWAL);
|
||||
|
||||
// amount:
|
||||
$amount = app('steam')->positive((string) ($row['amount'] ?? '0'));
|
||||
$foreignAmount = null;
|
||||
if (null !== $row['foreign_amount']) {
|
||||
$foreignAmount = app('steam')->positive($row['foreign_amount']);
|
||||
}
|
||||
|
||||
return [
|
||||
'user' => (string) $transaction['user_id'],
|
||||
'transaction_journal_id' => (string) $transaction['transaction_journal_id'],
|
||||
'type' => strtolower($type),
|
||||
'date' => $transaction['date']->toAtomString(),
|
||||
'order' => $transaction['order'],
|
||||
'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'],
|
||||
'foreign_currency_id' => $this->stringFromArray($transaction, 'foreign_currency_id', null),
|
||||
'foreign_currency_code' => $transaction['foreign_currency_code'],
|
||||
'foreign_currency_symbol' => $transaction['foreign_currency_symbol'],
|
||||
'foreign_currency_decimal_places' => $transaction['foreign_currency_decimal_places'],
|
||||
'amount' => $amount,
|
||||
'foreign_amount' => $foreignAmount,
|
||||
'description' => $transaction['description'],
|
||||
'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_name' => $transaction['destination_account_name'],
|
||||
'destination_iban' => $transaction['destination_account_iban'],
|
||||
'destination_type' => $transaction['destination_account_type'],
|
||||
'budget_id' => $this->stringFromArray($transaction, 'budget_id', null),
|
||||
'budget_name' => $transaction['budget_name'],
|
||||
'category_id' => $this->stringFromArray($transaction, 'category_id', null),
|
||||
'category_name' => $transaction['category_name'],
|
||||
'bill_id' => $this->stringFromArray($transaction, 'bill_id', null),
|
||||
'bill_name' => $transaction['bill_name'],
|
||||
'reconciled' => $transaction['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->stringFromArray($metaFieldData->getArrayCopy(), 'recurrence_id', null),
|
||||
// 'recurrence_total' => $this->integerFromArray($metaFieldData->getArrayCopy(), 'recurrence_total'),
|
||||
// 'recurrence_count' => $this->integerFromArray($metaFieldData->getArrayCopy(), 'recurrence_count'),
|
||||
// 'bunq_payment_id' => $metaFieldData['bunq_payment_id'],
|
||||
// 'external_url' => $metaFieldData['external_url'],
|
||||
// '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_db'],
|
||||
// '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'),
|
||||
|
||||
// location data
|
||||
// 'longitude' => $longitude,
|
||||
// 'latitude' => $latitude,
|
||||
// 'zoom_level' => $zoomLevel,
|
||||
//
|
||||
// 'has_attachments' => $this->hasAttachments((int) $row['transaction_journal_id']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO also in the old transformer.
|
||||
*
|
||||
* @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) && null === $array[$key]) {
|
||||
return null;
|
||||
}
|
||||
if (array_key_exists($key, $array) && null !== $array[$key]) {
|
||||
return (string) $array[$key];
|
||||
}
|
||||
|
||||
if (null !== $default) {
|
||||
return (string) $default;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user