mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-06 14:12:15 +00:00
Code that allows transaction reconciliation. #736
This commit is contained in:
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Repositories\Journal;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\User;
|
||||
@@ -123,6 +124,38 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
return $journal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return Transaction|null
|
||||
*/
|
||||
public function findOpposingTransaction(Transaction $transaction): ?Transaction
|
||||
{
|
||||
$opposing = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->where('transaction_journals.user_id', $this->user->id)
|
||||
->where('transactions.transaction_journal_id', $transaction->transaction_journal_id)
|
||||
->where('transactions.identifier', $transaction->identifier)
|
||||
->where('amount', bcmul($transaction->amount, '-1'))
|
||||
->first(['transactions.*']);
|
||||
|
||||
return $opposing;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $transactionid
|
||||
*
|
||||
* @return Transaction|null
|
||||
*/
|
||||
public function findTransaction(int $transactionid): ?Transaction
|
||||
{
|
||||
$transaction = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->where('transaction_journals.user_id', $this->user->id)
|
||||
->where('transactions.id', $transactionid)
|
||||
->first(['transactions.*']);
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get users first transaction journal
|
||||
*
|
||||
@@ -158,6 +191,32 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
return $journal->transactionType->type === TransactionType::TRANSFER;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function reconcile(Transaction $transaction): bool
|
||||
{
|
||||
Log::debug(sprintf('Going to reconcile transaction #%d', $transaction->id));
|
||||
$opposing = $this->findOpposingTransaction($transaction);
|
||||
|
||||
if (is_null($opposing)) {
|
||||
Log::debug('Opposing transaction is NULL. Cannot reconcile.');
|
||||
|
||||
return false;
|
||||
}
|
||||
Log::debug(sprintf('Opposing transaction ID is #%d', $opposing->id));
|
||||
|
||||
$transaction->reconciled = true;
|
||||
$opposing->reconciled = true;
|
||||
$transaction->save();
|
||||
$opposing->save();
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param int $order
|
||||
@@ -303,7 +362,7 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
}
|
||||
|
||||
// update note:
|
||||
if (isset($data['notes']) && !is_null($data['notes']) ) {
|
||||
if (isset($data['notes']) && !is_null($data['notes'])) {
|
||||
$this->updateNote($journal, strval($data['notes']));
|
||||
}
|
||||
|
||||
@@ -345,7 +404,7 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
$journal->budgets()->detach();
|
||||
|
||||
// update note:
|
||||
if (isset($data['notes']) && !is_null($data['notes']) ) {
|
||||
if (isset($data['notes']) && !is_null($data['notes'])) {
|
||||
$this->updateNote($journal, strval($data['notes']));
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Repositories\Journal;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\User;
|
||||
@@ -73,6 +74,20 @@ interface JournalRepositoryInterface
|
||||
*/
|
||||
public function find(int $journalId): TransactionJournal;
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return Transaction|null
|
||||
*/
|
||||
public function findOpposingTransaction(Transaction $transaction): ?Transaction;
|
||||
|
||||
/**
|
||||
* @param int $transactionid
|
||||
*
|
||||
* @return Transaction|null
|
||||
*/
|
||||
public function findTransaction(int $transactionid): ?Transaction;
|
||||
|
||||
/**
|
||||
* Get users very first transaction journal
|
||||
*
|
||||
@@ -92,6 +107,13 @@ interface JournalRepositoryInterface
|
||||
*/
|
||||
public function isTransfer(TransactionJournal $journal): bool;
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function reconcile(Transaction $transaction): bool;
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param int $order
|
||||
|
||||
@@ -28,6 +28,7 @@ use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\PiggyBankEvent;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Support\SingleCacheProperties;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
@@ -74,6 +75,13 @@ class JournalTasker implements JournalTaskerInterface
|
||||
*/
|
||||
public function getTransactionsOverview(TransactionJournal $journal): array
|
||||
{
|
||||
$cache = new SingleCacheProperties;
|
||||
$cache->addProperty('transaction-overview');
|
||||
$cache->addProperty($journal->id);
|
||||
$cache->addProperty($journal->updated_at);
|
||||
if ($cache->has()) {
|
||||
return $cache->get();
|
||||
}
|
||||
// get all transaction data + the opposite site in one list.
|
||||
$set = $journal
|
||||
->transactions()// "source"
|
||||
@@ -136,6 +144,7 @@ class JournalTasker implements JournalTaskerInterface
|
||||
'journal_type' => $transactionType,
|
||||
'updated_at' => $journal->updated_at,
|
||||
'source_id' => $entry->id,
|
||||
'source' => $journal->transactions()->find($entry->id),
|
||||
'source_amount' => $entry->amount,
|
||||
'foreign_source_amount' => $entry->foreign_amount,
|
||||
'description' => $entry->description,
|
||||
@@ -174,6 +183,7 @@ class JournalTasker implements JournalTaskerInterface
|
||||
|
||||
$transactions[] = $transaction;
|
||||
}
|
||||
$cache->store($transactions);
|
||||
|
||||
return $transactions;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user