mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Removed duplicate code.
This commit is contained in:
parent
3a06a6ac07
commit
4b7e1ae1c6
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -81,7 +81,7 @@ interface CategoryRepositoryInterface
|
||||
*
|
||||
* @param bool $shared
|
||||
*
|
||||
* @return float
|
||||
* @return string
|
||||
*/
|
||||
public function spentInPeriodCorrected(Category $category, Carbon $start, Carbon $end, $shared = false);
|
||||
|
||||
|
56
app/Repositories/Shared/ComponentRepository.php
Normal file
56
app/Repositories/Shared/ComponentRepository.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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()
|
||||
{
|
||||
|
@ -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()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user