mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Some new code.
This commit is contained in:
parent
db4adf399d
commit
bbab370b1e
@ -157,7 +157,7 @@ class TransactionController extends Controller
|
||||
}
|
||||
|
||||
$preFilled['amount'] = $journal->amount;
|
||||
$preFilled['account_id'] = $repository->getAssetAccount($journal);
|
||||
$preFilled['account_id'] = $journal->assetAccount->id;
|
||||
$preFilled['expense_account'] = $transactions[0]->account->name;
|
||||
$preFilled['revenue_account'] = $transactions[1]->account->name;
|
||||
$preFilled['account_from_id'] = $transactions[1]->account->id;
|
||||
|
@ -68,6 +68,34 @@ class TransactionJournal extends Model
|
||||
}
|
||||
}
|
||||
|
||||
public function getAssetAccountAttribute()
|
||||
{
|
||||
$positive = true; // the asset account is in the transaction with the positive amount.
|
||||
if ($this->transactionType->type === 'Withdrawal') {
|
||||
$positive = false;
|
||||
}
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($this->transactions()->get() as $transaction) {
|
||||
if (floatval($transaction->amount) > 0 && $positive === true) {
|
||||
return $transaction->account_id;
|
||||
}
|
||||
if (floatval($transaction->amount) < 0 && $positive === false) {
|
||||
return $transaction->account_id;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $this->transactions()->first()->account_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function transactions()
|
||||
{
|
||||
return $this->hasMany('FireflyIII\Models\Transaction');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
@ -201,6 +229,14 @@ class TransactionJournal extends Model
|
||||
$this->attributes['encrypted'] = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
public function tags()
|
||||
{
|
||||
return $this->belongsToMany('FireflyIII\Models\Tag');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
@ -225,14 +261,6 @@ class TransactionJournal extends Model
|
||||
return $this->belongsToMany('FireflyIII\Models\TransactionGroup');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function transactions()
|
||||
{
|
||||
return $this->hasMany('FireflyIII\Models\Transaction');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
@ -241,12 +269,4 @@ class TransactionJournal extends Model
|
||||
return $this->belongsTo('FireflyIII\User');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
public function tags()
|
||||
{
|
||||
return $this->belongsToMany('FireflyIII\Models\Tag');
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -34,36 +34,6 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
return Auth::user()->transactionjournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Get the account_id, which is the asset account that paid for the transaction.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getAssetAccount(TransactionJournal $journal)
|
||||
{
|
||||
$positive = true; // the asset account is in the transaction with the positive amount.
|
||||
switch ($journal->transactionType->type) {
|
||||
case 'Withdrawal':
|
||||
$positive = false;
|
||||
break;
|
||||
}
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($journal->transactions()->get() as $transaction) {
|
||||
if (floatval($transaction->amount) > 0 && $positive === true) {
|
||||
return $transaction->account_id;
|
||||
}
|
||||
if (floatval($transaction->amount) < 0 && $positive === false) {
|
||||
return $transaction->account_id;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $journal->transactions()->first()->account_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionType $dbType
|
||||
*
|
||||
@ -96,61 +66,12 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
*/
|
||||
public function saveTags(TransactionJournal $journal, array $array)
|
||||
{
|
||||
/** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
|
||||
$tagRepository = App::make('FireflyIII\Repositories\Tag\TagRepositoryInterface');
|
||||
|
||||
foreach ($array as $name) {
|
||||
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
|
||||
|
||||
if ($tag->tagMode == 'nothing') {
|
||||
// save it, no problem:
|
||||
$journal->tags()->save($tag);
|
||||
}
|
||||
if ($tag->tagMode == 'balancingAct') {
|
||||
// if Withdrawal, journals that are of type Withdrawal, max 1.
|
||||
$withdrawal = $this->getTransactionType('Withdrawal');
|
||||
$transfer = $this->getTransactionType('Transfer');
|
||||
|
||||
$withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
|
||||
$transfers = $tag->transactionjournals()->where('transaction_type_id', $transfer->id)->count();
|
||||
|
||||
// only if this is the only withdrawal.
|
||||
if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
|
||||
$journal->tags()->save($tag);
|
||||
}
|
||||
// and only if this is the only transfer
|
||||
if ($journal->transaction_type_id == $transfer->id && $transfers < 1) {
|
||||
$journal->tags()->save($tag);
|
||||
}
|
||||
|
||||
// ignore expense
|
||||
}
|
||||
|
||||
if ($tag->tagMode == 'advancePayment') {
|
||||
$withdrawal = $this->getTransactionType('Withdrawal');
|
||||
$deposit = $this->getTransactionType('Deposit');
|
||||
$withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
|
||||
|
||||
// only if this is the only withdrawal
|
||||
if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
|
||||
$journal->tags()->save($tag);
|
||||
}
|
||||
|
||||
// only if this is a deposit.
|
||||
if ($journal->transaction_type_id == $deposit->id) {
|
||||
|
||||
// if this is a deposit, account must match the current only journal
|
||||
// (if already present):
|
||||
$currentWithdrawal = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->first();
|
||||
if ($currentWithdrawal && $this->getAssetAccount($currentWithdrawal) == $this->getAssetAccount($journal)) {
|
||||
$journal->tags()->save($tag);
|
||||
} else {
|
||||
if (is_null($currentWithdrawal)) {
|
||||
$journal->tags()->save($tag);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
$tagRepository->connect($journal, $tag);
|
||||
}
|
||||
}
|
||||
|
||||
@ -287,6 +208,11 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
*/
|
||||
public function updateTags(TransactionJournal $journal, array $array)
|
||||
{
|
||||
// create tag repository
|
||||
/** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
|
||||
$tagRepository = App::make('FireflyIII\Repositories\Tag\TagRepositoryInterface');
|
||||
|
||||
|
||||
// find or create all tags:
|
||||
$tags = [];
|
||||
$ids = [];
|
||||
@ -304,50 +230,7 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
// connect each tag to journal (if not yet connected):
|
||||
/** @var Tag $tag */
|
||||
foreach ($tags as $tag) {
|
||||
if (!$journal->tags()->find($tag->id)) {
|
||||
if ($tag->tagMode == 'nothing') {
|
||||
// save it, no problem:
|
||||
$journal->tags()->save($tag);
|
||||
}
|
||||
if ($tag->tagMode == 'balancingAct') {
|
||||
// if Withdrawal, journals that are of type Withdrawal, max 1.
|
||||
$withdrawal = $this->getTransactionType('Withdrawal');
|
||||
$transfer = $this->getTransactionType('Transfer');
|
||||
|
||||
$withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
|
||||
$transfers = $tag->transactionjournals()->where('transaction_type_id', $transfer->id)->count();
|
||||
|
||||
// only if this is the only withdrawal.
|
||||
if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
|
||||
$journal->tags()->save($tag);
|
||||
}
|
||||
// and only if this is the only transfer
|
||||
if ($journal->transaction_type_id == $transfer->id && $transfers < 1) {
|
||||
$journal->tags()->save($tag);
|
||||
Log::debug('Saved tag! [' . $journal->transaction_type_id . ':' . $transfer->id . ':' . $transfers . ']');
|
||||
} else {
|
||||
Log::debug('Did not save tag. [' . $journal->transaction_type_id . ':' . $transfer->id . ':' . $transfers . ']');
|
||||
}
|
||||
|
||||
// ignore expense
|
||||
}
|
||||
|
||||
if ($tag->tagMode == 'advancePayment') {
|
||||
$withdrawal = $this->getTransactionType('Withdrawal');
|
||||
$deposit = $this->getTransactionType('Deposit');
|
||||
$withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
|
||||
|
||||
// only if this is the only withdrawal
|
||||
if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
|
||||
$journal->tags()->save($tag);
|
||||
}
|
||||
|
||||
// only if this is a deposit.
|
||||
if ($journal->transaction_type_id == $deposit->id) {
|
||||
$journal->tags()->save($tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
$tagRepository->connect($journal, $tag);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,23 +31,6 @@ interface JournalRepositoryInterface
|
||||
*/
|
||||
public function getAssetAccount(TransactionJournal $journal);
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $array
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updateTags(TransactionJournal $journal, array $array);
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $array
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function saveTags(TransactionJournal $journal, array $array);
|
||||
|
||||
|
||||
/**
|
||||
* @param TransactionType $dbType
|
||||
*
|
||||
@ -61,6 +44,22 @@ interface JournalRepositoryInterface
|
||||
* @return TransactionType
|
||||
*/
|
||||
public function getTransactionType($type);
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $array
|
||||
*
|
||||
* @return void
|
||||
|
||||
/**
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $array
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function saveTags(TransactionJournal $journal, array $array);
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
@ -75,4 +74,12 @@ interface JournalRepositoryInterface
|
||||
* @return mixed
|
||||
*/
|
||||
public function update(TransactionJournal $journal, array $data);
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $array
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function updateTags(TransactionJournal $journal, array $array);
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ namespace FireflyIII\Repositories\Tag;
|
||||
|
||||
use Auth;
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
@ -15,6 +17,97 @@ use Illuminate\Support\Collection;
|
||||
class TagRepository implements TagRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param Tag $tag
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function connect(TransactionJournal $journal, Tag $tag)
|
||||
{
|
||||
|
||||
/*
|
||||
* Already connected:
|
||||
*/
|
||||
if ($journal->tags()->find($tag->id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($tag->tagMode == 'nothing') {
|
||||
// save it, no problem:
|
||||
$journal->tags()->save($tag);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* get some withdrawal types:
|
||||
*/
|
||||
/** @var TransactionType $withdrawal */
|
||||
$withdrawal = TransactionType::whereType('Withdrawal')->first();
|
||||
/** @var TransactionType $deposit */
|
||||
$deposit = TransactionType::whereType('Deposit')->first();
|
||||
/** @var TransactionType $transfer */
|
||||
$transfer = TransactionType::whereType('Transfer')->first();
|
||||
|
||||
$withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
|
||||
$transfers = $tag->transactionjournals()->where('transaction_type_id', $transfer->id)->count();
|
||||
|
||||
if ($tag->tagMode == 'balancingAct') {
|
||||
|
||||
// only if this is the only withdrawal.
|
||||
if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
|
||||
$journal->tags()->save($tag);
|
||||
|
||||
return true;
|
||||
}
|
||||
// and only if this is the only transfer
|
||||
if ($journal->transaction_type_id == $transfer->id && $transfers < 1) {
|
||||
$journal->tags()->save($tag);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ignore expense
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($tag->tagMode == 'advancePayment') {
|
||||
|
||||
|
||||
// only if this is the only withdrawal
|
||||
if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
|
||||
$journal->tags()->save($tag);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// only if this is a deposit.
|
||||
if ($journal->transaction_type_id == $deposit->id) {
|
||||
|
||||
// if this is a deposit, account must match the current only journal
|
||||
// (if already present):
|
||||
$currentWithdrawal = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->first();
|
||||
|
||||
if ($currentWithdrawal && $currentWithdrawal->assetAccount->id == $journal->assetAccount->id) {
|
||||
$journal->tags()->save($tag);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
if (is_null($currentWithdrawal)) {
|
||||
$journal->tags()->save($tag);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tag $tag
|
||||
*
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace FireflyIII\Repositories\Tag;
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
|
||||
@ -38,4 +39,12 @@ interface TagRepositoryInterface {
|
||||
* @return boolean
|
||||
*/
|
||||
public function destroy(Tag $tag);
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param Tag $tag
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function connect(TransactionJournal $journal, Tag $tag);
|
||||
}
|
Loading…
Reference in New Issue
Block a user