Clean up method relying on old transaction journal code.

This commit is contained in:
James Cole
2016-03-02 20:11:28 +01:00
parent 5b949d6e00
commit 7234f011ec
19 changed files with 156 additions and 48 deletions

View File

@@ -80,7 +80,7 @@ class Entry
$entry = new self;
$entry->setDescription($journal->description);
$entry->setDate($journal->date->format('Y-m-d'));
$entry->setAmount($journal->amount); // TODO TransactionJournal cannot deliver "amount".
$entry->setAmount(TransactionJournal::amount($journal));
/** @var Budget $budget */
$budget = $journal->budgets->first();
@@ -102,14 +102,14 @@ class Entry
}
/** @var Account $sourceAccount */
$sourceAccount = $journal->source_account; // TODO TransactionJournal cannot deliver "source_account"
$sourceAccount = TransactionJournal::sourceAccount($journal);
$entry->setFromAccountId($sourceAccount->id);
$entry->setFromAccountName($sourceAccount->name);
$entry->setFromAccountIban($sourceAccount->iban);
$entry->setFromAccountType($sourceAccount->accountType->type);
/** @var Account $destination */
$destination = $journal->destination_account; // TODO TransactionJournal cannot deliver "destination_account"
$destination = TransactionJournal::destinationAccount($journal);
$entry->setToAccountId($destination->id);
$entry->setToAccountName($destination->name);
$entry->setToAccountIban($destination->iban);

View File

