Removed duplicate code.

This commit is contained in:
James Cole 2015-06-05 10:02:40 +02:00
parent 3a06a6ac07
commit 4b7e1ae1c6
7 changed files with 70 additions and 58 deletions

View File

@ -7,6 +7,7 @@ use Carbon\Carbon;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Shared\ComponentRepository;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Pagination\LengthAwarePaginator;
@ -18,7 +19,7 @@ use Input;
*
* @package FireflyIII\Repositories\Budget
*/
class BudgetRepository implements BudgetRepositoryInterface
class BudgetRepository extends ComponentRepository implements BudgetRepositoryInterface
{
/**
@ -262,32 +263,11 @@ class BudgetRepository implements BudgetRepositoryInterface
* @param Carbon $end
* @param bool $shared
*
* @return float
* @return string
*/
public function spentInPeriodCorrected(Budget $budget, Carbon $start, Carbon $end, $shared = true)
{
if ($shared === true) {
// get everything:
$sum = floatval($budget->transactionjournals()->before($end)->after($start)->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)
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->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;
return $this->spentInPeriod($budget, $start, $end, $shared);
}
/**

View File

@ -134,7 +134,7 @@ interface BudgetRepositoryInterface
* @param Carbon $end
* @param boolean $shared
*
* @return float
* @return string
*/
public function spentInPeriodCorrected(Budget $budget, Carbon $start, Carbon $end, $shared = true);

View File

@ -9,13 +9,13 @@ use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use FireflyIII\Repositories\Shared\ComponentRepository;
/**
* Class CategoryRepository
*
* @package FireflyIII\Repositories\Category
*/
class CategoryRepository implements CategoryRepositoryInterface
class CategoryRepository extends ComponentRepository implements CategoryRepositoryInterface
{
/**
@ -181,39 +181,11 @@ class CategoryRepository implements CategoryRepositoryInterface
*
* @param bool $shared
*
* @return float
* @return string
*/
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'])
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->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;
return $this->spentInPeriod($category, $start, $end, $shared);
}
/**

View File

@ -81,7 +81,7 @@ interface CategoryRepositoryInterface
*
* @param bool $shared
*
* @return float
* @return string
*/
public function spentInPeriodCorrected(Category $category, Carbon $start, Carbon $end, $shared = false);

View File

@ -0,0 +1,56 @@
<?php
namespace FireflyIII\Repositories\Shared;
use Carbon\Carbon;
use Illuminate\Database\Query\JoinClause;
/**
* Class ComponentRepository
*
* @package FireflyIII\Repositories\Shared
*/
class ComponentRepository
{
/**
* @param $object
* @param Carbon $start
* @param Carbon $end
*
* @param bool $shared
*
* @return string
*/
protected function spentInPeriod($object, Carbon $start, Carbon $end, $shared = false)
{
if ($shared === true) {
// shared is true.
// always ignore transfers between accounts!
$sum
= $object->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 = $object->transactionjournals()
->before($end)
->after($start)
->transactionTypes(['Withdrawal'])
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->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');
}
return $sum;
}
}

View File

@ -290,6 +290,7 @@ class BudgetRepositoryTest extends TestCase
/**
* @covers FireflyIII\Repositories\Budget\BudgetRepository::spentInPeriodCorrected
* @covers FireflyIII\Repositories\Shared\ComponentRepository::spentInPeriod
*/
public function testSpentInPeriodCorrected()
{
@ -301,6 +302,7 @@ class BudgetRepositoryTest extends TestCase
/**
* @covers FireflyIII\Repositories\Budget\BudgetRepository::spentInPeriodCorrected
* @covers FireflyIII\Repositories\Shared\ComponentRepository::spentInPeriod
*/
public function testSpentInPeriodCorrectedShared()
{

View File

@ -208,6 +208,7 @@ class CategoryRepositoryTest extends TestCase
/**
* @covers FireflyIII\Repositories\Category\CategoryRepository::spentInPeriodCorrected
* @covers FireflyIII\Repositories\Shared\ComponentRepository::spentInPeriod
*/
public function testSpentInPeriodSumCorrected()
{
@ -221,6 +222,7 @@ class CategoryRepositoryTest extends TestCase
/**
* @covers FireflyIII\Repositories\Category\CategoryRepository::spentInPeriodCorrected
* @covers FireflyIII\Repositories\Shared\ComponentRepository::spentInPeriod
*/
public function testSpentInPeriodSumCorrectedShared()
{