Files
firefly-iii/app/Transformers/PiggyBankTransformer.php

147 lines
5.7 KiB
PHP
Raw Normal View History

<?php
/**
* PiggyBankTransformer.php
2020-02-16 13:57:18 +01:00
* 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;
2022-12-29 19:42:40 +01:00
use FireflyIII\Exceptions\FireflyException;
2020-06-07 11:31:01 +02:00
use FireflyIII\Models\ObjectGroup;
use FireflyIII\Models\PiggyBank;
2018-04-21 23:48:54 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-02-17 10:47:06 +01:00
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
/**
* Class PiggyBankTransformer
*/
class PiggyBankTransformer extends AbstractTransformer
{
2021-03-21 09:15:40 +01:00
private AccountRepositoryInterface $accountRepos;
2021-03-06 12:45:49 +01:00
private PiggyBankRepositoryInterface $piggyRepos;
2018-12-20 05:46:05 +01:00
/**
2018-02-17 10:47:06 +01:00
* PiggyBankTransformer constructor.
*/
public function __construct()
{
2023-10-28 14:59:16 +02:00
$this->accountRepos = app(AccountRepositoryInterface::class);
$this->piggyRepos = app(PiggyBankRepositoryInterface::class);
}
2018-02-17 10:47:06 +01:00
/**
* Transform the piggy bank.
*
2022-12-29 19:42:40 +01:00
* @throws FireflyException
2023-12-22 20:12:38 +01:00
*/
public function transform(PiggyBank $piggyBank): array
{
2024-12-14 05:45:54 +01:00
$user = $piggyBank->accounts()->first()->user;
2018-04-21 23:48:54 +02:00
2018-12-20 05:46:05 +01:00
// set up repositories
$this->accountRepos->setUser($user);
$this->piggyRepos->setUser($user);
2018-02-17 10:47:06 +01:00
2018-12-20 05:46:05 +01:00
// note
2024-12-14 05:45:54 +01:00
$notes = $this->piggyRepos->getNoteText($piggyBank);
$notes = '' === $notes ? null : $notes;
2018-04-21 23:48:54 +02:00
2020-06-07 11:31:01 +02:00
$objectGroupId = null;
2020-06-20 16:28:23 +02:00
$objectGroupOrder = null;
2020-06-07 11:31:01 +02:00
$objectGroupTitle = null;
2023-12-20 19:35:52 +01:00
/** @var null|ObjectGroup $objectGroup */
2024-12-14 05:45:54 +01:00
$objectGroup = $piggyBank->objectGroups->first();
2020-06-07 11:31:01 +02:00
if (null !== $objectGroup) {
2023-11-05 19:55:39 +01:00
$objectGroupId = $objectGroup->id;
$objectGroupOrder = $objectGroup->order;
2020-06-07 11:31:01 +02:00
$objectGroupTitle = $objectGroup->title;
}
2018-02-17 10:47:06 +01:00
// get currently saved amount:
2024-12-14 05:45:54 +01:00
$currency = $piggyBank->transactionCurrency;
$currentAmount = $this->piggyRepos->getCurrentAmount($piggyBank);
2022-03-27 18:30:46 +02:00
// Amounts, depending on 0.0 state of target amount
2024-12-14 05:45:54 +01:00
$percentage = null;
$targetAmount = $piggyBank->target_amount;
$leftToSave = null;
$savePerMonth = null;
2022-12-29 11:29:00 +01:00
if (0 !== bccomp($targetAmount, '0')) { // target amount is not 0.00
2024-11-30 16:02:30 +01:00
$leftToSave = bcsub($piggyBank->target_amount, $currentAmount);
$percentage = (int) bcmul(bcdiv($currentAmount, $targetAmount), '100');
2022-12-29 11:29:00 +01:00
$targetAmount = app('steam')->bcround($targetAmount, $currency->decimal_places);
$leftToSave = app('steam')->bcround($leftToSave, $currency->decimal_places);
$savePerMonth = app('steam')->bcround($this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places);
2022-03-27 18:30:46 +02:00
}
2024-12-14 05:45:54 +01:00
$startDate = $piggyBank->start_date?->format('Y-m-d');
$targetDate = $piggyBank->target_date?->format('Y-m-d');
2021-03-21 09:15:40 +01:00
2020-10-23 19:11:25 +02:00
return [
'id' => (string) $piggyBank->id,
2018-12-20 05:46:05 +01:00
'created_at' => $piggyBank->created_at->toAtomString(),
'updated_at' => $piggyBank->updated_at->toAtomString(),
'accounts' => $this->renderAccounts($piggyBank),
2024-12-14 05:45:54 +01:00
// 'account_id' => (string)$piggyBank->account_id,
// 'account_name' => $piggyBank->account->name,
2018-12-20 05:46:05 +01:00
'name' => $piggyBank->name,
'currency_id' => (string) $currency->id,
2018-12-20 05:46:05 +01:00
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
2023-11-26 12:10:42 +01:00
'currency_decimal_places' => $currency->decimal_places,
2022-12-29 11:29:00 +01:00
'target_amount' => $targetAmount,
2018-12-20 05:46:05 +01:00
'percentage' => $percentage,
'current_amount' => $currentAmount,
2022-12-29 11:29:00 +01:00
'left_to_save' => $leftToSave,
2022-03-27 18:30:46 +02:00
'save_per_month' => $savePerMonth,
2018-12-20 05:46:05 +01:00
'start_date' => $startDate,
'target_date' => $targetDate,
2023-11-05 19:55:39 +01:00
'order' => $piggyBank->order,
2018-12-20 05:46:05 +01:00
'active' => true,
'notes' => $notes,
'object_group_id' => null !== $objectGroupId ? (string) $objectGroupId : null,
2020-06-07 11:31:01 +02:00
'object_group_order' => $objectGroupOrder,
'object_group_title' => $objectGroupTitle,
2018-12-20 05:46:05 +01:00
'links' => [
[
'rel' => 'self',
2024-12-14 05:45:54 +01:00
'uri' => '/piggy_banks/'.$piggyBank->id,
],
],
];
}
private function renderAccounts(PiggyBank $piggyBank): array
{
$return = [];
foreach ($piggyBank->accounts()->get() as $account) {
$return[] = [
2024-12-14 05:45:54 +01:00
'id' => $account->id,
'name' => $account->name,
2024-12-28 10:52:46 +01:00
'current_amount' => (string) $account->pivot->current_amount,
// TODO add balance, add left to save.
];
}
2024-12-14 05:45:54 +01:00
return $return;
}
2018-03-05 19:35:58 +01:00
}