This commit is contained in:
James Cole
2020-10-01 12:48:27 +02:00
parent 3ce949f0aa
commit b3edbecde2
4 changed files with 54 additions and 23 deletions

View File

@@ -447,9 +447,11 @@ class GroupCollector implements GroupCollectorInterface
*/
public function setUser(User $user): GroupCollectorInterface
{
if (null === $this->user) {
$this->user = $user;
$this->startQuery();
}
return $this;
}

View File

@@ -310,7 +310,6 @@ class IndexController extends Controller
}
$budgets[] = $array;
}
return $budgets;
}
}

View File

@@ -404,9 +404,6 @@ class BudgetController extends Controller
/**
* Shows a budget list with spent/left/overspent.
*
* TODO there are cases when this chart hides expenses: when budget has limits
* and limits are found and used, but the expense is in another currency.
*
* @return JsonResponse
*
*/

View File

@@ -357,24 +357,42 @@ class OperationsRepository implements OperationsRepositoryInterface
* @return array
*/
public function sumExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $budgets = null, ?TransactionCurrency $currency = null
): array {
): array
{
Log::debug(sprintf('Now in %s', __METHOD__));
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL]);
if (null !== $accounts && $accounts->count() > 0) {
if (null !== $accounts) {
$collector->setAccounts($accounts);
}
if (null === $budgets || (null !== $budgets && 0 === $budgets->count())) {
if (null === $budgets) {
$budgets = $this->getBudgets();
}
if (null !== $currency) {
$collector->setCurrency($currency);
}
$collector->setBudgets($budgets);
$collector->withBudgetInformation();
$journals = $collector->getExtractedJournals();
// same but for foreign currencies:
if (null !== $currency) {
Log::debug(sprintf('Currency is "%s".', $currency->name));
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])
->setForeignCurrency($currency)->setBudgets($budgets);
if (null !== $accounts) {
$collector->setAccounts($accounts);
}
$result = $collector->getExtractedJournals();
Log::debug(sprintf('Found %d journals with currency %s.', count($result), $currency->code));
// do not use array_merge because you want keys to overwrite (otherwise you get double results):
$journals = $result + $journals;
}
$array = [];
foreach ($journals as $journal) {
@@ -388,7 +406,22 @@ class OperationsRepository implements OperationsRepositoryInterface
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($journal['amount']));
// also do foreign amount:
$foreignId = (int) $journal['foreign_currency_id'];
if(0 !== $foreignId) {
$array[$foreignId] = $array[$foreignId] ?? [
'sum' => '0',
'currency_id' => $foreignId,
'currency_name' => $journal['foreign_currency_name'],
'currency_symbol' => $journal['foreign_currency_symbol'],
'currency_code' => $journal['foreign_currency_code'],
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
];
$array[$foreignId]['sum'] = bcadd($array[$foreignId]['sum'], app('steam')->negative($journal['foreign_amount']));
}
}
return $array;
}