Some code cleanup.

This commit is contained in:
James Cole 2015-08-16 20:26:11 +02:00
parent 28c753523f
commit be8eaaffdf
3 changed files with 43 additions and 33 deletions

View File

@ -41,34 +41,24 @@ class ChartJsAccountChartGenerator implements AccountChartGenerator
*/ */
public function expenseAccounts(Collection $accounts, Carbon $start, Carbon $end) public function expenseAccounts(Collection $accounts, Carbon $start, Carbon $end)
{ {
// language:
$data = [ $data = [
'count' => 1, 'count' => 1,
'labels' => [], 'labels' => [], 'datasets' => [[
'datasets' => [ 'label' => trans('firefly.spent'),
[ 'data' => []]]];
'label' => trans('firefly.spent'),
'data' => []
]
],
];
$ids = [];
foreach ($accounts as $account) { bcscale(2);
$ids[] = $account->id;
}
$start->subDay(); $start->subDay();
$ids = $this->getIdsFromCollection($accounts);
$startBalances = Steam::balancesById($ids, $start); $startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end); $endBalances = Steam::balancesById($ids, $end);
$accounts->each( $accounts->each(
function (Account $account) use ($startBalances, $endBalances) { function (Account $account) use ($startBalances, $endBalances) {
$id = $account->id; $id = $account->id;
$startBalance = isset($startBalances[$id]) ? $startBalances[$id] : 0; $startBalance = $this->isInArray($startBalances, $id);
$endBalance = isset($endBalances[$id]) ? $endBalances[$id] : 0; $endBalance = $this->isInArray($endBalances, $id);
$diff = $endBalance - $startBalance; $diff = bcsub($endBalance, $startBalance);
$account->difference = round($diff, 2); $account->difference = round($diff, 2);
} }
); );
@ -86,10 +76,24 @@ class ChartJsAccountChartGenerator implements AccountChartGenerator
} }
} }
return $data; return $data;
} }
/**
* @param $array
* @param $entryId
*
* @return string
*/
protected function isInArray($array, $entryId)
{
if (isset($array[$entryId])) {
return $array[$entryId];
}
return '0';
}
/** /**
* @param Collection $accounts * @param Collection $accounts
@ -171,4 +175,20 @@ class ChartJsAccountChartGenerator implements AccountChartGenerator
return $data; return $data;
} }
/**
* @param Collection $collection
*
* @return array
*/
protected function getIdsFromCollection(Collection $collection)
{
$ids = [];
foreach ($collection as $entry) {
$ids[] = $entry->id;
}
return array_unique($ids);
}
} }

View File

@ -78,15 +78,15 @@ class CategoryRepository extends ComponentRepository implements CategoryReposito
->transactionTypes(['Withdrawal']) ->transactionTypes(['Withdrawal'])
->get(['categories.id as category_id', 'categories.encrypted as category_encrypted', 'categories.name', 'transaction_journals.*']); ->get(['categories.id as category_id', 'categories.encrypted as category_encrypted', 'categories.name', 'transaction_journals.*']);
bcscale(2);
$result = []; $result = [];
foreach ($set as $entry) { foreach ($set as $entry) {
$categoryId = intval($entry->category_id); $categoryId = intval($entry->category_id);
if (isset($result[$categoryId])) { if (isset($result[$categoryId])) {
bcscale(2);
$result[$categoryId]['sum'] = bcadd($result[$categoryId]['sum'], $entry->amount); $result[$categoryId]['sum'] = bcadd($result[$categoryId]['sum'], $entry->amount);
} else { } else {
$isEncrypted = intval($entry->category_encrypted) == 1 ? true : false; $isEncrypted = intval($entry->category_encrypted) === 1 ? true : false;
$name = strlen($entry->name) == 0 ? trans('firefly.no_category') : $entry->name; $name = strlen($entry->name) === 0 ? trans('firefly.no_category') : $entry->name;
$name = $isEncrypted ? Crypt::decrypt($name) : $name; $name = $isEncrypted ? Crypt::decrypt($name) : $name;
$result[$categoryId] = [ $result[$categoryId] = [
'name' => $name, 'name' => $name,

View File

@ -62,21 +62,11 @@ class FireflyValidator extends Validator
*/ */
public function validateIban($attribute, $value) public function validateIban($attribute, $value)
{ {
if (!is_string($value)) { if (!is_string($value) || is_null($value) || strlen($value) < 6) {
return false;
}
if (is_null($value)) {
return false;
}
if (strlen($value) === 0) {
return false; return false;
} }
$value = strtoupper($value); $value = strtoupper($value);
if (strlen($value) < 6) {
return false;
}
$search = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; $search = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
$replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', $replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31',