mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Expanded recurring transactions.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user