mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-25 13:10:35 +00:00
Fix piggy bank orders
This commit is contained in:
@@ -39,7 +39,8 @@ use Illuminate\Database\QueryException;
|
||||
class PiggyBankFactory
|
||||
{
|
||||
use CreatesObjectGroups;
|
||||
public User $user {
|
||||
|
||||
public User $user {
|
||||
set(User $value) {
|
||||
$this->user = $value;
|
||||
$this->currencyRepository->setUser($value);
|
||||
@@ -47,14 +48,14 @@ class PiggyBankFactory
|
||||
$this->piggyBankRepository->setUser($value);
|
||||
}
|
||||
}
|
||||
private CurrencyRepositoryInterface $currencyRepository;
|
||||
private AccountRepositoryInterface $accountRepository;
|
||||
private CurrencyRepositoryInterface $currencyRepository;
|
||||
private AccountRepositoryInterface $accountRepository;
|
||||
private PiggyBankRepositoryInterface $piggyBankRepository;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
|
||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
|
||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||
$this->piggyBankRepository = app(PiggyBankRepositoryInterface::class);
|
||||
}
|
||||
|
||||
@@ -65,23 +66,24 @@ class PiggyBankFactory
|
||||
*
|
||||
* @return PiggyBank
|
||||
*/
|
||||
public function store(array $data): PiggyBank {
|
||||
public function store(array $data): PiggyBank
|
||||
{
|
||||
|
||||
$piggyBankData =$data;
|
||||
$piggyBankData = $data;
|
||||
|
||||
// unset some fields
|
||||
unset($piggyBankData['object_group_title'],$piggyBankData['transaction_currency_code'],$piggyBankData['transaction_currency_id'],$piggyBankData['accounts'], $piggyBankData['object_group_id'], $piggyBankData['notes']);
|
||||
unset($piggyBankData['object_group_title'], $piggyBankData['transaction_currency_code'], $piggyBankData['transaction_currency_id'], $piggyBankData['accounts'], $piggyBankData['object_group_id'], $piggyBankData['notes']);
|
||||
|
||||
// validate amount:
|
||||
if (array_key_exists('target_amount', $piggyBankData) && '' === (string)$piggyBankData['target_amount']) {
|
||||
if (array_key_exists('target_amount', $piggyBankData) && '' === (string) $piggyBankData['target_amount']) {
|
||||
$piggyBankData['target_amount'] = '0';
|
||||
}
|
||||
|
||||
$piggyBankData['start_date_tz'] = $piggyBankData['start_date']?->format('e');
|
||||
$piggyBankData['target_date_tz'] = $piggyBankData['target_date']?->format('e');
|
||||
$piggyBankData['account_id'] = null;
|
||||
$piggyBankData['start_date_tz'] = $piggyBankData['start_date']?->format('e');
|
||||
$piggyBankData['target_date_tz'] = $piggyBankData['target_date']?->format('e');
|
||||
$piggyBankData['account_id'] = null;
|
||||
$piggyBankData['transaction_currency_id'] = $this->getCurrency($data)->id;
|
||||
$piggyBankData['order'] = 131337;
|
||||
$piggyBankData['order'] = 131337;
|
||||
|
||||
try {
|
||||
/** @var PiggyBank $piggyBank */
|
||||
@@ -95,7 +97,7 @@ class PiggyBankFactory
|
||||
$this->linkToAccountIds($piggyBank, $data['accounts']);
|
||||
$this->piggyBankRepository->updateNote($piggyBank, $data['notes']);
|
||||
|
||||
$objectGroupTitle = $data['object_group_title'] ?? '';
|
||||
$objectGroupTitle = $data['object_group_title'] ?? '';
|
||||
if ('' !== $objectGroupTitle) {
|
||||
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
|
||||
if (null !== $objectGroup) {
|
||||
@@ -104,7 +106,7 @@ class PiggyBankFactory
|
||||
}
|
||||
}
|
||||
// try also with ID
|
||||
$objectGroupId = (int)($data['object_group_id'] ?? 0);
|
||||
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
|
||||
if (0 !== $objectGroupId) {
|
||||
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
||||
if (null !== $objectGroup) {
|
||||
@@ -118,15 +120,19 @@ class PiggyBankFactory
|
||||
|
||||
public function find(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank
|
||||
{
|
||||
$piggyBankId = (int)$piggyBankId;
|
||||
$piggyBankName = (string)$piggyBankName;
|
||||
$piggyBankId = (int) $piggyBankId;
|
||||
$piggyBankName = (string) $piggyBankName;
|
||||
if ('' === $piggyBankName && 0 === $piggyBankId) {
|
||||
return null;
|
||||
}
|
||||
// first find by ID:
|
||||
if ($piggyBankId > 0) {
|
||||
/** @var null|PiggyBank $piggyBank */
|
||||
$piggyBank = $this->user->piggyBanks()->find($piggyBankId);
|
||||
$piggyBank = PiggyBank
|
||||
::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->where('piggy_banks.id', $piggyBankId)
|
||||
->first(['piggy_banks.*']);
|
||||
if (null !== $piggyBank) {
|
||||
return $piggyBank;
|
||||
}
|
||||
@@ -146,26 +152,33 @@ class PiggyBankFactory
|
||||
|
||||
public function findByName(string $name): ?PiggyBank
|
||||
{
|
||||
return $this->user->piggyBanks()->where('piggy_banks.name', $name)->first();
|
||||
return PiggyBank
|
||||
::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->where('piggy_banks.name', $name)
|
||||
->first(['piggy_banks.*']);
|
||||
}
|
||||
|
||||
private function getCurrency(array $data): TransactionCurrency {
|
||||
private function getCurrency(array $data): TransactionCurrency
|
||||
{
|
||||
// currency:
|
||||
$defaultCurrency = app('amount')->getDefaultCurrency();
|
||||
$currency = null;
|
||||
if (array_key_exists('transaction_currency_code', $data)) {
|
||||
$currency = $this->currencyRepository->findByCode((string)($data['transaction_currency_code'] ?? ''));
|
||||
$currency = $this->currencyRepository->findByCode((string) ($data['transaction_currency_code'] ?? ''));
|
||||
}
|
||||
if (array_key_exists('transaction_currency_id', $data)) {
|
||||
$currency = $this->currencyRepository->find((int)($data['transaction_currency_id'] ?? 0));
|
||||
$currency = $this->currencyRepository->find((int) ($data['transaction_currency_id'] ?? 0));
|
||||
}
|
||||
$currency ??= $defaultCurrency;
|
||||
return $currency;
|
||||
}
|
||||
|
||||
private function setOrder(PiggyBank $piggyBank, array $data): PiggyBank {
|
||||
private function setOrder(PiggyBank $piggyBank, array $data): PiggyBank
|
||||
{
|
||||
$this->resetOrder();
|
||||
$order = $this->getMaxOrder() + 1;
|
||||
$order = $this->getMaxOrder() + 1;
|
||||
if (array_key_exists('order', $data)) {
|
||||
$order = $data['order'];
|
||||
}
|
||||
@@ -178,13 +191,12 @@ class PiggyBankFactory
|
||||
public function resetOrder(): void
|
||||
{
|
||||
// TODO duplicate code
|
||||
$set = PiggyBank
|
||||
$set = PiggyBank
|
||||
::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->with(
|
||||
[
|
||||
'account',
|
||||
'objectGroups',
|
||||
]
|
||||
)
|
||||
@@ -207,19 +219,20 @@ class PiggyBankFactory
|
||||
|
||||
}
|
||||
|
||||
public function linkToAccountIds(PiggyBank $piggyBank, array $accounts): void {
|
||||
public function linkToAccountIds(PiggyBank $piggyBank, array $accounts): void
|
||||
{
|
||||
$toBeLinked = [];
|
||||
/** @var array $info */
|
||||
foreach($accounts as $info) {
|
||||
$account = $this->accountRepository->find((int)($info['account_id'] ?? 0));
|
||||
if(null === $account) {
|
||||
foreach ($accounts as $info) {
|
||||
$account = $this->accountRepository->find((int) ($info['account_id'] ?? 0));
|
||||
if (null === $account) {
|
||||
continue;
|
||||
}
|
||||
if(array_key_exists('current_amount',$info)) {
|
||||
if (array_key_exists('current_amount', $info)) {
|
||||
$toBeLinked[$account->id] = ['current_amount' => $info['current_amount']];
|
||||
//$piggyBank->accounts()->syncWithoutDetaching([$account->id => ['current_amount' => $info['current_amount'] ?? '0']]);
|
||||
}
|
||||
if(!array_key_exists('current_amount', $info)) {
|
||||
if (!array_key_exists('current_amount', $info)) {
|
||||
$toBeLinked[$account->id] = [];
|
||||
//$piggyBank->accounts()->syncWithoutDetaching([$account->id]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user