Fix summary and dates, also fix #5770

This commit is contained in:
James Cole
2022-03-28 12:24:16 +02:00
parent f2849c8058
commit abb1095cef
9 changed files with 114 additions and 93 deletions

View File

@@ -25,9 +25,11 @@ namespace FireflyIII\Repositories\Budget;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\Budget;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
@@ -56,15 +58,15 @@ class OperationsRepository implements OperationsRepositoryInterface
foreach ($budget->budgetlimits as $limit) {
$diff = $limit->start_date->diffInDays($limit->end_date);
$diff = 0 === $diff ? 1 : $diff;
$amount = (string)$limit->amount;
$perDay = bcdiv($amount, (string)$diff);
$amount = (string) $limit->amount;
$perDay = bcdiv($amount, (string) $diff);
$total = bcadd($total, $perDay);
$count++;
Log::debug(sprintf('Found %d budget limits. Per day is %s, total is %s', $count, $perDay, $total));
}
$avg = $total;
if ($count > 0) {
$avg = bcdiv($total, (string)$count);
$avg = bcdiv($total, (string) $count);
}
Log::debug(sprintf('%s / %d = %s = average.', $total, $count, $avg));
@@ -98,9 +100,9 @@ class OperationsRepository implements OperationsRepositoryInterface
/** @var array $journal */
foreach ($journals as $journal) {
// prep data array for currency:
$budgetId = (int)$journal['budget_id'];
$budgetId = (int) $journal['budget_id'];
$budgetName = $journal['budget_name'];
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$key = sprintf('%d-%d', $budgetId, $currencyId);
$data[$key] = $data[$key] ?? [
@@ -152,9 +154,9 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$budgetId = (int)$journal['budget_id'];
$budgetName = (string)$journal['budget_name'];
$currencyId = (int) $journal['currency_id'];
$budgetId = (int) $journal['budget_id'];
$budgetName = (string) $journal['budget_name'];
// catch "no category" entries.
if (0 === $budgetId) {
@@ -180,7 +182,7 @@ class OperationsRepository implements OperationsRepositoryInterface
// add journal to array:
// only a subset of the fields.
$journalId = (int)$journal['transaction_journal_id'];
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['budgets'][$budgetId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->negative($journal['amount']),
'destination_account_id' => $journal['destination_account_id'],
@@ -257,12 +259,12 @@ class OperationsRepository implements OperationsRepositoryInterface
/** @var TransactionCurrency $currency */
$currency = $currencies[$code];
$return[] = [
'currency_id' => (string)$currency['id'],
'currency_id' => (string) $currency['id'],
'currency_code' => $code,
'currency_name' => $currency['name'],
'currency_symbol' => $currency['symbol'],
'currency_decimal_places' => $currency['decimal_places'],
'amount' => number_format((float)$spent, $currency['decimal_places'], '.', ''),
'amount' => number_format((float) $spent, $currency['decimal_places'], '.', ''),
];
}
@@ -279,13 +281,35 @@ 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__));
$start->startOfDay();
$end->endOfDay();
// this collector excludes all transfers TO
// liabilities (which are also withdrawals)
// because those expenses only become expenses
// once they move from the liability to the friend.
// TODO this filter must be somewhere in AccountRepositoryInterface because I suspect its needed more often (A113)
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($this->user);
$accounts = $repository->getAccountsByType(config('firefly.valid_liabilities'));
$selection = new Collection;
/** @var Account $account */
foreach ($accounts as $account) {
if ('credit' === $repository->getMetaValue($account, 'liability_direction')) {
$selection->push($account);
}
}
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL]);
$collector->setUser($this->user)
->setRange($start, $end)
->excludeDestinationAccounts($selection)
->setTypes([TransactionType::WITHDRAWAL]);
if (null !== $accounts) {
$collector->setAccounts($accounts);
@@ -318,7 +342,7 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
@@ -330,7 +354,7 @@ class OperationsRepository implements OperationsRepositoryInterface
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($journal['amount']));
// also do foreign amount:
$foreignId = (int)$journal['foreign_currency_id'];
$foreignId = (int) $journal['foreign_currency_id'];
if (0 !== $foreignId) {
$array[$foreignId] = $array[$foreignId] ?? [
'sum' => '0',