mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fix for #1111
This commit is contained in:
parent
9f63dfb9cb
commit
36354c3846
@ -82,9 +82,9 @@ class BudgetController extends Controller
|
||||
*/
|
||||
public function amount(Request $request, BudgetRepositoryInterface $repository, Budget $budget)
|
||||
{
|
||||
$amount = strval($request->get('amount'));
|
||||
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
|
||||
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
|
||||
$amount = strval($request->get('amount'));
|
||||
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
|
||||
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
|
||||
$budgetLimit = $this->repository->updateLimitAmount($budget, $start, $end, $amount);
|
||||
if (0 === bccomp($amount, '0')) {
|
||||
$budgetLimit = null;
|
||||
@ -421,7 +421,7 @@ class BudgetController extends Controller
|
||||
$defaultCurrency = app('amount')->getDefaultCurrency();
|
||||
$amount = $request->get('amount');
|
||||
$page = $request->integer('page') === 0 ? 1 : $request->integer('page');
|
||||
|
||||
$this->repository->cleanupBudgets();
|
||||
$this->repository->setAvailableBudget($defaultCurrency, $start, $end, $amount);
|
||||
Preferences::mark();
|
||||
|
||||
@ -504,7 +504,7 @@ class BudgetController extends Controller
|
||||
{
|
||||
$data = $request->getBudgetData();
|
||||
$budget = $this->repository->store($data);
|
||||
|
||||
$this->repository->cleanupBudgets();
|
||||
$request->session()->flash('success', strval(trans('firefly.stored_new_budget', ['name' => $budget->name])));
|
||||
Preferences::mark();
|
||||
|
||||
@ -531,6 +531,7 @@ class BudgetController extends Controller
|
||||
$this->repository->update($budget, $data);
|
||||
|
||||
$request->session()->flash('success', strval(trans('firefly.updated_budget', ['name' => $budget->name])));
|
||||
$this->repository->cleanupBudgets();
|
||||
Preferences::mark();
|
||||
|
||||
if (1 === intval($request->get('return_to_edit'))) {
|
||||
|
@ -60,19 +60,17 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
// delete limits with amount 0:
|
||||
BudgetLimit::where('amount', 0)->delete();
|
||||
|
||||
// clean up:
|
||||
$set = BudgetLimit::groupBy(['budget_id', 'start_date', 'end_date'])
|
||||
->get(['budget_id', 'start_date', 'end_date', DB::raw('COUNT(*) as ct')]);
|
||||
foreach ($set as $entry) {
|
||||
if ($entry->ct > 1) {
|
||||
$newest = BudgetLimit::where('start_date', $entry->start_date)->where('end_date', $entry->end_date)
|
||||
->where('budget_id', $entry->budget_id)->orderBy('updated_at', 'DESC')->first(['budget_limits.*']);
|
||||
if (!is_null($newest)) {
|
||||
BudgetLimit::where('start_date', $entry->start_date)->where('end_date', $entry->end_date)
|
||||
->where('budget_id', $entry->budget_id)
|
||||
->where('id', '!=', $newest->id)->delete();
|
||||
}
|
||||
// do the clean up by hand because Sqlite can be tricky with this.
|
||||
$budgetLimits = BudgetLimit::orderBy('created_at', 'DESC')->get(['id', 'budget_id', 'start_date', 'end_date']);
|
||||
$count = [];
|
||||
/** @var BudgetLimit $budgetLimit */
|
||||
foreach ($budgetLimits as $budgetLimit) {
|
||||
$key = $budgetLimit->budget_id . '-' . $budgetLimit->start_date->format('Y-m-d') . $budgetLimit->end_date->format('Y-m-d');
|
||||
if (isset($count[$key])) {
|
||||
// delete it!
|
||||
BudgetLimit::find($budgetLimit->id)->delete();
|
||||
}
|
||||
$count[$key] = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -629,6 +627,7 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
*/
|
||||
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): BudgetLimit
|
||||
{
|
||||
$this->cleanupBudgets();
|
||||
// count the limits:
|
||||
$limits = $budget->budgetlimits()
|
||||
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
||||
|
Loading…
Reference in New Issue
Block a user