@@ -44,9 +44,9 @@ class ConnectJournalToPiggyBank
}
bcscale(2);
$amount = $journal->amount_positive; // TODO TransactionJournal cannot deliver "amount_positive".
$amount = TransactionJournal::amountPositive($journal);
// if piggy account matches source account, the amount is positive
if ($piggyBank->account_id == $journal->source_account->id) {// TODO TransactionJournal cannot deliver "source_account"
if ($piggyBank->account_id == TransactionJournal::sourceAccount($journal)->id) {
$amount = bcmul($amount, '-1');
}

View File

@@ -5,6 +5,7 @@ namespace FireflyIII\Handlers\Events;
use FireflyIII\Events\TransactionJournalUpdated;
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\PiggyBankRepetition;
use FireflyIII\Models\TransactionJournal;
/**
* Class UpdateJournalConnection
@@ -44,7 +45,7 @@ class UpdateJournalConnection
}
bcscale(2);
$amount = $journal->amount; // TODO TransactionJournal cannot deliver "amount".
$amount = TransactionJournal::amount($journal);
$diff = bcsub($amount, $event->amount); // update current repetition
$repetition->currentamount = bcadd($repetition->currentamount, $diff);

View File

@@ -19,7 +19,6 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
use Input;
use Log;
use Preferences;
use Response;
use Session;
@@ -141,10 +140,6 @@ class TransactionController extends Controller
*/
public function edit(ARI $repository, TransactionJournal $journal)
{
// get journal again:
/** @var TransactionJournal $journal */
$journal = TransactionJournal::expanded()->where('transaction_journals.id', $journal->id)->first(TransactionJournal::QUERYFIELDS);
// TODO REMOVE this in favour of something static in TransactionJournal.
// cannot edit opening balance
if ($journal->isOpeningBalance()) {
throw new FireflyException('Cannot edit this transaction (#' . $journal->id . '). Edit the account instead!');
@@ -154,11 +149,11 @@ class TransactionController extends Controller
$maxFileSize = Steam::phpBytes(ini_get('upload_max_filesize'));
$maxPostSize = Steam::phpBytes(ini_get('post_max_size'));
$uploadSize = min($maxFileSize, $maxPostSize);
$what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
$what = strtolower(TransactionJournal::transactionTypeStr($journal));
$accounts = ExpandedForm::makeSelectList($repository->getAccounts(['Default account', 'Asset account']));
$budgets = ExpandedForm::makeSelectList(Auth::user()->budgets()->get());
$budgets = ExpandedForm::makeSelectList(Auth::user()->budgets()->get()); // TODO this must be a repository call
$budgets[0] = trans('form.noBudget');
$piggies = ExpandedForm::makeSelectList(Auth::user()->piggyBanks()->get());
$piggies = ExpandedForm::makeSelectList(Auth::user()->piggyBanks()->get()); // TODO this must be a repository call
$piggies[0] = trans('form.noPiggybank');
$subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]);
$preFilled = [
@@ -184,22 +179,22 @@ class TransactionController extends Controller
$preFilled['piggy_bank_id'] = $journal->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id;
}
$preFilled['amount'] = $journal->amount_positive; // TODO TransactionJournal cannot deliver "amount_positive".
$preFilled['amount'] = TransactionJournal::amountPositive($journal);
if ($journal->isWithdrawal()) {
$preFilled['account_id'] = $journal->source_account_id;
if ($journal->destination_account_type != 'Cash account') {
$preFilled['expense_account'] = $journal->destination_account_name;
$preFilled['account_id'] = TransactionJournal::sourceAccount($journal)->id;
if (TransactionJournal::destinationAccountTypeStr($journal) != 'Cash account') {
$preFilled['expense_account'] = TransactionJournal::destinationAccount($journal)->name;
}
} else {
$preFilled['account_id'] = $journal->destination_account_id;
if ($journal->source_account_type != 'Cash account') {
$preFilled['revenue_account'] = $journal->source_account_name;
$preFilled['account_id'] = TransactionJournal::destinationAccount($journal)->id;
if (TransactionJournal::sourceAccountTypeStr($journal) != 'Cash account') {
$preFilled['revenue_account'] = TransactionJournal::sourceAccount($journal)->name;
}
}
$preFilled['account_from_id'] = $journal->source_account_id;
$preFilled['account_to_id'] = $journal->destination_account_id;
$preFilled['account_from_id'] = TransactionJournal::sourceAccount($journal)->id;
$preFilled['account_to_id'] = TransactionJournal::destinationAccount($journal)->id;
Session::flash('preFilled', $preFilled);
Session::flash('gaEventCategory', 'transactions');

View File

@@ -212,7 +212,7 @@ class AccountRepository implements AccountRepositoryInterface
$offset = ($page - 1) * 50;
$query = Auth::user()
->transactionJournals()
->expanded()// TODO firefly will crash here.
->expanded()
->where(
function (Builder $q) use ($account) {
$q->where('destination.account_id', $account->id);

View File

@@ -443,13 +443,6 @@ class BillRepository implements BillRepositoryInterface
*/
public function scan(Bill $bill, TransactionJournal $journal): bool
{
// grab the expanded info for this journal.
// looks weird, but is useful:
/** @var TransactionJournal $journal */
$journal = TransactionJournal::expanded()->where('transaction_journals.id', $journal->id)->get(TransactionJournal::QUERYFIELDS)->first();
// TODO REMOVE this in favour of something static in TransactionJournal.
/*
* Can only support withdrawals.
*/
@@ -458,9 +451,9 @@ class BillRepository implements BillRepositoryInterface
}
$matches = explode(',', $bill->match);
$description = strtolower($journal->description) . ' ' . strtolower($journal->destination_account_name);
$description = strtolower($journal->description) . ' ' . strtolower(TransactionJournal::destinationAccount($journal)->name);
$wordMatch = $this->doWordMatch($matches, $description);
$amountMatch = $this->doAmountMatch($journal->destination_amount, $bill->amount_min, $bill->amount_max);
$amountMatch = $this->doAmountMatch(TransactionJournal::amountPositive($journal), $bill->amount_min, $bill->amount_max);
Log::debug('Journal #' . $journal->id . ' has description "' . $description . '"');

View File

@@ -416,7 +416,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
{
$offset = intval(Input::get('page')) > 0 ? intval(Input::get('page')) * $take : 0;
$setQuery = $budget->transactionjournals()->expanded()
->take($take)->offset($offset)// TODO firefly will crash here.
->take($take)->offset($offset)
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC');

View File

@@ -129,8 +129,8 @@ class TagRepository implements TagRepositoryInterface
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
if ($journal->destination_account->id == $account->id) { // TODO TransactionJournal cannot deliver "destination_account"
$amount = bcadd($amount, $journal->amount); // TODO TransactionJournal cannot deliver "amount".
if (TransactionJournal::destinationAccount($journal)->id == $account->id) {
$amount = bcadd($amount, TransactionJournal::amount($journal));
}
}
}
@@ -376,10 +376,11 @@ class TagRepository implements TagRepositoryInterface
foreach ($tag->transactionjournals as $check) {
// $checkAccount is the source_account for a withdrawal
// $checkAccount is the destination_account for a deposit
if ($check->isWithdrawal() && $check->source_account->id != $journal->destination_account->id) { // TODO TransactionJournal cannot deliver "source_account"
if ($check->isWithdrawal() && TransactionJournal::sourceAccount($check)->id != TransactionJournal::destinationAccount($journal)->id) {
$match = false;
}
if ($check->isDeposit() && $check->destination_account->id != $journal->destination_account->id) { // TODO TransactionJournal cannot deliver "destination_account"
if ($check->isDeposit() && TransactionJournal::destinationAccount($check)->id != TransactionJournal::destinationAccount($journal)->id) {
$match = false;
}

View File

@@ -54,7 +54,7 @@ final class AmountExactly extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal)
{
$amount = $journal->amount_positive; // TODO TransactionJournal cannot deliver "amount_positive".
$amount = TransactionJournal::amountPositive($journal);
$compare = $this->triggerValue;
$result = bccomp($amount, $compare, 4);
if ($result === 0) {

View File

@@ -54,7 +54,7 @@ final class AmountLess extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal)
{
$amount = $journal->amount_positive; // TODO TransactionJournal cannot deliver "amount_positive".
$amount = TransactionJournal::amountPositive($journal);
$compare = $this->triggerValue;
$result = bccomp($amount, $compare, 4);
if ($result === -1) {

View File

@@ -54,7 +54,7 @@ final class AmountMore extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal)
{
$amount = $journal->amount_positive; // TODO TransactionJournal cannot deliver "amount_positive".
$amount = TransactionJournal::amountPositive($journal);
$compare = $this->triggerValue;
$result = bccomp($amount, $compare, 4);
if ($result === 1) {

View File

@@ -53,7 +53,7 @@ final class FromAccountContains extends AbstractTrigger implements TriggerInterf
*/
public function triggered(TransactionJournal $journal)
{
$fromAccountName = strtolower($journal->source_account->name);// TODO TransactionJournal cannot deliver "source_account"
$fromAccountName = strtolower(TransactionJournal::sourceAccount($journal)->name);
$search = strtolower($this->triggerValue);
$strpos = strpos($fromAccountName, $search);

View File

@@ -53,7 +53,7 @@ final class FromAccountEnds extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal)
{
$name = strtolower($journal->source_account->name);// TODO TransactionJournal cannot deliver "source_account"
$name = strtolower(TransactionJournal::sourceAccount($journal)->name);
$nameLength = strlen($name);
$search = strtolower($this->triggerValue);
$searchLength = strlen($search);

View File

@@ -53,7 +53,7 @@ final class FromAccountStarts extends AbstractTrigger implements TriggerInterfac
*/
public function triggered(TransactionJournal $journal)
{
$fromAccountName = strtolower($journal->source_account->name);// TODO TransactionJournal cannot deliver "source_account"
$fromAccountName = strtolower(TransactionJournal::sourceAccount($journal)->name);
$search = strtolower($this->triggerValue);
$part = substr($fromAccountName, 0, strlen($search));

View File

@@ -53,7 +53,7 @@ final class ToAccountContains extends AbstractTrigger implements TriggerInterfac
*/
public function triggered(TransactionJournal $journal)
{
$toAccountName = strtolower($journal->destination_account->name); // TODO TransactionJournal cannot deliver "destination_account"
$toAccountName = strtolower(TransactionJournal::destinationAccount($journal)->name);
$search = strtolower($this->triggerValue);
$strpos = strpos($toAccountName, $search);

View File

@@ -53,7 +53,7 @@ final class ToAccountEnds extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal)
{
$toAccountName = strtolower($journal->destination_account->name); // TODO TransactionJournal cannot deliver "destination_account"
$toAccountName = strtolower(TransactionJournal::destinationAccount($journal)->name);
$toAccountNameLength = strlen($toAccountName);
$search = strtolower($this->triggerValue);
$searchLength = strlen($search);

View File

@@ -53,7 +53,7 @@ final class ToAccountIs extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal)
{
$toAccountName = strtolower($journal->destination_account->name); // TODO TransactionJournal cannot deliver "destination_account"
$toAccountName = strtolower(TransactionJournal::destinationAccount($journal)->name);
$search = strtolower($this->triggerValue);
if ($toAccountName == $search) {

View File

@@ -53,7 +53,7 @@ final class ToAccountStarts extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal)
{
$toAccountName = strtolower($journal->destination_account->name); // TODO TransactionJournal cannot deliver "destination_account"
$toAccountName = strtolower(TransactionJournal::destinationAccount($journal)->name);
$search = strtolower($this->triggerValue);
$part = substr($toAccountName, 0, strlen($search));

View File

@@ -10,6 +10,10 @@
namespace FireflyIII\Support\Models;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
@@ -20,6 +24,84 @@ use Illuminate\Database\Eloquent\Model;
*/
class TransactionJournalSupport extends Model
{
/**
* @param TransactionJournal $journal
*
* @return string
*/
public static function amount(TransactionJournal $journal): string
{
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('amount');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
bcscale(2);
$transaction = $journal->transactions->sortByDesc('amount')->first();
$amount = $transaction->amount;
if ($journal->isWithdrawal()) {
$amount = bcmul($amount, '-1');
}
$cache->store($amount);
return $amount;
}
/**
* @param TransactionJournal $journal
*
* @return string
*/
public static function amountPositive(TransactionJournal $journal): string
{
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('amount-positive');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$amount = '0';
/** @var Transaction $t */
foreach ($journal->transactions as $t) {
if ($t->amount > 0) {
$amount = $t->amount;
}
}
$cache->store($amount);
return $amount;
}
/**
* @param TransactionJournal $journal
*
* @return Account
*/
public static function destinationAccount(TransactionJournal $journal): Account
{
$account = $journal->transactions()->where('amount', '>', 0)->first()->account;
return $account ?? new Account;
}
/**
* @param TransactionJournal $journal
*
* @return string
*/
public static function destinationAccountTypeStr(TransactionJournal $journal): string
{
$account = self::destinationAccount($journal);
$type = $account->accountType ? $account->accountType->type : '(unknown)';
return $type;
}
/**
* @param Builder $query
* @param string $table
@@ -41,4 +123,40 @@ class TransactionJournalSupport extends Model
return false;
}
/**
* @param TransactionJournal $journal
*
* @return Account
*/
public static function sourceAccount(TransactionJournal $journal): Account
{
$account = $journal->transactions()->where('amount', '<', 0)->first()->account;
return $account ?? new Account;
}
/**
* @param TransactionJournal $journal
*
* @return string
*/
public static function sourceAccountTypeStr(TransactionJournal $journal): string
{
$account = self::sourceAccount($journal);
$type = $account->accountType ? $account->accountType->type : '(unknown)';
return $type;
}
/**
* @param TransactionJournal $journal
*
* @return string
*/
public static function transactionTypeStr(TransactionJournal $journal): string
{
return $journal->transaction_type_type ?? $journal->transactionType->type;
}
}