Code cleanup. Moving closer to new release.

This commit is contained in:
James Cole
2016-05-20 17:53:03 +02:00
parent 87f9ca3bb2
commit a74a646777
19 changed files with 248 additions and 191 deletions

View File

@@ -140,6 +140,8 @@ class AccountCrud implements AccountCrudInterface
* @param array $data * @param array $data
* *
* @return Account * @return Account
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/ */
public function store(array $data): Account public function store(array $data): Account
{ {
@@ -148,7 +150,6 @@ class AccountCrud implements AccountCrudInterface
$this->storeMetadata($newAccount, $data); $this->storeMetadata($newAccount, $data);
} }
// continue with the opposing account: // continue with the opposing account:
if ($data['openingBalance'] != 0) { if ($data['openingBalance'] != 0) {
$opposingData = [ $opposingData = [
@@ -202,7 +203,10 @@ class AccountCrud implements AccountCrudInterface
if ($data['openingBalance'] != 0) { if ($data['openingBalance'] != 0) {
if (!is_null($openingBalance->id)) { if (!is_null($openingBalance->id)) {
$this->updateInitialBalance($account, $openingBalance, $data); $this->updateInitialBalance($account, $openingBalance, $data);
} else {
return $account;
}
$type = $data['openingBalance'] < 0 ? 'expense' : 'revenue'; $type = $data['openingBalance'] < 0 ? 'expense' : 'revenue';
$opposingData = [ $opposingData = [
'user' => $data['user'], 'user' => $data['user'],
@@ -216,13 +220,14 @@ class AccountCrud implements AccountCrudInterface
if (!is_null($opposing)) { if (!is_null($opposing)) {
$this->storeInitialBalance($account, $opposing, $data); $this->storeInitialBalance($account, $opposing, $data);
} }
return $account;
} }
} else {
if ($openingBalance) { // opening balance is zero, should we delete it? if ($openingBalance) { // opening balance is zero, should we delete it?
$openingBalance->delete(); // delete existing opening balance. $openingBalance->delete(); // delete existing opening balance.
} }
}
return $account; return $account;
} }
@@ -292,16 +297,16 @@ class AccountCrud implements AccountCrudInterface
] ]
); );
$firstAccount = $account;
$secondAccount = $opposing;
$firstAmount = $data['openingBalance'];
$secondAmount = $data['openingBalance'] * -1;
if ($data['openingBalance'] < 0) { if ($data['openingBalance'] < 0) {
$firstAccount = $opposing; $firstAccount = $opposing;
$secondAccount = $account; $secondAccount = $account;
$firstAmount = $data['openingBalance'] * -1; $firstAmount = $data['openingBalance'] * -1;
$secondAmount = $data['openingBalance']; $secondAmount = $data['openingBalance'];
} else {
$firstAccount = $account;
$secondAccount = $opposing;
$firstAmount = $data['openingBalance'];
$secondAmount = $data['openingBalance'] * -1;
} }
$one = new Transaction(['account_id' => $firstAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $firstAmount]); $one = new Transaction(['account_id' => $firstAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $firstAmount]);
@@ -378,7 +383,9 @@ class AccountCrud implements AccountCrudInterface
if (!is_null($entry)) { if (!is_null($entry)) {
$entry->data = $data[$field]; $entry->data = $data[$field];
$entry->save(); $entry->save();
} else {
continue;
}
$metaData = new AccountMeta( $metaData = new AccountMeta(
[ [
'account_id' => $account->id, 'account_id' => $account->id,
@@ -389,7 +396,6 @@ class AccountCrud implements AccountCrudInterface
$metaData->save(); $metaData->save();
} }
} }
}
} }

View File

