Code cleanup

This commit is contained in:
James Cole
2021-03-21 09:15:40 +01:00
parent da1751940e
commit 206845575c
317 changed files with 7418 additions and 7362 deletions

View File

@@ -42,18 +42,39 @@ class AccountForm
{
use FormSupport;
/**
* Grouped dropdown list of all accounts that are valid as the destination of a withdrawal.
*
* @param string $name
* @param mixed $value
* @param array $options
*
* @return string
*/
public function activeDepositDestinations(string $name, $value = null, array $options = null): string
{
$types = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN, AccountType::REVENUE,];
$repository = $this->getAccountRepository();
$grouped = $this->getAccountsGrouped($types, $repository);
$cash = $repository->getCashAccount();
$key = (string)trans('firefly.cash_account_type');
$grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash'));
return $this->select($name, $grouped, $value, $options);
}
private function getAccountsGrouped(array $types, AccountRepositoryInterface $repository = null): array
{
if (null === $repository) {
$repository = $this->getAccountRepository();
}
$accountList = $repository->getActiveAccountsByType($types);
$liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,];
$grouped = [];
$accountList = $repository->getActiveAccountsByType($types);
$liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,];
$grouped = [];
/** @var Account $account */
foreach ($accountList as $account) {
$role = (string)$repository->getMetaValue($account, 'account_role');
$role = (string)$repository->getMetaValue($account, 'account_role');
if (in_array($account->accountType->type, $liabilityTypes, true)) {
$role = sprintf('l_%s', $account->accountType->type);
} elseif ('' === $role) {
@@ -65,7 +86,7 @@ class AccountForm
$role = 'no_account_type';
}
}
$key = (string) trans(sprintf('firefly.opt_group_%s', $role));
$key = (string)trans(sprintf('firefly.opt_group_%s', $role));
$grouped[$key][$account->id] = $account->name;
}
@@ -76,8 +97,8 @@ class AccountForm
* Grouped dropdown list of all accounts that are valid as the destination of a withdrawal.
*
* @param string $name
* @param mixed $value
* @param array $options
* @param mixed $value
* @param array $options
*
* @return string
*/
@@ -94,33 +115,11 @@ class AccountForm
return $this->select($name, $grouped, $value, $options);
}
/**
* Grouped dropdown list of all accounts that are valid as the destination of a withdrawal.
*
* @param string $name
* @param mixed $value
* @param array $options
*
* @return string
*/
public function activeDepositDestinations(string $name, $value = null, array $options = null): string
{
$types = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN, AccountType::REVENUE,];
$repository = $this->getAccountRepository();
$grouped = $this->getAccountsGrouped($types, $repository);
$cash = $repository->getCashAccount();
$key = (string)trans('firefly.cash_account_type');
$grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash'));
return $this->select($name, $grouped, $value, $options);
}
/**
* Check list of asset accounts.
*
* @param string $name
* @param array $options
* @param array $options
*
* @return string
*
@@ -134,8 +133,8 @@ class AccountForm
$selected = request()->old($name) ?? [];
// get all asset accounts:
$types = [AccountType::ASSET, AccountType::DEFAULT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::DEBT];
$grouped = $this->getAccountsGrouped($types);
$types = [AccountType::ASSET, AccountType::DEFAULT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::DEBT];
$grouped = $this->getAccountsGrouped($types);
unset($options['class']);
try {
@@ -152,8 +151,8 @@ class AccountForm
* Basic list of asset accounts.
*
* @param string $name
* @param mixed $value
* @param array $options
* @param mixed $value
* @param array $options
*
* @return string
*/
@@ -170,8 +169,8 @@ class AccountForm
* Same list but all liabilities as well.
*
* @param string $name
* @param mixed $value
* @param array $options
* @param mixed $value
* @param array $options
*
* @return string
*/

View File

