mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Renamed various fields from their old camel casing to new ones.
This commit is contained in:
@@ -24,18 +24,15 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Services\Internal\Support;
|
||||
|
||||
use Exception;
|
||||
use FireflyIII\Factory\AccountFactory;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\AccountMetaFactory;
|
||||
use FireflyIII\Factory\TransactionFactory;
|
||||
use FireflyIII\Factory\TransactionJournalFactory;
|
||||
use FireflyIII\Factory\TransactionGroupFactory;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
|
||||
use FireflyIII\User;
|
||||
use FireflyIII\Models\TransactionGroup;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Services\Internal\Destroy\TransactionGroupDestroyService;
|
||||
use Log;
|
||||
use Validator;
|
||||
|
||||
@@ -45,35 +42,15 @@ use Validator;
|
||||
*/
|
||||
trait AccountServiceTrait
|
||||
{
|
||||
/** @var AccountRepositoryInterface */
|
||||
protected $accountRepository;
|
||||
|
||||
/** @var array */
|
||||
public $validAssetFields = ['accountRole', 'accountNumber', 'currency_id', 'BIC', 'include_net_worth'];
|
||||
protected $validAssetFields = ['account_role', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
|
||||
/** @var array */
|
||||
public $validCCFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber', 'currency_id', 'BIC', 'include_net_worth'];
|
||||
protected $validCCFields = ['account_role', 'cc_monthly_payment_date', 'cc_type', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
|
||||
/** @var array */
|
||||
public $validFields = ['accountNumber', 'currency_id', 'BIC', 'interest', 'interest_period', 'include_net_worth'];
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteIB(Account $account): bool
|
||||
{
|
||||
Log::debug(sprintf('deleteIB() for account #%d', $account->id));
|
||||
$openingBalance = $this->getIBJournal($account);
|
||||
|
||||
// opening balance data? update it!
|
||||
if (null !== $openingBalance) {
|
||||
Log::debug('Opening balance journal found, delete journal.');
|
||||
/** @var JournalDestroyService $service */
|
||||
$service = app(JournalDestroyService::class);
|
||||
$service->destroy($openingBalance);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
protected $validFields = ['account_number', 'currency_id', 'BIC', 'interest', 'interest_period', 'include_net_worth'];
|
||||
|
||||
/**
|
||||
* @param null|string $iban
|
||||
@@ -98,217 +75,63 @@ trait AccountServiceTrait
|
||||
return $iban;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find existing opening balance.
|
||||
*
|
||||
* @param Account $account
|
||||
*
|
||||
* @return TransactionJournal|null
|
||||
*/
|
||||
public function getIBJournal(Account $account): ?TransactionJournal
|
||||
{
|
||||
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->transactionTypes([TransactionType::OPENING_BALANCE])
|
||||
->first(['transaction_journals.*']);
|
||||
if (null === $journal) {
|
||||
Log::debug('Could not find a opening balance journal, return NULL.');
|
||||
// /**
|
||||
// * @param User $user
|
||||
// * @param string $name
|
||||
// *
|
||||
// * @return Account
|
||||
// * @throws \FireflyIII\Exceptions\FireflyException
|
||||
// */
|
||||
// public function storeOpposingAccount(User $user, string $name): Account
|
||||
// {
|
||||
// $opposingAccountName = (string)trans('firefly.initial_balance_account', ['name' => $name]);
|
||||
// Log::debug('Going to create an opening balance opposing account.');
|
||||
// /** @var AccountFactory $factory */
|
||||
// $factory = app(AccountFactory::class);
|
||||
// $factory->setUser($user);
|
||||
//
|
||||
// return $factory->findOrCreate($opposingAccountName, AccountType::INITIAL_BALANCE);
|
||||
// }
|
||||
|
||||
return null;
|
||||
}
|
||||
Log::debug(sprintf('Found opening balance: journal #%d.', $journal->id));
|
||||
|
||||
return $journal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionJournal|null
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function storeIBJournal(Account $account, array $data): ?TransactionJournal
|
||||
{
|
||||
$amount = (string)$data['openingBalance'];
|
||||
Log::debug(sprintf('Submitted amount is %s', $amount));
|
||||
|
||||
if (0 === bccomp($amount, '0')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// store journal, without transactions:
|
||||
$name = $data['name'];
|
||||
$currencyId = $data['currency_id'];
|
||||
$journalData = [
|
||||
'type' => TransactionType::OPENING_BALANCE,
|
||||
'user' => $account->user->id,
|
||||
'transaction_currency_id' => $currencyId,
|
||||
'description' => (string)trans('firefly.initial_balance_description', ['account' => $account->name]),
|
||||
'completed' => true,
|
||||
'date' => $data['openingBalanceDate'],
|
||||
'bill_id' => null,
|
||||
'bill_name' => null,
|
||||
'piggy_bank_id' => null,
|
||||
'piggy_bank_name' => null,
|
||||
'tags' => null,
|
||||
'notes' => null,
|
||||
'transactions' => [],
|
||||
|
||||
];
|
||||
/** @var TransactionJournalFactory $factory */
|
||||
$factory = app(TransactionJournalFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
$journal = $factory->create($journalData);
|
||||
$opposing = $this->storeOpposingAccount($account->user, $name);
|
||||
Log::notice(sprintf('Created new opening balance journal: #%d', $journal->id));
|
||||
|
||||
$firstAccount = $account;
|
||||
$secondAccount = $opposing;
|
||||
$firstAmount = $amount;
|
||||
$secondAmount = bcmul($amount, '-1');
|
||||
Log::notice(sprintf('First amount is %s, second amount is %s', $firstAmount, $secondAmount));
|
||||
|
||||
if (bccomp($amount, '0') === -1) {
|
||||
Log::debug(sprintf('%s is a negative number.', $amount));
|
||||
$firstAccount = $opposing;
|
||||
$secondAccount = $account;
|
||||
$firstAmount = bcmul($amount, '-1');
|
||||
$secondAmount = $amount;
|
||||
Log::notice(sprintf('First amount is %s, second amount is %s', $firstAmount, $secondAmount));
|
||||
}
|
||||
/** @var TransactionFactory $factory */
|
||||
$factory = app(TransactionFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
$factory->create(
|
||||
[
|
||||
'account' => $firstAccount,
|
||||
'transaction_journal' => $journal,
|
||||
'amount' => $firstAmount,
|
||||
'currency_id' => $currencyId,
|
||||
'description' => null,
|
||||
'identifier' => 0,
|
||||
'foreign_amount' => null,
|
||||
'reconciled' => false,
|
||||
]
|
||||
);
|
||||
$factory->create(
|
||||
[
|
||||
'account' => $secondAccount,
|
||||
'transaction_journal' => $journal,
|
||||
'amount' => $secondAmount,
|
||||
'currency_id' => $currencyId,
|
||||
'description' => null,
|
||||
'identifier' => 0,
|
||||
'foreign_amount' => null,
|
||||
'reconciled' => false,
|
||||
]
|
||||
);
|
||||
|
||||
return $journal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param string $name
|
||||
*
|
||||
* @return Account
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public function storeOpposingAccount(User $user, string $name): Account
|
||||
{
|
||||
$opposingAccountName = (string)trans('firefly.initial_balance_account', ['name' => $name]);
|
||||
Log::debug('Going to create an opening balance opposing account.');
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($user);
|
||||
|
||||
return $factory->findOrCreate($opposingAccountName, AccountType::INITIAL_BALANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public function updateIB(Account $account, array $data): bool
|
||||
{
|
||||
Log::debug(sprintf('updateInitialBalance() for account #%d', $account->id));
|
||||
$openingBalance = $this->getIBJournal($account);
|
||||
|
||||
// no opening balance journal? create it:
|
||||
if (null === $openingBalance) {
|
||||
Log::debug('No opening balance journal yet, create journal.');
|
||||
$this->storeIBJournal($account, $data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// opening balance data? update it!
|
||||
if (null !== $openingBalance->id) {
|
||||
Log::debug('Opening balance journal found, update journal.');
|
||||
$this->updateIBJournal($account, $openingBalance, $data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return true; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function updateIBJournal(Account $account, TransactionJournal $journal, array $data): bool
|
||||
{
|
||||
$date = $data['openingBalanceDate'];
|
||||
$amount = (string)$data['openingBalance'];
|
||||
$negativeAmount = bcmul($amount, '-1');
|
||||
$currencyId = (int)$data['currency_id'];
|
||||
Log::debug(sprintf('Submitted amount for opening balance to update is "%s"', $amount));
|
||||
if (0 === bccomp($amount, '0')) {
|
||||
Log::notice(sprintf('Amount "%s" is zero, delete opening balance.', $amount));
|
||||
/** @var JournalDestroyService $service */
|
||||
$service = app(JournalDestroyService::class);
|
||||
$service->destroy($journal);
|
||||
|
||||
return true;
|
||||
}
|
||||
$journal->date = $date;
|
||||
$journal->transaction_currency_id = $currencyId;
|
||||
$journal->save();
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($journal->transactions()->get() as $transaction) {
|
||||
if ((int)$account->id === (int)$transaction->account_id) {
|
||||
Log::debug(sprintf('Will (eq) change transaction #%d amount from "%s" to "%s"', $transaction->id, $transaction->amount, $amount));
|
||||
$transaction->amount = $amount;
|
||||
$transaction->transaction_currency_id = $currencyId;
|
||||
$transaction->save();
|
||||
}
|
||||
if (!((int)$account->id === (int)$transaction->account_id)) {
|
||||
Log::debug(sprintf('Will (neq) change transaction #%d amount from "%s" to "%s"', $transaction->id, $transaction->amount, $negativeAmount));
|
||||
$transaction->amount = $negativeAmount;
|
||||
$transaction->transaction_currency_id = $currencyId;
|
||||
$transaction->save();
|
||||
}
|
||||
}
|
||||
Log::debug('Updated opening balance journal.');
|
||||
|
||||
return true;
|
||||
}
|
||||
// /**
|
||||
// * @param Account $account
|
||||
// * @param array $data
|
||||
// *
|
||||
// * @return bool
|
||||
// * @throws \FireflyIII\Exceptions\FireflyException
|
||||
// */
|
||||
// public function updateOB(Account $account, array $data): bool
|
||||
// {
|
||||
// Log::debug(sprintf('updateIB() for account #%d', $account->id));
|
||||
//
|
||||
// $openingBalanceGroup = $this->getOBGroup($account);
|
||||
//
|
||||
// // no opening balance journal? create it:
|
||||
// if (null === $openingBalanceGroup) {
|
||||
// Log::debug('No opening balance journal yet, create group.');
|
||||
// $this->storeOBGroup($account, $data);
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// // opening balance data? update it!
|
||||
// if (null !== $openingBalanceGroup->id) {
|
||||
// Log::debug('Opening balance group found, update group.');
|
||||
// $this->updateOBGroup($account, $openingBalanceGroup, $data);
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// return true; // @codeCoverageIgnore
|
||||
// }
|
||||
|
||||
/**
|
||||
* Update meta data for account. Depends on type which fields are valid.
|
||||
*
|
||||
* TODO this method treats expense accounts and liabilities the same way (tries to save interest)
|
||||
*
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
* @param array $data
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function updateMetaData(Account $account, array $data): void
|
||||
@@ -318,7 +141,7 @@ trait AccountServiceTrait
|
||||
if ($account->accountType->type === AccountType::ASSET) {
|
||||
$fields = $this->validAssetFields;
|
||||
}
|
||||
if ($account->accountType->type === AccountType::ASSET && 'ccAsset' === $data['accountRole']) {
|
||||
if ($account->accountType->type === AccountType::ASSET && 'ccAsset' === $data['account_role']) {
|
||||
$fields = $this->validCCFields;
|
||||
}
|
||||
/** @var AccountMetaFactory $factory */
|
||||
@@ -328,9 +151,120 @@ trait AccountServiceTrait
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Find existing opening balance.
|
||||
// *
|
||||
// * @param Account $account
|
||||
// *
|
||||
// * @return TransactionJournal|null
|
||||
// */
|
||||
// public function getIBJournal(Account $account): ?TransactionJournal
|
||||
// {
|
||||
// $journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
// ->where('transactions.account_id', $account->id)
|
||||
// ->transactionTypes([TransactionType::OPENING_BALANCE])
|
||||
// ->first(['transaction_journals.*']);
|
||||
// if (null === $journal) {
|
||||
// Log::debug('Could not find a opening balance journal, return NULL.');
|
||||
//
|
||||
// return null;
|
||||
// }
|
||||
// Log::debug(sprintf('Found opening balance: journal #%d.', $journal->id));
|
||||
//
|
||||
// return $journal;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * @param Account $account
|
||||
// * @param array $data
|
||||
// *
|
||||
// * @return TransactionJournal|null
|
||||
// * @throws \FireflyIII\Exceptions\FireflyException
|
||||
// * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
// */
|
||||
// public function storeIBJournal(Account $account, array $data): ?TransactionJournal
|
||||
// {
|
||||
// $amount = (string)$data['openingBalance'];
|
||||
// Log::debug(sprintf('Submitted amount is %s', $amount));
|
||||
//
|
||||
// if (0 === bccomp($amount, '0')) {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// // store journal, without transactions:
|
||||
// $name = $data['name'];
|
||||
// $currencyId = $data['currency_id'];
|
||||
// $journalData = [
|
||||
// 'type' => TransactionType::OPENING_BALANCE,
|
||||
// 'user' => $account->user->id,
|
||||
// 'transaction_currency_id' => $currencyId,
|
||||
// 'description' => (string)trans('firefly.initial_balance_description', ['account' => $account->name]),
|
||||
// 'completed' => true,
|
||||
// 'date' => $data['openingBalanceDate'],
|
||||
// 'bill_id' => null,
|
||||
// 'bill_name' => null,
|
||||
// 'piggy_bank_id' => null,
|
||||
// 'piggy_bank_name' => null,
|
||||
// 'tags' => null,
|
||||
// 'notes' => null,
|
||||
// 'transactions' => [],
|
||||
//
|
||||
// ];
|
||||
// /** @var TransactionJournalFactory $factory */
|
||||
// $factory = app(TransactionJournalFactory::class);
|
||||
// $factory->setUser($account->user);
|
||||
// $journal = $factory->create($journalData);
|
||||
// $opposing = $this->storeOpposingAccount($account->user, $name);
|
||||
// Log::notice(sprintf('Created new opening balance journal: #%d', $journal->id));
|
||||
//
|
||||
// $firstAccount = $account;
|
||||
// $secondAccount = $opposing;
|
||||
// $firstAmount = $amount;
|
||||
// $secondAmount = bcmul($amount, '-1');
|
||||
// Log::notice(sprintf('First amount is %s, second amount is %s', $firstAmount, $secondAmount));
|
||||
//
|
||||
// if (bccomp($amount, '0') === -1) {
|
||||
// Log::debug(sprintf('%s is a negative number.', $amount));
|
||||
// $firstAccount = $opposing;
|
||||
// $secondAccount = $account;
|
||||
// $firstAmount = bcmul($amount, '-1');
|
||||
// $secondAmount = $amount;
|
||||
// Log::notice(sprintf('First amount is %s, second amount is %s', $firstAmount, $secondAmount));
|
||||
// }
|
||||
// /** @var TransactionFactory $factory */
|
||||
// $factory = app(TransactionFactory::class);
|
||||
// $factory->setUser($account->user);
|
||||
// $factory->create(
|
||||
// [
|
||||
// 'account' => $firstAccount,
|
||||
// 'transaction_journal' => $journal,
|
||||
// 'amount' => $firstAmount,
|
||||
// 'currency_id' => $currencyId,
|
||||
// 'description' => null,
|
||||
// 'identifier' => 0,
|
||||
// 'foreign_amount' => null,
|
||||
// 'reconciled' => false,
|
||||
// ]
|
||||
// );
|
||||
// $factory->create(
|
||||
// [
|
||||
// 'account' => $secondAccount,
|
||||
// 'transaction_journal' => $journal,
|
||||
// 'amount' => $secondAmount,
|
||||
// 'currency_id' => $currencyId,
|
||||
// 'description' => null,
|
||||
// 'identifier' => 0,
|
||||
// 'foreign_amount' => null,
|
||||
// 'reconciled' => false,
|
||||
// ]
|
||||
// );
|
||||
//
|
||||
// return $journal;
|
||||
// }
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $note
|
||||
* @param string $note
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -366,10 +300,10 @@ trait AccountServiceTrait
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validIBData(array $data): bool
|
||||
public function validOBData(array $data): bool
|
||||
{
|
||||
$data['openingBalance'] = (string)($data['openingBalance'] ?? '');
|
||||
if ('' !== $data['openingBalance'] && isset($data['openingBalance'], $data['openingBalanceDate'])) {
|
||||
$data['opening_balance'] = (string)($data['opening_balance'] ?? '');
|
||||
if ('' !== $data['opening_balance'] && isset($data['opening_balance'], $data['opening_balance_date'])) {
|
||||
Log::debug('Array has valid opening balance data.');
|
||||
|
||||
return true;
|
||||
@@ -378,4 +312,172 @@ trait AccountServiceTrait
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @param Account $account
|
||||
// * @param TransactionJournal $journal
|
||||
// * @param array $data
|
||||
// *
|
||||
// * @return bool
|
||||
// * @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
// */
|
||||
// protected function updateIBJournal(Account $account, TransactionJournal $journal, array $data): bool
|
||||
// {
|
||||
// $date = $data['openingBalanceDate'];
|
||||
// $amount = (string)$data['openingBalance'];
|
||||
// $negativeAmount = bcmul($amount, '-1');
|
||||
// $currencyId = (int)$data['currency_id'];
|
||||
// Log::debug(sprintf('Submitted amount for opening balance to update is "%s"', $amount));
|
||||
// if (0 === bccomp($amount, '0')) {
|
||||
// Log::notice(sprintf('Amount "%s" is zero, delete opening balance.', $amount));
|
||||
// /** @var JournalDestroyService $service */
|
||||
// $service = app(JournalDestroyService::class);
|
||||
// $service->destroy($journal);
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
// $journal->date = $date;
|
||||
// $journal->transaction_currency_id = $currencyId;
|
||||
// $journal->save();
|
||||
// /** @var Transaction $transaction */
|
||||
// foreach ($journal->transactions()->get() as $transaction) {
|
||||
// if ((int)$account->id === (int)$transaction->account_id) {
|
||||
// Log::debug(sprintf('Will (eq) change transaction #%d amount from "%s" to "%s"', $transaction->id, $transaction->amount, $amount));
|
||||
// $transaction->amount = $amount;
|
||||
// $transaction->transaction_currency_id = $currencyId;
|
||||
// $transaction->save();
|
||||
// }
|
||||
// if (!((int)$account->id === (int)$transaction->account_id)) {
|
||||
// Log::debug(sprintf('Will (neq) change transaction #%d amount from "%s" to "%s"', $transaction->id, $transaction->amount, $negativeAmount));
|
||||
// $transaction->amount = $negativeAmount;
|
||||
// $transaction->transaction_currency_id = $currencyId;
|
||||
// $transaction->save();
|
||||
// }
|
||||
// }
|
||||
// Log::debug('Updated opening balance journal.');
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Delete TransactionGroup with opening balance in it.
|
||||
*
|
||||
* @param Account $account
|
||||
*/
|
||||
protected function deleteOBGroup(Account $account): void
|
||||
{
|
||||
Log::debug(sprintf('deleteOB() for account #%d', $account->id));
|
||||
$openingBalanceGroup = $this->getOBGroup($account);
|
||||
|
||||
// opening balance data? update it!
|
||||
if (null !== $openingBalanceGroup) {
|
||||
Log::debug('Opening balance journal found, delete journal.');
|
||||
/** @var TransactionGroupDestroyService $service */
|
||||
$service = app(TransactionGroupDestroyService::class);
|
||||
$service->destroy($openingBalanceGroup);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
* @return TransactionGroup|null
|
||||
*/
|
||||
protected function createOBGroup(Account $account, array $data): ?TransactionGroup
|
||||
{
|
||||
Log::debug('Now going to create an OB group.');
|
||||
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
|
||||
$sourceId = null;
|
||||
$sourceName = null;
|
||||
$destId = null;
|
||||
$destName = null;
|
||||
$amount = $data['opening_balance'];
|
||||
if (1 === bccomp($amount, '0')) {
|
||||
Log::debug(sprintf('Amount is %s, which is positive. Source is a new IB account, destination is #%d', $amount, $account->id));
|
||||
// amount is positive.
|
||||
$sourceName = trans('firefly.initial_balance_description', ['account' => $account->name], $language);
|
||||
$destId = $account->id;
|
||||
}
|
||||
if (-1 === bccomp($amount, '0')) {
|
||||
Log::debug(sprintf('Amount is %s, which is negative. Destination is a new IB account, source is #%d', $amount, $account->id));
|
||||
// amount is not positive
|
||||
$destName = trans('firefly.initial_balance_account', ['account' => $account->name], $language);
|
||||
$sourceId = $account->id;
|
||||
}
|
||||
$amount = app('steam')->positive($amount);
|
||||
$submission = [
|
||||
'group_title' => null,
|
||||
'user' => $account->user_id,
|
||||
'transactions' => [
|
||||
[
|
||||
'type' => 'Opening balance',
|
||||
'date' => $data['opening_balance_date'],
|
||||
'source_id' => $sourceId,
|
||||
'source_name' => $sourceName,
|
||||
'destination_id' => $destId,
|
||||
'destination_name' => $destName,
|
||||
'user' => $account->user_id,
|
||||
'order' => 0,
|
||||
'amount' => $amount,
|
||||
'foreign_amount' => null,
|
||||
'description' => trans('firefly.initial_balance_description', ['account' => $account->name]),
|
||||
'budget_id' => null,
|
||||
'budget_name' => null,
|
||||
'category_id' => null,
|
||||
'category_name' => null,
|
||||
'piggy_bank_id' => null,
|
||||
'piggy_bank_name' => null,
|
||||
'reconciled' => false,
|
||||
'notes' => null,
|
||||
'tags' => [],
|
||||
],
|
||||
],
|
||||
];
|
||||
Log::debug('Going for submission', $submission);
|
||||
$group = null;
|
||||
/** @var TransactionGroupFactory $factory */
|
||||
$factory = app(TransactionGroupFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
|
||||
try {
|
||||
$group = $factory->create($submission);
|
||||
// @codeCoverageIgnoreStart
|
||||
} catch (FireflyException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update or create the opening balance group. Assumes valid data in $data.
|
||||
*
|
||||
* Returns null if this fails.
|
||||
*
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
* @return TransactionGroup|null
|
||||
*/
|
||||
protected function updateOBGroup(Account $account, array $data): ?TransactionGroup
|
||||
{
|
||||
if (null === $this->getOBGroup($account)) {
|
||||
return $this->createOBGroup($account, $data);
|
||||
}
|
||||
|
||||
// edit in this method
|
||||
die('cannot handle edit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the opening balance group, or NULL if it does not exist.
|
||||
*
|
||||
* @param Account $account
|
||||
* @return TransactionGroup|null
|
||||
*/
|
||||
protected function getOBGroup(Account $account): ?TransactionGroup
|
||||
{
|
||||
return $this->accountRepository->getOpeningBalanceGroup($account);
|
||||
}
|
||||
}
|
||||
|
@@ -60,7 +60,6 @@ trait JournalServiceTrait
|
||||
*/
|
||||
protected function getForeignAmount(?string $amount): ?string
|
||||
{
|
||||
$result = null;
|
||||
if (null === $amount) {
|
||||
Log::debug('No foreign amount info in array. Return NULL');
|
||||
|
||||
@@ -152,7 +151,7 @@ trait JournalServiceTrait
|
||||
$result = $this->accountRepository->store(
|
||||
[
|
||||
'account_type_id' => null,
|
||||
'accountType' => $preferredType,
|
||||
'account_type' => $preferredType,
|
||||
'name' => $accountName,
|
||||
'active' => true,
|
||||
'iban' => null,
|
||||
@@ -243,9 +242,11 @@ trait JournalServiceTrait
|
||||
// try to delete existing notes.
|
||||
try {
|
||||
$note->delete();
|
||||
// @codeCoverageIgnoreStart
|
||||
} catch (Exception $e) {
|
||||
Log::debug(sprintf('Could not delete journal notes: %s', $e->getMessage()));
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -24,6 +24,8 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Services\Internal\Support;
|
||||
|
||||
use Exception;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\AccountFactory;
|
||||
use FireflyIII\Factory\BudgetFactory;
|
||||
use FireflyIII\Factory\CategoryFactory;
|
||||
use FireflyIII\Factory\PiggyBankFactory;
|
||||
@@ -35,7 +37,8 @@ use FireflyIII\Models\RecurrenceMeta;
|
||||
use FireflyIII\Models\RecurrenceRepetition;
|
||||
use FireflyIII\Models\RecurrenceTransaction;
|
||||
use FireflyIII\Models\RecurrenceTransactionMeta;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Validation\AccountValidator;
|
||||
use Log;
|
||||
|
||||
|
||||
@@ -47,9 +50,9 @@ trait RecurringTransactionTrait
|
||||
{
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $repetitions
|
||||
* @param array $repetitions
|
||||
*/
|
||||
public function createRepetitions(Recurrence $recurrence, array $repetitions): void
|
||||
protected function createRepetitions(Recurrence $recurrence, array $repetitions): void
|
||||
{
|
||||
/** @var array $array */
|
||||
foreach ($repetitions as $array) {
|
||||
@@ -66,33 +69,72 @@ trait RecurringTransactionTrait
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $expectedTypes
|
||||
* @param Account|null $account
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return Account
|
||||
*/
|
||||
protected function findAccount(array $expectedTypes, ?int $accountId, ?string $accountName): Account
|
||||
{
|
||||
$result = null;
|
||||
$accountId = (int)$accountId;
|
||||
$accountName = (string)$accountName;
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($this->user);
|
||||
|
||||
// if user has submitted an account ID, search for it.
|
||||
$result = $repository->findNull((int)$accountId);
|
||||
if (null !== $result) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
// if user has submitted a name, search for it:
|
||||
$result = $repository->findByName($accountName, $expectedTypes);
|
||||
if (null !== $result) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
// maybe we can create it? Try to avoid LOAN and other asset types.
|
||||
$cannotCreate = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD];
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($this->user);
|
||||
foreach ($expectedTypes as $expectedType) {
|
||||
if (in_array($expectedType, $cannotCreate, true)) {
|
||||
continue;
|
||||
}
|
||||
if (!in_array($expectedType, $cannotCreate, true)) {
|
||||
try {
|
||||
$result = $factory->findOrCreate($accountName, $expectedType);
|
||||
// @codeCoverageIgnoreStart
|
||||
} catch (FireflyException $e) {
|
||||
Log::error($e->getMessage());
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
}
|
||||
|
||||
return $result ?? $repository->getCashAccount();
|
||||
}
|
||||
|
||||
/**
|
||||
* Store transactions of a recurring transactions. It's complex but readable.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $transactions
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
* @param array $transactions
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function createTransactions(Recurrence $recurrence, array $transactions): void
|
||||
protected function createTransactions(Recurrence $recurrence, array $transactions): void
|
||||
{
|
||||
foreach ($transactions as $array) {
|
||||
$source = null;
|
||||
$destination = null;
|
||||
switch ($recurrence->transactionType->type) {
|
||||
case TransactionType::WITHDRAWAL:
|
||||
$source = $this->findAccount(AccountType::ASSET, null, $array['source_id'], $array['source_name']);
|
||||
$destination = $this->findAccount(AccountType::EXPENSE,null, $array['destination_id'], $array['destination_name']);
|
||||
break;
|
||||
case TransactionType::DEPOSIT:
|
||||
$source = $this->findAccount(AccountType::REVENUE, null, $array['source_id'], $array['source_name']);
|
||||
$destination = $this->findAccount(AccountType::ASSET, null, $array['destination_id'], $array['destination_name']);
|
||||
break;
|
||||
case TransactionType::TRANSFER:
|
||||
$source = $this->findAccount(AccountType::ASSET,null, $array['source_id'], $array['source_name']);
|
||||
$destination = $this->findAccount(AccountType::ASSET, null, $array['destination_id'], $array['destination_name']);
|
||||
break;
|
||||
}
|
||||
$sourceTypes = config(sprintf('firefly.expected_source_types.source.%s', $recurrence->transactionType->type));
|
||||
$destTypes = config(sprintf('firefly.expected_source_types.destination.%s', $recurrence->transactionType->type));
|
||||
$source = $this->findAccount($sourceTypes, $array['source_id'], $array['source_name']);
|
||||
$destination = $this->findAccount($destTypes, $array['destination_id'], $array['destination_name']);
|
||||
|
||||
/** @var TransactionCurrencyFactory $factory */
|
||||
$factory = app(TransactionCurrencyFactory::class);
|
||||
@@ -101,6 +143,20 @@ trait RecurringTransactionTrait
|
||||
if (null === $currency) {
|
||||
$currency = app('amount')->getDefaultCurrencyByUser($recurrence->user);
|
||||
}
|
||||
|
||||
// once the accounts have been determined, we still verify their validity:
|
||||
/** @var AccountValidator $validator */
|
||||
$validator = app(AccountValidator::class);
|
||||
$validator->setUser($recurrence->user);
|
||||
$validator->setTransactionType($recurrence->transactionType->type);
|
||||
if (!$validator->validateSource($source->id, null)) {
|
||||
throw new FireflyException(sprintf('Source invalid: %s', $validator->sourceError)); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
if (!$validator->validateDestination($destination->id, null)) {
|
||||
throw new FireflyException(sprintf('Destination invalid: %s', $validator->sourceError)); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$transaction = new RecurrenceTransaction(
|
||||
[
|
||||
'recurrence_id' => $recurrence->id,
|
||||
@@ -150,7 +206,7 @@ trait RecurringTransactionTrait
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
*/
|
||||
public function deleteRepetitions(Recurrence $recurrence): void
|
||||
protected function deleteRepetitions(Recurrence $recurrence): void
|
||||
{
|
||||
$recurrence->recurrenceRepetitions()->delete();
|
||||
}
|
||||
@@ -158,7 +214,7 @@ trait RecurringTransactionTrait
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
*/
|
||||
public function deleteTransactions(Recurrence $recurrence): void
|
||||
protected function deleteTransactions(Recurrence $recurrence): void
|
||||
{
|
||||
/** @var RecurrenceTransaction $transaction */
|
||||
foreach ($recurrence->recurrenceTransactions as $transaction) {
|
||||
@@ -171,23 +227,13 @@ trait RecurringTransactionTrait
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null|string $expectedType
|
||||
* @param Account|null $account
|
||||
* @param int|null $accountId
|
||||
* @param null|string $accountName
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
abstract public function findAccount(?string $expectedType, ?Account $account, ?int $accountId, ?string $accountName): ?Account;
|
||||
|
||||
/**
|
||||
* Update meta data for recurring transaction.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $data
|
||||
* @param array $data
|
||||
*/
|
||||
public function updateMetaData(Recurrence $recurrence, array $data): void
|
||||
protected function updateMetaData(Recurrence $recurrence, array $data): void
|
||||
{
|
||||
// only two special meta fields right now. Let's just hard code them.
|
||||
$piggyId = (int)($data['meta']['piggy_bank_id'] ?? 0.0);
|
||||
@@ -202,8 +248,8 @@ trait RecurringTransactionTrait
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param int $piggyId
|
||||
* @param string $piggyName
|
||||
* @param int $piggyId
|
||||
* @param string $piggyName
|
||||
*/
|
||||
protected function updatePiggyBank(Recurrence $recurrence, int $piggyId, string $piggyName): void
|
||||
{
|
||||
@@ -229,7 +275,7 @@ trait RecurringTransactionTrait
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $tags
|
||||
* @param array $tags
|
||||
*/
|
||||
protected function updateTags(Recurrence $recurrence, array $tags): void
|
||||
{
|
||||
|
@@ -43,129 +43,129 @@ use Log;
|
||||
trait TransactionServiceTrait
|
||||
{
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param string $direction
|
||||
*
|
||||
* @return string|null
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function accountType(TransactionJournal $journal, string $direction): ?string
|
||||
{
|
||||
$types = [];
|
||||
$type = $journal->transactionType->type;
|
||||
if (TransactionType::WITHDRAWAL === $type) {
|
||||
$types['source'] = AccountType::ASSET;
|
||||
$types['destination'] = AccountType::EXPENSE;
|
||||
}
|
||||
if (TransactionType::DEPOSIT === $type) {
|
||||
$types['source'] = AccountType::REVENUE;
|
||||
$types['destination'] = AccountType::ASSET;
|
||||
}
|
||||
if (TransactionType::TRANSFER === $type) {
|
||||
$types['source'] = AccountType::ASSET;
|
||||
$types['destination'] = AccountType::ASSET;
|
||||
}
|
||||
if (TransactionType::RECONCILIATION === $type) {
|
||||
$types['source'] = null;
|
||||
$types['destination'] = null;
|
||||
}
|
||||
// /**
|
||||
// * @param TransactionJournal $journal
|
||||
// * @param string $direction
|
||||
// *
|
||||
// * @return string|null
|
||||
// * @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
// */
|
||||
// public function accountType(TransactionJournal $journal, string $direction): ?string
|
||||
// {
|
||||
// $types = [];
|
||||
// $type = $journal->transactionType->type;
|
||||
// if (TransactionType::WITHDRAWAL === $type) {
|
||||
// $types['source'] = AccountType::ASSET;
|
||||
// $types['destination'] = AccountType::EXPENSE;
|
||||
// }
|
||||
// if (TransactionType::DEPOSIT === $type) {
|
||||
// $types['source'] = AccountType::REVENUE;
|
||||
// $types['destination'] = AccountType::ASSET;
|
||||
// }
|
||||
// if (TransactionType::TRANSFER === $type) {
|
||||
// $types['source'] = AccountType::ASSET;
|
||||
// $types['destination'] = AccountType::ASSET;
|
||||
// }
|
||||
// if (TransactionType::RECONCILIATION === $type) {
|
||||
// $types['source'] = null;
|
||||
// $types['destination'] = null;
|
||||
// }
|
||||
//
|
||||
// return $types[$direction] ?? null;
|
||||
// }
|
||||
|
||||
return $types[$direction] ?? null;
|
||||
}
|
||||
// /**
|
||||
// * @param string|null $expectedType
|
||||
// * @param Account|null $account
|
||||
// * @param int|null $accountId
|
||||
// * @param string|null $accountName
|
||||
// *
|
||||
// * @return Account|null
|
||||
// * @throws FireflyException
|
||||
// * @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
// */
|
||||
// public function findAccount(?string $expectedType, ?Account $account, ?int $accountId, ?string $accountName): ?Account
|
||||
// {
|
||||
// $result = null;
|
||||
//
|
||||
// if (null !== $account && $account->user_id === $this->user->id) {
|
||||
// return $account;
|
||||
// }
|
||||
//
|
||||
// $accountId = (int)$accountId;
|
||||
// $accountName = (string)$accountName;
|
||||
// $repository = app(AccountRepositoryInterface::class);
|
||||
// $repository->setUser($this->user);
|
||||
//
|
||||
// if (null === $expectedType) {
|
||||
// return $repository->findNull($accountId);
|
||||
// }
|
||||
//
|
||||
// if ($accountId > 0) {
|
||||
// // must be able to find it based on ID. Validator should catch invalid ID's.
|
||||
// return $repository->findNull($accountId);
|
||||
// }
|
||||
// if (AccountType::ASSET === $expectedType) {
|
||||
// return $repository->findByName($accountName, [AccountType::ASSET]);
|
||||
// }
|
||||
// // for revenue and expense:
|
||||
// if ('' !== $accountName) {
|
||||
// /** @var AccountFactory $factory */
|
||||
// $factory = app(AccountFactory::class);
|
||||
// $factory->setUser($this->user);
|
||||
//
|
||||
// return $factory->findOrCreate($accountName, $expectedType);
|
||||
// }
|
||||
//
|
||||
// return $repository->getCashAccount();
|
||||
// }
|
||||
|
||||
/**
|
||||
* @param string|null $expectedType
|
||||
* @param Account|null $account
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return Account|null
|
||||
* @throws FireflyException
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function findAccount(?string $expectedType, ?Account $account, ?int $accountId, ?string $accountName): ?Account
|
||||
{
|
||||
$result = null;
|
||||
|
||||
if (null !== $account && $account->user_id === $this->user->id) {
|
||||
return $account;
|
||||
}
|
||||
|
||||
$accountId = (int)$accountId;
|
||||
$accountName = (string)$accountName;
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($this->user);
|
||||
|
||||
if (null === $expectedType) {
|
||||
return $repository->findNull($accountId);
|
||||
}
|
||||
|
||||
if ($accountId > 0) {
|
||||
// must be able to find it based on ID. Validator should catch invalid ID's.
|
||||
return $repository->findNull($accountId);
|
||||
}
|
||||
if (AccountType::ASSET === $expectedType) {
|
||||
return $repository->findByName($accountName, [AccountType::ASSET]);
|
||||
}
|
||||
// for revenue and expense:
|
||||
if ('' !== $accountName) {
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($this->user);
|
||||
|
||||
return $factory->findOrCreate($accountName, $expectedType);
|
||||
}
|
||||
|
||||
return $repository->getCashAccount();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $currencyId
|
||||
* @param null|string $currencyCode
|
||||
*
|
||||
* @return TransactionCurrency|null
|
||||
*/
|
||||
protected function findCurrency(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
|
||||
{
|
||||
$factory = app(TransactionCurrencyFactory::class);
|
||||
|
||||
return $factory->find($currencyId, $currencyCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param string|null $amount
|
||||
*/
|
||||
protected function setForeignAmount(Transaction $transaction, ?string $amount): void
|
||||
{
|
||||
$amount = '' === (string)$amount ? null : $amount;
|
||||
$transaction->foreign_amount = $amount;
|
||||
$transaction->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param TransactionCurrency|null $currency
|
||||
*/
|
||||
protected function setForeignCurrency(Transaction $transaction, ?TransactionCurrency $currency): void
|
||||
{
|
||||
if (null === $currency) {
|
||||
$transaction->foreign_currency_id = null;
|
||||
$transaction->save();
|
||||
|
||||
return;
|
||||
}
|
||||
// enable currency if not enabled:
|
||||
if (false === $currency->enabled) {
|
||||
$currency->enabled = true;
|
||||
$currency->save();
|
||||
}
|
||||
|
||||
$transaction->foreign_currency_id = $currency->id;
|
||||
$transaction->save();
|
||||
|
||||
}
|
||||
// /**
|
||||
// * @param int|null $currencyId
|
||||
// * @param null|string $currencyCode
|
||||
// *
|
||||
// * @return TransactionCurrency|null
|
||||
// */
|
||||
// protected function findCurrency(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
|
||||
// {
|
||||
// $factory = app(TransactionCurrencyFactory::class);
|
||||
//
|
||||
// return $factory->find($currencyId, $currencyCode);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @param Transaction $transaction
|
||||
// * @param string|null $amount
|
||||
// */
|
||||
// protected function setForeignAmount(Transaction $transaction, ?string $amount): void
|
||||
// {
|
||||
// $amount = '' === (string)$amount ? null : $amount;
|
||||
// $transaction->foreign_amount = $amount;
|
||||
// $transaction->save();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @param Transaction $transaction
|
||||
// * @param TransactionCurrency|null $currency
|
||||
// */
|
||||
// protected function setForeignCurrency(Transaction $transaction, ?TransactionCurrency $currency): void
|
||||
// {
|
||||
// if (null === $currency) {
|
||||
// $transaction->foreign_currency_id = null;
|
||||
// $transaction->save();
|
||||
//
|
||||
// return;
|
||||
// }
|
||||
// // enable currency if not enabled:
|
||||
// if (false === $currency->enabled) {
|
||||
// $currency->enabled = true;
|
||||
// $currency->save();
|
||||
// }
|
||||
//
|
||||
// $transaction->foreign_currency_id = $currency->id;
|
||||
// $transaction->save();
|
||||
//
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user