mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-01-26 16:26:35 -06:00
Expand code.
This commit is contained in:
parent
e0c9d3627e
commit
f52144d8d9
@ -21,10 +21,13 @@
|
||||
|
||||
namespace FireflyIII\Api\V2\Controllers;
|
||||
|
||||
use FireflyIII\Transformers\AbstractTransformer;
|
||||
use FireflyIII\Transformers\V2\AbstractTransformer;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
use League\Fractal\Manager;
|
||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\Serializer\JsonApiSerializer;
|
||||
|
||||
@ -34,6 +37,19 @@ use League\Fractal\Serializer\JsonApiSerializer;
|
||||
class Controller extends BaseController
|
||||
{
|
||||
protected const CONTENT_TYPE = 'application/vnd.api+json';
|
||||
protected int $pageSize;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->pageSize = 50;
|
||||
if (auth()->check()) {
|
||||
$this->pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a JSON API object and returns it.
|
||||
*
|
||||
@ -53,4 +69,23 @@ class Controller extends BaseController
|
||||
return $manager->createData($resource)->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param LengthAwarePaginator $paginator
|
||||
* @param AbstractTransformer $transformer
|
||||
* @return array
|
||||
*/
|
||||
final protected function jsonApiList(string $key, LengthAwarePaginator $paginator, AbstractTransformer $transformer): array
|
||||
{
|
||||
$manager = new Manager;
|
||||
$baseUrl = request()->getSchemeAndHttpHost() . '/api/v2';
|
||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||
|
||||
$objects = $paginator->getCollection();
|
||||
$resource = new FractalCollection($objects, $transformer, $key);
|
||||
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||
|
||||
return $manager->createData($resource)->toArray();
|
||||
}
|
||||
|
||||
}
|
||||
|
44
app/Api/V2/Controllers/Model/Account/ShowController.php
Normal file
44
app/Api/V2/Controllers/Model/Account/ShowController.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/*
|
||||
* ShowController.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\Api\V2\Controllers\Model\Account;
|
||||
|
||||
use FireflyIII\Api\V2\Controllers\Controller;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Transformers\V2\AccountTransformer;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
/**
|
||||
* Class ShowController
|
||||
*/
|
||||
class ShowController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function show(Account $account): JsonResponse
|
||||
{
|
||||
$transformer = new AccountTransformer;
|
||||
return response()->json($this->jsonApiObject('accounts', $account, $transformer));
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@ namespace FireflyIII\Api\V2\Controllers\System;
|
||||
|
||||
use FireflyIII\Api\V2\Controllers\Controller;
|
||||
use FireflyIII\Models\Preference;
|
||||
use FireflyIII\Transformers\PreferenceTransformer;
|
||||
use FireflyIII\Transformers\V2\PreferenceTransformer;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/*
|
||||
* AccountController.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\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\Models\Account;
|
||||
use FireflyIII\Support\Http\Api\TransactionFilter;
|
||||
use FireflyIII\Transformers\V2\PreferenceTransformer;
|
||||
use FireflyIII\Transformers\V2\TransactionGroupTransformer;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Class AccountController
|
||||
*/
|
||||
class AccountController extends Controller
|
||||
{
|
||||
use TransactionFilter;
|
||||
|
||||
/**
|
||||
* @param ListRequest $request
|
||||
* @param Account $account
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function listTransactions(ListRequest $request, Account $account): JsonResponse
|
||||
{
|
||||
// collect transactions:
|
||||
$type = $request->get('type') ?? 'default';
|
||||
$limit = (int) $request->get('limit');
|
||||
$page = (int) $request->get('page');
|
||||
$page = max($page, 1);
|
||||
|
||||
if ($limit > 0 && $limit <= $this->pageSize) {
|
||||
$this->pageSize = $limit;
|
||||
}
|
||||
|
||||
$types = $this->mapTransactionTypes($type);
|
||||
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setAccounts(new Collection([$account]))
|
||||
->withAPIInformation()
|
||||
->setLimit($this->pageSize)
|
||||
->setPage($page)
|
||||
->setTypes($types);
|
||||
|
||||
// TODO date filter
|
||||
//if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
|
||||
// $collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
|
||||
//}
|
||||
|
||||
$paginator = $collector->getPaginatedGroups();
|
||||
$paginator->setPath(route('api.v2.accounts.transactions', [$account->id])); // TODO . $this->buildParams()
|
||||
|
||||
return response()
|
||||
->json($this->jsonApiList('transactions', $paginator, new TransactionGroupTransformer))
|
||||
->header('Content-Type', self::CONTENT_TYPE);
|
||||
}
|
||||
|
||||
}
|
44
app/Api/V2/Request/Transaction/ListRequest.php
Normal file
44
app/Api/V2/Request/Transaction/ListRequest.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/*
|
||||
* ListRequest.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\Api\V2\Request\Transaction;
|
||||
|
||||
use FireflyIII\Support\Request\ChecksLogin;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
/**
|
||||
* Class ListRequest
|
||||
*/
|
||||
class ListRequest extends FormRequest
|
||||
{
|
||||
use ChecksLogin;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'start' => 'date',
|
||||
'end' => 'date|after:start',
|
||||
];
|
||||
}
|
||||
}
|
68
app/Api/V2/Response/Sum/AutoSum.php
Normal file
68
app/Api/V2/Response/Sum/AutoSum.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/*
|
||||
* AutoSum.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\Api\V2\Response\Sum;
|
||||
|
||||
use Closure;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Class AutoSum
|
||||
* @deprecated
|
||||
*/
|
||||
class AutoSum
|
||||
{
|
||||
/**
|
||||
* @param Collection $objects
|
||||
* @param Closure $getCurrency
|
||||
* @param Closure $getSum
|
||||
* @return array
|
||||
*/
|
||||
public function autoSum(Collection $objects, Closure $getCurrency, Closure $getSum): array
|
||||
{
|
||||
$return = [];
|
||||
/** @var Model $object */
|
||||
foreach ($objects as $object) {
|
||||
/** @var TransactionCurrency $currency */
|
||||
$currency = $getCurrency($object);
|
||||
/** @var string $amount */
|
||||
$amount = $getSum($object);
|
||||
|
||||
$return[$currency->id] = $return[$currency->id] ?? [
|
||||
'id' => (string) $currency->id,
|
||||
'name' => $currency->name,
|
||||
'symbol' => $currency->symbol,
|
||||
'code' => $currency->code,
|
||||
'decimal_places' => $currency->decimal_places,
|
||||
'sum' => '0',
|
||||
];
|
||||
|
||||
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], $amount);
|
||||
}
|
||||
|
||||
var_dump(array_values($return));
|
||||
exit;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -42,10 +42,8 @@ class NetWorth implements NetWorthInterface
|
||||
{
|
||||
private AccountRepositoryInterface $accountRepository;
|
||||
|
||||
/** @var CurrencyRepositoryInterface */
|
||||
private $currencyRepos;
|
||||
/** @var User */
|
||||
private $user;
|
||||
private CurrencyRepositoryInterface $currencyRepos;
|
||||
private User $user;
|
||||
|
||||
/**
|
||||
* Returns the user's net worth in an array with the following layout:
|
||||
|
@ -123,9 +123,8 @@ class Account extends Model
|
||||
/** @var array Fields that can be filled */
|
||||
protected $fillable = ['user_id', 'account_type_id', 'name', 'active', 'virtual_balance', 'iban'];
|
||||
/** @var array Hidden from view */
|
||||
protected $hidden = ['encrypted'];
|
||||
/** @var bool */
|
||||
private $joinedAccountTypes;
|
||||
protected $hidden = ['encrypted'];
|
||||
private bool $joinedAccountTypes = false;
|
||||
|
||||
/**
|
||||
* Route binder. Converts the key in the URL to the specified object (or throw 404).
|
||||
@ -142,7 +141,7 @@ class Account extends Model
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
/** @var Account $account */
|
||||
$account = $user->accounts()->find($accountId);
|
||||
$account = $user->accounts()->with(['accountType'])->find($accountId);
|
||||
if (null !== $account) {
|
||||
return $account;
|
||||
}
|
||||
@ -250,7 +249,7 @@ class Account extends Model
|
||||
*/
|
||||
public function scopeAccountTypeIn(EloquentBuilder $query, array $types): void
|
||||
{
|
||||
if (null === $this->joinedAccountTypes) {
|
||||
if (false === $this->joinedAccountTypes) {
|
||||
$query->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id');
|
||||
$this->joinedAccountTypes = true;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
47
frontend/src/api/v2/accounts/get.js
Normal file
47
frontend/src/api/v2/accounts/get.js
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* get.js
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
import Api from "src/api/v2/root/api";
|
||||
|
||||
export default class Get extends Api {
|
||||
constructor() {
|
||||
super('accounts'); // call the super class constructor and pass in the name parameter
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param identifier
|
||||
* @param date
|
||||
* @returns {Promise<AxiosResponse<any>>}
|
||||
*/
|
||||
get(identifier, date) {
|
||||
let params = {date: date};
|
||||
if(!date) {
|
||||
return this.apiGet(identifier);
|
||||
}
|
||||
return this.apiGet(identifier, params);
|
||||
}
|
||||
transactions(identifier, params) {
|
||||
if(!params) {
|
||||
return this.apiGetTransactions(identifier);
|
||||
}
|
||||
return this.apiGetTransactions(identifier, params);
|
||||
}
|
||||
}
|
68
frontend/src/api/v2/root/api.js
Normal file
68
frontend/src/api/v2/root/api.js
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* api.js
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
import {api} from "boot/axios";
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
export default class Api {
|
||||
root = '/api/v2/';
|
||||
path = '';
|
||||
|
||||
constructor(path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
apiPath() {
|
||||
return this.root + this.path;
|
||||
}
|
||||
|
||||
apiPathWithObject(object) {
|
||||
return this.root + this.path + '/' + object;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param object
|
||||
* @param params
|
||||
* @returns {Promise<AxiosResponse<any>>}
|
||||
*/
|
||||
apiGet(object, params) {
|
||||
let url = this.apiPathWithObject(object);
|
||||
if (params) {
|
||||
return api.get(url, {params: params});
|
||||
}
|
||||
return api.get(url);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param object
|
||||
* @param params
|
||||
* @returns {Promise<AxiosResponse<any>>}
|
||||
*/
|
||||
apiGetTransactions(object, params) {
|
||||
let url = this.apiPathWithObject(object) + '/transactions';
|
||||
if (params) {
|
||||
return api.get(url, {params: params});
|
||||
}
|
||||
return api.get(url);
|
||||
}
|
||||
}
|
105
frontend/src/components/dashboard/TransactionList.vue
Normal file
105
frontend/src/components/dashboard/TransactionList.vue
Normal file
@ -0,0 +1,105 @@
|
||||
<!--
|
||||
- TransactionList.vue
|
||||
- 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/>.
|
||||
-->
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<q-card bordered>
|
||||
<q-item>
|
||||
<q-item-section>
|
||||
<q-item-label><strong>{{ accountName }}</strong></q-item-label>
|
||||
</q-item-section>
|
||||
</q-item>
|
||||
<q-separator/>
|
||||
<q-item>
|
||||
<q-card-section horizontal>
|
||||
Content
|
||||
</q-card-section>
|
||||
</q-item>
|
||||
|
||||
|
||||
<!-- X: {{ accountId }} -->
|
||||
</q-card>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import Get from "../../api/v2/accounts/get";
|
||||
import {useFireflyIIIStore} from "../../stores/fireflyiii";
|
||||
import {format} from "date-fns";
|
||||
|
||||
export default {
|
||||
name: "TransactionList",
|
||||
props: {
|
||||
accountId: 0,
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
store: null,
|
||||
accountName: ''
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.store = useFireflyIIIStore();
|
||||
if (0 !== this.accountId) {
|
||||
this.getAccount();
|
||||
// TODO this code snippet is recycled a lot.
|
||||
// subscribe, then update:
|
||||
this.store.$onAction(
|
||||
({name, $store, args, after, onError,}) => {
|
||||
after((result) => {
|
||||
if (name === 'setRange') {
|
||||
this.getTransactions();
|
||||
}
|
||||
})
|
||||
}
|
||||
)
|
||||
this.getTransactions();
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
getAccount: function () {
|
||||
(new Get).get(this.accountId).then((response) => this.parseAccount(response.data));
|
||||
},
|
||||
parseAccount: function (data) {
|
||||
this.accountName = data.data.attributes.name;
|
||||
},
|
||||
getTransactions: function () {
|
||||
if (null !== this.store.getRange.start && null !== this.store.getRange.end) {
|
||||
const start = new Date(this.store.getRange.start);
|
||||
const end = new Date(this.store.getRange.end);
|
||||
let startStr = format(start, 'y-MM-dd');
|
||||
let endStr = format(end, 'y-MM-dd');
|
||||
(new Get).transactions(this.accountId, {
|
||||
start: startStr,
|
||||
end: endStr
|
||||
}).then((response) => this.parseTransactions(response.data));
|
||||
}
|
||||
},
|
||||
parseTransactions: function () {
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
@ -19,15 +19,42 @@
|
||||
-->
|
||||
|
||||
<template>
|
||||
|
||||
<div class="row">
|
||||
<div class="col q-mr-sm" v-for="(account, index) in accounts">
|
||||
<TransactionList :account-id="account" />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import Preferences from "../../api/v2/preferences";
|
||||
import {defineAsyncComponent} from "vue";
|
||||
|
||||
export default {
|
||||
name: "TransactionLists"
|
||||
name: "TransactionLists",
|
||||
components: {
|
||||
TransactionList: defineAsyncComponent(() => import('./TransactionList.vue')),
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
accounts: [],
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.getAccounts();
|
||||
},
|
||||
methods: {
|
||||
getAccounts: function() {
|
||||
(new Preferences).get('frontpageAccounts').then((response) => this.parseAccounts(response.data));
|
||||
},
|
||||
parseAccounts: function(data) {
|
||||
const content = data.data.attributes.data;
|
||||
for(let i in content) {
|
||||
if(content.hasOwnProperty(i)) {
|
||||
this.accounts.push(content[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
||||
|
@ -89,12 +89,11 @@
|
||||
|
||||
<script>
|
||||
import {defineAsyncComponent} from "vue";
|
||||
import TransactionLists from "../../components/dashboard/TransactionLists";
|
||||
|
||||
export default {
|
||||
name: "Dashboard",
|
||||
components: {
|
||||
TransactionLists,
|
||||
TransactionLists: defineAsyncComponent(() => import("../../components/dashboard/TransactionLists.vue")),
|
||||
AccountChart: defineAsyncComponent(() => import('../../components/dashboard/AccountChart.vue')),
|
||||
NetWorthInsightBox: defineAsyncComponent(() => import('../../components/dashboard/NetWorthInsightBox.vue')),
|
||||
BillInsightBox: defineAsyncComponent(() => import('../../components/dashboard/BillInsightBox.vue')),
|
||||
|
@ -33,6 +33,17 @@ Route::group(
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* V2 API route for TransactionList API endpoints
|
||||
*/
|
||||
Route::group(
|
||||
['namespace' => 'FireflyIII\Api\V2\Controllers\Transaction\List', 'prefix' => 'v2',
|
||||
'as' => 'api.v2.',],
|
||||
static function () {
|
||||
Route::get('accounts/{account}/transactions', ['uses' => 'AccountController@listTransactions', 'as' => 'accounts.transactions']);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* V2 API route for net worth endpoint(s);
|
||||
*/
|
||||
@ -55,6 +66,17 @@ Route::group(
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* V2 API route for accounts.
|
||||
*/
|
||||
Route::group(
|
||||
['namespace' => 'FireflyIII\Api\V2\Controllers\Model\Account', 'prefix' => 'v2/accounts',
|
||||
'as' => 'api.v2.accounts.',],
|
||||
static function () {
|
||||
Route::get('{account}', ['uses' => 'ShowController@show', 'as' => 'show']);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* V2 API route for bills.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user