@@ -31,10 +31,6 @@ use stdClass;
*/ */
class CategoryController extends Controller class CategoryController extends Controller
{ {
const MAKE_POSITIVE = -1;
const KEEP_POSITIVE = 1;
/** @var CategoryChartGeneratorInterface */ /** @var CategoryChartGeneratorInterface */
protected $generator; protected $generator;
@@ -192,20 +188,20 @@ class CategoryController extends Controller
// get data: // get data:
if (is_null($category->id)) { if (is_null($category->id)) {
$name = trans('firefly.noCategory'); $entry['name'] = trans('firefly.noCategory');
$spent = $repository->spentInPeriodWithoutCategory($accounts, $currentStart, $currentEnd); $entry['spent'][$year] = ($repository->spentInPeriodWithoutCategory($accounts, $currentStart, $currentEnd) * -1);
$earned = $repository->earnedInPeriodWithoutCategory($accounts, $currentStart, $currentEnd); $entry['earned'][$year] = $repository->earnedInPeriodWithoutCategory($accounts, $currentStart, $currentEnd);
} else {
// jump to next year.
$currentStart = clone $currentEnd;
$currentStart->addDay();
continue;
$name = $category->name;
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $currentStart, $currentEnd);
$earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $currentStart, $currentEnd);
} }
// alternative is a normal category:
// save to array: $entry['name'] = $category->name;
$entry['name'] = $name; $entry['spent'][$year] = ($repository->spentInPeriod(new Collection([$category]), $accounts, $currentStart, $currentEnd) * -1);
$entry['spent'][$year] = ($spent * -1); $entry['earned'][$year] = $repository->earnedInPeriod(new Collection([$category]), $accounts, $currentStart, $currentEnd);
$entry['earned'][$year] = $earned;
// jump to next year. // jump to next year.
$currentStart = clone $currentEnd; $currentStart = clone $currentEnd;
@@ -213,8 +209,8 @@ class CategoryController extends Controller
} }
$entries->push($entry); $entries->push($entry);
} }
// generate chart with data:
// generate chart with data:
$data = $this->generator->multiYear($entries); $data = $this->generator->multiYear($entries);
$cache->store($data); $cache->store($data);

View File

