Improve recurrences

This commit is contained in:
James Cole
2019-06-29 19:47:40 +02:00
parent 947b83cbd1
commit 6197c77303
13 changed files with 210 additions and 36 deletions

View File

@@ -31,6 +31,7 @@ use FireflyIII\Models\Recurrence;
use FireflyIII\Services\Internal\Support\RecurringTransactionTrait;
use FireflyIII\Services\Internal\Support\TransactionTypeTrait;
use FireflyIII\User;
use Illuminate\Support\MessageBag;
use Log;
/**
@@ -41,6 +42,9 @@ class RecurrenceFactory
/** @var User */
private $user;
/** @var MessageBag */
private $errors;
use TransactionTypeTrait, RecurringTransactionTrait;
/**
@@ -52,22 +56,25 @@ class RecurrenceFactory
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
$this->errors = new MessageBag;
}
/**
* @param array $data
*
* @return Recurrence
* @throws FireflyException
*/
public function create(array $data): ?Recurrence
public function create(array $data): Recurrence
{
try {
$type = $this->findTransactionType(ucfirst($data['recurrence']['type']));
} catch (FireflyException $e) {
Log::error(sprintf('Cannot make a recurring transaction of type "%s"', $data['recurrence']['type']));
Log::error($e->getMessage());
$message = sprintf('Cannot make a recurring transaction of type "%s"', $data['recurrence']['type']);
Log::error($message);
Log::error($e->getTraceAsString());
return null;
throw new FireflyException($message);
}
/** @var Carbon $firstDate */
$firstDate = $data['recurrence']['first_date'];
@@ -95,9 +102,14 @@ class RecurrenceFactory
$this->createTransactions($recurrence, $data['transactions'] ?? []);
// @codeCoverageIgnoreStart
} catch (FireflyException $e) {
// TODO make sure error props to the user.
Log::error($e->getMessage());
$recurrence->forceDelete();
$message = sprintf('Could not create recurring transaction: %s', $e->getMessage());
$this->errors->add('store', $message);
throw new FireflyException($message);
}
// @codeCoverageIgnoreEnd
return $recurrence;