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

87 lines
3.5 KiB
PHP
Raw Normal View History

2021-03-05 07:03:28 +01:00
<?php
/*
* 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-05 07:03:28 +01:00
2021-03-28 11:39:26 +02:00
namespace FireflyIII\Api\V1\Controllers\Insight\Expense;
2021-03-05 07:03:28 +01:00
use FireflyIII\Api\V1\Controllers\Controller;
2021-03-05 20:17:39 +01:00
use FireflyIII\Api\V1\Requests\Insight\GenericRequest;
2021-03-05 07:03:28 +01:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\TransactionType;
use Illuminate\Http\JsonResponse;
/**
* Class PeriodController
*/
class PeriodController extends Controller
{
/**
2021-09-18 05:58:22 +02:00
* This endpoint is documented at:
* https://api-docs.firefly-iii.org/#/insight/insightExpenseTotal
*
2021-03-05 20:17:39 +01:00
* @param GenericRequest $request
2021-03-05 07:03:28 +01:00
*
* @return JsonResponse
*/
2021-03-05 20:17:39 +01:00
public function total(GenericRequest $request): JsonResponse
2021-03-05 07:03:28 +01:00
{
2021-03-05 16:28:59 +01:00
$accounts = $request->getAssetAccounts();
$start = $request->getStart();
$end = $request->getEnd();
2021-03-05 07:03:28 +01:00
$response = [];
// collect all expenses in this period (regardless of type)
$collector = app(GroupCollectorInterface::class);
2021-03-05 16:28:59 +01:00
$collector->setTypes([TransactionType::WITHDRAWAL])->setRange($start, $end)->setSourceAccounts($accounts);
2021-03-05 07:03:28 +01:00
$genericSet = $collector->getExtractedJournals();
foreach ($genericSet as $journal) {
2022-03-29 14:56:27 +02:00
$currencyId = (int) $journal['currency_id'];
$foreignCurrencyId = (int) $journal['foreign_currency_id'];
2021-03-05 07:03:28 +01:00
if (0 !== $currencyId) {
$response[$currencyId] = $response[$currencyId] ?? [
'difference' => '0',
'difference_float' => 0,
2022-03-29 14:56:27 +02:00
'currency_id' => (string) $currencyId,
2021-03-05 07:03:28 +01:00
'currency_code' => $journal['currency_code'],
];
$response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], $journal['amount']);
2022-10-16 14:44:11 +02:00
$response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; // float but on purpose.
2021-03-05 07:03:28 +01:00
}
if (0 !== $foreignCurrencyId) {
2021-03-05 16:28:59 +01:00
$response[$foreignCurrencyId] = $response[$foreignCurrencyId] ?? [
2021-03-05 07:03:28 +01:00
'difference' => '0',
'difference_float' => 0,
2022-03-29 14:56:27 +02:00
'currency_id' => (string) $foreignCurrencyId,
2021-03-05 07:03:28 +01:00
'currency_code' => $journal['foreign_currency_code'],
];
$response[$foreignCurrencyId]['difference'] = bcadd($response[$foreignCurrencyId]['difference'], $journal['foreign_amount']);
2022-10-16 14:44:11 +02:00
$response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; // float but on purpose.
2021-03-05 07:03:28 +01:00
}
}
return response()->json(array_values($response));
}
2021-03-28 11:39:26 +02:00
}