Update / create methods can handle order #3200

This commit is contained in:
James Cole
2020-07-24 16:41:58 +02:00
parent 1286694efd
commit 74247c292f
4 changed files with 30 additions and 29 deletions

View File

@@ -43,17 +43,12 @@ class AccountFactory
use AccountServiceTrait, LocationServiceTrait; use AccountServiceTrait, LocationServiceTrait;
/** @var AccountRepositoryInterface */ /** @var AccountRepositoryInterface */
protected $accountRepository; protected $accountRepository;
/** @var array */ protected array $validAssetFields;
protected $validAssetFields = ['account_role', 'account_number', 'currency_id', 'BIC', 'include_net_worth']; protected array $validCCFields;
/** @var array */ protected array $validFields;
protected $validCCFields = ['account_role', 'cc_monthly_payment_date', 'cc_type', 'account_number', 'currency_id', 'BIC', 'include_net_worth']; private array $canHaveVirtual;
/** @var array */ private User $user;
protected $validFields = ['account_number', 'currency_id', 'BIC', 'interest', 'interest_period', 'include_net_worth'];
/** @var array */
private $canHaveVirtual;
/** @var User */
private $user;
/** /**
* AccountFactory constructor. * AccountFactory constructor.
@@ -67,6 +62,10 @@ class AccountFactory
} }
$this->canHaveVirtual = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD]; $this->canHaveVirtual = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD];
$this->accountRepository = app(AccountRepositoryInterface::class); $this->accountRepository = app(AccountRepositoryInterface::class);
$this->validAssetFields = ['account_role', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
$this->validCCFields = ['account_role', 'cc_monthly_payment_date', 'cc_type', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
$this->validFields = ['account_number', 'currency_id', 'BIC', 'interest', 'interest_period', 'include_net_worth'];
} }
/** /**
@@ -98,6 +97,7 @@ class AccountFactory
'user_id' => $this->user->id, 'user_id' => $this->user->id,
'account_type_id' => $type->id, 'account_type_id' => $type->id,
'name' => $data['name'], 'name' => $data['name'],
'order' => $data['order'] ?? 0,
'virtual_balance' => $data['virtual_balance'] ?? null, 'virtual_balance' => $data['virtual_balance'] ?? null,
'active' => true === $data['active'], 'active' => true === $data['active'],
'iban' => $data['iban'], 'iban' => $data['iban'],

View File

@@ -274,12 +274,11 @@ class AccountRepository implements AccountRepositoryInterface
if (count($accountIds) > 0) { if (count($accountIds) > 0) {
$query->whereIn('accounts.id', $accountIds); $query->whereIn('accounts.id', $accountIds);
} }
$query->orderBy('accounts.order', 'ASC');
$query->orderBy('accounts.active', 'DESC'); $query->orderBy('accounts.active', 'DESC');
$query->orderBy('accounts.name', 'ASC'); $query->orderBy('accounts.name', 'ASC');
$result = $query->get(['accounts.*']); return $query->get(['accounts.*']);
return $result;
} }
/** /**
@@ -294,12 +293,12 @@ class AccountRepository implements AccountRepositoryInterface
if (count($types) > 0) { if (count($types) > 0) {
$query->accountTypeIn($types); $query->accountTypeIn($types);
} }
$query->orderBy('accounts.order', 'ASC');
$query->orderBy('accounts.active', 'DESC'); $query->orderBy('accounts.active', 'DESC');
$query->orderBy('accounts.name', 'ASC'); $query->orderBy('accounts.name', 'ASC');
$result = $query->get(['accounts.*']);
return $query->get(['accounts.*']);
return $result;
} }
/** /**
@@ -320,6 +319,7 @@ class AccountRepository implements AccountRepositoryInterface
} }
$query->where('active', 1); $query->where('active', 1);
$query->orderBy('accounts.account_type_id', 'ASC'); $query->orderBy('accounts.account_type_id', 'ASC');
$query->orderBy('accounts.order', 'ASC');
$query->orderBy('accounts.name', 'ASC'); $query->orderBy('accounts.name', 'ASC');
return $query->get(['accounts.*']); return $query->get(['accounts.*']);
@@ -567,6 +567,7 @@ class AccountRepository implements AccountRepositoryInterface
{ {
$dbQuery = $this->user->accounts() $dbQuery = $this->user->accounts()
->where('active', 1) ->where('active', 1)
->orderBy('accounts.order', 'ASC')
->orderBy('accounts.name', 'ASC') ->orderBy('accounts.name', 'ASC')
->with(['accountType']); ->with(['accountType']);
if ('' !== $query) { if ('' !== $query) {
@@ -643,6 +644,7 @@ class AccountRepository implements AccountRepositoryInterface
} }
$query->where('active', 0); $query->where('active', 0);
$query->orderBy('accounts.account_type_id', 'ASC'); $query->orderBy('accounts.account_type_id', 'ASC');
$query->orderBy('accounts.order', 'ASC');
$query->orderBy('accounts.name', 'ASC'); $query->orderBy('accounts.name', 'ASC');
return $query->get(['accounts.*']); return $query->get(['accounts.*']);

View File

@@ -38,18 +38,12 @@ class AccountUpdateService
{ {
use AccountServiceTrait; use AccountServiceTrait;
/** @var AccountRepositoryInterface */ protected AccountRepositoryInterface $accountRepository;
protected $accountRepository; protected array $validAssetFields;
/** @var array */ protected array $validCCFields;
protected $validAssetFields = ['account_role', 'account_number', 'currency_id', 'BIC', 'include_net_worth']; protected array $validFields;
/** @var array */ private array $canHaveVirtual;
protected $validCCFields = ['account_role', 'cc_monthly_payment_date', 'cc_type', 'account_number', 'currency_id', 'BIC', 'include_net_worth']; private User $user;
/** @var array */
protected $validFields = ['account_number', 'currency_id', 'BIC', 'interest', 'interest_period', 'include_net_worth'];
/** @var array */
private $canHaveVirtual;
/** @var User */
private $user;
/** /**
* Constructor. * Constructor.
@@ -62,6 +56,9 @@ class AccountUpdateService
// TODO move to configuration. // TODO move to configuration.
$this->canHaveVirtual = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD]; $this->canHaveVirtual = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD];
$this->accountRepository = app(AccountRepositoryInterface::class); $this->accountRepository = app(AccountRepositoryInterface::class);
$this->validAssetFields = ['account_role', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
$this->validCCFields = ['account_role', 'cc_monthly_payment_date', 'cc_type', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
$this->validFields = ['account_number', 'currency_id', 'BIC', 'interest', 'interest_period', 'include_net_worth'];
} }
/** /**
@@ -174,6 +171,7 @@ class AccountUpdateService
$account->name = $data['name'] ?? $account->name; $account->name = $data['name'] ?? $account->name;
$account->active = $data['active'] ?? $account->active; $account->active = $data['active'] ?? $account->active;
$account->iban = $data['iban'] ?? $account->iban; $account->iban = $data['iban'] ?? $account->iban;
$account->order = $data['order'] ?? $account->order;
// if account type is a liability, the liability type (account type) // if account type is a liability, the liability type (account type)
// can be updated to another one. // can be updated to another one.

View File

@@ -96,10 +96,11 @@ class AccountTransformer extends AbstractTransformer
$zoomLevel = $location->zoom_level; $zoomLevel = $location->zoom_level;
} }
return [ return [
'id' => (int)$account->id, 'id' => (int) $account->id,
'created_at' => $account->created_at->toAtomString(), 'created_at' => $account->created_at->toAtomString(),
'updated_at' => $account->updated_at->toAtomString(), 'updated_at' => $account->updated_at->toAtomString(),
'active' => $account->active, 'active' => $account->active,
'order' => $account->order,
'name' => $account->name, 'name' => $account->name,
'type' => $accountType, 'type' => $accountType,
'account_role' => $accountRole, 'account_role' => $accountRole,