Files
firefly-iii/app/Api/V1/Controllers/Insight/Transfer/PeriodController.php

86 lines
3.5 KiB
PHP
Raw Normal View History

2021-03-07 15:26:42 +01:00
<?php
2021-03-07 15:26:42 +01:00
/*
* PeriodController.php
* Copyright (c) 2021 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/>.
*/
2021-03-28 11:39:26 +02:00
declare(strict_types=1);
2021-03-07 15:26:42 +01:00
2021-03-28 11:39:26 +02:00
namespace FireflyIII\Api\V1\Controllers\Insight\Transfer;
2021-03-07 15:26:42 +01:00
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Insight\GenericRequest;
2025-01-03 09:05:56 +01:00
use FireflyIII\Enums\TransactionTypeEnum;
2021-03-07 15:26:42 +01:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Support\Facades\Amount;
2021-03-07 15:26:42 +01:00
use Illuminate\Http\JsonResponse;
/**
* Class PeriodController
*/
class PeriodController extends Controller
{
/**
2021-09-18 05:58:22 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/insight/insightTransferTotal
2021-03-07 15:26:42 +01:00
*/
public function total(GenericRequest $request): JsonResponse
{
$accounts = $request->getAssetAccounts();
$start = $request->getStart();
$end = $request->getEnd();
$response = [];
$convertToNative = Amount::convertToNative();
2025-01-19 19:07:19 +01:00
$default = Amount::getNativeCurrency();
2021-03-07 15:26:42 +01:00
// collect all expenses in this period (regardless of type)
$collector = app(GroupCollectorInterface::class);
2025-01-03 09:05:56 +01:00
$collector->setTypes([TransactionTypeEnum::TRANSFER->value])->setRange($start, $end)->setDestinationAccounts($accounts);
$genericSet = $collector->getExtractedJournals();
2021-03-07 15:26:42 +01:00
foreach ($genericSet as $journal) {
// currency
$currencyId = $journal['currency_id'];
$currencyCode = $journal['currency_code'];
$field = $convertToNative && $currencyId !== $default->id ? 'native_amount' : 'amount';
// perhaps use default currency instead?
if ($convertToNative && $journal['currency_id'] !== $default->id) {
$currencyId = $default->id;
$currencyCode = $default->code;
}
// 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';
}
2021-03-07 15:26:42 +01:00
$response[$currencyId] ??= [
'difference' => '0',
'difference_float' => 0,
'currency_id' => (string) $currencyId,
'currency_code' => $currencyCode,
];
$response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], app('steam')->positive($journal[$field]));
$response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference'];
2021-03-07 15:26:42 +01:00
}
return response()->json(array_values($response));
}
2021-03-28 11:39:26 +02:00
}