Code cleanup.

This commit is contained in:
James Cole 2015-05-26 12:19:11 +02:00
parent 3af0dd2e3b
commit 2d2f18e538
22 changed files with 285 additions and 298 deletions

View File

@ -60,6 +60,7 @@ use Watson\Validating\ValidatingTrait;
* @method static \FireflyIII\Models\TransactionJournal onDate($date) * @method static \FireflyIII\Models\TransactionJournal onDate($date)
* @method static \FireflyIII\Models\TransactionJournal transactionTypes($types) * @method static \FireflyIII\Models\TransactionJournal transactionTypes($types)
* @method static \FireflyIII\Models\TransactionJournal withRelevantData() * @method static \FireflyIII\Models\TransactionJournal withRelevantData()
* @property-read mixed $expense_account
*/ */
class TransactionJournal extends Model class TransactionJournal extends Model
{ {
@ -196,22 +197,21 @@ class TransactionJournal extends Model
*/ */
public function getAssetAccountAttribute() public function getAssetAccountAttribute()
{ {
$positive = true; // the asset account is in the transaction with the positive amount. // if it's a deposit, it's the one thats positive
if ($this->transactionType->type === 'Withdrawal') { // if it's a withdrawal, it's the one thats negative
$positive = false; // otherwise, it's either (return first one):
}
/** @var Transaction $transaction */ switch ($this->transactionType->type) {
foreach ($this->transactions()->get() as $transaction) { case 'Deposit':
if (floatval($transaction->amount) > 0 && $positive === true) { return $this->transactions()->where('amount', '>', 0)->first()->account;
return $transaction->account; break;
} case 'Withdrawal':
if (floatval($transaction->amount) < 0 && $positive === false) { return $this->transactions()->where('amount', '<', 0)->first()->account;
return $transaction->account;
}
} }
return $this->transactions()->first()->account; return $this->transactions()->first()->account;
} }
/** /**
@ -285,6 +285,28 @@ class TransactionJournal extends Model
return $this->transactions()->first()->account; return $this->transactions()->first()->account;
} }
/**
* @return Account
*/
public function getExpenseAccountAttribute()
{
// if it's a deposit, it's the one thats negative
// if it's a withdrawal, it's the one thats positive
// otherwise, it's either (return first one):
switch ($this->transactionType->type) {
case 'Deposit':
return $this->transactions()->where('amount', '<', 0)->first()->account;
break;
case 'Withdrawal':
return $this->transactions()->where('amount', '>', 0)->first()->account;
}
return $this->transactions()->first()->account;
}
/** /**
* @codeCoverageIgnore * @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return \Illuminate\Database\Eloquent\Relations\HasMany

View File

@ -5,13 +5,10 @@ namespace FireflyIII\Repositories\Bill;
use Auth; use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use DB; use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log;
use Navigation; use Navigation;
/** /**
@ -268,26 +265,7 @@ class BillRepository implements BillRepositoryInterface
$amountMatch = false; $amountMatch = false;
$wordMatch = false; $wordMatch = false;
$matches = explode(',', $bill->match); $matches = explode(',', $bill->match);
$description = strtolower($journal->description); $description = strtolower($journal->description) . ' ' . strtolower($journal->expense_account->name);
/*
* Attach expense account to description for more narrow matching.
*/
$transactions = $journal->transactions()->get();
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
/** @var Account $account */
$account = $transaction->account()->first();
/** @var AccountType $type */
$type = $account->accountType()->first();
if ($type->type == 'Expense account' || $type->type == 'Beneficiary account') {
$description .= ' ' . strtolower($account->name);
}
}
Log::debug('Final description: ' . $description);
Log::debug('Matches searched: ' . join(':', $matches));
$count = 0; $count = 0;
foreach ($matches as $word) { foreach ($matches as $word) {
if (!(strpos($description, strtolower($word)) === false)) { if (!(strpos($description, strtolower($word)) === false)) {
@ -296,37 +274,24 @@ class BillRepository implements BillRepositoryInterface
} }
if ($count >= count($matches)) { if ($count >= count($matches)) {
$wordMatch = true; $wordMatch = true;
Log::debug('word match is true');
} else {
Log::debug('Count: ' . $count . ', count(matches): ' . count($matches));
} }
/* /*
* Match amount. * Match amount.
*/ */
if ($journal->amount >= $bill->amount_min && $journal->amount <= $bill->amount_max) {
if (count($transactions) > 1) {
$amount = max(floatval($transactions[0]->amount), floatval($transactions[1]->amount));
$min = floatval($bill->amount_min);
$max = floatval($bill->amount_max);
if ($amount >= $min && $amount <= $max) {
$amountMatch = true; $amountMatch = true;
Log::debug('Amount match is true!');
} }
}
/* /*
* If both, update! * If both, update!
*/ */
if ($wordMatch && $amountMatch) { if ($wordMatch && $amountMatch) {
Log::debug('TOTAL match is true!');
$journal->bill()->associate($bill); $journal->bill()->associate($bill);
$journal->save(); $journal->save();
} else { } else {
if ((!$wordMatch || !$amountMatch) && $bill->id == $journal->bill_id) { if ($bill->id == $journal->bill_id) {
// if no match, but bill used to match, remove it: // if no match, but bill used to match, remove it:
$journal->bill_id = null; $journal->bill_id = null;
$journal->save(); $journal->save();