Some cleaning up, and I hope simplification.

This commit is contained in:
James Cole 2015-12-30 08:15:04 +01:00
parent 39b88e8207
commit a3d2a9e00b
4 changed files with 114 additions and 84 deletions

View File

@ -129,7 +129,6 @@ class CategoryController extends Controller
}
/**
* @param CategoryRepositoryInterface $repository
* @param $reportType
* @param Carbon $start
* @param Carbon $end
@ -140,9 +139,9 @@ class CategoryController extends Controller
*/
public function multiYear($reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $categories)
{
/** @var CategoryRepositoryInterface $repository */
/** @var CRI $repository */
$repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
/** @var SingleCategoryRepositoryInterface $singleRepository */
/** @var SCRI $singleRepository */
$singleRepository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface');
// chart properties for cache:
@ -182,8 +181,8 @@ class CategoryController extends Controller
// get data:
if (is_null($category->id)) {
$name = trans('firefly.noCategory');
$spent = $repository->spentNoCategoryForAccounts($accounts, $currentStart, $currentEnd);
$earned = $repository->earnedNoCategoryForAccounts($accounts, $currentStart, $currentEnd);
$spent = $repository->sumSpentNoCategory($accounts, $currentStart, $currentEnd);
$earned = $repository->sumEarnedNoCategory($accounts, $currentStart, $currentEnd);
} else {
$name = $category->name;
$spent = $singleRepository->spentInPeriodForAccounts($category, $accounts, $currentStart, $currentEnd);

View File

@ -7,6 +7,7 @@ use Carbon\Carbon;
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;
@ -44,72 +45,6 @@ class CategoryRepository implements CategoryRepositoryInterface
return $set;
}
/**
* Returns the amount earned without category by accounts in period.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function earnedNoCategoryForAccounts(Collection $accounts, Carbon $start, Carbon $end)
{
$accountIds = [];
foreach ($accounts as $account) {
$accountIds[] = $account->id;
}
// is deposit AND account_from is in the list of $accounts
// not from any of the accounts in the list?
return Auth::user()
->transactionjournals()
->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->whereNull('category_transaction_journal.id')
->before($end)
->after($start)
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->whereIn('transactions.account_id', $accountIds)
->transactionTypes([TransactionType::DEPOSIT])
->get(['transaction_journals.*'])->sum('amount');
}
/**
* Returns the amount spent without category by accounts in period.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function spentNoCategoryForAccounts(Collection $accounts, Carbon $start, Carbon $end)
{
$accountIds = [];
foreach ($accounts as $account) {
$accountIds[] = $account->id;
}
// is withdrawal or transfer AND account_from is in the list of $accounts
return Auth::user()
->transactionjournals()
->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->whereNull('category_transaction_journal.id')
->before($end)
->after($start)
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->whereIn('transactions.account_id', $accountIds)
->transactionTypes([TransactionType::WITHDRAWAL])
->get(['transaction_journals.*'])->sum('amount');
}
/**
* @param Carbon $start
* @param Carbon $end
@ -147,6 +82,7 @@ class CategoryRepository implements CategoryRepositoryInterface
}
// without category:
// TODO REMOVE ME
$single = Auth::user()->transactionjournals()
->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
@ -392,4 +328,79 @@ class CategoryRepository implements CategoryRepositoryInterface
return $collection;
}
/**
* Returns the total amount of money related to transactions without any category connected to
* it. Returns either the spent amount.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function sumSpentNoCategory(Collection $accounts, Carbon $start, Carbon $end)
{
return $this->sumNoCategory($accounts, $start, $end, Query::SPENT);
}
/**
* Returns the total amount of money related to transactions without any category connected to
* it. Returns either the earned amount.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function sumEarnedNoCategory(Collection $accounts, Carbon $start, Carbon $end)
{
return $this->sumNoCategory($accounts, $start, $end, Query::EARNED);
}
/**
* Returns the total amount of money related to transactions without any category connected to
* it. Returns either the earned or the spent amount.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param int $group
*
* @return string
*/
protected function sumNoCategory(Collection $accounts, Carbon $start, Carbon $end, $group = Query::EARNED)
{
$accountIds = [];
foreach ($accounts as $account) {
$accountIds[] = $account->id;
}
if ($group == Query::EARNED) {
$types = [TransactionType::DEPOSIT];
} else {
$types = [TransactionType::WITHDRAWAL];
}
// is withdrawal or transfer AND account_from is in the list of $accounts
$single = Auth::user()
->transactionjournals()
->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->whereNull('category_transaction_journal.id')
->before($end)
->after($start)
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->whereIn('transactions.account_id', $accountIds)
->transactionTypes($types)
->first(
[
DB::Raw('SUM(`transactions`.`amount`) as `sum`)')]
);
if (!is_null($single)) {
return $single->sum;
}
return '0';
}
}

View File

@ -3,6 +3,7 @@
namespace FireflyIII\Repositories\Category;
use Carbon\Carbon;
use FireflyIII\Sql\Query;
use Illuminate\Support\Collection;
/**
@ -39,17 +40,6 @@ interface CategoryRepositoryInterface
*/
public function earnedForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end);
/**
* Returns the amount earned without category by accounts in period.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function earnedNoCategoryForAccounts(Collection $accounts, Carbon $start, Carbon $end);
/**
* @return Collection
*/
@ -100,7 +90,8 @@ interface CategoryRepositoryInterface
public function spentForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end);
/**
* Returns the amount spent without category by accounts in period.
* Returns the total amount of money related to transactions without any category connected to
* it. Returns either the spent amount.
*
* @param Collection $accounts
* @param Carbon $start
@ -108,6 +99,18 @@ interface CategoryRepositoryInterface
*
* @return string
*/
public function spentNoCategoryForAccounts(Collection $accounts, Carbon $start, Carbon $end);
public function sumSpentNoCategory(Collection $accounts, Carbon $start, Carbon $end);
/**
* Returns the total amount of money related to transactions without any category connected to
* it. Returns either the earned amount.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function sumEarnedNoCategory(Collection $accounts, Carbon $start, Carbon $end);
}

17
app/Sql/Query.php Normal file
View File

@ -0,0 +1,17 @@
<?php
namespace FireflyIII\Sql;
/**
* Class Query
*
* This class holds some useful static constants.
*
* @package FireflyIII\Sql
*/
class Query
{
const SPENT = 1;
const EARNED = 2;
}