@@ -128,14 +128,17 @@ class ReportController extends Controller
if ($start->diffInMonths($end) > 12) { if ($start->diffInMonths($end) > 12) {
// data = method X // data = method X
$data = $this->multiYearInOut($earnedArray, $spentArray, $start, $end); $data = $this->multiYearInOut($earnedArray, $spentArray, $start, $end);
} else {
// data = method Y
$data = $this->singleYearInOut($earnedArray, $spentArray, $start, $end);
}
$cache->store($data); $cache->store($data);
return Response::json($data); return Response::json($data);
}
// data = method Y
$data = $this->singleYearInOut($earnedArray, $spentArray, $start, $end);
$cache->store($data);
return Response::json($data);
} }
@@ -179,14 +182,17 @@ class ReportController extends Controller
if ($start->diffInMonths($end) > 12) { if ($start->diffInMonths($end) > 12) {
// per year // per year
$data = $this->multiYearInOutSummarized($earnedArray, $spentArray, $start, $end); $data = $this->multiYearInOutSummarized($earnedArray, $spentArray, $start, $end);
} else {
// per month!
$data = $this->singleYearInOutSummarized($earnedArray, $spentArray, $start, $end);
}
$cache->store($data); $cache->store($data);
return Response::json($data); return Response::json($data);
} }
// per month!
$data = $this->singleYearInOutSummarized($earnedArray, $spentArray, $start, $end);
$cache->store($data);
return Response::json($data);
}
/** /**
* @param array $earned * @param array $earned

View File

@@ -67,6 +67,39 @@ class NewUserController extends Controller
{ {
$count = 1; $count = 1;
// create normal asset account: // create normal asset account:
$this->createAssetAccount($request, $crud);
// create savings account
if (strlen($request->get('savings_balance') > 0)) {
$this->createSavingsAccount($request, $crud);
$count++;
}
// create credit card.
if (strlen($request->get('credit_card_limit') > 0)) {
$this->storeCreditCard($request, $crud);
$count++;
}
$message = strval(trans('firefly.stored_new_accounts_new_user'));
if ($count == 1) {
$message = strval(trans('firefly.stored_new_account_new_user'));
}
Session::flash('success', $message);
Preferences::mark();
return redirect(route('index'));
}
/**
* @param NewUserFormRequest $request
* @param AccountCrudInterface $crud
*
* @return bool
*/
private function createAssetAccount(NewUserFormRequest $request, AccountCrudInterface $crud): bool
{
$assetAccount = [ $assetAccount = [
'name' => $request->get('bank_name'), 'name' => $request->get('bank_name'),
'iban' => null, 'iban' => null,
@@ -82,8 +115,17 @@ class NewUserController extends Controller
$crud->store($assetAccount); $crud->store($assetAccount);
// create savings account return true;
if (strlen($request->get('savings_balance') > 0)) { }
/**
* @param NewUserFormRequest $request
* @param AccountCrudInterface $crud
*
* @return bool
*/
private function createSavingsAccount(NewUserFormRequest $request, AccountCrudInterface $crud): bool
{
$savingsAccount = [ $savingsAccount = [
'name' => $request->get('bank_name') . ' savings account', 'name' => $request->get('bank_name') . ' savings account',
'iban' => null, 'iban' => null,
@@ -97,12 +139,18 @@ class NewUserController extends Controller
'openingBalanceCurrency' => intval($request->input('amount_currency_id_savings_balance')), 'openingBalanceCurrency' => intval($request->input('amount_currency_id_savings_balance')),
]; ];
$crud->store($savingsAccount); $crud->store($savingsAccount);
$count++;
return true;
} }
/**
// create credit card. * @param NewUserFormRequest $request
if (strlen($request->get('credit_card_limit') > 0)) { * @param AccountCrudInterface $crud
*
* @return bool
*/
private function storeCreditCard(NewUserFormRequest $request, AccountCrudInterface $crud): bool
{
$creditAccount = [ $creditAccount = [
'name' => 'Credit card', 'name' => 'Credit card',
'iban' => null, 'iban' => null,
@@ -120,16 +168,7 @@ class NewUserController extends Controller
// store meta for CC: // store meta for CC:
$crud->storeMeta($creditCard, 'ccType', 'monthlyFull'); $crud->storeMeta($creditCard, 'ccType', 'monthlyFull');
$crud->storeMeta($creditCard, 'ccMonthlyPaymentDate', Carbon::now()->year . '-01-01'); $crud->storeMeta($creditCard, 'ccMonthlyPaymentDate', Carbon::now()->year . '-01-01');
$count++;
}
$message = strval(trans('firefly.stored_new_accounts_new_user'));
if ($count == 1) {
$message = strval(trans('firefly.stored_new_account_new_user'));
}
Session::flash('success', $message); return true;
Preferences::mark();
return redirect(route('index'));
} }
} }

View File