@@ -44,56 +44,67 @@ class CurrencyForm
use FormSupport;
/**
* TODO cleanup and describe.
* @param string $name
* @param mixed $value
* @param array $options
*
* @return string
*/
public function currencyList(string $name, $value = null, array $options = null): string
public function amount(string $name, $value = null, array $options = null): string
{
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
// get all currencies:
$list = $currencyRepos->get();
$array = [];
/** @var TransactionCurrency $currency */
foreach ($list as $currency) {
$array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')';
}
return $this->select($name, $array, $value, $options);
return $this->currencyField($name, 'amount', $value, $options);
}
/**
* TODO cleanup and describe.
*
* @param string $name
* @param string $view
* @param mixed $value
* @param array $options
*
* @return string
*
*/
public function currencyListEmpty(string $name, $value = null, array $options = null): string
protected function currencyField(string $name, string $view, $value = null, array $options = null): string
{
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = 'any';
$defaultCurrency = $options['currency'] ?? Amt::getDefaultCurrency();
/** @var Collection $currencies */
$currencies = app('amount')->getCurrencies();
unset($options['currency'], $options['placeholder']);
// get all currencies:
$list = $currencyRepos->get();
$array = [
0 => (string)trans('firefly.no_currency'),
];
/** @var TransactionCurrency $currency */
foreach ($list as $currency) {
$array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')';
// perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount)
$preFilled = session('preFilled');
$key = 'amount_currency_id_' . $name;
$sentCurrencyId = isset($preFilled[$key]) ? (int)$preFilled[$key] : $defaultCurrency->id;
Log::debug(sprintf('Sent currency ID is %d', $sentCurrencyId));
// find this currency in set of currencies:
foreach ($currencies as $currency) {
if ($currency->id === $sentCurrencyId) {
$defaultCurrency = $currency;
Log::debug(sprintf('default currency is now %s', $defaultCurrency->code));
break;
}
}
return $this->select($name, $array, $value, $options);
}
// make sure value is formatted nicely:
if (null !== $value && '' !== $value) {
$value = round((float)$value, $defaultCurrency->decimal_places);
}
try {
$html = prefixView('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
} catch (Throwable $e) {
Log::debug(sprintf('Could not render currencyField(): %s', $e->getMessage()));
$html = 'Could not render currencyField.';
}
return $html;
}
/**
* TODO describe and cleanup.
@@ -151,7 +162,7 @@ class CurrencyForm
// make sure value is formatted nicely:
if (null !== $value && '' !== $value) {
$value = round((float) $value, $defaultCurrency->decimal_places);
$value = round((float)$value, $defaultCurrency->decimal_places);
}
try {
$html = prefixView('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
@@ -164,66 +175,55 @@ class CurrencyForm
}
/**
* TODO cleanup and describe.
*
* @param string $name
* @param string $view
* @param mixed $value
* @param array $options
*
* @return string
*
*/
protected function currencyField(string $name, string $view, $value = null, array $options = null): string
public function currencyList(string $name, $value = null, array $options = null): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = 'any';
$defaultCurrency = $options['currency'] ?? Amt::getDefaultCurrency();
/** @var Collection $currencies */
$currencies = app('amount')->getCurrencies();
unset($options['currency'], $options['placeholder']);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
// perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount)
$preFilled = session('preFilled');
$key = 'amount_currency_id_' . $name;
$sentCurrencyId = isset($preFilled[$key]) ? (int)$preFilled[$key] : $defaultCurrency->id;
Log::debug(sprintf('Sent currency ID is %d', $sentCurrencyId));
// find this currency in set of currencies:
foreach ($currencies as $currency) {
if ($currency->id === $sentCurrencyId) {
$defaultCurrency = $currency;
Log::debug(sprintf('default currency is now %s', $defaultCurrency->code));
break;
}
// get all currencies:
$list = $currencyRepos->get();
$array = [];
/** @var TransactionCurrency $currency */
foreach ($list as $currency) {
$array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')';
}
// make sure value is formatted nicely:
if (null !== $value && '' !== $value) {
$value = round((float) $value, $defaultCurrency->decimal_places);
}
try {
$html = prefixView('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
} catch (Throwable $e) {
Log::debug(sprintf('Could not render currencyField(): %s', $e->getMessage()));
$html = 'Could not render currencyField.';
}
return $html;
return $this->select($name, $array, $value, $options);
}
/**
* TODO cleanup and describe.
*
* @param string $name
* @param mixed $value
* @param array $options
* @param mixed $value
* @param array $options
*
* @return string
*/
public function amount(string $name, $value = null, array $options = null): string
public function currencyListEmpty(string $name, $value = null, array $options = null): string
{
return $this->currencyField($name, 'amount', $value, $options);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
// get all currencies:
$list = $currencyRepos->get();
$array = [
0 => (string)trans('firefly.no_currency'),
];
/** @var TransactionCurrency $currency */
foreach ($list as $currency) {
$array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')';
}
return $this->select($name, $array, $value, $options);
}
}

View File

@@ -38,30 +38,6 @@ trait FormSupport
{
/**
* @return AccountRepositoryInterface
*/
protected function getAccountRepository(): AccountRepositoryInterface
{
return app(AccountRepositoryInterface::class);
}
/**
* @return Carbon
*/
protected function getDate(): Carbon
{
/** @var Carbon $date */
$date = null;
try {
$date = today(config('app.timezone'));
} catch (Exception $e) {
$e->getMessage();
}
return $date;
}
/**
* @param string $name
* @param array $list
@@ -88,6 +64,23 @@ trait FormSupport
return $html;
}
/**
* @param $name
* @param $options
*
* @return string
*/
protected function label(string $name, array $options = null): string
{
$options = $options ?? [];
if (isset($options['label'])) {
return $options['label'];
}
$name = str_replace('[]', '', $name);
return (string)trans('form.' . $name);
}
/**
* @param $name
* @param $label
@@ -107,6 +100,25 @@ trait FormSupport
return $options;
}
/**
* @param $name
*
* @return string
*/
protected function getHolderClasses(string $name): string
{
// Get errors from session:
/** @var MessageBag $errors */
$errors = session('errors');
$classes = 'form-group';
if (null !== $errors && $errors->has($name)) {
$classes = 'form-group has-error has-feedback';
}
return $classes;
}
/**
* @param string $name
* @param $value
@@ -137,38 +149,26 @@ trait FormSupport
}
/**
* @param $name
*
* @return string
* @return AccountRepositoryInterface
*/
protected function getHolderClasses(string $name): string
protected function getAccountRepository(): AccountRepositoryInterface
{
// Get errors from session:
/** @var MessageBag $errors */
$errors = session('errors');
$classes = 'form-group';
if (null !== $errors && $errors->has($name)) {
$classes = 'form-group has-error has-feedback';
}
return $classes;
return app(AccountRepositoryInterface::class);
}
/**
* @param $name
* @param $options
*
* @return string
* @return Carbon
*/
protected function label(string $name, array $options = null): string
protected function getDate(): Carbon
{
$options = $options ?? [];
if (isset($options['label'])) {
return $options['label'];
/** @var Carbon $date */
$date = null;
try {
$date = today(config('app.timezone'));
} catch (Exception $e) {
$e->getMessage();
}
$name = str_replace('[]', '', $name);
return (string)trans('form.' . $name);
return $date;
}
}

View File

@@ -40,8 +40,8 @@ class PiggyBankForm
* TODO cleanup and describe.
*
* @param string $name
* @param mixed $value
* @param array $options
* @param mixed $value
* @param array $options
*
* @return string
*/
@@ -51,7 +51,7 @@ class PiggyBankForm
/** @var PiggyBankRepositoryInterface $repository */
$repository = app(PiggyBankRepositoryInterface::class);
$piggyBanks = $repository->getPiggyBanksWithAmount();
$title = (string) trans('firefly.default_group_title_name');
$title = (string)trans('firefly.default_group_title_name');
$array = [];
$subList = [
0 => [
@@ -59,7 +59,7 @@ class PiggyBankForm
'title' => $title,
],
'piggies' => [
(string) trans('firefly.none_in_select_list'),
(string)trans('firefly.none_in_select_list'),
],
],
];

View File

@@ -36,10 +36,11 @@ use Illuminate\Support\HtmlString;
class RuleForm
{
use FormSupport;
/**
* @param string $name
* @param mixed $value
* @param array $options
* @param mixed $value
* @param array $options
*
* @return string
*/
@@ -60,8 +61,8 @@ class RuleForm
}
/**
* @param string $name
* @param null $value
* @param string $name
* @param null $value
* @param array|null $options
*
* @return HtmlString