mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-18 12:28:46 +00:00
Expanded recurring transactions.
This commit is contained in:
@@ -20,4 +20,19 @@ interface RecurringInterface
|
||||
*/
|
||||
public function getJournalForRecurringInRange(\RecurringTransaction $recurring, Carbon $start, Carbon $end);
|
||||
|
||||
/**
|
||||
* @param \RecurringTransaction $recurring
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function scanEverything(\RecurringTransaction $recurring);
|
||||
|
||||
/**
|
||||
* @param \RecurringTransaction $recurring
|
||||
* @param \TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function scan(\RecurringTransaction $recurring,\TransactionJournal $journal);
|
||||
|
||||
}
|
||||
@@ -11,6 +11,7 @@ use FireflyIII\Exception\NotImplementedException;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use LaravelBook\Ardent\Ardent;
|
||||
use stdObject;
|
||||
|
||||
/**
|
||||
* Class Recurring
|
||||
@@ -36,8 +37,9 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface
|
||||
*/
|
||||
public function destroy(Ardent $model)
|
||||
{
|
||||
// TODO: Implement destroy() method.
|
||||
throw new NotImplementedException;
|
||||
$model->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,8 +49,38 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface
|
||||
*/
|
||||
public function store(array $data)
|
||||
{
|
||||
// TODO: Implement store() method.
|
||||
throw new NotImplementedException;
|
||||
var_dump($data);
|
||||
$recurring = new \RecurringTransaction;
|
||||
$recurring->user()->associate($this->getUser());
|
||||
$recurring->name = $data['name'];
|
||||
$recurring->match = $data['match'];
|
||||
$recurring->amount_max = floatval($data['amount_max']);
|
||||
$recurring->amount_min = floatval($data['amount_min']);
|
||||
|
||||
$date = new Carbon($data['date']);
|
||||
|
||||
|
||||
$recurring->active = isset($data['active']) && intval($data['active']) == 1 ? 1 : 0;
|
||||
$recurring->automatch = isset($data['automatch']) && intval($data['automatch']) == 1 ? 1 : 0;
|
||||
$recurring->repeat_freq = $data['repeat_freq'];
|
||||
|
||||
/*
|
||||
* Jump to the start of the period.
|
||||
*/
|
||||
/** @var \FireflyIII\Shared\Toolkit\Date $toolkit */
|
||||
$toolkit = \App::make('FireflyIII\Shared\Toolkit\Date');
|
||||
$date = $toolkit->startOfPeriod($date, $data['repeat_freq']);
|
||||
$recurring->date = $date;
|
||||
$recurring->skip = intval($data['skip']);
|
||||
|
||||
if (!$recurring->validate()) {
|
||||
var_dump($recurring->errors());
|
||||
exit();
|
||||
}
|
||||
|
||||
$recurring->save();
|
||||
|
||||
return $recurring;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,8 +91,29 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface
|
||||
*/
|
||||
public function update(Ardent $model, array $data)
|
||||
{
|
||||
// TODO: Implement update() method.
|
||||
throw new NotImplementedException;
|
||||
var_dump($data);
|
||||
|
||||
$model->name = $data['name'];
|
||||
$model->match = $data['match'];
|
||||
$model->amount_max = floatval($data['amount_max']);
|
||||
$model->amount_min = floatval($data['amount_min']);
|
||||
|
||||
$date = new Carbon($data['date']);
|
||||
|
||||
$model->date = $date;
|
||||
$model->active = isset($data['active']) && intval($data['active']) == 1 ? 1 : 0;
|
||||
$model->automatch = isset($data['automatch']) && intval($data['automatch']) == 1 ? 1 : 0;
|
||||
$model->repeat_freq = $data['repeat_freq'];
|
||||
$model->skip = intval($data['skip']);
|
||||
|
||||
if (!$model->validate()) {
|
||||
var_dump($model->errors());
|
||||
exit();
|
||||
}
|
||||
|
||||
$model->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,7 +147,7 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface
|
||||
if (isset($model['amount_max']) && floatval($model['amount_max']) < 0.02) {
|
||||
$errors->add('amount_max', 'Maximum amount must be higher.');
|
||||
}
|
||||
if(isset($model['amount_min']) && isset($model['amount_max']) && floatval($model['amount_min']) > floatval($model['amount_max'])) {
|
||||
if (isset($model['amount_min']) && isset($model['amount_max']) && floatval($model['amount_min']) > floatval($model['amount_max'])) {
|
||||
$errors->add('amount_max', 'Maximum amount can not be less than minimum amount.');
|
||||
$errors->add('amount_min', 'Minimum amount can not be more than maximum amount.');
|
||||
}
|
||||
@@ -116,10 +169,10 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface
|
||||
$errors->add('skip', 'Invalid skip.');
|
||||
}
|
||||
|
||||
$set = ['name','match','amount_min','amount_max','date','repeat_freq','skip','automatch','active'];
|
||||
foreach($set as $entry) {
|
||||
if(!$errors->has($entry)) {
|
||||
$successes->add($entry,'OK');
|
||||
$set = ['name', 'match', 'amount_min', 'amount_max', 'date', 'repeat_freq', 'skip', 'automatch', 'active'];
|
||||
foreach ($set as $entry) {
|
||||
if (!$errors->has($entry)) {
|
||||
$successes->add($entry, 'OK');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,4 +252,111 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface
|
||||
return $this->getUser()->transactionjournals()->where('recurring_transaction_id', $recurring->id)->after($start)->before($end)->first();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \RecurringTransaction $recurring
|
||||
* @param \TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function scan(\RecurringTransaction $recurring, \TransactionJournal $journal)
|
||||
{
|
||||
/*
|
||||
* Match words.
|
||||
*/
|
||||
$wordMatch = false;
|
||||
$matches = explode(',', $recurring->match);
|
||||
$description = strtolower($journal->description);
|
||||
|
||||
/*
|
||||
* Attach expense account to description for more narrow matching.
|
||||
*/
|
||||
if (count($journal->transactions) < 2) {
|
||||
$transactions = $journal->transactions()->get();
|
||||
} else {
|
||||
$transactions = $journal->transactions;
|
||||
}
|
||||
/** @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) {
|
||||
if (!(strpos($description, strtolower($word)) === false)) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
if ($count >= count($matches)) {
|
||||
$wordMatch = true;
|
||||
\Log::debug('word match is true');
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Match amount.
|
||||
*/
|
||||
|
||||
$amountMatch = false;
|
||||
if (count($transactions) > 1) {
|
||||
|
||||
$amount = max(floatval($transactions[0]->amount), floatval($transactions[1]->amount));
|
||||
$min = floatval($recurring->amount_min);
|
||||
$max = floatval($recurring->amount_max);
|
||||
if ($amount >= $min && $amount <= $max) {
|
||||
$amountMatch = true;
|
||||
\Log::debug('Amount match is true!');
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* If both, update!
|
||||
*/
|
||||
if ($wordMatch && $amountMatch) {
|
||||
$journal->recurringTransaction()->associate($recurring);
|
||||
$journal->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \RecurringTransaction $recurring
|
||||
*
|
||||
* @return bool
|
||||
* @throws NotImplementedException
|
||||
*/
|
||||
public function scanEverything(\RecurringTransaction $recurring)
|
||||
{
|
||||
// get all journals that (may) be relevant.
|
||||
// this is usually almost all of them.
|
||||
|
||||
/** @var \FireflyIII\Database\TransactionJournal $journalRepository */
|
||||
$journalRepository = \App::make('FireflyIII\Database\TransactionJournal');
|
||||
|
||||
$set = \DB::table('transactions')->where('amount', '>', 0)->where('amount', '>=', $recurring->amount_min)->where(
|
||||
'amount', '<=', $recurring->amount_max
|
||||
)->get(['transaction_journal_id']);
|
||||
$ids = [];
|
||||
|
||||
/** @var \Transaction $entry */
|
||||
foreach ($set as $entry) {
|
||||
$ids[] = intval($entry->transaction_journal_id);
|
||||
}
|
||||
if (count($ids) > 0) {
|
||||
$journals = $journalRepository->getByIds($ids);
|
||||
/** @var \TransactionJournal $journal */
|
||||
foreach ($journals as $journal) {
|
||||
$this->scan($recurring, $journal);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -355,8 +355,7 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
*/
|
||||
public function getByIds(array $ids)
|
||||
{
|
||||
// TODO: Implement getByIds() method.
|
||||
throw new NotImplementedException;
|
||||
return $this->getUser()->transactionjournals()->with('transactions')->whereIn('id', $ids)->orderBy('date', 'ASC')->get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -390,9 +389,10 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
$end->endOfMonth();
|
||||
|
||||
$sum = \DB::table('transactions')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->leftJoin(
|
||||
'transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id'
|
||||
)->where('amount', '>', 0)->where('transaction_types.type', '=', 'Withdrawal')->where('transaction_journals.date', '>=', $date->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->sum('transactions.amount');
|
||||
'transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id'
|
||||
)->where('amount', '>', 0)->where('transaction_types.type', '=', 'Withdrawal')->where('transaction_journals.date', '>=', $date->format('Y-m-d'))->where(
|
||||
'transaction_journals.date', '<=', $end->format('Y-m-d')
|
||||
)->sum('transactions.amount');
|
||||
$sum = floatval($sum);
|
||||
|
||||
return $sum;
|
||||
@@ -410,9 +410,10 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
$end->endOfMonth();
|
||||
|
||||
$sum = \DB::table('transactions')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->leftJoin(
|
||||
'transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id'
|
||||
)->where('amount', '>', 0)->where('transaction_types.type', '=', 'Deposit')->where('transaction_journals.date', '>=', $date->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->sum('transactions.amount');
|
||||
'transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id'
|
||||
)->where('amount', '>', 0)->where('transaction_types.type', '=', 'Deposit')->where('transaction_journals.date', '>=', $date->format('Y-m-d'))->where(
|
||||
'transaction_journals.date', '<=', $end->format('Y-m-d')
|
||||
)->sum('transactions.amount');
|
||||
$sum = floatval($sum);
|
||||
|
||||
return $sum;
|
||||
@@ -441,12 +442,12 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
|
||||
$accountID = $account->id;
|
||||
$query = $this->_user->transactionjournals()->with(['transactions', 'transactioncurrency', 'transactiontype'])->leftJoin(
|
||||
'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'
|
||||
)->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')->where('accounts.id', $accountID)->where(
|
||||
'date', '>=', $start->format('Y-m-d')
|
||||
)->where('date', '<=', $end->format('Y-m-d'))->orderBy('transaction_journals.date', 'DESC')->orderBy('transaction_journals.id', 'DESC')->take(
|
||||
$count
|
||||
)->get(['transaction_journals.*']);
|
||||
'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'
|
||||
)->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')->where('accounts.id', $accountID)->where(
|
||||
'date', '>=', $start->format('Y-m-d')
|
||||
)->where('date', '<=', $end->format('Y-m-d'))->orderBy('transaction_journals.date', 'DESC')->orderBy('transaction_journals.id', 'DESC')->take(
|
||||
$count
|
||||
)->get(['transaction_journals.*']);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user