mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Code reordering and reformatting. I should really start employing style CI.
This commit is contained in:
@@ -101,7 +101,7 @@ trait AccountServiceTrait
|
||||
/**
|
||||
* Update meta data for account. Depends on type which fields are valid.
|
||||
*
|
||||
* See reference nr. 97
|
||||
* See reference nr. 97
|
||||
*
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
@@ -207,22 +207,95 @@ trait AccountServiceTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete TransactionGroup with opening balance in it.
|
||||
*
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
* @deprecated
|
||||
*/
|
||||
protected function deleteOBGroup(Account $account): void
|
||||
protected function createOBGroup(Account $account, array $data): TransactionGroup
|
||||
{
|
||||
Log::debug(sprintf('deleteOB() for account #%d', $account->id));
|
||||
$openingBalanceGroup = $this->getOBGroup($account);
|
||||
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 = array_key_exists('opening_balance', $data) ? $data['opening_balance'] : '0';
|
||||
|
||||
// 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);
|
||||
// amount is positive.
|
||||
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));
|
||||
$sourceName = trans('firefly.initial_balance_description', ['account' => $account->name], $language);
|
||||
$destId = $account->id;
|
||||
}
|
||||
// amount is not positive
|
||||
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));
|
||||
$destName = trans('firefly.initial_balance_account', ['account' => $account->name], $language);
|
||||
$sourceId = $account->id;
|
||||
}
|
||||
// amount is 0
|
||||
if (0 === bccomp($amount, '0')) {
|
||||
Log::debug('Amount is zero, so will not make an OB group.');
|
||||
throw new FireflyException('Amount for new opening balance was unexpectedly 0.');
|
||||
}
|
||||
|
||||
// make amount positive, regardless:
|
||||
$amount = app('steam')->positive($amount);
|
||||
|
||||
// get or grab currency:
|
||||
$currency = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = app('default')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
// submit to factory:
|
||||
$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,
|
||||
'currency_id' => $currency->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 in createOBGroup', $submission);
|
||||
|
||||
/** @var TransactionGroupFactory $factory */
|
||||
$factory = app(TransactionGroupFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
|
||||
try {
|
||||
$group = $factory->create($submission);
|
||||
} catch (DuplicateTransactionException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
throw new FireflyException($e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -243,18 +316,6 @@ trait AccountServiceTrait
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the credit transaction group, or NULL if it does not exist.
|
||||
*
|
||||
@@ -269,6 +330,37 @@ trait AccountServiceTrait
|
||||
return $this->accountRepository->getCreditTransactionGroup($account);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $currencyId
|
||||
* @param string $currencyCode
|
||||
@@ -293,6 +385,132 @@ trait AccountServiceTrait
|
||||
return $currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the opposing "credit liability" transaction for credit liabilities.
|
||||
*
|
||||
* @param Account $account
|
||||
* @param string $openingBalance
|
||||
* @param Carbon $openingBalanceDate
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function updateCreditTransaction(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
|
||||
if (0 === bccomp($openingBalance, '0')) {
|
||||
Log::debug('Amount is zero, so will not update liability credit group.');
|
||||
throw new FireflyException('Amount for update liability credit was unexpectedly 0.');
|
||||
}
|
||||
|
||||
// create if not exists:
|
||||
$clGroup = $this->getCreditTransaction($account);
|
||||
if (null === $clGroup) {
|
||||
return $this->createCreditTransaction($account, $openingBalance, $openingBalanceDate);
|
||||
}
|
||||
// if exists, update:
|
||||
$currency = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = app('default')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
// simply grab the first journal and change it:
|
||||
$journal = $this->getObJournal($clGroup);
|
||||
$clTransaction = $this->getOBTransaction($journal, $account);
|
||||
$accountTransaction = $this->getNotOBTransaction($journal, $account);
|
||||
$journal->date = $openingBalanceDate;
|
||||
$journal->transactionCurrency()->associate($currency);
|
||||
|
||||
// account always gains money:
|
||||
$accountTransaction->amount = app('steam')->positive($openingBalance);
|
||||
$accountTransaction->transaction_currency_id = $currency->id;
|
||||
|
||||
// CL account always loses money:
|
||||
$clTransaction->amount = app('steam')->negative($openingBalance);
|
||||
$clTransaction->transaction_currency_id = $currency->id;
|
||||
// save both
|
||||
$accountTransaction->save();
|
||||
$clTransaction->save();
|
||||
$journal->save();
|
||||
$clGroup->refresh();
|
||||
|
||||
return $clGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $openingBalance
|
||||
* @param Carbon $openingBalanceDate
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function createCreditTransaction(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
Log::debug('Now going to create an createCreditTransaction.');
|
||||
|
||||
if (0 === bccomp($openingBalance, '0')) {
|
||||
Log::debug('Amount is zero, so will not make an liability credit group.');
|
||||
throw new FireflyException('Amount for new liability credit was unexpectedly 0.');
|
||||
}
|
||||
|
||||
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
|
||||
$amount = app('steam')->positive($openingBalance);
|
||||
|
||||
// get or grab currency:
|
||||
$currency = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = app('default')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
// submit to factory:
|
||||
$submission = [
|
||||
'group_title' => null,
|
||||
'user' => $account->user_id,
|
||||
'transactions' => [
|
||||
[
|
||||
'type' => 'Liability credit',
|
||||
'date' => $openingBalanceDate,
|
||||
'source_id' => null,
|
||||
'source_name' => trans('firefly.liability_credit_description', ['account' => $account->name], $language),
|
||||
'destination_id' => $account->id,
|
||||
'destination_name' => null,
|
||||
'user' => $account->user_id,
|
||||
'currency_id' => $currency->id,
|
||||
'order' => 0,
|
||||
'amount' => $amount,
|
||||
'foreign_amount' => null,
|
||||
'description' => trans('firefly.liability_credit_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 in createCreditTransaction', $submission);
|
||||
|
||||
/** @var TransactionGroupFactory $factory */
|
||||
$factory = app(TransactionGroupFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
|
||||
try {
|
||||
$group = $factory->create($submission);
|
||||
} catch (DuplicateTransactionException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
throw new FireflyException($e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update or create the opening balance group.
|
||||
* Since opening balance and date can still be empty strings, it may fail.
|
||||
@@ -355,97 +573,6 @@ trait AccountServiceTrait
|
||||
return $obGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the opposing "credit liability" transaction for credit liabilities.
|
||||
*
|
||||
* @param Account $account
|
||||
* @param string $openingBalance
|
||||
* @param Carbon $openingBalanceDate
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function updateCreditTransaction(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
|
||||
if (0 === bccomp($openingBalance, '0')) {
|
||||
Log::debug('Amount is zero, so will not update liability credit group.');
|
||||
throw new FireflyException('Amount for update liability credit was unexpectedly 0.');
|
||||
}
|
||||
|
||||
// create if not exists:
|
||||
$clGroup = $this->getCreditTransaction($account);
|
||||
if (null === $clGroup) {
|
||||
return $this->createCreditTransaction($account, $openingBalance, $openingBalanceDate);
|
||||
}
|
||||
// if exists, update:
|
||||
$currency = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = app('default')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
// simply grab the first journal and change it:
|
||||
$journal = $this->getObJournal($clGroup);
|
||||
$clTransaction = $this->getOBTransaction($journal, $account);
|
||||
$accountTransaction = $this->getNotOBTransaction($journal, $account);
|
||||
$journal->date = $openingBalanceDate;
|
||||
$journal->transactionCurrency()->associate($currency);
|
||||
|
||||
// account always gains money:
|
||||
$accountTransaction->amount = app('steam')->positive($openingBalance);
|
||||
$accountTransaction->transaction_currency_id = $currency->id;
|
||||
|
||||
// CL account always loses money:
|
||||
$clTransaction->amount = app('steam')->negative($openingBalance);
|
||||
$clTransaction->transaction_currency_id = $currency->id;
|
||||
// save both
|
||||
$accountTransaction->save();
|
||||
$clTransaction->save();
|
||||
$journal->save();
|
||||
$clGroup->refresh();
|
||||
|
||||
return $clGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* See reference nr. 98
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param Account $account
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getOBTransaction(TransactionJournal $journal, Account $account): Transaction
|
||||
{
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $journal->transactions()->where('account_id', '!=', $account->id)->first();
|
||||
if (null === $transaction) {
|
||||
throw new FireflyException(sprintf('Could not get OB transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param Account $account
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getNotOBTransaction(TransactionJournal $journal, Account $account): Transaction
|
||||
{
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $journal->transactions()->where('account_id', $account->id)->first();
|
||||
if (null === $transaction) {
|
||||
throw new FireflyException(sprintf('Could not get non-OB transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $openingBalance
|
||||
@@ -538,173 +665,7 @@ trait AccountServiceTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $openingBalance
|
||||
* @param Carbon $openingBalanceDate
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function createCreditTransaction(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
Log::debug('Now going to create an createCreditTransaction.');
|
||||
|
||||
if (0 === bccomp($openingBalance, '0')) {
|
||||
Log::debug('Amount is zero, so will not make an liability credit group.');
|
||||
throw new FireflyException('Amount for new liability credit was unexpectedly 0.');
|
||||
}
|
||||
|
||||
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
|
||||
$amount = app('steam')->positive($openingBalance);
|
||||
|
||||
// get or grab currency:
|
||||
$currency = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = app('default')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
// submit to factory:
|
||||
$submission = [
|
||||
'group_title' => null,
|
||||
'user' => $account->user_id,
|
||||
'transactions' => [
|
||||
[
|
||||
'type' => 'Liability credit',
|
||||
'date' => $openingBalanceDate,
|
||||
'source_id' => null,
|
||||
'source_name' => trans('firefly.liability_credit_description', ['account' => $account->name], $language),
|
||||
'destination_id' => $account->id,
|
||||
'destination_name' => null,
|
||||
'user' => $account->user_id,
|
||||
'currency_id' => $currency->id,
|
||||
'order' => 0,
|
||||
'amount' => $amount,
|
||||
'foreign_amount' => null,
|
||||
'description' => trans('firefly.liability_credit_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 in createCreditTransaction', $submission);
|
||||
|
||||
/** @var TransactionGroupFactory $factory */
|
||||
$factory = app(TransactionGroupFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
|
||||
try {
|
||||
$group = $factory->create($submission);
|
||||
} catch (DuplicateTransactionException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
throw new FireflyException($e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
* @deprecated
|
||||
*/
|
||||
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 = array_key_exists('opening_balance', $data) ? $data['opening_balance'] : '0';
|
||||
|
||||
// amount is positive.
|
||||
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));
|
||||
$sourceName = trans('firefly.initial_balance_description', ['account' => $account->name], $language);
|
||||
$destId = $account->id;
|
||||
}
|
||||
// amount is not positive
|
||||
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));
|
||||
$destName = trans('firefly.initial_balance_account', ['account' => $account->name], $language);
|
||||
$sourceId = $account->id;
|
||||
}
|
||||
// amount is 0
|
||||
if (0 === bccomp($amount, '0')) {
|
||||
Log::debug('Amount is zero, so will not make an OB group.');
|
||||
throw new FireflyException('Amount for new opening balance was unexpectedly 0.');
|
||||
}
|
||||
|
||||
// make amount positive, regardless:
|
||||
$amount = app('steam')->positive($amount);
|
||||
|
||||
// get or grab currency:
|
||||
$currency = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = app('default')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
// submit to factory:
|
||||
$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,
|
||||
'currency_id' => $currency->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 in createOBGroup', $submission);
|
||||
|
||||
/** @var TransactionGroupFactory $factory */
|
||||
$factory = app(TransactionGroupFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
|
||||
try {
|
||||
$group = $factory->create($submission);
|
||||
} catch (DuplicateTransactionException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
throw new FireflyException($e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* See reference nr. 99
|
||||
* See reference nr. 99
|
||||
*
|
||||
* @param TransactionGroup $group
|
||||
*
|
||||
@@ -721,4 +682,42 @@ trait AccountServiceTrait
|
||||
|
||||
return $journal;
|
||||
}
|
||||
|
||||
/**
|
||||
* See reference nr. 98
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param Account $account
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getOBTransaction(TransactionJournal $journal, Account $account): Transaction
|
||||
{
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $journal->transactions()->where('account_id', '!=', $account->id)->first();
|
||||
if (null === $transaction) {
|
||||
throw new FireflyException(sprintf('Could not get OB transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param Account $account
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getNotOBTransaction(TransactionJournal $journal, Account $account): Transaction
|
||||
{
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $journal->transactions()->where('account_id', $account->id)->first();
|
||||
if (null === $transaction) {
|
||||
throw new FireflyException(sprintf('Could not get non-OB transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
}
|
||||
|
@@ -48,9 +48,9 @@ trait BillServiceTrait
|
||||
return;
|
||||
}
|
||||
$ruleIds = $bill->user->rules()->get(['id'])->pluck('id')->toArray();
|
||||
$set = RuleAction::whereIn('rule_id', $ruleIds)
|
||||
->where('action_type', 'link_to_bill')
|
||||
->where('action_value', $oldName)->get();
|
||||
$set = RuleAction::whereIn('rule_id', $ruleIds)
|
||||
->where('action_type', 'link_to_bill')
|
||||
->where('action_value', $oldName)->get();
|
||||
|
||||
/** @var RuleAction $ruleAction */
|
||||
foreach ($set as $ruleAction) {
|
||||
@@ -59,6 +59,7 @@ trait BillServiceTrait
|
||||
$ruleAction->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Bill $bill
|
||||
* @param string $note
|
||||
|
@@ -40,8 +40,8 @@ class CreditRecalculateService
|
||||
{
|
||||
private ?Account $account;
|
||||
private ?TransactionGroup $group;
|
||||
private array $work;
|
||||
private AccountRepositoryInterface $repository;
|
||||
private array $work;
|
||||
|
||||
/**
|
||||
* CreditRecalculateService constructor.
|
||||
@@ -79,50 +79,6 @@ class CreditRecalculateService
|
||||
$this->processWork();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function processWork(): void
|
||||
{
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
foreach ($this->work as $account) {
|
||||
$this->processWorkAccount($account);
|
||||
}
|
||||
Log::debug(sprintf('Done with %s', __METHOD__));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*/
|
||||
private function processWorkAccount(Account $account): void
|
||||
{
|
||||
Log::debug(sprintf('Now in %s(#%d)', __METHOD__, $account->id));
|
||||
|
||||
// get opening balance (if present)
|
||||
$this->repository->setUser($account->user);
|
||||
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0';
|
||||
$leftOfDebt = app('steam')->positive($startOfDebt);
|
||||
|
||||
/** @var AccountMetaFactory $factory */
|
||||
$factory = app(AccountMetaFactory::class);
|
||||
$factory->crud($account, 'start_of_debt', $startOfDebt);
|
||||
|
||||
// get direction of liability:
|
||||
$direction = (string)$this->repository->getMetaValue($account, 'liability_direction');
|
||||
|
||||
// now loop all transactions (except opening balance and credit thing)
|
||||
$transactions = $account->transactions()->get();
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$leftOfDebt = $this->processTransaction($account, $direction, $transaction, $leftOfDebt);
|
||||
}
|
||||
$factory->crud($account, 'current_debt', $leftOfDebt);
|
||||
|
||||
|
||||
Log::debug(sprintf('Done with %s(#%d)', __METHOD__, $account->id));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -221,19 +177,46 @@ class CreditRecalculateService
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
*
|
||||
*/
|
||||
public function setAccount(?Account $account): void
|
||||
private function processWork(): void
|
||||
{
|
||||
$this->account = $account;
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
foreach ($this->work as $account) {
|
||||
$this->processWorkAccount($account);
|
||||
}
|
||||
Log::debug(sprintf('Done with %s', __METHOD__));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionGroup $group
|
||||
* @param Account $account
|
||||
*/
|
||||
public function setGroup(TransactionGroup $group): void
|
||||
private function processWorkAccount(Account $account): void
|
||||
{
|
||||
$this->group = $group;
|
||||
Log::debug(sprintf('Now in %s(#%d)', __METHOD__, $account->id));
|
||||
|
||||
// get opening balance (if present)
|
||||
$this->repository->setUser($account->user);
|
||||
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0';
|
||||
$leftOfDebt = app('steam')->positive($startOfDebt);
|
||||
|
||||
/** @var AccountMetaFactory $factory */
|
||||
$factory = app(AccountMetaFactory::class);
|
||||
$factory->crud($account, 'start_of_debt', $startOfDebt);
|
||||
|
||||
// get direction of liability:
|
||||
$direction = (string)$this->repository->getMetaValue($account, 'liability_direction');
|
||||
|
||||
// now loop all transactions (except opening balance and credit thing)
|
||||
$transactions = $account->transactions()->get();
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$leftOfDebt = $this->processTransaction($account, $direction, $transaction, $leftOfDebt);
|
||||
}
|
||||
$factory->crud($account, 'current_debt', $leftOfDebt);
|
||||
|
||||
|
||||
Log::debug(sprintf('Done with %s(#%d)', __METHOD__, $account->id));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -292,5 +275,21 @@ class CreditRecalculateService
|
||||
return $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
*/
|
||||
public function setAccount(?Account $account): void
|
||||
{
|
||||
$this->account = $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionGroup $group
|
||||
*/
|
||||
public function setGroup(TransactionGroup $group): void
|
||||
{
|
||||
$this->group = $group;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -96,7 +96,7 @@ trait JournalServiceTrait
|
||||
$search = null;
|
||||
// first attempt, find by ID.
|
||||
if (null !== $data['id']) {
|
||||
$search = $this->accountRepository->find((int) $data['id']);
|
||||
$search = $this->accountRepository->find((int)$data['id']);
|
||||
if (null !== $search && in_array($search->accountType->type, $types, true)) {
|
||||
Log::debug(
|
||||
sprintf('Found "account_id" object: #%d, "%s" of type %s', $search->id, $search->name, $search->accountType->type)
|
||||
@@ -174,10 +174,10 @@ trait JournalServiceTrait
|
||||
if (null === $account && null !== $data['number']) {
|
||||
Log::debug(sprintf('Searching for account number "%s".', $data['number']));
|
||||
// find by preferred type.
|
||||
$source = $this->accountRepository->findByAccountNumber((string) $data['number'], [$types[0]]);
|
||||
$source = $this->accountRepository->findByAccountNumber((string)$data['number'], [$types[0]]);
|
||||
|
||||
// or any expected type.
|
||||
$source = $source ?? $this->accountRepository->findByAccountNumber((string) $data['number'], $types);
|
||||
$source = $source ?? $this->accountRepository->findByAccountNumber((string)$data['number'], $types);
|
||||
|
||||
if (null !== $source) {
|
||||
Log::debug(sprintf('Found account: #%d, %s', $source->id, $source->name));
|
||||
@@ -214,7 +214,7 @@ trait JournalServiceTrait
|
||||
|
||||
// final attempt, create it.
|
||||
if (AccountType::ASSET === $preferredType) {
|
||||
throw new FireflyException(sprintf('TransactionFactory: Cannot create asset account with these values: %s',json_encode($data)));
|
||||
throw new FireflyException(sprintf('TransactionFactory: Cannot create asset account with these values: %s', json_encode($data)));
|
||||
}
|
||||
// fix name of account if only IBAN is given:
|
||||
if ('' === (string)$data['name'] && '' !== (string)$data['iban']) {
|
||||
@@ -227,7 +227,7 @@ trait JournalServiceTrait
|
||||
$data['name'] = $data['number'];
|
||||
}
|
||||
// if name is still NULL, return NULL.
|
||||
if(null === $data['name']) {
|
||||
if (null === $data['name']) {
|
||||
return null;
|
||||
}
|
||||
$data['name'] = $data['name'] ?? '(no name)';
|
||||
|
@@ -48,8 +48,8 @@ trait TransactionTypeTrait
|
||||
$factory = app(TransactionTypeFactory::class);
|
||||
$transactionType = $factory->find($type);
|
||||
if (null === $transactionType) {
|
||||
Log::error(sprintf('Could not find transaction type for "%s"', $type));
|
||||
throw new FireflyException(sprintf('Could not find transaction type for "%s"', $type));
|
||||
Log::error(sprintf('Could not find transaction type for "%s"', $type));
|
||||
throw new FireflyException(sprintf('Could not find transaction type for "%s"', $type));
|
||||
}
|
||||
|
||||
return $transactionType;
|
||||
|
Reference in New Issue
Block a user