mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-06 12:45:30 +00:00
Update API to convert to native.
This commit is contained in:
@@ -29,7 +29,9 @@ use FireflyIII\Api\V1\Requests\Insight\GenericRequest;
|
|||||||
use FireflyIII\Enums\TransactionTypeEnum;
|
use FireflyIII\Enums\TransactionTypeEnum;
|
||||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||||
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||||
|
use FireflyIII\Support\Facades\Amount;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BillController
|
* Class BillController
|
||||||
@@ -63,11 +65,13 @@ class BillController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function bill(GenericRequest $request): JsonResponse
|
public function bill(GenericRequest $request): JsonResponse
|
||||||
{
|
{
|
||||||
$accounts = $request->getAssetAccounts();
|
$accounts = $request->getAssetAccounts();
|
||||||
$bills = $request->getBills();
|
$bills = $request->getBills();
|
||||||
$start = $request->getStart();
|
$start = $request->getStart();
|
||||||
$end = $request->getEnd();
|
$end = $request->getEnd();
|
||||||
$response = [];
|
$convertToNative = Amount::convertToNative();
|
||||||
|
$default = Amount::getDefaultCurrency();
|
||||||
|
$response = [];
|
||||||
|
|
||||||
// get all bills:
|
// get all bills:
|
||||||
if (0 === $bills->count()) {
|
if (0 === $bills->count()) {
|
||||||
@@ -75,40 +79,43 @@ class BillController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
// collect all expenses in this period (regardless of type) by the given bills and accounts.
|
// collect all expenses in this period (regardless of type) by the given bills and accounts.
|
||||||
$collector = app(GroupCollectorInterface::class);
|
$collector = app(GroupCollectorInterface::class);
|
||||||
$collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value])->setRange($start, $end)->setSourceAccounts($accounts);
|
$collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value])->setRange($start, $end)->setSourceAccounts($accounts);
|
||||||
$collector->setBills($bills);
|
$collector->setBills($bills);
|
||||||
|
|
||||||
$genericSet = $collector->getExtractedJournals();
|
$genericSet = $collector->getExtractedJournals();
|
||||||
foreach ($genericSet as $journal) {
|
foreach ($genericSet as $journal) {
|
||||||
$billId = (int) $journal['bill_id'];
|
$billId = (int) $journal['bill_id'];
|
||||||
$currencyId = (int) $journal['currency_id'];
|
$currencyId = (int) $journal['currency_id'];
|
||||||
$foreignCurrencyId = (int) $journal['foreign_currency_id'];
|
$currencyCode = $journal['currency_code'];
|
||||||
$key = sprintf('%d-%d', $billId, $currencyId);
|
$field = 'amount';
|
||||||
$foreignKey = sprintf('%d-%d', $billId, $foreignCurrencyId);
|
|
||||||
|
// use the native amount if the user wants to convert to native currency
|
||||||
|
if ($convertToNative && $currencyId !== $default->id) {
|
||||||
|
$currencyId = $default->id;
|
||||||
|
$currencyCode = $default->code;
|
||||||
|
$field = 'native_amount';
|
||||||
|
}
|
||||||
|
// use foreign amount when the foreign currency IS the default currency.
|
||||||
|
if ($convertToNative && $journal['currency_id'] !== $default->id && $default->id === $journal['foreign_currency_id']) {
|
||||||
|
$field = 'foreign_amount';
|
||||||
|
}
|
||||||
|
Log::debug(sprintf('Journal #%d in bill #%d will use %s (%s %s)', $journal['transaction_group_id'], $billId, $field, $currencyCode, $journal[$field] ?? '0'));
|
||||||
|
|
||||||
|
$key = sprintf('%d-%d', $billId, $currencyId);
|
||||||
|
|
||||||
if (0 !== $currencyId) {
|
if (0 !== $currencyId) {
|
||||||
$response[$key] ??= [
|
$response[$key] ??= [
|
||||||
'id' => (string) $billId,
|
'id' => (string) $billId,
|
||||||
'name' => $journal['bill_name'],
|
'name' => $journal['bill_name'],
|
||||||
'difference' => '0',
|
'difference' => '0',
|
||||||
'difference_float' => 0,
|
'difference_float' => 0,
|
||||||
'currency_id' => (string) $currencyId,
|
'currency_id' => (string) $currencyId,
|
||||||
'currency_code' => $journal['currency_code'],
|
'currency_code' => $currencyCode,
|
||||||
];
|
];
|
||||||
$response[$key]['difference'] = bcadd($response[$key]['difference'], $journal['amount']);
|
$response[$key]['difference'] = bcadd($response[$key]['difference'], (string) ($journal[$field] ?? '0'));
|
||||||
$response[$key]['difference_float'] = (float) $response[$key]['difference']; // intentional float
|
$response[$key]['difference_float'] = (float) $response[$key]['difference']; // intentional float
|
||||||
}
|
}
|
||||||
if (0 !== $foreignCurrencyId) {
|
|
||||||
$response[$foreignKey] ??= [
|
|
||||||
'difference' => '0',
|
|
||||||
'difference_float' => 0,
|
|
||||||
'currency_id' => (string) $foreignCurrencyId,
|
|
||||||
'currency_code' => $journal['foreign_currency_code'],
|
|
||||||
];
|
|
||||||
$response[$foreignKey]['difference'] = bcadd($response[$foreignKey]['difference'], $journal['foreign_amount']);
|
|
||||||
$response[$foreignKey]['difference_float'] = (float) $response[$foreignKey]['difference']; // intentional float
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json(array_values($response));
|
return response()->json(array_values($response));
|
||||||
@@ -122,42 +129,47 @@ class BillController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function noBill(GenericRequest $request): JsonResponse
|
public function noBill(GenericRequest $request): JsonResponse
|
||||||
{
|
{
|
||||||
$accounts = $request->getAssetAccounts();
|
$accounts = $request->getAssetAccounts();
|
||||||
$start = $request->getStart();
|
$start = $request->getStart();
|
||||||
$end = $request->getEnd();
|
$end = $request->getEnd();
|
||||||
$response = [];
|
$convertToNative = Amount::convertToNative();
|
||||||
|
$default = Amount::getDefaultCurrency();
|
||||||
|
$response = [];
|
||||||
|
|
||||||
// collect all expenses in this period (regardless of type) by the given bills and accounts.
|
// collect all expenses in this period (regardless of type) by the given bills and accounts.
|
||||||
$collector = app(GroupCollectorInterface::class);
|
$collector = app(GroupCollectorInterface::class);
|
||||||
$collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value])->setRange($start, $end)->setSourceAccounts($accounts);
|
$collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value])->setRange($start, $end)->setSourceAccounts($accounts);
|
||||||
$collector->withoutBill();
|
$collector->withoutBill();
|
||||||
|
|
||||||
$genericSet = $collector->getExtractedJournals();
|
$genericSet = $collector->getExtractedJournals();
|
||||||
|
|
||||||
foreach ($genericSet as $journal) {
|
foreach ($genericSet as $journal) {
|
||||||
$currencyId = (int) $journal['currency_id'];
|
$currencyId = (int) $journal['currency_id'];
|
||||||
$foreignCurrencyId = (int) $journal['foreign_currency_id'];
|
$currencyCode = $journal['currency_code'];
|
||||||
|
$field = 'amount';
|
||||||
|
|
||||||
|
// use the native amount if the user wants to convert to native currency
|
||||||
|
if ($convertToNative && $currencyId !== $default->id) {
|
||||||
|
$currencyId = $default->id;
|
||||||
|
$currencyCode = $default->code;
|
||||||
|
$field = 'native_amount';
|
||||||
|
}
|
||||||
|
// use foreign amount when the foreign currency IS the default currency.
|
||||||
|
if ($convertToNative && $journal['currency_id'] !== $default->id && $default->id === $journal['foreign_currency_id']) {
|
||||||
|
$field = 'foreign_amount';
|
||||||
|
}
|
||||||
|
Log::debug(sprintf('Journal #%d will use %s (%s %s)', $journal['transaction_group_id'], $field, $currencyCode, $journal[$field] ?? '0'));
|
||||||
|
|
||||||
if (0 !== $currencyId) {
|
if (0 !== $currencyId) {
|
||||||
$response[$currencyId] ??= [
|
$response[$currencyId] ??= [
|
||||||
'difference' => '0',
|
'difference' => '0',
|
||||||
'difference_float' => 0,
|
'difference_float' => 0,
|
||||||
'currency_id' => (string) $currencyId,
|
'currency_id' => (string) $currencyId,
|
||||||
'currency_code' => $journal['currency_code'],
|
'currency_code' => $currencyCode,
|
||||||
];
|
];
|
||||||
$response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], $journal['amount']);
|
$response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], (string) ($journal[$field] ?? '0'));
|
||||||
$response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; // intentional float
|
$response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; // intentional float
|
||||||
}
|
}
|
||||||
if (0 !== $foreignCurrencyId) {
|
|
||||||
$response[$foreignCurrencyId] ??= [
|
|
||||||
'difference' => '0',
|
|
||||||
'difference_float' => 0,
|
|
||||||
'currency_id' => (string) $foreignCurrencyId,
|
|
||||||
'currency_code' => $journal['foreign_currency_code'],
|
|
||||||
];
|
|
||||||
$response[$foreignCurrencyId]['difference'] = bcadd($response[$foreignCurrencyId]['difference'], $journal['foreign_amount']);
|
|
||||||
$response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; // intentional float
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json(array_values($response));
|
return response()->json(array_values($response));
|
||||||
|
@@ -39,7 +39,7 @@ class AvailableBudgetObserver
|
|||||||
|
|
||||||
public function updated(AvailableBudget $availableBudget): void
|
public function updated(AvailableBudget $availableBudget): void
|
||||||
{
|
{
|
||||||
Log::debug('Observe "updated" of an available budget.');
|
//Log::debug('Observe "updated" of an available budget.');
|
||||||
$this->updateNativeAmount($availableBudget);
|
$this->updateNativeAmount($availableBudget);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -25,12 +25,14 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Repositories\Account;
|
namespace FireflyIII\Repositories\Account;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use FireflyIII\Enums\TransactionTypeEnum;
|
||||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Support\Facades\Amount;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Contracts\Auth\Authenticatable;
|
use Illuminate\Contracts\Auth\Authenticatable;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class OperationsRepository
|
* Class OperationsRepository
|
||||||
@@ -46,7 +48,7 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function listExpenses(Carbon $start, Carbon $end, Collection $accounts): array
|
public function listExpenses(Carbon $start, Carbon $end, Collection $accounts): array
|
||||||
{
|
{
|
||||||
$journals = $this->getTransactions($start, $end, $accounts, TransactionType::WITHDRAWAL);
|
$journals = $this->getTransactions($start, $end, $accounts, TransactionTypeEnum::WITHDRAWAL->value);
|
||||||
|
|
||||||
return $this->sortByCurrency($journals, 'negative');
|
return $this->sortByCurrency($journals, 'negative');
|
||||||
}
|
}
|
||||||
@@ -65,7 +67,7 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
return $collector->getExtractedJournals();
|
return $collector->getExtractedJournals();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setUser(null|Authenticatable|User $user): void
|
public function setUser(null | Authenticatable | User $user): void
|
||||||
{
|
{
|
||||||
if ($user instanceof User) {
|
if ($user instanceof User) {
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
@@ -76,8 +78,8 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
{
|
{
|
||||||
$array = [];
|
$array = [];
|
||||||
foreach ($journals as $journal) {
|
foreach ($journals as $journal) {
|
||||||
$currencyId = (int) $journal['currency_id'];
|
$currencyId = (int) $journal['currency_id'];
|
||||||
$journalId = (int) $journal['transaction_journal_id'];
|
$journalId = (int) $journal['transaction_journal_id'];
|
||||||
$array[$currencyId] ??= [
|
$array[$currencyId] ??= [
|
||||||
'currency_id' => $journal['currency_id'],
|
'currency_id' => $journal['currency_id'],
|
||||||
'currency_name' => $journal['currency_name'],
|
'currency_name' => $journal['currency_name'],
|
||||||
@@ -115,7 +117,7 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function listIncome(Carbon $start, Carbon $end, ?Collection $accounts = null): array
|
public function listIncome(Carbon $start, Carbon $end, ?Collection $accounts = null): array
|
||||||
{
|
{
|
||||||
$journals = $this->getTransactions($start, $end, $accounts, TransactionType::DEPOSIT);
|
$journals = $this->getTransactions($start, $end, $accounts, TransactionTypeEnum::DEPOSIT->value);
|
||||||
|
|
||||||
return $this->sortByCurrency($journals, 'positive');
|
return $this->sortByCurrency($journals, 'positive');
|
||||||
}
|
}
|
||||||
@@ -129,8 +131,9 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
?Collection $accounts = null,
|
?Collection $accounts = null,
|
||||||
?Collection $expense = null,
|
?Collection $expense = null,
|
||||||
?TransactionCurrency $currency = null
|
?TransactionCurrency $currency = null
|
||||||
): array {
|
): array
|
||||||
$journals = $this->getTransactionsForSum(TransactionType::WITHDRAWAL, $start, $end, $accounts, $expense, $currency);
|
{
|
||||||
|
$journals = $this->getTransactionsForSum(TransactionTypeEnum::WITHDRAWAL->value, $start, $end, $accounts, $expense, $currency);
|
||||||
|
|
||||||
return $this->groupByCurrency($journals, 'negative');
|
return $this->groupByCurrency($journals, 'negative');
|
||||||
}
|
}
|
||||||
@@ -146,7 +149,8 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
?Collection $accounts = null,
|
?Collection $accounts = null,
|
||||||
?Collection $opposing = null,
|
?Collection $opposing = null,
|
||||||
?TransactionCurrency $currency = null
|
?TransactionCurrency $currency = null
|
||||||
): array {
|
): array
|
||||||
|
{
|
||||||
$start->startOfDay();
|
$start->startOfDay();
|
||||||
$end->endOfDay();
|
$end->endOfDay();
|
||||||
|
|
||||||
@@ -155,7 +159,7 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
$collector->setUser($this->user)->setRange($start, $end)->setTypes([$type])->withAccountInformation();
|
$collector->setUser($this->user)->setRange($start, $end)->setTypes([$type])->withAccountInformation();
|
||||||
|
|
||||||
// depends on transaction type:
|
// depends on transaction type:
|
||||||
if (TransactionType::WITHDRAWAL === $type) {
|
if (TransactionTypeEnum::WITHDRAWAL->value === $type) {
|
||||||
if (null !== $accounts) {
|
if (null !== $accounts) {
|
||||||
$collector->setSourceAccounts($accounts);
|
$collector->setSourceAccounts($accounts);
|
||||||
}
|
}
|
||||||
@@ -163,7 +167,7 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
$collector->setDestinationAccounts($opposing);
|
$collector->setDestinationAccounts($opposing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (TransactionType::DEPOSIT === $type) {
|
if (TransactionTypeEnum::DEPOSIT->value === $type) {
|
||||||
if (null !== $accounts) {
|
if (null !== $accounts) {
|
||||||
$collector->setDestinationAccounts($accounts);
|
$collector->setDestinationAccounts($accounts);
|
||||||
}
|
}
|
||||||
@@ -172,23 +176,22 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// supports only accounts, not opposing.
|
// supports only accounts, not opposing.
|
||||||
if (TransactionType::TRANSFER === $type && null !== $accounts) {
|
if (TransactionTypeEnum::TRANSFER->value === $type && null !== $accounts) {
|
||||||
$collector->setAccounts($accounts);
|
$collector->setAccounts($accounts);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $currency) {
|
if (null !== $currency) {
|
||||||
$collector->setCurrency($currency);
|
$collector->setCurrency($currency);
|
||||||
}
|
}
|
||||||
$journals = $collector->getExtractedJournals();
|
$journals = $collector->getExtractedJournals();
|
||||||
|
|
||||||
// same but for foreign currencies:
|
// same but for foreign currencies:
|
||||||
if (null !== $currency) {
|
if (null !== $currency) {
|
||||||
/** @var GroupCollectorInterface $collector */
|
/** @var GroupCollectorInterface $collector */
|
||||||
$collector = app(GroupCollectorInterface::class);
|
$collector = app(GroupCollectorInterface::class);
|
||||||
$collector->setUser($this->user)->setRange($start, $end)->setTypes([$type])->withAccountInformation()
|
$collector->setUser($this->user)->setRange($start, $end)->setTypes([$type])->withAccountInformation()
|
||||||
->setForeignCurrency($currency)
|
->setForeignCurrency($currency);
|
||||||
;
|
if (TransactionTypeEnum::WITHDRAWAL->value === $type) {
|
||||||
if (TransactionType::WITHDRAWAL === $type) {
|
|
||||||
if (null !== $accounts) {
|
if (null !== $accounts) {
|
||||||
$collector->setSourceAccounts($accounts);
|
$collector->setSourceAccounts($accounts);
|
||||||
}
|
}
|
||||||
@@ -196,7 +199,7 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
$collector->setDestinationAccounts($opposing);
|
$collector->setDestinationAccounts($opposing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (TransactionType::DEPOSIT === $type) {
|
if (TransactionTypeEnum::DEPOSIT->value === $type) {
|
||||||
if (null !== $accounts) {
|
if (null !== $accounts) {
|
||||||
$collector->setDestinationAccounts($accounts);
|
$collector->setDestinationAccounts($accounts);
|
||||||
}
|
}
|
||||||
@@ -205,10 +208,10 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $collector->getExtractedJournals();
|
$result = $collector->getExtractedJournals();
|
||||||
|
|
||||||
// do not use array_merge because you want keys to overwrite (otherwise you get double results):
|
// do not use array_merge because you want keys to overwrite (otherwise you get double results):
|
||||||
$journals = $result + $journals;
|
$journals = $result + $journals;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $journals;
|
return $journals;
|
||||||
@@ -220,7 +223,7 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
|
|
||||||
foreach ($journals as $journal) {
|
foreach ($journals as $journal) {
|
||||||
$currencyId = (int) $journal['currency_id'];
|
$currencyId = (int) $journal['currency_id'];
|
||||||
$array[$currencyId] ??= [
|
$array[$currencyId] ??= [
|
||||||
'sum' => '0',
|
'sum' => '0',
|
||||||
'currency_id' => $currencyId,
|
'currency_id' => $currencyId,
|
||||||
'currency_name' => $journal['currency_name'],
|
'currency_name' => $journal['currency_name'],
|
||||||
@@ -231,9 +234,9 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->{$direction}($journal['amount'])); // @phpstan-ignore-line
|
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->{$direction}($journal['amount'])); // @phpstan-ignore-line
|
||||||
|
|
||||||
// also do foreign amount:
|
// also do foreign amount:
|
||||||
$foreignId = (int) $journal['foreign_currency_id'];
|
$foreignId = (int) $journal['foreign_currency_id'];
|
||||||
if (0 !== $foreignId) {
|
if (0 !== $foreignId) {
|
||||||
$array[$foreignId] ??= [
|
$array[$foreignId] ??= [
|
||||||
'sum' => '0',
|
'sum' => '0',
|
||||||
'currency_id' => $foreignId,
|
'currency_id' => $foreignId,
|
||||||
'currency_name' => $journal['foreign_currency_name'],
|
'currency_name' => $journal['foreign_currency_name'],
|
||||||
@@ -257,36 +260,65 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
?Collection $accounts = null,
|
?Collection $accounts = null,
|
||||||
?Collection $expense = null,
|
?Collection $expense = null,
|
||||||
?TransactionCurrency $currency = null
|
?TransactionCurrency $currency = null
|
||||||
): array {
|
): array
|
||||||
$journals = $this->getTransactionsForSum(TransactionType::WITHDRAWAL, $start, $end, $accounts, $expense, $currency);
|
{
|
||||||
|
$journals = $this->getTransactionsForSum(TransactionTypeEnum::WITHDRAWAL->value, $start, $end, $accounts, $expense, $currency);
|
||||||
|
|
||||||
return $this->groupByDirection($journals, 'destination', 'negative');
|
return $this->groupByDirection($journals, 'destination', 'negative');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function groupByDirection(array $journals, string $direction, string $method): array
|
private function groupByDirection(array $journals, string $direction, string $method): array
|
||||||
{
|
{
|
||||||
$array = [];
|
$array = [];
|
||||||
$idKey = sprintf('%s_account_id', $direction);
|
$idKey = sprintf('%s_account_id', $direction);
|
||||||
$nameKey = sprintf('%s_account_name', $direction);
|
$nameKey = sprintf('%s_account_name', $direction);
|
||||||
|
$convertToNative = Amount::convertToNative($this->user);
|
||||||
|
$default = Amount::getDefaultCurrencyByUserGroup($this->user->userGroup);
|
||||||
|
Log::debug(sprintf('groupByDirection(array, %s, %s).', $direction, $method));
|
||||||
foreach ($journals as $journal) {
|
foreach ($journals as $journal) {
|
||||||
$key = sprintf('%s-%s', $journal[$idKey], $journal['currency_id']);
|
// currency
|
||||||
|
$currencyId = $journal['currency_id'];
|
||||||
|
$currencyName = $journal['currency_name'];
|
||||||
|
$currencySymbol = $journal['currency_symbol'];
|
||||||
|
$currencyCode = $journal['currency_code'];
|
||||||
|
$currencyDecimalPlaces = $journal['currency_decimal_places'];
|
||||||
|
$field = $convertToNative && $currencyId !== $default->id ? 'native_amount' : 'amount';
|
||||||
|
|
||||||
|
// perhaps use default currency instead?
|
||||||
|
if ($convertToNative && $journal['currency_id'] !== $default->id) {
|
||||||
|
$currencyId = $default->id;
|
||||||
|
$currencyName = $default->name;
|
||||||
|
$currencySymbol = $default->symbol;
|
||||||
|
$currencyCode = $default->code;
|
||||||
|
$currencyDecimalPlaces = $default->decimal_places;
|
||||||
|
}
|
||||||
|
// use foreign amount when the foreign currency IS the default currency.
|
||||||
|
if ($convertToNative && $journal['currency_id'] !== $default->id && $default->id === $journal['foreign_currency_id']) {
|
||||||
|
$field = 'foreign_amount';
|
||||||
|
}
|
||||||
|
$key = sprintf('%s-%s', $journal[$idKey], $currencyId);
|
||||||
|
// sum it all up or create a new array.
|
||||||
$array[$key] ??= [
|
$array[$key] ??= [
|
||||||
'id' => $journal[$idKey],
|
'id' => $journal[$idKey],
|
||||||
'name' => $journal[$nameKey],
|
'name' => $journal[$nameKey],
|
||||||
'sum' => '0',
|
'sum' => '0',
|
||||||
'currency_id' => $journal['currency_id'],
|
'currency_id' => $currencyId,
|
||||||
'currency_name' => $journal['currency_name'],
|
'currency_name' => $currencyName,
|
||||||
'currency_symbol' => $journal['currency_symbol'],
|
'currency_symbol' => $currencySymbol,
|
||||||
'currency_code' => $journal['currency_code'],
|
'currency_code' => $currencyCode,
|
||||||
'currency_decimal_places' => $journal['currency_decimal_places'],
|
'currency_decimal_places' => $currencyDecimalPlaces,
|
||||||
];
|
];
|
||||||
$array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string) $journal['amount'])); // @phpstan-ignore-line
|
|
||||||
|
|
||||||
// also do foreign amount:
|
// add the data from the $field to the array.
|
||||||
if (0 !== (int) $journal['foreign_currency_id']) {
|
$array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string) ($journal[$field] ?? '0'))); // @phpstan-ignore-line
|
||||||
|
Log::debug(sprintf('Field for transaction #%d is "%s" (%s). Sum: %s', $journal['transaction_group_id'], $currencyCode, $field, $array[$key]['sum']));
|
||||||
|
|
||||||
|
// also do foreign amount, but only when convertToNative is false (otherwise we have it already)
|
||||||
|
// or when convertToNative is true and the foreign currency is ALSO not the default currency.
|
||||||
|
if ((!$convertToNative || $journal['foreign_currency_id'] !== $default->id) && 0 !== (int) $journal['foreign_currency_id']) {
|
||||||
|
Log::debug(sprintf('Use foreign amount from transaction #%d: %s %s. Sum: %s', $journal['transaction_group_id'], $currencyCode, $journal['foreign_amount'], $array[$key]['sum']));
|
||||||
$key = sprintf('%s-%s', $journal[$idKey], $journal['foreign_currency_id']);
|
$key = sprintf('%s-%s', $journal[$idKey], $journal['foreign_currency_id']);
|
||||||
$array[$key] ??= [
|
$array[$key] ??= [
|
||||||
'id' => $journal[$idKey],
|
'id' => $journal[$idKey],
|
||||||
'name' => $journal[$nameKey],
|
'name' => $journal[$nameKey],
|
||||||
'sum' => '0',
|
'sum' => '0',
|
||||||
@@ -312,8 +344,9 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
?Collection $accounts = null,
|
?Collection $accounts = null,
|
||||||
?Collection $expense = null,
|
?Collection $expense = null,
|
||||||
?TransactionCurrency $currency = null
|
?TransactionCurrency $currency = null
|
||||||
): array {
|
): array
|
||||||
$journals = $this->getTransactionsForSum(TransactionType::WITHDRAWAL, $start, $end, $accounts, $expense, $currency);
|
{
|
||||||
|
$journals = $this->getTransactionsForSum(TransactionTypeEnum::WITHDRAWAL->value, $start, $end, $accounts, $expense, $currency);
|
||||||
|
|
||||||
return $this->groupByDirection($journals, 'source', 'negative');
|
return $this->groupByDirection($journals, 'source', 'negative');
|
||||||
}
|
}
|
||||||
@@ -327,8 +360,9 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
?Collection $accounts = null,
|
?Collection $accounts = null,
|
||||||
?Collection $revenue = null,
|
?Collection $revenue = null,
|
||||||
?TransactionCurrency $currency = null
|
?TransactionCurrency $currency = null
|
||||||
): array {
|
): array
|
||||||
$journals = $this->getTransactionsForSum(TransactionType::DEPOSIT, $start, $end, $accounts, $revenue, $currency);
|
{
|
||||||
|
$journals = $this->getTransactionsForSum(TransactionTypeEnum::DEPOSIT->value, $start, $end, $accounts, $revenue, $currency);
|
||||||
|
|
||||||
return $this->groupByCurrency($journals, 'positive');
|
return $this->groupByCurrency($journals, 'positive');
|
||||||
}
|
}
|
||||||
@@ -342,8 +376,9 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
?Collection $accounts = null,
|
?Collection $accounts = null,
|
||||||
?Collection $revenue = null,
|
?Collection $revenue = null,
|
||||||
?TransactionCurrency $currency = null
|
?TransactionCurrency $currency = null
|
||||||
): array {
|
): array
|
||||||
$journals = $this->getTransactionsForSum(TransactionType::DEPOSIT, $start, $end, $accounts, $revenue, $currency);
|
{
|
||||||
|
$journals = $this->getTransactionsForSum(TransactionTypeEnum::DEPOSIT->value, $start, $end, $accounts, $revenue, $currency);
|
||||||
|
|
||||||
return $this->groupByDirection($journals, 'destination', 'positive');
|
return $this->groupByDirection($journals, 'destination', 'positive');
|
||||||
}
|
}
|
||||||
@@ -357,15 +392,16 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
?Collection $accounts = null,
|
?Collection $accounts = null,
|
||||||
?Collection $revenue = null,
|
?Collection $revenue = null,
|
||||||
?TransactionCurrency $currency = null
|
?TransactionCurrency $currency = null
|
||||||
): array {
|
): array
|
||||||
$journals = $this->getTransactionsForSum(TransactionType::DEPOSIT, $start, $end, $accounts, $revenue, $currency);
|
{
|
||||||
|
$journals = $this->getTransactionsForSum(TransactionTypeEnum::DEPOSIT->value, $start, $end, $accounts, $revenue, $currency);
|
||||||
|
|
||||||
return $this->groupByDirection($journals, 'source', 'positive');
|
return $this->groupByDirection($journals, 'source', 'positive');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sumTransfers(Carbon $start, Carbon $end, ?Collection $accounts = null, ?TransactionCurrency $currency = null): array
|
public function sumTransfers(Carbon $start, Carbon $end, ?Collection $accounts = null, ?TransactionCurrency $currency = null): array
|
||||||
{
|
{
|
||||||
$journals = $this->getTransactionsForSum(TransactionType::TRANSFER, $start, $end, $accounts, null, $currency);
|
$journals = $this->getTransactionsForSum(TransactionTypeEnum::TRANSFER->value, $start, $end, $accounts, null, $currency);
|
||||||
|
|
||||||
return $this->groupByEither($journals);
|
return $this->groupByEither($journals);
|
||||||
}
|
}
|
||||||
@@ -378,7 +414,7 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
foreach ($journals as $journal) {
|
foreach ($journals as $journal) {
|
||||||
$return = $this->groupByEitherJournal($return, $journal);
|
$return = $this->groupByEitherJournal($return, $journal);
|
||||||
}
|
}
|
||||||
$final = [];
|
$final = [];
|
||||||
foreach ($return as $array) {
|
foreach ($return as $array) {
|
||||||
$array['difference_float'] = (float) $array['difference'];
|
$array['difference_float'] = (float) $array['difference'];
|
||||||
$array['in_float'] = (float) $array['in'];
|
$array['in_float'] = (float) $array['in'];
|
||||||
@@ -391,12 +427,12 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
|
|
||||||
private function groupByEitherJournal(array $return, array $journal): array
|
private function groupByEitherJournal(array $return, array $journal): array
|
||||||
{
|
{
|
||||||
$sourceId = $journal['source_account_id'];
|
$sourceId = $journal['source_account_id'];
|
||||||
$destinationId = $journal['destination_account_id'];
|
$destinationId = $journal['destination_account_id'];
|
||||||
$currencyId = $journal['currency_id'];
|
$currencyId = $journal['currency_id'];
|
||||||
$sourceKey = sprintf('%d-%d', $currencyId, $sourceId);
|
$sourceKey = sprintf('%d-%d', $currencyId, $sourceId);
|
||||||
$destKey = sprintf('%d-%d', $currencyId, $destinationId);
|
$destKey = sprintf('%d-%d', $currencyId, $destinationId);
|
||||||
$amount = app('steam')->positive($journal['amount']);
|
$amount = app('steam')->positive($journal['amount']);
|
||||||
|
|
||||||
// source first
|
// source first
|
||||||
$return[$sourceKey] ??= [
|
$return[$sourceKey] ??= [
|
||||||
@@ -413,7 +449,7 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
];
|
];
|
||||||
|
|
||||||
// dest next:
|
// dest next:
|
||||||
$return[$destKey] ??= [
|
$return[$destKey] ??= [
|
||||||
'id' => (string) $destinationId,
|
'id' => (string) $destinationId,
|
||||||
'name' => $journal['destination_account_name'],
|
'name' => $journal['destination_account_name'],
|
||||||
'difference' => '0',
|
'difference' => '0',
|
||||||
@@ -431,15 +467,15 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
$return[$sourceKey]['difference'] = bcadd($return[$sourceKey]['out'], $return[$sourceKey]['in']);
|
$return[$sourceKey]['difference'] = bcadd($return[$sourceKey]['out'], $return[$sourceKey]['in']);
|
||||||
|
|
||||||
// destination account? money comes in:
|
// destination account? money comes in:
|
||||||
$return[$destKey]['in'] = bcadd($return[$destKey]['in'], $amount);
|
$return[$destKey]['in'] = bcadd($return[$destKey]['in'], $amount);
|
||||||
$return[$destKey]['difference'] = bcadd($return[$destKey]['out'], $return[$destKey]['in']);
|
$return[$destKey]['difference'] = bcadd($return[$destKey]['out'], $return[$destKey]['in']);
|
||||||
|
|
||||||
// foreign currency
|
// foreign currency
|
||||||
if (null !== $journal['foreign_currency_id'] && null !== $journal['foreign_amount']) {
|
if (null !== $journal['foreign_currency_id'] && null !== $journal['foreign_amount']) {
|
||||||
$currencyId = $journal['foreign_currency_id'];
|
$currencyId = $journal['foreign_currency_id'];
|
||||||
$sourceKey = sprintf('%d-%d', $currencyId, $sourceId);
|
$sourceKey = sprintf('%d-%d', $currencyId, $sourceId);
|
||||||
$destKey = sprintf('%d-%d', $currencyId, $destinationId);
|
$destKey = sprintf('%d-%d', $currencyId, $destinationId);
|
||||||
$amount = app('steam')->positive($journal['foreign_amount']);
|
$amount = app('steam')->positive($journal['foreign_amount']);
|
||||||
|
|
||||||
// same as above:
|
// same as above:
|
||||||
// source first
|
// source first
|
||||||
@@ -457,7 +493,7 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
];
|
];
|
||||||
|
|
||||||
// dest next:
|
// dest next:
|
||||||
$return[$destKey] ??= [
|
$return[$destKey] ??= [
|
||||||
'id' => (string) $destinationId,
|
'id' => (string) $destinationId,
|
||||||
'name' => $journal['destination_account_name'],
|
'name' => $journal['destination_account_name'],
|
||||||
'difference' => '0',
|
'difference' => '0',
|
||||||
@@ -474,8 +510,8 @@ class OperationsRepository implements OperationsRepositoryInterface
|
|||||||
$return[$sourceKey]['difference'] = bcadd($return[$sourceKey]['out'], $return[$sourceKey]['in']);
|
$return[$sourceKey]['difference'] = bcadd($return[$sourceKey]['out'], $return[$sourceKey]['in']);
|
||||||
|
|
||||||
// destination account? money comes in:
|
// destination account? money comes in:
|
||||||
$return[$destKey]['in'] = bcadd($return[$destKey]['in'], $amount);
|
$return[$destKey]['in'] = bcadd($return[$destKey]['in'], $amount);
|
||||||
$return[$destKey]['difference'] = bcadd($return[$destKey]['out'], $return[$destKey]['in']);
|
$return[$destKey]['difference'] = bcadd($return[$destKey]['out'], $return[$destKey]['in']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
|
12
composer.lock
generated
12
composer.lock
generated
@@ -9682,16 +9682,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "twig/twig",
|
"name": "twig/twig",
|
||||||
"version": "v3.17.1",
|
"version": "v3.18.0",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/twigphp/Twig.git",
|
"url": "https://github.com/twigphp/Twig.git",
|
||||||
"reference": "677ef8da6497a03048192aeeb5aa3018e379ac71"
|
"reference": "acffa88cc2b40dbe42eaf3a5025d6c0d4600cc50"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/twigphp/Twig/zipball/677ef8da6497a03048192aeeb5aa3018e379ac71",
|
"url": "https://api.github.com/repos/twigphp/Twig/zipball/acffa88cc2b40dbe42eaf3a5025d6c0d4600cc50",
|
||||||
"reference": "677ef8da6497a03048192aeeb5aa3018e379ac71",
|
"reference": "acffa88cc2b40dbe42eaf3a5025d6c0d4600cc50",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@@ -9746,7 +9746,7 @@
|
|||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/twigphp/Twig/issues",
|
"issues": "https://github.com/twigphp/Twig/issues",
|
||||||
"source": "https://github.com/twigphp/Twig/tree/v3.17.1"
|
"source": "https://github.com/twigphp/Twig/tree/v3.18.0"
|
||||||
},
|
},
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@@ -9758,7 +9758,7 @@
|
|||||||
"type": "tidelift"
|
"type": "tidelift"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2024-12-12T09:58:10+00:00"
|
"time": "2024-12-29T10:51:50+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "verifiedjoseph/ntfy-php-library",
|
"name": "verifiedjoseph/ntfy-php-library",
|
||||||
|
@@ -26,7 +26,6 @@ $(function () {
|
|||||||
$('.make_default').on('click', setDefaultCurrency);
|
$('.make_default').on('click', setDefaultCurrency);
|
||||||
$('.enable-currency').on('click', enableCurrency);
|
$('.enable-currency').on('click', enableCurrency);
|
||||||
$('.disable-currency').on('click', disableCurrency);
|
$('.disable-currency').on('click', disableCurrency);
|
||||||
console.log('Loaded3');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function setDefaultCurrency(e) {
|
function setDefaultCurrency(e) {
|
||||||
@@ -47,6 +46,7 @@ function setDefaultCurrency(e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
|
timeout: 30000, // sets timeout to 30 seconds
|
||||||
url: updateCurrencyUrl + '/' + currencyCode,
|
url: updateCurrencyUrl + '/' + currencyCode,
|
||||||
data: JSON.stringify(params),
|
data: JSON.stringify(params),
|
||||||
type: 'PUT',
|
type: 'PUT',
|
||||||
|
Reference in New Issue
Block a user