More code cleanup.

This commit is contained in:
James Cole
2015-12-30 08:21:11 +01:00
parent a3d2a9e00b
commit 6a9d931ba3
5 changed files with 39 additions and 156 deletions

View File

@@ -65,7 +65,7 @@ class ReportHelper implements ReportHelperInterface
/** @var \FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface $singleRepository */
$singleRepository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface');
$set = $repository->getCategories();
$set = $repository->listCategories();
foreach ($set as $category) {
$spent = $singleRepository->balanceInPeriod($category, $start, $end, $accounts);
$category->spent = $spent;

View File

@@ -115,7 +115,7 @@ class CategoryController extends Controller
*/
public function index(CRI $repository, SCRI $singleRepository)
{
$categories = $repository->getCategories();
$categories = $repository->listCategories();
$categories->each(
function (Category $category) use ($singleRepository) {
@@ -135,7 +135,7 @@ class CategoryController extends Controller
{
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->startOfMonth());
$list = $repository->getWithoutCategory($start, $end);
$list = $repository->listNoCategory($start, $end);
$subTitle = trans(
'firefly.without_category_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]

View File

@@ -179,7 +179,7 @@ class JsonController extends Controller
*/
public function categories(CRI $repository)
{
$list = $repository->getCategories();
$list = $repository->listCategories();
$return = [];
foreach ($list as $entry) {
$return[] = $entry->name;

View File

@@ -8,7 +8,6 @@ use DB;
use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionType;
use FireflyIII\Sql\Query;
use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
@@ -21,31 +20,8 @@ class CategoryRepository implements CategoryRepositoryInterface
{
/**
* @return Collection
*/
public function getCategories()
{
$cache = new CacheProperties;
$cache->addProperty('category-list');
if ($cache->has()) {
return $cache->get();
}
/** @var Collection $set */
$set = Auth::user()->categories()->orderBy('name', 'ASC')->get();
$set = $set->sortBy(
function (Category $category) {
return strtolower($category->name);
}
);
$cache->store($set);
return $set;
}
/**
* TODO REMOVE ME
*
* @param Carbon $start
* @param Carbon $end
*
@@ -106,12 +82,33 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* Returns a list of all the categories belonging to a user.
*
* @return Collection
*/
public function listCategories()
{
/** @var Collection $set */
$set = Auth::user()->categories()->orderBy('name', 'ASC')->get();
$set = $set->sortBy(
function (Category $category) {
return strtolower($category->name);
}
);
return $set;
}
/**
* Returns a list of transaction journals in the range (all types, all accounts) that have no category
* associated to them.
*
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getWithoutCategory(Carbon $start, Carbon $end)
public function listNoCategory(Carbon $start, Carbon $end)
{
return Auth::user()
->transactionjournals()
@@ -181,100 +178,6 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* Returns a collection of Categories appended with the amount of money that has been earned
* in these categories, based on the $accounts involved, in period X.
* The amount earned in category X in period X is saved in field "earned".
*
* @param $accounts
* @param $start
* @param $end
*
* @return Collection
*/
public function earnedForAccounts(Collection $accounts, Carbon $start, Carbon $end)
{
$accountIds = [];
foreach ($accounts as $account) {
$accountIds[] = $account->id;
}
$collection = Auth::user()->categories()
->leftJoin('category_transaction_journal', 'category_transaction_journal.category_id', '=', 'categories.id')
->leftJoin('transaction_journals', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->leftJoin(
'transactions AS t_src', function (JoinClause $join) {
$join->on('t_src.transaction_journal_id', '=', 'transaction_journals.id')->where('t_src.amount', '<', 0);
}
)
->leftJoin(
'transactions AS t_dest', function (JoinClause $join) {
$join->on('t_dest.transaction_journal_id', '=', 'transaction_journals.id')->where('t_dest.amount', '>', 0);
}
)
->whereIn('t_dest.account_id', $accountIds)// to these accounts (earned)
->whereNotIn('t_src.account_id', $accountIds)//-- but not from these accounts
->whereIn(
'transaction_types.type', [TransactionType::DEPOSIT, TransactionType::TRANSFER, TransactionType::OPENING_BALANCE]
)
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
->groupBy('categories.id')
->get(['categories.*', DB::Raw('SUM(`t_dest`.`amount`) AS `earned`')]);
return $collection;
}
/**
* Returns a collection of Categories appended with the amount of money that has been spent
* in these categories, based on the $accounts involved, in period X.
* The amount earned in category X in period X is saved in field "spent".
*
* @param $accounts
* @param $start
* @param $end
*
* @return Collection
*/
public function spentForAccounts(Collection $accounts, Carbon $start, Carbon $end)
{
$accountIds = [];
foreach ($accounts as $account) {
$accountIds[] = $account->id;
}
$collection = Auth::user()->categories()
->leftJoin('category_transaction_journal', 'category_transaction_journal.category_id', '=', 'categories.id')
->leftJoin('transaction_journals', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->leftJoin(
'transactions AS t_src', function (JoinClause $join) {
$join->on('t_src.transaction_journal_id', '=', 'transaction_journals.id')->where('t_src.amount', '<', 0);
}
)
->leftJoin(
'transactions AS t_dest', function (JoinClause $join) {
$join->on('t_dest.transaction_journal_id', '=', 'transaction_journals.id')->where('t_dest.amount', '>', 0);
}
)
->whereIn('t_src.account_id', $accountIds)// from these accounts (spent)
->whereNotIn('t_dest.account_id', $accountIds)//-- but not from these accounts (spent internally)
->whereIn(
'transaction_types.type', [TransactionType::WITHDRAWAL, TransactionType::TRANSFER, TransactionType::OPENING_BALANCE]
)// spent on these things.
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
->groupBy('categories.id')
->get(['categories.*', DB::Raw('SUM(`t_dest`.`amount`) AS `spent`')]);
return $collection;
}
/**
* Returns a collection of Categories appended with the amount of money that has been spent
* in these categories, based on the $accounts involved, in period X, grouped per month.

View File

@@ -14,18 +14,6 @@ use Illuminate\Support\Collection;
interface CategoryRepositoryInterface
{
/**
* Returns a collection of Categories appended with the amount of money that has been earned
* in these categories, based on the $accounts involved, in period X.
* The amount earned in category X in period X is saved in field "earned".
*
* @param $accounts
* @param $start
* @param $end
*
* @return Collection
*/
public function earnedForAccounts(Collection $accounts, Carbon $start, Carbon $end);
/**
* Returns a collection of Categories appended with the amount of money that has been earned
@@ -40,11 +28,6 @@ interface CategoryRepositoryInterface
*/
public function earnedForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end);
/**
* @return Collection
*/
public function getCategories();
/**
* Corrected for tags.
*
@@ -56,25 +39,22 @@ interface CategoryRepositoryInterface
public function getCategoriesAndExpenses(Carbon $start, Carbon $end);
/**
* Returns a list of all the categories belonging to a user.
*
* @return Collection
*/
public function listCategories();
/**
* Returns a list of transaction journals in the range (all types, all accounts) that have no category
* associated to them.
*
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getWithoutCategory(Carbon $start, Carbon $end);
/**
* Returns a collection of Categories appended with the amount of money that has been spent
* in these categories, based on the $accounts involved, in period X.
* The amount earned in category X in period X is saved in field "spent".
*
* @param $accounts
* @param $start
* @param $end
*
* @return Collection
*/
public function spentForAccounts(Collection $accounts, Carbon $start, Carbon $end);
public function listNoCategory(Carbon $start, Carbon $end);
/**
* Returns a collection of Categories appended with the amount of money that has been spent