mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-30 12:43:57 -06:00
Available budget end point also gives info on spent info #1884
This commit is contained in:
parent
d27176c0b3
commit
91593335ef
@ -793,6 +793,54 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return (string)$set->sum('transaction_amount');
|
return (string)$set->sum('transaction_amount');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array
|
||||||
|
{
|
||||||
|
/** @var TransactionCollectorInterface $collector */
|
||||||
|
$collector = app(TransactionCollectorInterface::class);
|
||||||
|
$collector->setUser($this->user);
|
||||||
|
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();
|
||||||
|
|
||||||
|
if ($accounts->count() > 0) {
|
||||||
|
$collector->setAccounts($accounts);
|
||||||
|
}
|
||||||
|
if (0 === $accounts->count()) {
|
||||||
|
$collector->setAllAssetAccounts();
|
||||||
|
}
|
||||||
|
|
||||||
|
$set = $collector->getTransactions();
|
||||||
|
$return = [];
|
||||||
|
$total = [];
|
||||||
|
$currencies = [];
|
||||||
|
/** @var Transaction $transaction */
|
||||||
|
foreach ($set as $transaction) {
|
||||||
|
$code = $transaction->transaction_currency_code;
|
||||||
|
if (!isset($currencies[$code])) {
|
||||||
|
$currencies[$code] = $transaction->transactionCurrency;
|
||||||
|
}
|
||||||
|
$total[$code] = isset($total[$code]) ? bcadd($total[$code], $transaction->transaction_amount) : $transaction->transaction_amount;
|
||||||
|
}
|
||||||
|
foreach ($total as $code => $spent) {
|
||||||
|
/** @var TransactionCurrency $currency */
|
||||||
|
$currency = $currencies[$code];
|
||||||
|
$return[] = [
|
||||||
|
'currency_id' => $currency->id,
|
||||||
|
'currency_code' => $code,
|
||||||
|
'currency_symbol' => $currency->symbol,
|
||||||
|
'currency_decimal_places' => $currency->decimal_places,
|
||||||
|
'amount' => round($spent, $currency->decimal_places),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
*
|
*
|
||||||
|
@ -251,6 +251,16 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function spentInPeriodWoBudget(Collection $accounts, Carbon $start, Carbon $end): string;
|
public function spentInPeriodWoBudget(Collection $accounts, Carbon $start, Carbon $end): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
*
|
*
|
||||||
|
@ -25,6 +25,8 @@ namespace FireflyIII\Transformers;
|
|||||||
|
|
||||||
|
|
||||||
use FireflyIII\Models\AvailableBudget;
|
use FireflyIII\Models\AvailableBudget;
|
||||||
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
use Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,6 +34,9 @@ use Log;
|
|||||||
*/
|
*/
|
||||||
class AvailableBudgetTransformer extends AbstractTransformer
|
class AvailableBudgetTransformer extends AbstractTransformer
|
||||||
{
|
{
|
||||||
|
/** @var BudgetRepositoryInterface */
|
||||||
|
private $repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CurrencyTransformer constructor.
|
* CurrencyTransformer constructor.
|
||||||
*
|
*
|
||||||
@ -39,6 +44,7 @@ class AvailableBudgetTransformer extends AbstractTransformer
|
|||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
$this->repository = app(BudgetRepositoryInterface::class);
|
||||||
if ('testing' === config('app.env')) {
|
if ('testing' === config('app.env')) {
|
||||||
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
|
||||||
}
|
}
|
||||||
@ -53,6 +59,8 @@ class AvailableBudgetTransformer extends AbstractTransformer
|
|||||||
*/
|
*/
|
||||||
public function transform(AvailableBudget $availableBudget): array
|
public function transform(AvailableBudget $availableBudget): array
|
||||||
{
|
{
|
||||||
|
$this->repository->setUser($availableBudget->user);
|
||||||
|
|
||||||
$currency = $availableBudget->transactionCurrency;
|
$currency = $availableBudget->transactionCurrency;
|
||||||
$data = [
|
$data = [
|
||||||
'id' => (int)$availableBudget->id,
|
'id' => (int)$availableBudget->id,
|
||||||
@ -65,7 +73,8 @@ class AvailableBudgetTransformer extends AbstractTransformer
|
|||||||
'amount' => round($availableBudget->amount, $currency->decimal_places),
|
'amount' => round($availableBudget->amount, $currency->decimal_places),
|
||||||
'start' => $availableBudget->start_date->format('Y-m-d'),
|
'start' => $availableBudget->start_date->format('Y-m-d'),
|
||||||
'end' => $availableBudget->end_date->format('Y-m-d'),
|
'end' => $availableBudget->end_date->format('Y-m-d'),
|
||||||
|
'spent_in_budgets' => [],
|
||||||
|
'spent_no_budget' => [],
|
||||||
'links' => [
|
'links' => [
|
||||||
[
|
[
|
||||||
'rel' => 'self',
|
'rel' => 'self',
|
||||||
@ -73,8 +82,34 @@ class AvailableBudgetTransformer extends AbstractTransformer
|
|||||||
],
|
],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
$start = $this->parameters->get('start');
|
||||||
|
$end = $this->parameters->get('end');
|
||||||
|
if (null !== $start && null !== $end) {
|
||||||
|
$data['spent_in_budgets'] = $this->getSpentInBudgets();
|
||||||
|
$data['spent_no_budget'] = $this->spentOutsideBudgets();
|
||||||
|
}
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getSpentInBudgets(): array
|
||||||
|
{
|
||||||
|
$allActive = $this->repository->getActiveBudgets();
|
||||||
|
return $this->repository->spentInPeriodMc(
|
||||||
|
$allActive, new Collection, $this->parameters->get('start'), $this->parameters->get('end')
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function spentOutsideBudgets(): array
|
||||||
|
{
|
||||||
|
return $this->repository->spentInPeriodWoBudgetMc(new Collection,$this->parameters->get('start'), $this->parameters->get('end'));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user