Added some correcting methods. [skip ci]

This commit is contained in:
James Cole 2015-05-20 06:50:15 +02:00
parent 9217c2f003
commit 42b49d0e4b
6 changed files with 174 additions and 4 deletions

View File

@ -99,15 +99,22 @@ class TransactionJournal extends Model
/** @var Tag $tag */ /** @var Tag $tag */
$tag = $this->tags()->where('tagMode', 'advancePayment')->first(); $tag = $this->tags()->where('tagMode', 'advancePayment')->first();
if ($tag && $this->transactionType->type == 'Withdrawal') { if ($tag && $this->transactionType->type == 'Withdrawal') {
// loop other deposits, remove from our amount. // loop other deposits, remove from our amount.
$others = $tag->transactionJournals()->transactionTypes(['Deposit'])->get(); $others = $tag->transactionJournals()->transactionTypes(['Deposit'])->get();
foreach ($others as $other) { foreach ($others as $other) {
$amount -= $other->amount; $amount -= $other->actualAmount;
} }
return $amount; return $amount;
} }
// if this journal is part of an advancePayment AND the journal is a deposit,
// then the journal amount is correcting a withdrawal, and the amount is zero:
if ($tag && $this->transactionType->type == 'Deposit') {
return 0;
}
return $amount; return $amount;
} }

View File

@ -99,6 +99,7 @@ class BudgetRepository implements BudgetRepositoryInterface
public function getBudgets() public function getBudgets()
{ {
$budgets = Auth::user()->budgets()->get(); $budgets = Auth::user()->budgets()->get();
return $budgets; return $budgets;
} }
@ -287,6 +288,40 @@ class BudgetRepository implements BudgetRepositoryInterface
return $sum; return $sum;
} }
/**
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
* @param bool $shared
*
* @return float
*/
public function spentInPeriodCorrected(Budget $budget, Carbon $start, Carbon $end, $shared = true)
{
if ($shared === true) {
// get everything:
$sum = floatval($budget->transactionjournals()->before($end)->after($start)->lessThan(0)->get(['transaction_journals.*'])->sum('amount'));
} else {
// get all journals in this month where the asset account is NOT shared.
$sum = $budget->transactionjournals()
->before($end)
->after($start)
->lessThan(0)
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->leftJoin(
'account_meta', function (JoinClause $join) {
$join->on('account_meta.account_id', '=', 'accounts.id')->where('account_meta.name', '=', 'accountRole');
}
)
->where('account_meta.data', '!=', '"sharedAsset"')
->get(['transaction_journals.*'])
->sum('amount');
$sum = floatval($sum);
}
return $sum;
}
/** /**
* @param array $data * @param array $data
* *
@ -372,4 +407,16 @@ class BudgetRepository implements BudgetRepositoryInterface
} }
/**
* @param Budget $budget
* @param Carbon $date
*
* @return float
*/
public function expensesOnDayCorrected(Budget $budget, Carbon $date)
{
$sum = floatval($budget->transactionjournals()->transactionTypes(['Withdrawal'])->onDate($date)->get(['transaction_journals.*'])->sum('amount'));
return $sum * -1;
}
} }

View File