@@ -152,17 +152,17 @@ class RuleController extends Controller
*/ */
public function edit(RuleRepositoryInterface $repository, Rule $rule) public function edit(RuleRepositoryInterface $repository, Rule $rule)
{ {
$oldTriggers = $this->getCurrentTriggers($rule);
$triggerCount = count($oldTriggers);
$oldActions = $this->getCurrentActions($rule);
$actionCount = count($oldActions);
// has old input? // has old input?
if (Input::old()) { if (Input::old()) {
$oldTriggers = $this->getPreviousTriggers(); $oldTriggers = $this->getPreviousTriggers();
$triggerCount = count($oldTriggers); $triggerCount = count($oldTriggers);
$oldActions = $this->getPreviousActions(); $oldActions = $this->getPreviousActions();
$actionCount = count($oldActions); $actionCount = count($oldActions);
} else {
$oldTriggers = $this->getCurrentTriggers($rule);
$triggerCount = count($oldTriggers);
$oldActions = $this->getCurrentActions($rule);
$actionCount = count($oldActions);
} }
// get rule trigger for update / store-journal: // get rule trigger for update / store-journal:
@@ -306,11 +306,10 @@ class RuleController extends Controller
$warning = ''; $warning = '';
if (count($matchingTransactions) == $limit) { if (count($matchingTransactions) == $limit) {
$warning = trans('firefly.warning_transaction_subset', ['max_num_transactions' => $limit]); $warning = trans('firefly.warning_transaction_subset', ['max_num_transactions' => $limit]);
} else { }
if (count($matchingTransactions) == 0) { if (count($matchingTransactions) == 0) {
$warning = trans('firefly.warning_no_matching_transactions', ['num_transactions' => $range]); $warning = trans('firefly.warning_no_matching_transactions', ['num_transactions' => $range]);
} }
}
// Return json response // Return json response
$view = view('list.journals-tiny', ['transactions' => $matchingTransactions])->render(); $view = view('list.journals-tiny', ['transactions' => $matchingTransactions])->render();

View File

@@ -37,17 +37,16 @@ class Authenticate
if (Auth::guard($guard)->guest()) { if (Auth::guard($guard)->guest()) {
if ($request->ajax()) { if ($request->ajax()) {
return response('Unauthorized.', 401); return response('Unauthorized.', 401);
} else { }
return redirect()->guest('login'); return redirect()->guest('login');
} }
} else {
if (intval(Auth::user()->blocked) === 1) { if (intval(Auth::user()->blocked) === 1) {
Auth::guard($guard)->logout(); Auth::guard($guard)->logout();
Session::flash('logoutMessage', trans('firefly.block_account_logout')); Session::flash('logoutMessage', trans('firefly.block_account_logout'));
return redirect()->guest('login'); return redirect()->guest('login');
} }
}
return $next($request); return $next($request);
} }

View File

@@ -40,10 +40,10 @@ class AuthenticateTwoFactor
if (Auth::guard($guard)->guest()) { if (Auth::guard($guard)->guest()) {
if ($request->ajax()) { if ($request->ajax()) {
return response('Unauthorized.', 401); return response('Unauthorized.', 401);
} else { }
return redirect()->guest('login'); return redirect()->guest('login');
} }
} else {
if (intval(Auth::user()->blocked) === 1) { if (intval(Auth::user()->blocked) === 1) {
Auth::guard($guard)->logout(); Auth::guard($guard)->logout();
@@ -51,7 +51,6 @@ class AuthenticateTwoFactor
return redirect()->guest('login'); return redirect()->guest('login');
} }
}
$is2faEnabled = Preferences::get('twoFactorAuthEnabled', false)->data; $is2faEnabled = Preferences::get('twoFactorAuthEnabled', false)->data;
$has2faSecret = !is_null(Preferences::get('twoFactorAuthSecret')); $has2faSecret = !is_null(Preferences::get('twoFactorAuthSecret'));
$is2faAuthed = Session::get('twofactor-authenticated'); $is2faAuthed = Session::get('twofactor-authenticated');

View File

@@ -38,16 +38,15 @@ class IsAdmin
if (Auth::guard($guard)->guest()) { if (Auth::guard($guard)->guest()) {
if ($request->ajax()) { if ($request->ajax()) {
return response('Unauthorized.', 401); return response('Unauthorized.', 401);
} else { }
return redirect()->guest('login'); return redirect()->guest('login');
} }
} else {
/** @var User $user */ /** @var User $user */
$user = Auth::user(); $user = Auth::user();
if (!$user->hasRole('owner')) { if (!$user->hasRole('owner')) {
return redirect(route('home')); return redirect(route('home'));
} }
}
return $next($request); return $next($request);
} }

View File

