mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-27 19:31:01 -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');
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*
|
||||
|
@ -251,6 +251,16 @@ interface BudgetRepositoryInterface
|
||||
*/
|
||||
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
|
||||
*
|
||||
|
@ -25,6 +25,8 @@ namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Models\AvailableBudget;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
@ -32,6 +34,9 @@ use Log;
|
||||
*/
|
||||
class AvailableBudgetTransformer extends AbstractTransformer
|
||||
{
|
||||
/** @var BudgetRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* CurrencyTransformer constructor.
|
||||
*
|
||||
@ -39,6 +44,7 @@ class AvailableBudgetTransformer extends AbstractTransformer
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->repository = app(BudgetRepositoryInterface::class);
|
||||
if ('testing' === config('app.env')) {
|
||||
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
|
||||
{
|
||||
$this->repository->setUser($availableBudget->user);
|
||||
|
||||
$currency = $availableBudget->transactionCurrency;
|
||||
$data = [
|
||||
'id' => (int)$availableBudget->id,
|
||||
@ -65,16 +73,43 @@ class AvailableBudgetTransformer extends AbstractTransformer
|
||||
'amount' => round($availableBudget->amount, $currency->decimal_places),
|
||||
'start' => $availableBudget->start_date->format('Y-m-d'),
|
||||
'end' => $availableBudget->end_date->format('Y-m-d'),
|
||||
|
||||
'links' => [
|
||||
'spent_in_budgets' => [],
|
||||
'spent_no_budget' => [],
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/available_budgets/' . $availableBudget->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
$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 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