Clean up some code routines.

This commit is contained in:
James Cole
2015-07-07 01:07:19 +02:00
parent 2d4b148b2c
commit bdf7eee72f
5 changed files with 135 additions and 56 deletions

View File

@@ -5,7 +5,6 @@ namespace FireflyIII\Helpers\Csv\Converter;
use Auth; use Auth;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use Log;
/** /**
* Class AssetAccountIban * Class AssetAccountIban
@@ -28,31 +27,42 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface
} }
if (strlen($this->value) > 0) { if (strlen($this->value) > 0) {
// find or create new account: // find or create new account:
$account = $this->findAccount();
$accountType = AccountType::where('type', 'Asset account')->first(); $accountType = AccountType::where('type', 'Asset account')->first();
$set = Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->get(['accounts.*']);
/** @var Account $entry */
foreach ($set as $entry) {
if ($entry->iban == $this->value) {
Log::debug('AssetAccountIban::convert found an Account (#' . $entry->id . ': ' . $entry->name . ') with IBAN ' . $this->value);
return $entry; if (is_null($account)) {
} // create it if doesn't exist.
$account = Account::firstOrCreateEncrypted(
[
'name' => $this->value,
'iban' => $this->value,
'user_id' => Auth::user()->id,
'account_type_id' => $accountType->id,
'active' => 1,
]
);
} }
// create it if doesn't exist.
$account = Account::firstOrCreateEncrypted(
[
'name' => $this->value,
'iban' => $this->value,
'user_id' => Auth::user()->id,
'account_type_id' => $accountType->id,
'active' => 1,
]
);
return $account; return $account;
} }
return null; return null;
} }
/**
* @return Account|null
*/
protected function findAccount()
{
$set = Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->get(['accounts.*']);
/** @var Account $entry */
foreach ($set as $entry) {
if ($entry->iban == $this->value) {
return $entry;
}
}
return null;
}
} }

View File

@@ -27,18 +27,31 @@ class OpposingAccountIban extends BasicConverter implements ConverterInterface
return $account; return $account;
} else { } else {
if (strlen($this->value) > 0) { if (strlen($this->value) > 0) {
$set = Auth::user()->accounts()->get(); $account = $this->findAccount();
/** @var Account $account */ if (!is_null($account)) {
foreach ($set as $account) { return $account;
if ($account->iban == $this->value) {
Log::debug('OpposingAccountIban::convert found an Account (#' . $account->id . ': ' . $account->name . ') with IBAN ' . $this->value);
return $account;
}
} }
} }
return $this->value; return $this->value;
} }
} }
/**
* @return Account|null
*/
protected function findAccount()
{
$set = Auth::user()->accounts()->get();
/** @var Account $account */
foreach ($set as $account) {
if ($account->iban == $this->value) {
Log::debug('OpposingAccountIban::convert found an Account (#' . $account->id . ': ' . $account->name . ') with IBAN ' . $this->value);
return $account;
}
}
return null;
}
} }

View File

