2014-08-28 07:53:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Firefly\Trigger\Recurring;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2014-10-13 17:54:20 +02:00
|
|
|
use Firefly\Exception\FireflyException;
|
2014-08-28 07:53:54 +02:00
|
|
|
use Illuminate\Events\Dispatcher;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class EloquentRecurringTrigger
|
|
|
|
*
|
|
|
|
* @package Firefly\Trigger\Recurring
|
|
|
|
*/
|
|
|
|
class EloquentRecurringTrigger
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \RecurringTransaction $recurring
|
|
|
|
*/
|
|
|
|
public function destroy(\RecurringTransaction $recurring)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \RecurringTransaction $recurring
|
|
|
|
*/
|
|
|
|
public function store(\RecurringTransaction $recurring)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-10-12 09:31:25 +02:00
|
|
|
/**
|
|
|
|
* @param \RecurringTransaction $recurring
|
|
|
|
* @param \TransactionJournal $journal
|
|
|
|
*/
|
2014-10-12 08:19:18 +02:00
|
|
|
public function rescan(\RecurringTransaction $recurring, \TransactionJournal $journal)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Match words.
|
|
|
|
*/
|
|
|
|
$wordMatch = false;
|
|
|
|
$matches = explode(' ', $recurring->match);
|
|
|
|
$description = strtolower($journal->description);
|
2014-10-14 07:24:59 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = 0;
|
2014-10-12 08:19:18 +02:00
|
|
|
foreach ($matches as $word) {
|
2014-10-12 09:31:25 +02:00
|
|
|
if (!(strpos($description, strtolower($word)) === false)) {
|
2014-10-12 08:19:18 +02:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
2014-10-14 07:24:59 +02:00
|
|
|
if ($count >= count($matches)) {
|
2014-10-12 08:19:18 +02:00
|
|
|
$wordMatch = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Match amount.
|
|
|
|
*/
|
2014-10-14 07:24:59 +02:00
|
|
|
|
|
|
|
$amountMatch = false;
|
2014-10-12 08:19:18 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If both, update!
|
|
|
|
*/
|
|
|
|
if ($wordMatch && $amountMatch) {
|
|
|
|
$journal->recurringTransaction()->associate($recurring);
|
|
|
|
$journal->save();
|
|
|
|
}
|
2014-10-14 07:24:59 +02:00
|
|
|
|
2014-10-12 08:19:18 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 07:53:54 +02:00
|
|
|
/**
|
|
|
|
* Trigger!
|
|
|
|
*
|
|
|
|
* @param Dispatcher $events
|
|
|
|
*/
|
|
|
|
public function subscribe(Dispatcher $events)
|
|
|
|
{
|
2014-10-12 08:19:18 +02:00
|
|
|
$events->listen('recurring.rescan', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@rescan');
|
2014-08-28 07:53:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \RecurringTransaction $recurring
|
|
|
|
*/
|
|
|
|
public function update(\RecurringTransaction $recurring)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|