/** * @return array */ public function rules() { $nameRule = 'required|between:1,100|uniqueObjectForUser:budgets,name'; if (Budget::find(Input::get('id'))) { $nameRule = 'required|between:1,100|uniqueObjectForUser:budgets,name,' . intval(Input::get('id')); } return ['name' => $nameRule, 'active' => 'numeric|between:0,1']; }
/** * @return Budget */ public function convert() { // is mapped? Then it's easy! if (isset($this->mapped[$this->index][$this->value])) { $budget = Auth::user()->budgets()->find($this->mapped[$this->index][$this->value]); } else { $budget = Budget::firstOrCreateEncrypted(['name' => $this->value, 'user_id' => Auth::user()->id, 'active' => true]); } return $budget; }
/** * @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 $value * @param $route * * @return mixed */ public static function routeBinder($value, $route) { if (Auth::check()) { $ids = explode(',', $value); /** @var \Illuminate\Support\Collection $object */ $object = Budget::where('active', 1)->whereIn('id', $ids)->where('user_id', Auth::user()->id)->get(); // add empty budget if applicable. if (in_array('0', $ids)) { $object->push(new Budget()); } if ($object->count() > 0) { return $object; } } throw new NotFoundHttpException(); }
/** * @param array $fields * * @return Budget */ public static function firstOrCreateEncrypted(array $fields) { // everything but the name: $query = Budget::orderBy('id'); $search = $fields; unset($search['name']); foreach ($search as $name => $value) { $query->where($name, $value); } $set = $query->get(['budgets.*']); /** @var Budget $budget */ foreach ($set as $budget) { if ($budget->name == $fields['name']) { return $budget; } } // create it! $budget = Budget::create($fields); return $budget; }
/** * @param TransactionJournal $journal * @param array $transaction * * @return Collection */ public function storeTransaction(TransactionJournal $journal, array $transaction) : Collection { // store accounts (depends on type) list($sourceAccount, $destinationAccount) = $this->storeAccounts($journal->transactionType->type, $transaction); // store transaction one way: /** @var Transaction $one */ $one = Transaction::create(['account_id' => $sourceAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $transaction['amount'] * -1, 'description' => $transaction['description']]); $two = Transaction::create(['account_id' => $destinationAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $transaction['amount'], 'description' => $transaction['description']]); if (strlen($transaction['category']) > 0) { $category = Category::firstOrCreateEncrypted(['name' => $transaction['category'], 'user_id' => $journal->user_id]); $one->categories()->save($category); $two->categories()->save($category); } if (intval($transaction['budget_id']) > 0) { $budget = Budget::find($transaction['budget_id']); $one->budgets()->save($budget); $two->budgets()->save($budget); } if ($transaction['piggy_bank_id'] > 0) { $transaction['date'] = $journal->date->format('Y-m-d'); event(new TransactionStored($transaction)); } return new Collection([$one, $two]); }
/** * @param BudgetRepositoryInterface $repository * @param Budget $budget * * @return View * @throws FireflyException */ public function show(BudgetRepositoryInterface $repository, Budget $budget) { /** @var Carbon $start */ $start = session('first', Carbon::create()->startOfYear()); $end = new Carbon(); $page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page')); $pageSize = Preferences::get('transactionPageSize', 50)->data; $offset = ($page - 1) * $pageSize; $journals = $repository->journalsInPeriod(new Collection([$budget]), new Collection(), $start, $end); $count = $journals->count(); $journals = $journals->slice($offset, $pageSize); $journals = new LengthAwarePaginator($journals, $count, $pageSize); $journals->setPath('/budgets/show/' . $budget->id); $set = $budget->limitrepetitions()->orderBy('startdate', 'DESC')->get(); $subTitle = e($budget->name); $limits = new Collection(); /** @var LimitRepetition $entry */ foreach ($set as $entry) { $entry->spent = $repository->spentInPeriod(new Collection([$budget]), new Collection(), $entry->startdate, $entry->enddate); $limits->push($entry); } return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle')); }
} } throw new NotFoundHttpException(); }); Route::bind('bill', function ($value) { if (Auth::check()) { $object = Bill::where('id', $value)->where('user_id', Auth::user()->id)->first(); if ($object) { return $object; } } throw new NotFoundHttpException(); }); Route::bind('budget', function ($value) { if (Auth::check()) { $object = Budget::where('id', $value)->where('user_id', Auth::user()->id)->first(); if ($object) { return $object; } } throw new NotFoundHttpException(); }); Route::bind('limitrepetition', function ($value) { if (Auth::check()) { $object = LimitRepetition::where('limit_repetitions.id', $value)->leftjoin('budget_limits', 'budget_limits.id', '=', 'limit_repetitions.budget_limit_id')->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')->where('budgets.user_id', Auth::user()->id)->first(['limit_repetitions.*']); if ($object) { return $object; } } throw new NotFoundHttpException(); });
/** * @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; }
/** * @covers FireflyIII\Repositories\Budget\BudgetRepository::destroy */ public function testDestroy() { $budget = FactoryMuffin::create('FireflyIII\\Models\\Budget'); $this->object->destroy($budget); $this->assertCount(0, Budget::where('id', $budget->id)->whereNull('deleted_at')->get()); }
/** * Reports on budgets without any transactions. */ private function reportBudgets() { $set = Budget::leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id')->leftJoin('users', 'budgets.user_id', '=', 'users.id')->distinct()->whereNull('budget_transaction_journal.budget_id')->whereNull('budgets.deleted_at')->get(['budgets.id', 'budgets.name', 'budgets.user_id', 'users.email']); /** @var stdClass $entry */ foreach ($set as $entry) { $line = 'Notice: User #' . $entry->user_id . ' (' . $entry->email . ') has budget #' . $entry->id . ' ("' . Crypt::decrypt($entry->name) . '") which has no transactions.'; $this->line($line); } }
/** * @param TransactionJournal $journal * @param array $data * * @return TransactionJournal */ public function update(TransactionJournal $journal, array $data) { // update actual journal. $journal->transaction_currency_id = $data['amount_currency_id']; $journal->description = $data['description']; $journal->date = $data['date']; // unlink all categories, recreate them: $journal->categories()->detach(); if (strlen($data['category']) > 0) { $category = Category::firstOrCreateEncrypted(['name' => $data['category'], 'user_id' => $data['user']]); $journal->categories()->save($category); } // unlink all budgets and recreate them: $journal->budgets()->detach(); if (intval($data['budget_id']) > 0) { /** @var \FireflyIII\Models\Budget $budget */ $budget = Budget::find($data['budget_id']); $journal->budgets()->save($budget); } // store accounts (depends on type) list($from, $to) = $this->storeAccounts($journal->transactionType, $data); // update the from and to transaction. /** @var Transaction $transaction */ foreach ($journal->transactions()->get() as $transaction) { if (floatval($transaction->amount) < 0) { // this is the from transaction, negative amount: $transaction->amount = $data['amount'] * -1; $transaction->account_id = $from->id; $transaction->save(); } if (floatval($transaction->amount) > 0) { $transaction->amount = $data['amount']; $transaction->account_id = $to->id; $transaction->save(); } } $journal->save(); // update tags: if (isset($data['tags']) && is_array($data['tags'])) { $this->updateTags($journal, $data['tags']); } return $journal; }
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(); } } }); }
/** * @param $name * * @return Budget|null */ protected function findBudget($name) { // account $user = User::whereEmail('*****@*****.**')->first(); /** @var Budget $budget */ foreach (Budget::get() as $budget) { if ($budget->name == $name && $user->id == $budget->user_id) { return $budget; break; } } return null; }
/** * @param $name * * @return Budget|null */ protected function findBudget($name) { /** @var Budget $budget */ foreach (Budget::get() as $budget) { if ($budget->name == $name && $this->user->id == $budget->user_id) { return $budget; break; } } return null; }
/** * @param TransactionJournal $journal * @param array $data * * @return TransactionJournal */ public function update(TransactionJournal $journal, array $data) : TransactionJournal { // update actual journal. $journal->transaction_currency_id = $data['amount_currency_id_amount']; $journal->description = $data['description']; $journal->date = $data['date']; // unlink all categories, recreate them: $journal->categories()->detach(); if (strlen($data['category']) > 0) { $category = Category::firstOrCreateEncrypted(['name' => $data['category'], 'user_id' => $data['user']]); $journal->categories()->save($category); } // unlink all budgets and recreate them: $journal->budgets()->detach(); if (intval($data['budget_id']) > 0 && $journal->transactionType->type !== TransactionType::TRANSFER) { /** @var \FireflyIII\Models\Budget $budget */ $budget = Budget::where('user_id', $this->user->id)->where('id', $data['budget_id'])->first(); $journal->budgets()->save($budget); } // store accounts (depends on type) list($fromAccount, $toAccount) = $this->storeAccounts($journal->transactionType, $data); // update the from and to transaction. /** @var Transaction $transaction */ foreach ($journal->transactions()->get() as $transaction) { if ($transaction->amount < 0) { // this is the from transaction, negative amount: $transaction->amount = $data['amount'] * -1; $transaction->account_id = $fromAccount->id; $transaction->save(); } if ($transaction->amount > 0) { $transaction->amount = $data['amount']; $transaction->account_id = $toAccount->id; $transaction->save(); } } $journal->save(); // update tags: if (isset($data['tags']) && is_array($data['tags'])) { $this->updateTags($journal, $data['tags']); } // update meta fields: $result = $journal->save(); if ($result) { foreach ($data as $key => $value) { if (in_array($key, $this->validMetaFields)) { $journal->setMeta($key, $value); continue; } Log::debug(sprintf('Could not store meta field "%s" with value "%s" for journal #%d', json_encode($key), json_encode($value), $journal->id)); } return $journal; } return $journal; }