This commit is contained in:
James Cole
2025-04-22 20:41:08 +02:00
parent dba8bba41a
commit 576c5f242c
3 changed files with 96 additions and 56 deletions

View File

@@ -43,6 +43,7 @@ use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use FireflyIII\Support\Report\Summarizer\TransactionSummarizer; use FireflyIII\Support\Report\Summarizer\TransactionSummarizer;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
/** /**
@@ -539,28 +540,65 @@ class BasicController extends Controller
), ),
]; ];
} }
unset($leftToSpend);
if (0 === count($return)) { if (0 === count($return)) {
$currency = $this->nativeCurrency; // a small trick to get every expense in this period, regardless of budget.
$return[$currency->id] = [ $spent = $this->opsRepository->sumExpenses($start, $end, null, new Collection());
'key' => sprintf('left-to-spend-in-%s', $currency->code), foreach ($spent as $row) {
'title' => trans('firefly.box_left_to_spend_in_currency', ['currency' => $currency->symbol]), // either an amount was budgeted or 0 is available.
'monetary_value' => '0', $currencyId = (int) $row['currency_id'];
$spentInCurrency = $row['sum'];
$perDay = '0';
if (0 !== $days && -1 === bccomp($spentInCurrency, '0')) {
$perDay = bcdiv($spentInCurrency, (string) $days);
}
Log::debug(sprintf('Spent %s %s', $row['currency_code'], $row['sum']));
$return[$currencyId] = [
'key' => sprintf('left-to-spend-in-%s', $row['currency_code']),
'title' => trans('firefly.spent'),
'no_available_budgets' => true, 'no_available_budgets' => true,
'currency_id' => (string) $currency->id, 'monetary_value' => $spentInCurrency,
'currency_code' => $currency->code, 'currency_id' => (string) $row['currency_id'],
'currency_symbol' => $currency->symbol, 'currency_code' => $row['currency_code'],
'currency_decimal_places' => $currency->decimal_places, 'currency_symbol' => $row['currency_symbol'],
'value_parsed' => app('amount')->formatFlat($currency->symbol, $currency->decimal_places, '0', false), 'currency_decimal_places' => $row['currency_decimal_places'],
'value_parsed' => app('amount')->formatFlat($row['currency_symbol'], $row['currency_decimal_places'], $spentInCurrency, false),
'local_icon' => 'money', 'local_icon' => 'money',
'sub_title' => app('amount')->formatFlat( 'sub_title' => app('amount')->formatFlat(
$currency->symbol, $row['currency_symbol'],
$currency->decimal_places, $row['currency_decimal_places'],
'0', $perDay,
false false
), ),
]; ];
} }
// $amount = '0';
// // $days
// // fill in by money spent, just count it.
// $currency = $this->nativeCurrency;
// $return[$currency->id] = [
// 'key' => sprintf('left-to-spend-in-%s', $currency->code),
// 'title' => trans('firefly.box_left_to_spend_in_currency', ['currency' => $currency->symbol]),
// 'monetary_value' => '0',
// 'no_available_budgets' => true,
// 'currency_id' => (string) $currency->id,
// 'currency_code' => $currency->code,
// 'currency_symbol' => $currency->symbol,
// 'currency_decimal_places' => $currency->decimal_places,
// 'value_parsed' => app('amount')->formatFlat($currency->symbol, $currency->decimal_places, '0', false),
// 'local_icon' => 'money',
// 'sub_title' => app('amount')->formatFlat(
// $currency->symbol,
// $currency->decimal_places,
// '0',
// false
// ),
// ];
}
return array_values($return); return array_values($return);
} }

View File

@@ -31,7 +31,6 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\Budget; use FireflyIII\Models\Budget;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Report\Summarizer\TransactionSummarizer; use FireflyIII\Support\Report\Summarizer\TransactionSummarizer;
use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface; use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface;
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait; use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
@@ -204,7 +203,8 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
?Collection $budgets = null, ?Collection $budgets = null,
?TransactionCurrency $currency = null, ?TransactionCurrency $currency = null,
bool $convertToNative = false bool $convertToNative = false
): array { ): array
{
Log::debug(sprintf('Start of %s(date, date, array, array, "%s", "%s").', __METHOD__, $currency?->code, var_export($convertToNative, true))); Log::debug(sprintf('Start of %s(date, date, array, array, "%s", "%s").', __METHOD__, $currency?->code, var_export($convertToNative, true)));
// this collector excludes all transfers TO liabilities (which are also withdrawals) // 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. // because those expenses only become expenses once they move from the liability to the friend.
@@ -227,8 +227,7 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
$collector->setUser($this->user) $collector->setUser($this->user)
->setRange($start, $end) ->setRange($start, $end)
// ->excludeDestinationAccounts($selection) // ->excludeDestinationAccounts($selection)
->setTypes([TransactionTypeEnum::WITHDRAWAL->value]) ->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
;
if (null !== $accounts) { if (null !== $accounts) {
$collector->setAccounts($accounts); $collector->setAccounts($accounts);
@@ -240,7 +239,9 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
Log::debug(sprintf('Limit to normal currency %s', $currency->code)); Log::debug(sprintf('Limit to normal currency %s', $currency->code));
$collector->setNormalCurrency($currency); $collector->setNormalCurrency($currency);
} }
if ($budgets->count() > 0) {
$collector->setBudgets($budgets); $collector->setBudgets($budgets);
}
$journals = $collector->getExtractedJournals(); $journals = $collector->getExtractedJournals();
// same but for transactions in the foreign currency: // same but for transactions in the foreign currency:

View File

@@ -62,6 +62,7 @@ function drawChart() {
// net worth // net worth
var net_worth = []; var net_worth = [];
var keepGreen = false; var keepGreen = false;
var makeBlue = false;
for (key in data) { for (key in data) {
// balance // balance
@@ -80,17 +81,14 @@ function drawChart() {
// left to spend // left to spend
if (key.substring(0, 17) === 'left-to-spend-in-') { if (key.substring(0, 17) === 'left-to-spend-in-') {
if(true === data[key].no_available_budgets) {
left_to_spend_top.push('---');
left_to_spend_bottom.push('---');
keepGreen = true;
}
if(false === data[key].no_available_budgets) {
left_to_spend_top.push(data[key].value_parsed); left_to_spend_top.push(data[key].value_parsed);
left_to_spend_bottom.push(data[key].sub_title); left_to_spend_bottom.push(data[key].sub_title);
if (parseFloat(data[key].monetary_value) > 0) { if(true === data[key].no_available_budgets) {
keepGreen = true; makeBlue = true;
$('#box-left-to-spend-text').text(data[key].title);
} }
if(false === data[key].no_available_budgets && parseFloat(data[key].monetary_value) > 0) {
keepGreen = true;
} }
} }
@@ -102,6 +100,9 @@ function drawChart() {
if(!keepGreen) { if(!keepGreen) {
$('#box-left-to-spend-box').removeClass('bg-green-gradient').addClass('bg-red-gradient') $('#box-left-to-spend-box').removeClass('bg-green-gradient').addClass('bg-red-gradient')
} }
if(makeBlue) {
$('#box-left-to-spend-box').removeClass('bg-red-gradient').removeClass('bg-green-gradient').addClass('bg-blue-gradient')
}
// balance // balance
$('#box-balance-sums').html(balance_top.join(', ')); $('#box-balance-sums').html(balance_top.join(', '));