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
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
4 changed files with 30 additions and 29 deletions

View File

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

View File

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

View File

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

View File

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