mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Expand API to return transaction groups.
This commit is contained in:
parent
7a2d4c9bd2
commit
02c906afe6
@ -148,7 +148,7 @@ class Controller extends BaseController
|
|||||||
$objects = $paginator->getCollection();
|
$objects = $paginator->getCollection();
|
||||||
|
|
||||||
// the transformer, at this point, needs to collect information that ALL items in the collection
|
// the transformer, at this point, needs to collect information that ALL items in the collection
|
||||||
// require, like meta data and stuff like that, and save it for later.
|
// require, like meta-data and stuff like that, and save it for later.
|
||||||
$transformer->collectMetaData($objects);
|
$transformer->collectMetaData($objects);
|
||||||
|
|
||||||
$resource = new FractalCollection($objects, $transformer, $key);
|
$resource = new FractalCollection($objects, $transformer, $key);
|
||||||
|
@ -49,19 +49,17 @@ class AccountController extends Controller
|
|||||||
*
|
*
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*/
|
*/
|
||||||
public function listTransactions(ListRequest $request, Account $account): JsonResponse
|
public function list(ListRequest $request, Account $account): JsonResponse
|
||||||
{
|
{
|
||||||
// collect transactions:
|
// collect transactions:
|
||||||
$type = $request->get('type') ?? 'default';
|
$limit = $request->getLimit();
|
||||||
$limit = (int)$request->get('limit');
|
$page = $request->getPage();
|
||||||
$page = (int)$request->get('page');
|
|
||||||
$page = max($page, 1);
|
$page = max($page, 1);
|
||||||
|
|
||||||
if ($limit > 0 && $limit <= $this->pageSize) {
|
if ($limit > 0 && $limit <= $this->pageSize) {
|
||||||
$this->pageSize = $limit;
|
$this->pageSize = $limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$types = $this->mapTransactionTypes($type);
|
|
||||||
|
|
||||||
/** @var GroupCollectorInterface $collector */
|
/** @var GroupCollectorInterface $collector */
|
||||||
$collector = app(GroupCollectorInterface::class);
|
$collector = app(GroupCollectorInterface::class);
|
||||||
@ -69,15 +67,23 @@ class AccountController extends Controller
|
|||||||
->withAPIInformation()
|
->withAPIInformation()
|
||||||
->setLimit($this->pageSize)
|
->setLimit($this->pageSize)
|
||||||
->setPage($page)
|
->setPage($page)
|
||||||
->setTypes($types);
|
->setTypes($request->getTransactionTypes());
|
||||||
|
|
||||||
// TODO date filter
|
$start = $request->getStartDate();
|
||||||
//if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
|
$end = $request->getEndDate();
|
||||||
// $collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
|
if (null !== $start) {
|
||||||
//}
|
$collector->setStart($start);
|
||||||
|
}
|
||||||
|
if (null !== $end) {
|
||||||
|
$collector->setEnd($start);
|
||||||
|
}
|
||||||
|
|
||||||
$paginator = $collector->getPaginatedGroups();
|
$paginator = $collector->getPaginatedGroups();
|
||||||
$paginator->setPath(route('api.v2.accounts.transactions', [$account->id])); // TODO . $this->buildParams()
|
$paginator->setPath(
|
||||||
|
sprintf('%s?%s',
|
||||||
|
route('api.v2.accounts.transactions', [$account->id]),
|
||||||
|
$request->buildParams())
|
||||||
|
);
|
||||||
|
|
||||||
return response()
|
return response()
|
||||||
->json($this->jsonApiList('transactions', $paginator, new TransactionGroupTransformer()))
|
->json($this->jsonApiList('transactions', $paginator, new TransactionGroupTransformer()))
|
||||||
|
@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* TransactionController.php
|
||||||
|
* Copyright (c) 2023 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\Api\V2\Controllers\Transaction\List;
|
||||||
|
|
||||||
|
use FireflyIII\Api\V2\Controllers\Controller;
|
||||||
|
use FireflyIII\Api\V2\Request\Transaction\ListRequest;
|
||||||
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||||
|
use FireflyIII\Transformers\V2\TransactionGroupTransformer;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class TransactionController
|
||||||
|
*/
|
||||||
|
class TransactionController extends Controller
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ListRequest $request
|
||||||
|
*
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
public function list(ListRequest $request): JsonResponse
|
||||||
|
{
|
||||||
|
// collect transactions:
|
||||||
|
$limit = $request->getLimit();
|
||||||
|
$page = $request->getPage();
|
||||||
|
$page = max($page, 1);
|
||||||
|
|
||||||
|
if ($limit > 0 && $limit <= $this->pageSize) {
|
||||||
|
$this->pageSize = $limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** @var GroupCollectorInterface $collector */
|
||||||
|
$collector = app(GroupCollectorInterface::class);
|
||||||
|
$collector->setUserGroup(auth()->user()->userGroup)
|
||||||
|
->withAPIInformation()
|
||||||
|
->setLimit($this->pageSize)
|
||||||
|
->setPage($page)
|
||||||
|
->setTypes($request->getTransactionTypes());
|
||||||
|
|
||||||
|
$start = $request->getStartDate();
|
||||||
|
$end = $request->getEndDate();
|
||||||
|
if (null !== $start) {
|
||||||
|
$collector->setStart($start);
|
||||||
|
}
|
||||||
|
if (null !== $end) {
|
||||||
|
$collector->setEnd($start);
|
||||||
|
}
|
||||||
|
|
||||||
|
$paginator = $collector->getPaginatedGroups();
|
||||||
|
$paginator->setPath(
|
||||||
|
sprintf('%s?%s',
|
||||||
|
route('api.v2.transactions.list'),
|
||||||
|
$request->buildParams())
|
||||||
|
);
|
||||||
|
|
||||||
|
return response()
|
||||||
|
->json($this->jsonApiList('transactions', $paginator, new TransactionGroupTransformer()))
|
||||||
|
->header('Content-Type', self::CONTENT_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -24,15 +24,84 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Api\V2\Request\Transaction;
|
namespace FireflyIII\Api\V2\Request\Transaction;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use FireflyIII\Support\Http\Api\TransactionFilter;
|
||||||
use FireflyIII\Support\Request\ChecksLogin;
|
use FireflyIII\Support\Request\ChecksLogin;
|
||||||
|
use FireflyIII\Support\Request\ConvertsDataTypes;
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ListRequest
|
* Class ListRequest
|
||||||
|
* Used specifically to list transactions.
|
||||||
*/
|
*/
|
||||||
class ListRequest extends FormRequest
|
class ListRequest extends FormRequest
|
||||||
{
|
{
|
||||||
use ChecksLogin;
|
use ChecksLogin;
|
||||||
|
use ConvertsDataTypes;
|
||||||
|
use TransactionFilter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function buildParams(): string
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
'page' => $this->getPage(),
|
||||||
|
];
|
||||||
|
|
||||||
|
$start = $this->getStartDate();
|
||||||
|
$end = $this->getEndDate();
|
||||||
|
if (null !== $start && null !== $end) {
|
||||||
|
$array['start'] = $start->format('Y-m-d');
|
||||||
|
$array['end'] = $end->format('Y-m-d');
|
||||||
|
}
|
||||||
|
if (0 !== $this->getLimit()) {
|
||||||
|
$array['limit'] = $this->getLimit();
|
||||||
|
}
|
||||||
|
return http_build_query($array);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getPage(): int
|
||||||
|
{
|
||||||
|
$page = $this->convertInteger('page');
|
||||||
|
return 0 === $page || $page > 65536 ? 1 : $page;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Carbon|null
|
||||||
|
*/
|
||||||
|
public function getStartDate(): ?Carbon
|
||||||
|
{
|
||||||
|
return $this->getCarbonDate('start');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Carbon|null
|
||||||
|
*/
|
||||||
|
public function getEndDate(): ?Carbon
|
||||||
|
{
|
||||||
|
return $this->getCarbonDate('end');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getLimit(): int
|
||||||
|
{
|
||||||
|
return $this->convertInteger('limit');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getTransactionTypes(): array
|
||||||
|
{
|
||||||
|
$type = (string)$this->get('type', 'default');
|
||||||
|
return $this->mapTransactionTypes($type);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
|
@ -675,6 +675,23 @@ trait TimeCollection
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the end time of the results to return.
|
||||||
|
*
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return GroupCollectorInterface
|
||||||
|
*/
|
||||||
|
public function setEnd(Carbon $end): GroupCollectorInterface
|
||||||
|
{
|
||||||
|
// always got to end of day / start of day for ranges.
|
||||||
|
$endStr = $end->format('Y-m-d 23:59:59');
|
||||||
|
|
||||||
|
$this->query->where('transaction_journals.date', '<=', $endStr);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Carbon $date
|
* @param Carbon $date
|
||||||
* @param string $field
|
* @param string $field
|
||||||
@ -822,6 +839,22 @@ trait TimeCollection
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the start time of the results to return.
|
||||||
|
*
|
||||||
|
* @param Carbon $start
|
||||||
|
*
|
||||||
|
* @return GroupCollectorInterface
|
||||||
|
*/
|
||||||
|
public function setStart(Carbon $start): GroupCollectorInterface
|
||||||
|
{
|
||||||
|
$startStr = $start->format('Y-m-d 00:00:00');
|
||||||
|
|
||||||
|
$this->query->where('transaction_journals.date', '>=', $startStr);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collect transactions updated on a specific date.
|
* Collect transactions updated on a specific date.
|
||||||
*
|
*
|
||||||
|
@ -1078,6 +1078,15 @@ interface GroupCollectorInterface
|
|||||||
*/
|
*/
|
||||||
public function setDestinationAccounts(Collection $accounts): GroupCollectorInterface;
|
public function setDestinationAccounts(Collection $accounts): GroupCollectorInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the end time of the results to return.
|
||||||
|
*
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return GroupCollectorInterface
|
||||||
|
*/
|
||||||
|
public function setEnd(Carbon $end): GroupCollectorInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool $expandGroupSearch
|
* @param bool $expandGroupSearch
|
||||||
*/
|
*/
|
||||||
@ -1262,6 +1271,15 @@ interface GroupCollectorInterface
|
|||||||
*/
|
*/
|
||||||
public function setSourceAccounts(Collection $accounts): GroupCollectorInterface;
|
public function setSourceAccounts(Collection $accounts): GroupCollectorInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the start time of the results to return.
|
||||||
|
*
|
||||||
|
* @param Carbon $start
|
||||||
|
*
|
||||||
|
* @return GroupCollectorInterface
|
||||||
|
*/
|
||||||
|
public function setStart(Carbon $start): GroupCollectorInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Limit results to a specific tag.
|
* Limit results to a specific tag.
|
||||||
*
|
*
|
||||||
|
59
app/Support/Binder/UserGroupAccount.php
Normal file
59
app/Support/Binder/UserGroupAccount.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AccountList.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\Support\Binder;
|
||||||
|
|
||||||
|
use FireflyIII\Models\Account;
|
||||||
|
use FireflyIII\User;
|
||||||
|
use Illuminate\Routing\Route;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UserGroupAccount.
|
||||||
|
*/
|
||||||
|
class UserGroupAccount implements BinderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
* @param Route $route
|
||||||
|
*
|
||||||
|
* @return Account
|
||||||
|
* @throws NotFoundHttpException
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static function routeBinder(string $value, Route $route): Account
|
||||||
|
{
|
||||||
|
if (auth()->check()) {
|
||||||
|
/** @var User $user */
|
||||||
|
$user = auth()->user();
|
||||||
|
$currency = Account::where('id', (int)$value)
|
||||||
|
->where('user_group_id', $user->user_group_id)
|
||||||
|
->first();
|
||||||
|
if (null !== $currency) {
|
||||||
|
return $currency;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new NotFoundHttpException();
|
||||||
|
}
|
||||||
|
}
|
@ -41,7 +41,7 @@ trait TransactionFilter
|
|||||||
*/
|
*/
|
||||||
protected function mapTransactionTypes(string $type): array
|
protected function mapTransactionTypes(string $type): array
|
||||||
{
|
{
|
||||||
$types = [
|
$types = [
|
||||||
'all' => [
|
'all' => [
|
||||||
TransactionType::WITHDRAWAL,
|
TransactionType::WITHDRAWAL,
|
||||||
TransactionType::DEPOSIT,
|
TransactionType::DEPOSIT,
|
||||||
@ -65,7 +65,11 @@ trait TransactionFilter
|
|||||||
'specials' => [TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,],
|
'specials' => [TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,],
|
||||||
'default' => [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER,],
|
'default' => [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER,],
|
||||||
];
|
];
|
||||||
|
$return = [];
|
||||||
return $types[$type] ?? $types['default'];
|
$parts = explode(',', $type);
|
||||||
|
foreach ($parts as $part) {
|
||||||
|
$return = array_merge($return, $types[$part] ?? $types['default']);
|
||||||
|
}
|
||||||
|
return array_unique($return);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,8 @@ abstract class AbstractTransformer extends TransformerAbstract
|
|||||||
protected ParameterBag $parameters;
|
protected ParameterBag $parameters;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* This method is called exactly ONCE from FireflyIII\Api\V2\Controllers\Controller::jsonApiList
|
||||||
|
*
|
||||||
* @param Collection $objects
|
* @param Collection $objects
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
|
@ -25,13 +25,19 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Transformers\V2;
|
namespace FireflyIII\Transformers\V2;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
|
use FireflyIII\Models\Note;
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Models\TransactionJournalMeta;
|
use FireflyIII\Models\TransactionJournalMeta;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\Support\Http\Api\ConvertsExchangeRates;
|
use FireflyIII\Support\Http\Api\ConvertsExchangeRates;
|
||||||
|
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
|
||||||
use FireflyIII\Support\NullArrayObject;
|
use FireflyIII\Support\NullArrayObject;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class TransactionGroupTransformer
|
* Class TransactionGroupTransformer
|
||||||
@ -40,9 +46,12 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
{
|
{
|
||||||
use ConvertsExchangeRates;
|
use ConvertsExchangeRates;
|
||||||
|
|
||||||
private array $currencies = [];
|
private ExchangeRateConverter $converter;
|
||||||
private TransactionCurrency $default;
|
private array $currencies = [];
|
||||||
private array $meta;
|
private TransactionCurrency $default;
|
||||||
|
private array $meta;
|
||||||
|
private array $notes;
|
||||||
|
private array $tags;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
@ -55,15 +64,12 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
/** @var array $object */
|
/** @var array $object */
|
||||||
foreach ($objects as $object) {
|
foreach ($objects as $object) {
|
||||||
foreach ($object['sums'] as $sum) {
|
foreach ($object['sums'] as $sum) {
|
||||||
$id = $sum['currency_id'];
|
$id = (int)$sum['currency_id'];
|
||||||
if (!array_key_exists($id, $currencies)) {
|
$currencies[$id] = $currencies[$id] ?? TransactionCurrency::find($sum['currency_id']);
|
||||||
$currencyObject = TransactionCurrency::find($sum['currency_id']);
|
|
||||||
$currencies[$id] = $currencyObject;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/** @var array $transaction */
|
/** @var array $transaction */
|
||||||
foreach ($object['transactions'] as $transaction) {
|
foreach ($object['transactions'] as $transaction) {
|
||||||
$id = $transaction['transaction_journal_id'];
|
$id = (int)$transaction['transaction_journal_id'];
|
||||||
$journals[$id] = [];
|
$journals[$id] = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -77,6 +83,28 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
$id = (int)$entry->transaction_journal_id;
|
$id = (int)$entry->transaction_journal_id;
|
||||||
$this->meta[$id][$entry->name] = $entry->data;
|
$this->meta[$id][$entry->name] = $entry->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// grab all notes for all journals:
|
||||||
|
$notes = Note::whereNoteableType(TransactionJournal::class)->whereIn('noteable_id', array_keys($journals))->get();
|
||||||
|
/** @var Note $note */
|
||||||
|
foreach ($notes as $note) {
|
||||||
|
$id = (int)$note->noteable_id;
|
||||||
|
$this->notes[$id] = $note;
|
||||||
|
}
|
||||||
|
|
||||||
|
// grab all tags for all journals:
|
||||||
|
$tags = DB::table('tag_transaction_journal')
|
||||||
|
->leftJoin('tags', 'tags.id', 'tag_transaction_journal.tag_id')
|
||||||
|
->whereIn('tag_transaction_journal.transaction_journal_id', array_keys($journals))
|
||||||
|
->get(['tag_transaction_journal.transaction_journal_id', 'tags.tag']);
|
||||||
|
/** @var stdClass $tag */
|
||||||
|
foreach ($tags as $tag) {
|
||||||
|
$id = (int)$tag->transaction_journal_id;
|
||||||
|
$this->tags[$id][] = $tag->tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create converter
|
||||||
|
$this->converter = new ExchangeRateConverter();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -92,6 +120,7 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
'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' => (string)$first['user_id'],
|
'user' => (string)$first['user_id'],
|
||||||
|
'user_group' => (string)$first['user_group_id'],
|
||||||
'group_title' => $group['title'] ?? null,
|
'group_title' => $group['title'] ?? null,
|
||||||
'transactions' => $this->transformTransactions($group['transactions'] ?? []),
|
'transactions' => $this->transformTransactions($group['transactions'] ?? []),
|
||||||
'links' => [
|
'links' => [
|
||||||
@ -122,108 +151,102 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
* @param array $transaction
|
* @param array $transaction
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
private function transformTransaction(array $transaction): array
|
private function transformTransaction(array $transaction): array
|
||||||
{
|
{
|
||||||
$transaction = new NullArrayObject($transaction);
|
$transaction = new NullArrayObject($transaction);
|
||||||
$journalId = (int)$transaction['transaction_journal_id'];
|
$type = $this->stringFromArray($transaction, 'transaction_type_type', TransactionType::WITHDRAWAL);
|
||||||
$meta = new NullArrayObject($this->meta[$journalId] ?? []);
|
$journalId = (int)$transaction['transaction_journal_id'];
|
||||||
$type = $this->stringFromArray($transaction, 'transaction_type_type', TransactionType::WITHDRAWAL);
|
$meta = new NullArrayObject($this->meta[$journalId] ?? []);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert and use amount:
|
||||||
|
*/
|
||||||
$amount = app('steam')->positive((string)($transaction['amount'] ?? '0'));
|
$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;
|
$foreignAmount = null;
|
||||||
$nativeForeignAmount = null;
|
$nativeForeignAmount = null;
|
||||||
if (null !== $transaction['foreign_amount']) {
|
if (null !== $transaction['foreign_amount']) {
|
||||||
|
$foreignCurrencyId = (int)$transaction['foreign_currency_id'];
|
||||||
$foreignAmount = app('steam')->positive($transaction['foreign_amount']);
|
$foreignAmount = app('steam')->positive($transaction['foreign_amount']);
|
||||||
$nativeForeignAmount = $foreignAmount;
|
$nativeForeignAmount = $this->converter->convert($this->default, $this->currencies[$foreignCurrencyId], $transaction['date'], $foreignAmount);
|
||||||
if ($transaction['foreign_currency_id'] !== $this->default->id) {
|
|
||||||
$rate = $this->getRate($this->currencies[$transaction['foreign_currency_id']], $this->default, $transaction['date']);
|
|
||||||
$nativeForeignAmount = bcmul($foreignAmount, $rate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$nativeAmount = $amount;
|
|
||||||
if ($transaction['currency_id'] !== $this->default->id) {
|
|
||||||
$rate = $this->getRate($this->currencies[$transaction['currency_id']], $this->default, $transaction['date']);
|
|
||||||
$nativeAmount = bcmul($amount, $rate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'user' => (string)$transaction['user_id'],
|
'user' => (string)$transaction['user_id'],
|
||||||
'transaction_journal_id' => (string)$transaction['transaction_journal_id'],
|
'user_group' => (string)$transaction['user_group_id'],
|
||||||
'type' => strtolower($type),
|
'transaction_journal_id' => (string)$transaction['transaction_journal_id'],
|
||||||
'date' => $transaction['date']->toAtomString(),
|
'type' => strtolower($type),
|
||||||
'order' => $transaction['order'],
|
'date' => $transaction['date']->toAtomString(),
|
||||||
'currency_id' => (string)$transaction['currency_id'],
|
'order' => $transaction['order'],
|
||||||
'currency_code' => $transaction['currency_code'],
|
'amount' => $amount,
|
||||||
'currency_name' => $transaction['currency_name'],
|
'native_amount' => $nativeAmount,
|
||||||
'currency_symbol' => $transaction['currency_symbol'],
|
'foreign_amount' => $foreignAmount,
|
||||||
'currency_decimal_places' => (int)$transaction['currency_decimal_places'],
|
'native_foreign_amount' => $nativeForeignAmount,
|
||||||
|
'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'],
|
||||||
|
|
||||||
// converted to native currency
|
// converted to native currency
|
||||||
'native_currency_converted' => $transaction['currency_id'] !== $this->default->id,
|
'native_id' => (string)$this->default->id,
|
||||||
'native_currency_id' => (string)$this->default->id,
|
'native_code' => $this->default->code,
|
||||||
'native_currency_code' => $this->default->code,
|
'native_name' => $this->default->name,
|
||||||
'native_currency_name' => $this->default->name,
|
'native_symbol' => $this->default->symbol,
|
||||||
'native_currency_symbol' => $this->default->symbol,
|
'native_decimal_places' => (int)$this->default->decimal_places,
|
||||||
'native_currency_decimal_places' => (int)$this->default->decimal_places,
|
|
||||||
|
|
||||||
|
// foreign currency amount:
|
||||||
'foreign_currency_id' => $this->stringFromArray($transaction, 'foreign_currency_id', null),
|
'foreign_currency_id' => $this->stringFromArray($transaction, 'foreign_currency_id', null),
|
||||||
'foreign_currency_code' => $transaction['foreign_currency_code'],
|
'foreign_currency_code' => $transaction['foreign_currency_code'],
|
||||||
'foreign_currency_name' => $transaction['foreign_currency_name'],
|
'foreign_currency_name' => $transaction['foreign_currency_name'],
|
||||||
'foreign_currency_symbol' => $transaction['foreign_currency_symbol'],
|
'foreign_currency_symbol' => $transaction['foreign_currency_symbol'],
|
||||||
'foreign_currency_decimal_places' => $transaction['foreign_currency_decimal_places'],
|
'foreign_currency_decimal_places' => $transaction['foreign_currency_decimal_places'],
|
||||||
|
|
||||||
// foreign converted to native currency:
|
// foreign converted to native:
|
||||||
'foreign_currency_converted' => null !== $transaction['foreign_currency_id'] && $transaction['foreign_currency_id'] !== $this->default->id,
|
'description' => $transaction['description'],
|
||||||
|
'source_id' => (string)$transaction['source_account_id'],
|
||||||
'amount' => $amount,
|
'source_name' => $transaction['source_account_name'],
|
||||||
'native_amount' => $nativeAmount,
|
'source_iban' => $transaction['source_account_iban'],
|
||||||
'foreign_amount' => $foreignAmount,
|
'source_type' => $transaction['source_account_type'],
|
||||||
'native_foreign_amount' => $nativeForeignAmount,
|
'destination_id' => (string)$transaction['destination_account_id'],
|
||||||
'description' => $transaction['description'],
|
'destination_name' => $transaction['destination_account_name'],
|
||||||
'source_id' => (string)$transaction['source_account_id'],
|
'destination_iban' => $transaction['destination_account_iban'],
|
||||||
'source_name' => $transaction['source_account_name'],
|
'destination_type' => $transaction['destination_account_type'],
|
||||||
'source_iban' => $transaction['source_account_iban'],
|
'budget_id' => $this->stringFromArray($transaction, 'budget_id', null),
|
||||||
'source_type' => $transaction['source_account_type'],
|
'budget_name' => $transaction['budget_name'],
|
||||||
'destination_id' => (string)$transaction['destination_account_id'],
|
'category_id' => $this->stringFromArray($transaction, 'category_id', null),
|
||||||
'destination_name' => $transaction['destination_account_name'],
|
'category_name' => $transaction['category_name'],
|
||||||
'destination_iban' => $transaction['destination_account_iban'],
|
'bill_id' => $this->stringFromArray($transaction, 'bill_id', null),
|
||||||
'destination_type' => $transaction['destination_account_type'],
|
'bill_name' => $transaction['bill_name'],
|
||||||
'budget_id' => $this->stringFromArray($transaction, 'budget_id', null),
|
'reconciled' => $transaction['reconciled'],
|
||||||
'budget_name' => $transaction['budget_name'],
|
'notes' => $this->notes[$journalId] ?? null,
|
||||||
'category_id' => $this->stringFromArray($transaction, 'category_id', null),
|
'tags' => $this->tags[$journalId] ?? [],
|
||||||
'category_name' => $transaction['category_name'],
|
'internal_reference' => $meta['internal_reference'],
|
||||||
'bill_id' => $this->stringFromArray($transaction, 'bill_id', null),
|
'external_id' => $meta['external_id'],
|
||||||
'bill_name' => $transaction['bill_name'],
|
'original_source' => $meta['original_source'],
|
||||||
'reconciled' => $transaction['reconciled'],
|
'recurrence_id' => $meta['recurrence_id'],
|
||||||
|
'recurrence_total' => $meta['recurrence_total'],
|
||||||
//'notes' => $this->groupRepos->getNoteText((int) $row['transaction_journal_id']),
|
'recurrence_count' => $meta['recurrence_count'],
|
||||||
//'tags' => $this->groupRepos->getTags((int) $row['transaction_journal_id']),
|
'bunq_payment_id' => $meta['bunq_payment_id'],
|
||||||
|
'external_url' => $meta['external_url'],
|
||||||
'internal_reference' => $meta['internal_reference'],
|
'import_hash_v2' => $meta['import_hash_v2'],
|
||||||
'external_id' => $meta['external_id'],
|
'sepa_cc' => $meta['sepa_cc'],
|
||||||
'original_source' => $meta['original_source'],
|
'sepa_ct_op' => $meta['sepa_ct_op'],
|
||||||
'recurrence_id' => $meta['recurrence_id'],
|
'sepa_ct_id' => $meta['sepa_ct_id'],
|
||||||
'recurrence_total' => $meta['recurrence_total'],
|
'sepa_db' => $meta['sepa_db'],
|
||||||
'recurrence_count' => $meta['recurrence_count'],
|
'sepa_country' => $meta['sepa_country'],
|
||||||
'bunq_payment_id' => $meta['bunq_payment_id'],
|
'sepa_ep' => $meta['sepa_ep'],
|
||||||
'external_url' => $meta['external_url'],
|
'sepa_ci' => $meta['sepa_ci'],
|
||||||
'import_hash_v2' => $meta['import_hash_v2'],
|
'sepa_batch_id' => $meta['sepa_batch_id'],
|
||||||
'sepa_cc' => $meta['sepa_cc'],
|
'interest_date' => $this->date($meta['interest_date']),
|
||||||
'sepa_ct_op' => $meta['sepa_ct_op'],
|
'book_date' => $this->date($meta['book_date']),
|
||||||
'sepa_ct_id' => $meta['sepa_ct_id'],
|
'process_date' => $this->date($meta['process_date']),
|
||||||
'sepa_db' => $meta['sepa_db'],
|
'due_date' => $this->date($meta['due_date']),
|
||||||
'sepa_country' => $meta['sepa_country'],
|
'payment_date' => $this->date($meta['payment_date']),
|
||||||
'sepa_ep' => $meta['sepa_ep'],
|
'invoice_date' => $this->date($meta['invoice_date']),
|
||||||
'sepa_ci' => $meta['sepa_ci'],
|
|
||||||
'sepa_batch_id' => $meta['sepa_batch_id'],
|
|
||||||
|
|
||||||
'interest_date' => $this->date($meta['interest_date']),
|
|
||||||
'book_date' => $this->date($meta['book_date']),
|
|
||||||
'process_date' => $this->date($meta['process_date']),
|
|
||||||
'due_date' => $this->date($meta['due_date']),
|
|
||||||
'payment_date' => $this->date($meta['payment_date']),
|
|
||||||
'invoice_date' => $this->date($meta['invoice_date']),
|
|
||||||
|
|
||||||
// location data
|
// location data
|
||||||
// 'longitude' => $longitude,
|
// 'longitude' => $longitude,
|
||||||
@ -237,6 +260,9 @@ class TransactionGroupTransformer extends AbstractTransformer
|
|||||||
/**
|
/**
|
||||||
* TODO also in the old transformer.
|
* TODO also in the old transformer.
|
||||||
*
|
*
|
||||||
|
* Used to extract a value from the given array, and fall back on a sensible default or NULL
|
||||||
|
* if it can't be helped.
|
||||||
|
*
|
||||||
* @param NullArrayObject $array
|
* @param NullArrayObject $array
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param string|null $default
|
* @param string|null $default
|
||||||
|
@ -60,6 +60,7 @@ use FireflyIII\Support\Binder\EitherConfigKey;
|
|||||||
use FireflyIII\Support\Binder\JournalList;
|
use FireflyIII\Support\Binder\JournalList;
|
||||||
use FireflyIII\Support\Binder\TagList;
|
use FireflyIII\Support\Binder\TagList;
|
||||||
use FireflyIII\Support\Binder\TagOrId;
|
use FireflyIII\Support\Binder\TagOrId;
|
||||||
|
use FireflyIII\Support\Binder\UserGroupAccount;
|
||||||
use FireflyIII\TransactionRules\Actions\AddTag;
|
use FireflyIII\TransactionRules\Actions\AddTag;
|
||||||
use FireflyIII\TransactionRules\Actions\AppendDescription;
|
use FireflyIII\TransactionRules\Actions\AppendDescription;
|
||||||
use FireflyIII\TransactionRules\Actions\AppendDescriptionToNotes;
|
use FireflyIII\TransactionRules\Actions\AppendDescriptionToNotes;
|
||||||
@ -476,6 +477,9 @@ return [
|
|||||||
'dynamicConfigKey' => DynamicConfigKey::class,
|
'dynamicConfigKey' => DynamicConfigKey::class,
|
||||||
'eitherConfigKey' => EitherConfigKey::class,
|
'eitherConfigKey' => EitherConfigKey::class,
|
||||||
|
|
||||||
|
// V2 API endpoints:
|
||||||
|
'userGroupAccount' => UserGroupAccount::class,
|
||||||
|
|
||||||
|
|
||||||
],
|
],
|
||||||
'rule-actions' => [
|
'rule-actions' => [
|
||||||
|
@ -58,7 +58,8 @@ Route::group(
|
|||||||
'as' => 'api.v2.',
|
'as' => 'api.v2.',
|
||||||
],
|
],
|
||||||
static function () {
|
static function () {
|
||||||
Route::get('accounts/{account}/transactions', ['uses' => 'AccountController@listTransactions', 'as' => 'accounts.transactions']);
|
Route::get('transactions', ['uses' => 'TransactionController@list', 'as' => 'transactions.list']);
|
||||||
|
Route::get('accounts/{userGroupAccount}/transactions', ['uses' => 'AccountController@list', 'as' => 'accounts.transactions']);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user