Available budget end point also gives info on spent info #1884

This commit is contained in:
James Cole 2018-12-22 06:40:25 +01:00
parent d27176c0b3
commit 91593335ef
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 95 additions and 2 deletions

View File

@ -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
*

View File

@ -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
*

View File

@ -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'));
}
}