/** * Register any other events for your application. * * @param \Illuminate\Contracts\Events\Dispatcher $events * * @return void */ public function boot(DispatcherContract $events) { parent::boot($events); $this->registerDeleteEvents(); $this->registerCreateEvents(); BudgetLimit::saved(function (BudgetLimit $budgetLimit) { $end = Navigation::addPeriod(clone $budgetLimit->startdate, $budgetLimit->repeat_freq, 0); $end->subDay(); $set = $budgetLimit->limitrepetitions()->where('startdate', $budgetLimit->startdate->format('Y-m-d 00:00:00'))->where('enddate', $end->format('Y-m-d 00:00:00'))->get(); if ($set->count() == 0) { $repetition = new LimitRepetition(); $repetition->startdate = $budgetLimit->startdate; $repetition->enddate = $end; $repetition->amount = $budgetLimit->amount; $repetition->budgetLimit()->associate($budgetLimit); try { $repetition->save(); } catch (QueryException $e) { Log::error('Trying to save new LimitRepetition failed: ' . $e->getMessage()); // @codeCoverageIgnore } } else { if ($set->count() == 1) { $repetition = $set->first(); $repetition->amount = $budgetLimit->amount; $repetition->save(); } } }); }
/** * @covers FireflyIII\Repositories\Budget\BudgetRepository::cleanupBudgets * @covers FireflyIII\Providers\EventServiceProvider::boot */ public function testCleanupBudgets() { // create some budgets: for ($i = 0; $i < 3; $i++) { $budget = FactoryMuffin::create('FireflyIII\\Models\\Budget'); $limit = FactoryMuffin::create('FireflyIII\\Models\\BudgetLimit'); $limit->budget_id = $budget->id; $limit->amount = 0; $limit->save(); } $this->object->cleanupBudgets(); $this->assertCount(0, BudgetLimit::get()); }
/** * @param Carbon $current * @param $name * @param $amount */ protected function createBudgetLimit(Carbon $current, $name, $amount) { $start = clone $current; $end = clone $current; $budget = $this->findBudget($name); $start->startOfMonth(); $end->endOfMonth(); BudgetLimit::create(['budget_id' => $budget->id, 'startdate' => $start->format('Y-m-d'), 'amount' => $amount, 'repeats' => 0, 'repeat_freq' => 'monthly']); }
/** * */ public function createBudgets() { $user = User::whereEmail('*****@*****.**')->first(); $groceries = Budget::create(['user_id' => $user->id, 'name' => 'Groceries']); $bills = Budget::create(['user_id' => $user->id, 'name' => 'Bills']); $deleteMe = Budget::create(['user_id' => $user->id, 'name' => 'Delete me']); Budget::create(['user_id' => $user->id, 'name' => 'Budget without repetition']); BudgetLimit::create(['startdate' => $this->som, 'amount' => 201, 'repeats' => 0, 'repeat_freq' => 'monthly', 'budget_id' => $groceries->id]); BudgetLimit::create(['startdate' => $this->som, 'amount' => 202, 'repeats' => 0, 'repeat_freq' => 'monthly', 'budget_id' => $bills->id]); BudgetLimit::create(['startdate' => $this->som, 'amount' => 203, 'repeats' => 0, 'repeat_freq' => 'monthly', 'budget_id' => $deleteMe->id]); // and because we have no filters, some repetitions: }
/** * @param $user */ private function createBudgets($user) { $set = [Budget::firstOrCreateEncrypted(['name' => 'Groceries', 'user_id' => $user->id]), Budget::firstOrCreateEncrypted(['name' => 'Bills', 'user_id' => $user->id])]; $current = new Carbon(); /** @var Budget $budget */ foreach ($set as $budget) { // some budget limits: $start = clone $current; $end = clone $current; $start->startOfMonth(); $end->endOfMonth(); BudgetLimit::create(['budget_id' => $budget->id, 'startdate' => $start->format('Y-m-d'), 'amount' => 500, 'repeats' => 0, 'repeat_freq' => 'monthly']); } }
/** * @param Budget $budget * @param Carbon $date * @param $amount * * @return BudgetLimit */ public function updateLimitAmount(Budget $budget, Carbon $date, $amount) { // there should be a budget limit for this startdate: /** @var BudgetLimit $limit */ $limit = $budget->budgetlimits()->where('budget_limits.startdate', $date)->first(['budget_limits.*']); if (!$limit) { // if not, create one! $limit = new BudgetLimit(); $limit->budget()->associate($budget); $limit->startdate = $date; $limit->amount = $amount; $limit->repeat_freq = 'monthly'; $limit->repeats = 0; $limit->save(); // likewise, there should be a limit repetition to match the end date // (which is always the end of the month) but that is caught by an event. } else { if ($amount > 0) { $limit->amount = $amount; $limit->save(); } else { $limit->delete(); } } return $limit; }
/** * @param Budget $budget * @param Carbon $start * @param Carbon $end * @param string $range * @param int $amount * * @return BudgetLimit */ public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $range, int $amount) : BudgetLimit { // there might be a budget limit for this startdate: $repeatFreq = config('firefly.range_to_repeat_freq.' . $range); /** @var BudgetLimit $limit */ $limit = $budget->budgetlimits()->where('budget_limits.startdate', $start)->where('budget_limits.repeat_freq', $repeatFreq)->first(['budget_limits.*']); // delete if amount is zero. if (!is_null($limit) && $amount <= 0.0) { $limit->delete(); return new BudgetLimit(); } // update if exists: if (!is_null($limit)) { $limit->amount = $amount; $limit->save(); // fire event to create or update LimitRepetition. event(new BudgetLimitUpdated($limit, $end)); return $limit; } // create one and return it. $limit = new BudgetLimit(); $limit->budget()->associate($budget); $limit->startdate = $start; $limit->amount = $amount; $limit->repeat_freq = $repeatFreq; $limit->repeats = 0; $limit->save(); event(new BudgetLimitStored($limit, $end)); // likewise, there should be a limit repetition to match the end date // (which is always the end of the month) but that is caught by an event. // so handled automatically. return $limit; }
public function moveComponentIdToBudgetId() { BudgetLimit::get()->each(function (BudgetLimit $bl) { Log::debug('Now at budgetLimit #' . $bl->id . ' with component_id: ' . $bl->component_id); $component = Component::find($bl->component_id); if ($component) { Log::debug('Found component with id #' . $component->id . ' and name ' . $component->name); $budget = Budget::whereName($component->name)->whereUserId($component->user_id)->first(); if ($budget) { Log::debug('Found a budget with ID #' . $budget->id . ' and name ' . $budget->name); $bl->budget_id = $budget->id; $bl->save(); Log::debug('Connected budgetLimit #' . $bl->id . ' to budget_id' . $budget->id); } else { Log::debug('Could not find a matching budget with name ' . $component->name); } } else { Log::debug('Could not find a component with id ' . $bl->component_id); } }); }
private function updateComponentInBudgetLimits() { BudgetLimit::get()->each(function (BudgetLimit $bl) { $budgetId = $bl->budget_id; $budget = Budget::find($budgetId); if ($budget) { $component = Component::where('class', 'Budget')->where('user_id', $budget->user_id)->where('name', $budget->name)->first(); if ($component) { $bl->component_id = $component->id; $bl->save(); } } }); }