@@ -38,10 +38,10 @@ class IsConfirmed
if (Auth::guard($guard)->guest()) { if (Auth::guard($guard)->guest()) {
if ($request->ajax()) { if ($request->ajax()) {
return response('Unauthorized.', 401); return response('Unauthorized.', 401);
} else { }
return redirect()->guest('login'); return redirect()->guest('login');
} }
} else {
// must the user be confirmed in the first place? // must the user be confirmed in the first place?
$confirmAccount = env('MUST_CONFIRM_ACCOUNT', false); $confirmAccount = env('MUST_CONFIRM_ACCOUNT', false);
// user must be logged in, then continue: // user must be logged in, then continue:
@@ -53,7 +53,6 @@ class IsConfirmed
// confirmation page: // confirmation page:
return redirect(route('confirmation_error')); return redirect(route('confirmation_error'));
} }
}
return $next($request); return $next($request);
} }

View File

@@ -38,10 +38,10 @@ class IsNotConfirmed
if (Auth::guard($guard)->guest()) { if (Auth::guard($guard)->guest()) {
if ($request->ajax()) { if ($request->ajax()) {
return response('Unauthorized.', 401); return response('Unauthorized.', 401);
} else { }
return redirect()->guest('login'); return redirect()->guest('login');
} }
} else {
// must the user be confirmed in the first place? // must the user be confirmed in the first place?
$confirmAccount = env('MUST_CONFIRM_ACCOUNT', false); $confirmAccount = env('MUST_CONFIRM_ACCOUNT', false);
// user must be logged in, then continue: // user must be logged in, then continue:
@@ -50,7 +50,6 @@ class IsNotConfirmed
// user account is confirmed, simply send them home. // user account is confirmed, simply send them home.
return redirect(route('home')); return redirect(route('home'));
} }
}
return $next($request); return $next($request);
} }

View File

