Clean up some code.

This commit is contained in:
James Cole
2016-02-12 17:34:42 +01:00
parent e5402ea7c1
commit 42daf7ed32
3 changed files with 38 additions and 38 deletions

View File

@@ -23,6 +23,10 @@ use stdClass;
*/
class CategoryController extends Controller
{
const MAKE_POSITIVE = -1;
const KEEP_POSITIVE = 1;
/** @var \FireflyIII\Generator\Chart\Category\CategoryChartGeneratorInterface */
protected $generator;
@@ -134,31 +138,7 @@ class CategoryController extends Controller
return $category->name;
}
);
$entries = new Collection;
while ($start < $end) { // filter the set:
$row = [clone $start];
$currentSet = $set->filter( // get possibly relevant entries from the big $set
function (Category $category) use ($start) {
return $category->dateFormatted == $start->format('Y-m');
}
);
/** @var Category $category */
foreach ($categories as $category) { // check for each category if its in the current set.
$entry = $currentSet->filter( // if its in there, use the value.
function (Category $cat) use ($category) {
return ($cat->id == $category->id);
}
)->first();
if (!is_null($entry)) {
$row[] = round($entry->earned, 2);
} else {
$row[] = 0;
}
}
$entries->push($row);
$start->addMonth();
}
$entries = $this->filterCollection($start, $end, $set, $categories);
$data = $this->generator->earnedInPeriod($categories, $entries);
$cache->store($data);
@@ -340,12 +320,31 @@ class CategoryController extends Controller
return Response::json($cache->get()); // @codeCoverageIgnore
}
$set = $repository->spentForAccountsPerMonth($accounts, $start, $end);
$categories = $set->unique('id')->sortBy(
function (Category $category) {
return $category->name;
}
);
$entries = $this->filterCollection($start, $end, $set, $categories);
$data = $this->generator->spentInPeriod($categories, $entries);
$cache->store($data);
return $data;
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $set
* @param Collection $categories
*
* @return Collection
*/
private function filterCollection(Carbon $start, Carbon $end, Collection $set, Collection $categories): Collection
{
$entries = new Collection;
while ($start < $end) { // filter the set:
@@ -363,19 +362,16 @@ class CategoryController extends Controller
}
)->first();
if (!is_null($entry)) {
$row[] = round(($entry->spent * -1), 2);
$row[] = round($entry->earned, 2);
} else {
$row[] = 0;
}
}
$entries->push($row);
$start->addMonth();
}
$data = $this->generator->spentInPeriod($categories, $entries);
$cache->store($data);
return $data;
return $entries;
}
/**
@@ -419,5 +415,4 @@ class CategoryController extends Controller
return $data;
}
}

View File

@@ -832,8 +832,6 @@ class TestData
*/
public static function createTransactions(TransactionJournal $journal, Account $from, Account $to, string $amount): bool
{
// Log::debug('---- Transaction From: ' . bcmul($amount, '-1'));
// Log::debug('---- Transaction To : ' . $amount);
Transaction::create(
[
'account_id' => $from->id,

View File

@@ -199,6 +199,13 @@ class FireflyValidator extends Validator
return false;
}
/**
* @param $attribute
* @param $value
* @param $parameters
*
* @return bool
*/
public function validateUniqueAccountNumberForUser($attribute, $value, $parameters): bool
{
$accountId = $this->data['id'] ?? 0;