Code cleanup according to PHPStorm.

This commit is contained in:
James Cole 2016-01-02 16:57:31 +01:00
parent 294df4a2b3
commit 3888b8cceb
7 changed files with 157 additions and 99 deletions

View File

@ -49,19 +49,12 @@ class ChartJsBillChartGenerator implements BillChartGenerator
*/
public function single(Bill $bill, Collection $entries)
{
// language:
$format = trans('config.month');
$data = [
'count' => 3,
'labels' => [],
'datasets' => [],
];
// dataset: max amount
// dataset: min amount
// dataset: actual amount
$minAmount = [];
$maxAmount = [];
$actualAmount = [];

View File

@ -73,20 +73,20 @@ class ChartJsBudgetChartGenerator implements BudgetChartGenerator
'labels' => [],
'datasets' => [],
];
// dataset: left
// dataset: spent
// dataset: overspent
$left = [];
$spent = [];
$overspent = [];
foreach ($entries as $entry) {
if ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0) {
$filtered = $entries->filter(
function ($entry) {
return ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0);
}
);
foreach ($filtered as $entry) {
$data['labels'][] = $entry[0];
$left[] = round($entry[1], 2);
$spent[] = round($entry[2] * -1, 2); // spent is coming in negative, must be positive
$overspent[] = round($entry[3] * -1, 2); // same
}
}
$data['datasets'][] = [
'label' => trans('firefly.left'),

View File

@ -154,29 +154,20 @@ class AccountController extends Controller
$subTitleIcon = Config::get('firefly.subIconsByIdentifier.' . $what);
$types = Config::get('firefly.accountTypesByIdentifier.' . $what);
$accounts = $repository->getAccounts($types);
// last activity:
/**
* HERE WE ARE
*/
$start = clone Session::get('start', Carbon::now()->startOfMonth());
$end = clone Session::get('end', Carbon::now()->endOfMonth());
$start->subDay();
// start balances:
$ids = [];
foreach ($accounts as $account) {
$ids[] = $account->id;
}
$ids = $accounts->pluck('id')->toArray();
$startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end);
$activities = Steam::getLastActivities($ids);
$accounts->each(
function (Account $account) use ($activities, $startBalances, $endBalances) {
$account->lastActivityDate = isset($activities[$account->id]) ? $activities[$account->id] : null;
$account->startBalance = isset($startBalances[$account->id]) ? $startBalances[$account->id] : null;
$account->endBalance = isset($endBalances[$account->id]) ? $endBalances[$account->id] : null;
$account->lastActivityDate = $this->isInArray($activities, $account->id);
$account->startBalance = $this->isInArray($startBalances, $account->id);
$account->endBalance = $this->isInArray($endBalances, $account->id);
}
);
@ -283,4 +274,20 @@ class AccountController extends Controller
}
/**
* @param array $array
* @param $entryId
*
* @return null|mixed
*/
protected function isInArray(array $array, $entryId)
{
if (isset($array[$entryId])) {
return $array[$entryId];
}
return null;
}
}

View File

