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

@ -13,34 +13,34 @@ use Watson\Validating\ValidatingTrait;
* *
* @package FireflyIII\Models * @package FireflyIII\Models
* @SuppressWarnings (PHPMD.TooManyMethods) * @SuppressWarnings (PHPMD.TooManyMethods)
* @property integer $id * @property integer $id
* @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at * @property \Carbon\Carbon $updated_at
* @property \Carbon\Carbon $deleted_at * @property \Carbon\Carbon $deleted_at
* @property integer $user_id * @property integer $user_id
* @property integer $transaction_type_id * @property integer $transaction_type_id
* @property integer $bill_id * @property integer $bill_id
* @property integer $transaction_currency_id * @property integer $transaction_currency_id
* @property string $description * @property string $description
* @property boolean $completed * @property boolean $completed
* @property \Carbon\Carbon $date * @property \Carbon\Carbon $date
* @property boolean $encrypted * @property boolean $encrypted
* @property integer $order * @property integer $order
* @property-read \FireflyIII\Models\Bill $bill * @property-read \FireflyIII\Models\Bill $bill
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Budget[] $budgets * @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Category[] $categories * @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Category[] $categories
* @property-read mixed $actual_amount * @property-read mixed $actual_amount
* @property-read mixed $amount * @property-read mixed $amount
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Tag[] $tags * @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Tag[] $tags
* @property-read mixed $asset_account * @property-read mixed $asset_account
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Transaction[] $transactions * @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Transaction[] $transactions
* @property-read mixed $corrected_actual_amount * @property-read mixed $corrected_actual_amount
* @property-read mixed $destination_account * @property-read mixed $destination_account
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\PiggyBankEvent[] $piggyBankEvents * @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\PiggyBankEvent[] $piggyBankEvents
* @property-read \FireflyIII\Models\TransactionCurrency $transactionCurrency * @property-read \FireflyIII\Models\TransactionCurrency $transactionCurrency
* @property-read \FireflyIII\Models\TransactionType $transactionType * @property-read \FireflyIII\Models\TransactionType $transactionType
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\TransactionGroup[] $transactiongroups * @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\TransactionGroup[] $transactiongroups
* @property-read \FireflyIII\User $user * @property-read \FireflyIII\User $user
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereId($value) * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereCreatedAt($value) * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereUpdatedAt($value) * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereUpdatedAt($value)
@ -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,27 +265,8 @@ 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);
$count = 0;
/*
* 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;
foreach ($matches as $word) { foreach ($matches as $word) {
if (!(strpos($description, strtolower($word)) === false)) { if (!(strpos($description, strtolower($word)) === false)) {
$count++; $count++;
@ -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) { $amountMatch = true;
$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;
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();