mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-14 00:04:24 +00:00
Clean up some code.
This commit is contained in:
@@ -84,7 +84,7 @@ class BudgetLimitController extends Controller
|
||||
$budgetLimits = $this->blRepository->getBudgetLimits($budget, $start, $end);
|
||||
|
||||
// remove already budgeted currencies with the same date range
|
||||
$currencies = $collection->filter(
|
||||
$currencies = $collection->filter(
|
||||
static function (TransactionCurrency $currency) use ($budgetLimits, $start, $end) {
|
||||
/** @var BudgetLimit $limit */
|
||||
foreach ($budgetLimits as $limit) {
|
||||
@@ -107,6 +107,7 @@ class BudgetLimitController extends Controller
|
||||
public function show(BudgetLimit $budgetLimit)
|
||||
{
|
||||
$notes = $this->blRepository->getNoteText($budgetLimit);
|
||||
|
||||
return view('budgets.budget-limits.show', compact('budgetLimit', 'notes'));
|
||||
}
|
||||
|
||||
@@ -116,6 +117,7 @@ class BudgetLimitController extends Controller
|
||||
public function edit(BudgetLimit $budgetLimit)
|
||||
{
|
||||
$notes = $this->blRepository->getNoteText($budgetLimit);
|
||||
|
||||
return view('budgets.budget-limits.edit', compact('budgetLimit', 'notes'));
|
||||
}
|
||||
|
||||
@@ -135,7 +137,7 @@ class BudgetLimitController extends Controller
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function store(Request $request): JsonResponse | RedirectResponse
|
||||
public function store(Request $request): JsonResponse|RedirectResponse
|
||||
{
|
||||
app('log')->debug('Going to store new budget-limit.', $request->all());
|
||||
// first search for existing one and update it if necessary.
|
||||
@@ -144,14 +146,14 @@ class BudgetLimitController extends Controller
|
||||
if (null === $currency || null === $budget) {
|
||||
throw new FireflyException('No valid currency or budget.');
|
||||
}
|
||||
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
|
||||
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
|
||||
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
|
||||
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
|
||||
|
||||
if (null === $start || null === $end) {
|
||||
return response()->json([]);
|
||||
}
|
||||
|
||||
$amount = (string) $request->get('amount');
|
||||
$amount = (string) $request->get('amount');
|
||||
$start->startOfDay();
|
||||
$end->startOfDay();
|
||||
|
||||
@@ -161,7 +163,7 @@ class BudgetLimitController extends Controller
|
||||
|
||||
app('log')->debug(sprintf('Start: %s, end: %s', $start->format('Y-m-d'), $end->format('Y-m-d')));
|
||||
|
||||
$limit = $this->blRepository->find($budget, $currency, $start, $end);
|
||||
$limit = $this->blRepository->find($budget, $currency, $start, $end);
|
||||
|
||||
// sanity check on amount:
|
||||
if (0 === bccomp($amount, '0')) {
|
||||
@@ -195,25 +197,25 @@ class BudgetLimitController extends Controller
|
||||
);
|
||||
}
|
||||
// parse notes, if any.
|
||||
$notes = (string) $request->get('notes');
|
||||
$notes = (string) $request->get('notes');
|
||||
$this->blRepository->setNoteText($limit, $notes);
|
||||
|
||||
if ($request->expectsJson()) {
|
||||
$array = $limit->toArray();
|
||||
$array = $limit->toArray();
|
||||
// add some extra metadata:
|
||||
$spentArr = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, null, new Collection([$budget]), $currency);
|
||||
$array['spent'] = $spentArr[$currency->id]['sum'] ?? '0';
|
||||
$array['left_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], $array['amount']));
|
||||
$array['amount_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $limit['amount']);
|
||||
$array['days_left'] = (string) $this->activeDaysLeft($start, $end);
|
||||
$spentArr = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, null, new Collection([$budget]), $currency);
|
||||
$array['spent'] = $spentArr[$currency->id]['sum'] ?? '0';
|
||||
$array['left_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], $array['amount']));
|
||||
$array['amount_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $limit['amount']);
|
||||
$array['days_left'] = (string) $this->activeDaysLeft($start, $end);
|
||||
// left per day:
|
||||
$array['left_per_day'] = 0 === bccomp('0', $array['days_left']) ? bcadd($array['spent'], $array['amount']) : bcdiv(bcadd($array['spent'], $array['amount']), $array['days_left']);
|
||||
$array['left_per_day'] = 0 === bccomp('0', $array['days_left']) ? bcadd($array['spent'], $array['amount']) : bcdiv(bcadd($array['spent'], $array['amount']), $array['days_left']);
|
||||
|
||||
// left per day formatted.
|
||||
$array['left_per_day_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $array['left_per_day']);
|
||||
|
||||
// notes:
|
||||
$array['notes'] = $this->blRepository->getNoteText($limit);
|
||||
$array['notes'] = $this->blRepository->getNoteText($limit);
|
||||
|
||||
return response()->json($array);
|
||||
}
|
||||
@@ -223,7 +225,7 @@ class BudgetLimitController extends Controller
|
||||
|
||||
public function update(Request $request, BudgetLimit $budgetLimit): JsonResponse|RedirectResponse
|
||||
{
|
||||
$amount = (string) $request->get('amount');
|
||||
$amount = (string) $request->get('amount');
|
||||
if ('' === $amount) {
|
||||
$amount = '0';
|
||||
}
|
||||
@@ -235,7 +237,7 @@ class BudgetLimitController extends Controller
|
||||
$budgetId = $budgetLimit->budget_id;
|
||||
$currency = $budgetLimit->transactionCurrency;
|
||||
$this->blRepository->destroyBudgetLimit($budgetLimit);
|
||||
$array = [
|
||||
$array = [
|
||||
'budget_id' => $budgetId,
|
||||
'left_formatted' => app('amount')->formatAnything($currency, '0'),
|
||||
'left_per_day_formatted' => app('amount')->formatAnything($currency, '0'),
|
||||
@@ -248,29 +250,29 @@ class BudgetLimitController extends Controller
|
||||
if (-1 === bccomp($amount, '0')) {
|
||||
$amount = bcmul($amount, '-1');
|
||||
}
|
||||
$notes = (string)$request->get('notes');
|
||||
if(strlen($notes) > 32768) {
|
||||
$notes = (string)$request->get('notes');
|
||||
if (strlen($notes) > 32768) {
|
||||
$notes = substr($notes, 0, 32768);
|
||||
}
|
||||
|
||||
|
||||
$limit = $this->blRepository->update($budgetLimit, ['amount' => $amount,'notes' => $notes]);
|
||||
$limit = $this->blRepository->update($budgetLimit, ['amount' => $amount, 'notes' => $notes]);
|
||||
app('preferences')->mark();
|
||||
$array = $limit->toArray();
|
||||
$array = $limit->toArray();
|
||||
|
||||
$spentArr = $this->opsRepository->sumExpenses(
|
||||
$spentArr = $this->opsRepository->sumExpenses(
|
||||
$limit->start_date,
|
||||
$limit->end_date,
|
||||
null,
|
||||
new Collection([$budgetLimit->budget]),
|
||||
$budgetLimit->transactionCurrency
|
||||
);
|
||||
$daysLeft = $this->activeDaysLeft($limit->start_date, $limit->end_date);
|
||||
$array['spent'] = $spentArr[$budgetLimit->transactionCurrency->id]['sum'] ?? '0';
|
||||
$array['left_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], $array['amount']));
|
||||
$array['amount_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $limit['amount']);
|
||||
$array['days_left'] = (string) $daysLeft;
|
||||
$array['left_per_day'] = 0 === $daysLeft ? bcadd($array['spent'], $array['amount']) : bcdiv(bcadd($array['spent'], $array['amount']), $array['days_left']);
|
||||
$daysLeft = $this->activeDaysLeft($limit->start_date, $limit->end_date);
|
||||
$array['spent'] = $spentArr[$budgetLimit->transactionCurrency->id]['sum'] ?? '0';
|
||||
$array['left_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], $array['amount']));
|
||||
$array['amount_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $limit['amount']);
|
||||
$array['days_left'] = (string) $daysLeft;
|
||||
$array['left_per_day'] = 0 === $daysLeft ? bcadd($array['spent'], $array['amount']) : bcdiv(bcadd($array['spent'], $array['amount']), $array['days_left']);
|
||||
|
||||
// left per day formatted.
|
||||
$array['amount'] = app('steam')->bcround($limit['amount'], $limit->transactionCurrency->decimal_places);
|
||||
@@ -278,6 +280,7 @@ class BudgetLimitController extends Controller
|
||||
if ('true' === $request->get('redirect')) {
|
||||
return redirect(route('budgets.index'));
|
||||
}
|
||||
|
||||
return response()->json($array);
|
||||
}
|
||||
}
|
||||
|
@@ -213,7 +213,7 @@ class IndexController extends Controller
|
||||
$array['budgeted'][] = [
|
||||
'id' => $limit->id,
|
||||
'amount' => $amount,
|
||||
'notes' => $this->blRepository->getNoteText($limit),
|
||||
'notes' => $this->blRepository->getNoteText($limit),
|
||||
'start_date' => $limit->start_date->isoFormat($this->monthAndDayFormat),
|
||||
'end_date' => $limit->end_date->isoFormat($this->monthAndDayFormat),
|
||||
'in_range' => $limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end),
|
||||
|
Reference in New Issue
Block a user