@@ -77,16 +77,13 @@ class Range
/** @var JournalRepositoryInterface $repository */ /** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class); $repository = app(JournalRepositoryInterface::class);
$journal = $repository->first(); $journal = $repository->first();
$first = Carbon::now()->startOfYear();
if (!is_null($journal->id)) { if (!is_null($journal->id)) {
Session::put('first', $journal->date); $first = $journal->date;
} else {
Session::put('first', Carbon::now()->startOfYear());
} }
Session::put('first', $first);
} }
// check "sum of everything".
$current = Carbon::now()->formatLocalized('%B %Y'); $current = Carbon::now()->formatLocalized('%B %Y');
$next = Carbon::now()->endOfMonth()->addDay()->formatLocalized('%B %Y'); $next = Carbon::now()->endOfMonth()->addDay()->formatLocalized('%B %Y');
$prev = Carbon::now()->startOfMonth()->subDay()->formatLocalized('%B %Y'); $prev = Carbon::now()->startOfMonth()->subDay()->formatLocalized('%B %Y');

View File

@@ -299,17 +299,24 @@ class AccountRepository implements AccountRepositoryInterface
if ($diff < 0 && $account->startBalance > 0) { if ($diff < 0 && $account->startBalance > 0) {
// percentage lost compared to start. // percentage lost compared to start.
$pct = (($diff * -1) / $account->startBalance) * 100; $pct = (($diff * -1) / $account->startBalance) * 100;
} else {
if ($diff >= 0 && $account->startBalance > 0) {
$pct = ($diff / $account->startBalance) * 100;
} else {
$pct = 100;
}
}
$pct = $pct > 100 ? 100 : $pct; $pct = $pct > 100 ? 100 : $pct;
$account->difference = $diff; $account->difference = $diff;
$account->percentage = round($pct); $account->percentage = round($pct);
return;
}
if ($diff >= 0 && $account->startBalance > 0) {
$pct = ($diff / $account->startBalance) * 100;
$pct = $pct > 100 ? 100 : $pct;
$account->difference = $diff;
$account->percentage = round($pct);
return;
}
$account->difference = $diff;
$account->percentage = 100;
} }
); );

View File

@@ -531,12 +531,11 @@ class JournalRepository implements JournalRepositoryInterface
); );
return [$fromAccount, $destinationAccount]; return [$fromAccount, $destinationAccount];
} else { }
$fromType = AccountType::where('type', 'Cash account')->first(); $fromType = AccountType::where('type', 'Cash account')->first();
$fromAccount = Account::firstOrCreateEncrypted( $fromAccount = Account::firstOrCreateEncrypted(
['user_id' => $data['user'], 'account_type_id' => $fromType->id, 'name' => 'Cash account', 'active' => 1] ['user_id' => $data['user'], 'account_type_id' => $fromType->id, 'name' => 'Cash account', 'active' => 1]
); );
}
return [$fromAccount, $destinationAccount]; return [$fromAccount, $destinationAccount];
} }

View File

@@ -60,12 +60,12 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
if (is_null($moveTo)) { if (is_null($moveTo)) {
$rule->delete(); $rule->delete();
} else { continue;
}
// move // move
$rule->ruleGroup()->associate($moveTo); $rule->ruleGroup()->associate($moveTo);
$rule->save(); $rule->save();
} }
}
$ruleGroup->delete(); $ruleGroup->delete();

View File

@@ -111,9 +111,14 @@ class Navigation
$function = $functionMap[$repeatFreq]; $function = $functionMap[$repeatFreq];
if (isset($modifierMap[$repeatFreq])) { if (isset($modifierMap[$repeatFreq])) {
$currentEnd->$function($modifierMap[$repeatFreq]); $currentEnd->$function($modifierMap[$repeatFreq]);
} else {
$currentEnd->$function(); if (in_array($repeatFreq, $subDay)) {
$currentEnd->subDay();
} }
return $currentEnd;
}
$currentEnd->$function();
if (in_array($repeatFreq, $subDay)) { if (in_array($repeatFreq, $subDay)) {
$currentEnd->subDay(); $currentEnd->subDay();
} }
@@ -370,11 +375,14 @@ class Navigation
if ($range == '6M') { if ($range == '6M') {
if ($start->month >= 7) { if ($start->month >= 7) {
$start->startOfYear()->addMonths(6); $start->startOfYear()->addMonths(6);
} else {
$start->startOfYear();
}
return $start; return $start;
}
$start->startOfYear();
return $start;
} }
throw new FireflyException('updateStartDate cannot handle $range "' . $range . '"'); throw new FireflyException('updateStartDate cannot handle $range "' . $range . '"');
} }

View File

@@ -144,13 +144,18 @@ class Preferences
if (!is_null($pref)) { if (!is_null($pref)) {
$pref->data = $value; $pref->data = $value;
} else { $pref->save();
Cache::forever($fullName, $pref);
return $pref;
}
$pref = new Preference; $pref = new Preference;
$pref->name = $name; $pref->name = $name;
$pref->data = $value; $pref->data = $value;
$pref->user()->associate($user); $pref->user()->associate($user);
}
$pref->save(); $pref->save();
Cache::forever($fullName, $pref); Cache::forever($fullName, $pref);

View File

@@ -7,7 +7,7 @@ use Illuminate\Database\Schema\Blueprint;
/** /**
* @SuppressWarnings(PHPMD.ShortMethodName) * @SuppressWarnings(PHPMD.ShortMethodName)
* @SuppressWarnings("PHPMD.ExcessiveMethodLength") * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* *
* Class ChangesForV322 * Class ChangesForV322
*/ */

View File

@@ -7,7 +7,7 @@ use Illuminate\Database\Schema\Blueprint;
/** /**
* @SuppressWarnings(PHPMD.ShortMethodName) * @SuppressWarnings(PHPMD.ShortMethodName)
* @SuppressWarnings("PHPMD.ExcessiveMethodLength") * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* *
* Class ChangesForV325 * Class ChangesForV325
*/ */

View File

@@ -7,7 +7,7 @@ use Illuminate\Database\Schema\Blueprint;
/** /**
* @SuppressWarnings(PHPMD.ShortMethodName) * @SuppressWarnings(PHPMD.ShortMethodName)
* @SuppressWarnings("PHPMD.ExcessiveMethodLength") * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* *
* Class ChangesForV336 * Class ChangesForV336
*/ */