@ -61,37 +61,15 @@ class ReportController extends Controller
$spentArray = $query->spentPerMonth($accounts, $start, $end);
$earnedArray = $query->earnedPerMonth($accounts, $start, $end);
// per year? put all months together.
if ($start->diffInMonths($end) > 12) {
$entries = new Collection;
while ($start < $end) {
$incomeSum = $this->pluckFromArray($start->year, $earnedArray);
$expenseSum = $this->pluckFromArray($start->year, $spentArray) * -1;
$entries->push([clone $start, $incomeSum, $expenseSum]);
$start->addYear();
}
$data = $this->generator->multiYearInOut($entries);
$cache->store($data);
// data = method X
$data = $this->multiYearInOut($earnedArray, $spentArray, $start, $end);
} else {
// per month? simply use each month.
$entries = new Collection;
while ($start < $end) {
// total income and total expenses:
$date = $start->format('Y-m');
$incomeSum = isset($earnedArray[$date]) ? $earnedArray[$date] : 0;
$expenseSum = isset($spentArray[$date]) ? ($spentArray[$date] * -1) : 0;
$entries->push([clone $start, $incomeSum, $expenseSum]);
$start->addMonth();
// data = method Y
$data = $this->singleYearInOut($earnedArray, $spentArray, $start, $end);
}
$data = $this->generator->yearInOut($entries);
$cache->store($data);
}
return Response::json($data);
@ -125,33 +103,35 @@ class ReportController extends Controller
// grouped by month
$spentArray = $query->spentPerMonth($accounts, $start, $end);
$earnedArray = $query->earnedPerMonth($accounts, $start, $end);
if ($start->diffInMonths($end) > 12) {
// per year
$data = $this->multiYearInOutSummarized($earnedArray, $spentArray, $start, $end);
} else {
// per month!
$data = $this->singleYearInOutSummarized($earnedArray, $spentArray, $start, $end);
}
$cache->store($data);
return Response::json($data);
}
/**
* @param array $earned
* @param array $spent
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
protected function singleYearInOutSummarized(array $earned, array $spent, Carbon $start, Carbon $end)
{
$income = '0';
$expense = '0';
$count = 0;
bcscale(2);
if ($start->diffInMonths($end) > 12) {
// per year
while ($start < $end) {
$currentIncome = $this->pluckFromArray($start->year, $earnedArray);
$currentExpense = $this->pluckFromArray($start->year, $spentArray) * -1;
$income = bcadd($income, $currentIncome);
$expense = bcadd($expense, $currentExpense);
$count++;
$start->addYear();
}
$data = $this->generator->multiYearInOutSummarized($income, $expense, $count);
$cache->store($data);
} else {
// per month!
while ($start < $end) {
$date = $start->format('Y-m');
$currentIncome = isset($earnedArray[$date]) ? $earnedArray[$date] : 0;
$currentExpense = isset($spentArray[$date]) ? ($spentArray[$date] * -1) : 0;
$currentIncome = isset($earned[$date]) ? $earned[$date] : 0;
$currentExpense = isset($spent[$date]) ? ($spent[$date] * -1) : 0;
$income = bcadd($income, $currentIncome);
$expense = bcadd($expense, $currentExpense);
@ -160,12 +140,90 @@ class ReportController extends Controller
}
$data = $this->generator->yearInOutSummarized($income, $expense, $count);
$cache->store($data);
return $data;
}
/**
* @param array $earned
* @param array $spent
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
protected function multiYearInOutSummarized(array $earned, array $spent, Carbon $start, Carbon $end)
{
$income = '0';
$expense = '0';
$count = 0;
while ($start < $end) {
return Response::json($data);
$currentIncome = $this->pluckFromArray($start->year, $earned);
$currentExpense = $this->pluckFromArray($start->year, $spent) * -1;
$income = bcadd($income, $currentIncome);
$expense = bcadd($expense, $currentExpense);
$count++;
$start->addYear();
}
$data = $this->generator->multiYearInOutSummarized($income, $expense, $count);
return $data;
}
/**
* @param array $earned
* @param array $spent
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
protected function multiYearInOut(array $earned, array $spent, Carbon $start, Carbon $end)
{
$entries = new Collection;
while ($start < $end) {
$incomeSum = $this->pluckFromArray($start->year, $earned);
$expenseSum = $this->pluckFromArray($start->year, $spent) * -1;
$entries->push([clone $start, $incomeSum, $expenseSum]);
$start->addYear();
}
$data = $this->generator->multiYearInOut($entries);
return $data;
}
/**
* @param array $earned
* @param array $spent
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
protected function singleYearInOut(array $earned, array $spent, Carbon $start, Carbon $end)
{
// per month? simply use each month.
$entries = new Collection;
while ($start < $end) {
// total income and total expenses:
$date = $start->format('Y-m');
$incomeSum = isset($earned[$date]) ? $earned[$date] : 0;
$expenseSum = isset($spent[$date]) ? ($spent[$date] * -1) : 0;
$entries->push([clone $start, $incomeSum, $expenseSum]);
$start->addMonth();
}
$data = $this->generator->yearInOut($entries);
return $data;
}
/**

View File

@ -44,7 +44,7 @@ interface AccountRepositoryInterface
/**
* @param array $types
*
* @return mixed
* @return Collection
*/
public function getAccounts(array $types);

View File

@ -127,7 +127,7 @@ class Steam
* @param array $ids
* @param \Carbon\Carbon $date
*
* @return float
* @return array
*/
public function balancesById(array $ids, Carbon $date)
{

View File

@ -43,8 +43,8 @@ class FireflyValidator extends Validator
*
* @return bool
*/
public function validateBelongsToUser($attribute, $value, $parameters
) {
public function validateBelongsToUser($attribute, $value, $parameters)
{
$count = DB::table($parameters[0])->where('user_id', Auth::user()->id)->where('id', $value)->count();
if ($count == 1) {