@ -34,6 +34,16 @@ interface BudgetRepositoryInterface
*/ */
public function expensesOnDay(Budget $budget, Carbon $date); public function expensesOnDay(Budget $budget, Carbon $date);
/**
* Takes tags into account.
*
* @param Budget $budget
* @param Carbon $date
*
* @return float
*/
public function expensesOnDayCorrected(Budget $budget, Carbon $date);
/** /**
* @return Collection * @return Collection
*/ */
@ -122,16 +132,30 @@ interface BudgetRepositoryInterface
*/ */
public function getWithoutBudgetSum(Carbon $start, Carbon $end); public function getWithoutBudgetSum(Carbon $start, Carbon $end);
/** /**
* @param Budget $budget * @param Budget $budget
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* @param boolean $shared * @param boolean $shared
* *
* @return float * @return float
*/ */
public function spentInPeriod(Budget $budget, Carbon $start, Carbon $end, $shared = true); public function spentInPeriod(Budget $budget, Carbon $start, Carbon $end, $shared = true);
/**
*
* Same as ::spentInPeriod but corrects journals for their amount (tags).
*
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
* @param boolean $shared
*
* @return float
*/
public function spentInPeriodCorrected(Budget $budget, Carbon $start, Carbon $end, $shared = true);
/** /**
* @param array $data * @param array $data
* *

View File

@ -205,6 +205,48 @@ class CategoryRepository implements CategoryRepositoryInterface
return $sum; return $sum;
} }
/**
* @param Category $category
* @param Carbon $start
* @param Carbon $end
*
* @param bool $shared
*
* @return float
*/
public function spentInPeriodCorrected(Category $category, Carbon $start, Carbon $end, $shared = false)
{
if ($shared === true) {
// shared is true.
// always ignore transfers between accounts!
$sum = floatval(
$category->transactionjournals()
->transactionTypes(['Withdrawal'])
->before($end)->after($start)->get(['transaction_journals.*'])->sum('amount')
);
} else {
// do something else, SEE budgets.
// get all journals in this month where the asset account is NOT shared.
$sum = $category->transactionjournals()
->before($end)
->after($start)
->transactionTypes(['Withdrawal'])
->lessThan(0)
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->leftJoin(
'account_meta', function (JoinClause $join) {
$join->on('account_meta.account_id', '=', 'accounts.id')->where('account_meta.name', '=', 'accountRole');
}
)
->where('account_meta.data', '!=', '"sharedAsset"')
->get(['transaction_journals.*'])->sum('amount');
$sum = floatval($sum);
}
return $sum;
}
/** /**
* @param Category $category * @param Category $category
* @param Carbon $date * @param Carbon $date
@ -216,6 +258,18 @@ class CategoryRepository implements CategoryRepositoryInterface
return floatval($category->transactionjournals()->onDate($date)->lessThan(0)->sum('amount')) * -1; return floatval($category->transactionjournals()->onDate($date)->lessThan(0)->sum('amount')) * -1;
} }
/**
* Corrected for tags
* @param Category $category
* @param Carbon $date
*
* @return float
*/
public function spentOnDaySumCorrected(Category $category, Carbon $date)
{
return floatval($category->transactionjournals()->onDate($date)->get(['transaction_journals.*'])->sum('amount'));
}
/** /**
* @param array $data * @param array $data
* *

View File

@ -81,6 +81,19 @@ interface CategoryRepositoryInterface
*/ */
public function spentInPeriod(Category $category, Carbon $start, Carbon $end, $shared = false); public function spentInPeriod(Category $category, Carbon $start, Carbon $end, $shared = false);
/**
* Corrected for tags.
*
* @param Category $category
* @param \Carbon\Carbon $start
* @param \Carbon\Carbon $end
*
* @param bool $shared
*
* @return float
*/
public function spentInPeriodCorrected(Category $category, Carbon $start, Carbon $end, $shared = false);
/** /**
* @param Category $category * @param Category $category
* @param Carbon $date * @param Carbon $date
@ -89,6 +102,16 @@ interface CategoryRepositoryInterface
*/ */
public function spentOnDaySum(Category $category, Carbon $date); public function spentOnDaySum(Category $category, Carbon $date);
/**
*
* Corrected for tags.
* @param Category $category
* @param Carbon $date
*
* @return float
*/
public function spentOnDaySumCorrected(Category $category, Carbon $date);
/** /**
* @param array $data * @param array $data
* *

View File

@ -39,6 +39,21 @@ class Budget extends Twig_Extension
} }
); );
$functions[] = new Twig_SimpleFunction(
'spentInRepetitionCorrected', function (LimitRepetition $repetition) {
$sum =
Auth::user()->transactionjournals()
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budget_transaction_journal.budget_id')
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
->before($repetition->enddate)
->after($repetition->startdate)
->where('limit_repetitions.id', '=', $repetition->id)
->get(['transaction_journals.*'])->sum('amount');
return floatval($sum);
}
);
return $functions; return $functions;
} }