Make sure the user can create liabilities in the "credit" direction with the right transactions.

This commit is contained in:
James Cole
2021-04-10 17:26:36 +02:00
parent 5d7ca1ef9a
commit 0426fa63d0
9 changed files with 636 additions and 149 deletions

View File

@@ -23,43 +23,115 @@ namespace FireflyIII\Services\Internal\Support;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\AccountMetaFactory;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Log;
class CreditRecalculateService
{
private TransactionGroup $group;
private ?Account $account;
private ?TransactionGroup $group;
private array $work;
private AccountRepositoryInterface $repository;
/**
* CreditRecalculateService constructor.
*/
public function __construct()
{
$this->group = null;
$this->account = null;
$this->work = [];
}
/**
*
*/
public function recalculate(): void
{
Log::debug(sprintf('Now in %s', __METHOD__));
if (true !== config('firefly.feature_flags.handle_debts')) {
Log::debug('handle_debts is disabled.');
return;
}
Log::error('TODO');
if (null !== $this->group && null === $this->account) {
$this->processGroup();
}
if (null !== $this->account && null === $this->group) {
// work based on account.
$this->processAccount();
}
if (0 === count($this->work)) {
Log::debug('No work accounts, do not do CreditRecalculationService');
return;
return;
}
$this->processWork();
Log::debug('Will now do CreditRecalculationService');
// do something
}
/**
*
*/
private function processWork(): void
{
$this->repository = app(AccountRepositoryInterface::class);
Log::debug(sprintf('Now in %s', __METHOD__));
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';
/** @var AccountMetaFactory $factory */
$factory = app(AccountMetaFactory::class);
$factory->crud($account, 'start_of_debt', $startOfDebt);
$factory->crud($account, 'current_debt', $startOfDebt);
// update meta data:
Log::debug(sprintf('Done with %s(#%d)', __METHOD__, $account->id));
}
/**
*
*/
private function processGroup(): void
{
Log::debug(sprintf('Now in %s', __METHOD__));
/** @var TransactionJournal $journal */
foreach ($this->group->transactionJournals as $journal) {
try {
$this->recalculateJournal($journal);
} catch (FireflyException $e) {
Log::error($e->getTraceAsString());
Log::error('Could not recalculate');
if (0 === count($this->work)) {
try {
$this->findByJournal($journal);
} catch (FireflyException $e) {
Log::error($e->getTraceAsString());
Log::error(sprintf('Could not find work account for transaction group #%d.', $this->group->id));
}
}
}
Log::debug(sprintf('Done with %s', __METHOD__));;
Log::debug(sprintf('Done with %s', __METHOD__));
}
/**
@@ -67,37 +139,22 @@ class CreditRecalculateService
*
* @throws FireflyException
*/
private function recalculateJournal(TransactionJournal $journal): void
private function findByJournal(TransactionJournal $journal): void
{
if (TransactionType::DEPOSIT !== $journal->transactionType->type) {
Log::debug('Journal is not a deposit.');
return;
}
Log::debug(sprintf('Now in %s', __METHOD__));
$source = $this->getSourceAccount($journal);
$destination = $this->getDestinationAccount($journal);
// destination must be liability, source must be expense.
if (AccountType::REVENUE !== $source->accountType->type) {
Log::debug('Source is not a revenue account.');
return;
// destination or source must be liability.
$valid = config('firefly.valid_liabilities');
if (in_array($destination->accountType->type, $valid)) {
Log::debug(sprintf('Dest account type is "%s", include it.', $destination->accountType->type));
$this->work[] = $destination;
}
if (!in_array($destination->accountType->type, config('firefly.valid_liabilities'))) {
Log::debug('Destination is not a liability.');
return;
if (in_array($source->accountType->type, $valid)) {
Log::debug(sprintf('Src account type is "%s", include it.', $source->accountType->type));
$this->work[] = $source;
}
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($destination->user);
$direction = $repository->getMetaValue($destination, 'liability_direction');
if ('credit' !== $direction) {
Log::debug(sprintf('Destination liabiltiy direction is "%s", do nothing.', $direction));
}
/*
* This destination is a liability and an incoming debt. The amount paid into the liability changes the original debt amount.
*
*/
Log::debug('Do something!');
}
/**
@@ -108,18 +165,7 @@ class CreditRecalculateService
*/
private function getSourceAccount(TransactionJournal $journal): Account
{
return $this->getAccount($journal, '<');
}
/**
* @param TransactionJournal $journal
*
* @return Account
* @throws FireflyException
*/
private function getDestinationAccount(TransactionJournal $journal): Account
{
return $this->getAccount($journal, '>');
return $this->getAccountByDirection($journal, '<');
}
/**
@@ -129,7 +175,7 @@ class CreditRecalculateService
* @return Account
* @throws FireflyException
*/
private function getAccount(TransactionJournal $journal, string $direction): Account
private function getAccountByDirection(TransactionJournal $journal, string $direction): Account
{
/** @var Transaction $transaction */
$transaction = $journal->transactions()->where('amount', $direction, '0')->first();
@@ -144,6 +190,38 @@ class CreditRecalculateService
return $account;
}
/**
* @param TransactionJournal $journal
*
* @return Account
* @throws FireflyException
*/
private function getDestinationAccount(TransactionJournal $journal): Account
{
return $this->getAccountByDirection($journal, '>');
}
/**
*
*/
private function processAccount(): void
{
Log::debug(sprintf('Now in %s', __METHOD__));
$valid = config('firefly.valid_liabilities');
if (in_array($this->account->accountType->type, $valid)) {
Log::debug(sprintf('Account type is "%s", include it.', $this->account->accountType->type));
$this->work[] = $this->account;
}
}
/**
* @param Account|null $account
*/
public function setAccount(?Account $account): void
{
$this->account = $account;
}
/**
* @param TransactionGroup $group
*/