@@ -24,36 +24,19 @@ class OpposingAccount implements PostProcessorInterface
*/ */
public function process() public function process()
{ {
if ($this->data['opposing-account-id'] instanceof Account) { // first priority. try to find the account based on ID, if any $result = $this->checkIdNameObject();
$this->data['opposing-account-object'] = $this->data['opposing-account-id']; if (!is_null($result)) {
return $result;
return $this->data;
} }
if ($this->data['opposing-account-iban'] instanceof Account) { // second: try to find the account based on IBAN, if any.
$this->data['opposing-account-object'] = $this->data['opposing-account-iban'];
return $this->data; $result = $this->checkIbanString();
if (!is_null($result)) {
return $result;
} }
$rules = ['iban' => 'iban'];
$check = ['iban' => $this->data['opposing-account-iban']];
$validator = Validator::make($check, $rules);
$result = !$validator->fails();
if (is_string($this->data['opposing-account-iban']) && strlen($this->data['opposing-account-iban']) > 0) {
if ($result) {
$this->data['opposing-account-object'] = $this->parseIbanString();
return $this->data; $result = $this->checkNameString();
} if (!is_null($result)) {
} return $result;
if ($this->data['opposing-account-name'] instanceof Account) { // third: try to find account based on name, if any.
$this->data['opposing-account-object'] = $this->data['opposing-account-name'];
return $this->data;
}
if (is_string($this->data['opposing-account-name'])) {
$this->data['opposing-account-object'] = $this->parseNameString();
return $this->data;
} }
return null; return null;
@@ -67,20 +50,67 @@ class OpposingAccount implements PostProcessorInterface
$this->data = $data; $this->data = $data;
} }
/**
* @return array
*/
protected function checkIdNameObject()
{
if ($this->data['opposing-account-id'] instanceof Account) { // first priority. try to find the account based on ID, if any
$this->data['opposing-account-object'] = $this->data['opposing-account-id'];
return $this->data;
}
if ($this->data['opposing-account-iban'] instanceof Account) { // second: try to find the account based on IBAN, if any.
$this->data['opposing-account-object'] = $this->data['opposing-account-iban'];
return $this->data;
}
return null;
}
/**
* @return array|null
*/
protected function checkIbanString()
{
$rules = ['iban' => 'iban'];
$check = ['iban' => $this->data['opposing-account-iban']];
$validator = Validator::make($check, $rules);
if (!$validator->fails()) {
$this->data['opposing-account-object'] = $this->parseIbanString();
return $this->data;
}
return null;
}
/** /**
* @return Account|null * @return Account|null
*/ */
protected function parseIbanString() protected function parseIbanString()
{ {
// create by name and/or iban. // create by name and/or iban.
$accountType = $this->getAccountType(); $accounts = Auth::user()->accounts()->get();
$accounts = Auth::user()->accounts()->get();
foreach ($accounts as $entry) { foreach ($accounts as $entry) {
if ($entry->iban == $this->data['opposing-account-iban']) { if ($entry->iban == $this->data['opposing-account-iban']) {
return $entry; return $entry;
} }
} }
$account = $this->createAccount();
return $account;
}
/**
* @return Account|null
*/
protected function createAccount()
{
$accountType = $this->getAccountType();
// create if not exists: // create if not exists:
$name = is_string($this->data['opposing-account-name']) && strlen($this->data['opposing-account-name']) > 0 ? $this->data['opposing-account-name'] $name = is_string($this->data['opposing-account-name']) && strlen($this->data['opposing-account-name']) > 0 ? $this->data['opposing-account-name']
: $this->data['opposing-account-iban']; : $this->data['opposing-account-iban'];
@@ -117,6 +147,25 @@ class OpposingAccount implements PostProcessorInterface
} }
} }
/**
* @return array|null
*/
protected function checkNameString()
{
if ($this->data['opposing-account-name'] instanceof Account) { // third: try to find account based on name, if any.
$this->data['opposing-account-object'] = $this->data['opposing-account-name'];
return $this->data;
}
if (is_string($this->data['opposing-account-name'])) {
$this->data['opposing-account-object'] = $this->parseNameString();
return $this->data;
}
return null;
}
/** /**
* @return Account|null * @return Account|null
*/ */

View File

@@ -88,7 +88,7 @@ class ReportController extends Controller
$budgets = $this->helper->getBudgetReport($start, $end, $shared); $budgets = $this->helper->getBudgetReport($start, $end, $shared);
$categories = $this->helper->getCategoryReport($start, $end, $shared); $categories = $this->helper->getCategoryReport($start, $end, $shared);
$balance = $this->helper->getBalanceReport($start, $end, $shared); $balance = $this->helper->getBalanceReport($start, $end, $shared);
$bills = $this->helper->getBillReport($start, $end, $shared); $bills = $this->helper->getBillReport($start, $end);
Session::flash('gaEventCategory', 'report'); Session::flash('gaEventCategory', 'report');
Session::flash('gaEventAction', 'month'); Session::flash('gaEventAction', 'month');

View File

@@ -62,6 +62,13 @@ class FireflyValidator extends Validator
*/ */
public function validateIban($attribute, $value) public function validateIban($attribute, $value)
{ {
if (!is_string($value)) {
return false;
}
if (strlen($value) === 0) {
return false;
}
$value = strtoupper($value); $value = strtoupper($value);
if (strlen($value) < 6) { if (strlen($value) < 6) {