mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-18 12:28:46 +00:00
All kinds of code cleanup, mostly to get some mess detection fixed.
This commit is contained in:
@@ -235,11 +235,10 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
|
||||
public function repetitionOnStartingOnDate(\Budget $budget, Carbon $date)
|
||||
{
|
||||
return \LimitRepetition::
|
||||
leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')->leftJoin(
|
||||
'budgets', 'budget_limits.budget_id', '=', 'budgets.id'
|
||||
)->where('limit_repetitions.startdate', $date->format('Y-m-d'))->where(
|
||||
'budgets.id', $budget->id
|
||||
)->first(['limit_repetitions.*']);
|
||||
leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
||||
->where('limit_repetitions.startdate', $date->format('Y-m-d'))
|
||||
->where('budget_limits.budget_id', $budget->id)
|
||||
->first(['limit_repetitions.*']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -251,15 +250,22 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
|
||||
public function transactionsWithoutBudgetInDateRange(Carbon $start, Carbon $end)
|
||||
{
|
||||
// Add expenses that have no budget:
|
||||
return $this->getUser()->transactionjournals()->whereNotIn(
|
||||
'transaction_journals.id', function ($query) use ($start, $end) {
|
||||
$query->select('transaction_journals.id')->from('transaction_journals')->leftJoin(
|
||||
'budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id'
|
||||
)->leftJoin('budgets', 'budgets.id', '=', 'budget_transaction_journal.budget_id')->where(
|
||||
'transaction_journals.date', '>=', $start->format('Y-m-d')
|
||||
)->where('transaction_journals.date', '<=', $end->format('Y-m-d'));
|
||||
return $this->getUser()
|
||||
->transactionjournals()
|
||||
->whereNotIn('transaction_journals.id', function ($query) use ($start, $end) {
|
||||
$query
|
||||
->select('transaction_journals.id')
|
||||
->from('transaction_journals')
|
||||
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'));
|
||||
}
|
||||
)->before($end)->after($start)->lessThan(0)->transactionTypes(['Withdrawal'])->get();
|
||||
)
|
||||
->before($end)
|
||||
->after($start)
|
||||
->lessThan(0)
|
||||
->transactionTypes(['Withdrawal'])
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -232,6 +232,7 @@ class PiggyBank implements CUD, CommonDatabaseCalls, PiggyBankInterface
|
||||
*/
|
||||
public function findRepetitionByDate(\Piggybank $piggybank, Carbon $date)
|
||||
{
|
||||
/** @var Collection $reps */
|
||||
$reps = $piggybank->piggybankrepetitions()->get();
|
||||
if ($reps->count() == 1) {
|
||||
return $reps->first();
|
||||
@@ -239,7 +240,19 @@ class PiggyBank implements CUD, CommonDatabaseCalls, PiggyBankInterface
|
||||
if ($reps->count() == 0) {
|
||||
throw new FireflyException('Should always find a piggy bank repetition.');
|
||||
}
|
||||
throw new NotImplementedException;
|
||||
// should filter the one we need:
|
||||
$repetitions = $reps->filter(
|
||||
function (\PiggybankRepetition $rep) use ($date) {
|
||||
if ($date >= $rep->startdate && $date <= $rep->targetdate) {
|
||||
return $rep;
|
||||
}
|
||||
}
|
||||
);
|
||||
if ($repetitions->count() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $repetitions->first();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -117,12 +117,10 @@ class RepeatedExpense implements CUD, CommonDatabaseCalls, PiggyBankInterface
|
||||
public function store(array $data)
|
||||
{
|
||||
|
||||
$data['rep_every'] = isset($data['rep_every']) ? $data['rep_every'] : 0;
|
||||
$data['reminder_skip'] = isset($data['reminder_skip']) ? $data['reminder_skip'] : 0;
|
||||
$data['order'] = isset($data['order']) ? $data['order'] : 0;
|
||||
$data['remind_me'] = isset($data['remind_me']) ? intval($data['remind_me']) : 0;
|
||||
$data['startdate'] = isset($data['startdate']) ? $data['startdate'] : Carbon::now()->format('Y-m-d');
|
||||
$data['targetdate'] = isset($data['targetdate']) && $data['targetdate'] != '' ? $data['targetdate'] : null;
|
||||
$data['rep_every'] = intval($data['rep_every']);
|
||||
$data['reminder_skip'] = intval($data['reminder_skip']);
|
||||
$data['order'] = intval($data['order']);
|
||||
$data['remind_me'] = intval($data['remind_me']);
|
||||
$data['account_id'] = intval($data['account_id']);
|
||||
|
||||
|
||||
@@ -130,12 +128,7 @@ class RepeatedExpense implements CUD, CommonDatabaseCalls, PiggyBankInterface
|
||||
$data['reminder'] = null;
|
||||
}
|
||||
|
||||
|
||||
$repeated = new \Piggybank($data);
|
||||
if (!$repeated->isValid()) {
|
||||
var_dump($repeated->getErrors()->all());
|
||||
exit;
|
||||
}
|
||||
$repeated->save();
|
||||
|
||||
return $repeated;
|
||||
|
||||
@@ -92,78 +92,20 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
*/
|
||||
public function update(\Eloquent $model, array $data)
|
||||
{
|
||||
/** @var \FireflyIII\Database\TransactionType\TransactionType $typeRepository */
|
||||
$typeRepository = \App::make('FireflyIII\Database\TransactionType\TransactionType');
|
||||
|
||||
/** @var \FireflyIII\Database\Account\Account $accountRepository */
|
||||
$accountRepository = \App::make('FireflyIII\Database\Account\Account');
|
||||
|
||||
/** @var \FireflyIII\Database\TransactionCurrency\TransactionCurrency $currencyRepository */
|
||||
$currencyRepository = \App::make('FireflyIII\Database\TransactionCurrency\TransactionCurrency');
|
||||
|
||||
$journalType = $typeRepository->findByWhat($data['what']);
|
||||
$currency = $currencyRepository->findByCode($data['currency']);
|
||||
$journalType = $this->getJournalType($data['what']);
|
||||
$currency = $this->getJournalCurrency($data['currency']);
|
||||
$model->description = $data['description'];
|
||||
$model->date = $data['date'];
|
||||
|
||||
$model->transactionType()->associate($journalType);
|
||||
$model->transactionCurrency()->associate($currency);
|
||||
$model->user()->associate($this->getUser());
|
||||
$model->description = $data['description'];
|
||||
$model->date = $data['date'];
|
||||
|
||||
/*
|
||||
* This must be enough to store the journal:
|
||||
*/
|
||||
if (!$model->isValid()) {
|
||||
\Log::error($model->getErrors()->all());
|
||||
throw new FireflyException('store() transaction journal failed, but it should not!');
|
||||
}
|
||||
$model->save();
|
||||
|
||||
/*
|
||||
* Still need to find the accounts related to the transactions.
|
||||
* This depends on the type of transaction.
|
||||
*/
|
||||
switch ($data['what']) {
|
||||
case 'withdrawal':
|
||||
$data['from'] = $accountRepository->find($data['account_id']);
|
||||
$data['to'] = $accountRepository->firstExpenseAccountOrCreate($data['expense_account']);
|
||||
break;
|
||||
case 'opening':
|
||||
break;
|
||||
case 'deposit':
|
||||
$data['to'] = $accountRepository->find($data['account_id']);
|
||||
$data['from'] = $accountRepository->firstRevenueAccountOrCreate($data['revenue_account']);
|
||||
break;
|
||||
case 'transfer':
|
||||
$data['from'] = $accountRepository->find($data['account_from_id']);
|
||||
$data['to'] = $accountRepository->find($data['account_to_id']);
|
||||
break;
|
||||
list($fromAccount, $toAccount) = $this->storeAccounts($data);
|
||||
|
||||
default:
|
||||
throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".');
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Update the budget and the category.
|
||||
*/
|
||||
|
||||
if (isset($data['budget_id']) && intval($data['budget_id']) > 0) {
|
||||
/** @var \FireflyIII\Database\Budget\Budget $budgetRepository */
|
||||
$budgetRepository = \App::make('FireflyIII\Database\Budget\Budget');
|
||||
$budget = $budgetRepository->find(intval($data['budget_id']));
|
||||
if ($budget) {
|
||||
$model->budgets()->sync([$budget->id]);
|
||||
}
|
||||
}
|
||||
if (strlen($data['category']) > 0) {
|
||||
/** @var \FireflyIII\Database\Category\Category $categoryRepository */
|
||||
$categoryRepository = \App::make('FireflyIII\Database\Category\Category');
|
||||
$category = $categoryRepository->firstOrCreate($data['category']);
|
||||
if ($category) {
|
||||
$model->categories()->sync([$category->id]);
|
||||
}
|
||||
}
|
||||
$this->storeBudget($data, $model);
|
||||
$this->storeCategory($data, $model);
|
||||
|
||||
/*
|
||||
* TODO move to transaction thing.
|
||||
@@ -174,10 +116,10 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
foreach ($model->transactions()->get() as $transaction) {
|
||||
if (floatval($transaction->amount) > 0) {
|
||||
// the TO transaction.
|
||||
$transaction->account()->associate($data['to']);
|
||||
$transaction->account()->associate($toAccount);
|
||||
$transaction->amount = $amount;
|
||||
} else {
|
||||
$transaction->account()->associate($data['from']);
|
||||
$transaction->account()->associate($fromAccount);
|
||||
$transaction->amount = $amount * -1;
|
||||
}
|
||||
if (!$transaction->isValid()) {
|
||||
@@ -443,8 +385,8 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param \TransactionJournal $journal
|
||||
* @param array $data
|
||||
* @param \TransactionJournal|\Eloquent $journal
|
||||
*/
|
||||
public function storeBudget($data, \TransactionJournal $journal)
|
||||
{
|
||||
@@ -453,14 +395,14 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
$budgetRepository = \App::make('FireflyIII\Database\Budget\Budget');
|
||||
$budget = $budgetRepository->find(intval($data['budget_id']));
|
||||
if ($budget) {
|
||||
$journal->budgets()->save($budget);
|
||||
$journal->budgets()->sync([$budget->id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param \TransactionJournal $journal
|
||||
* @param array $data
|
||||
* @param \TransactionJournal|\Eloquent $journal
|
||||
*/
|
||||
public function storeCategory(array $data, \TransactionJournal $journal)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user