Clean up repositories and cron code.

This commit is contained in:
James Cole
2019-06-07 17:57:46 +02:00
parent a845cb9af9
commit e4a9abc315
19 changed files with 547 additions and 1188 deletions

View File

@@ -28,13 +28,43 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Jobs\CreateRecurringTransactions;
use FireflyIII\Models\Configuration;
use Log;
use Preferences;
/**
* Class RecurringCronjob
*/
class RecurringCronjob extends AbstractCronjob
{
/** @var bool */
private $force;
/** @var Carbon */
private $date;
/**
* RecurringCronjob constructor.
* @throws \Exception
*/
public function __construct()
{
$this->force = false;
$this->date = new Carbon;
}
/**
* @param bool $force
*/
public function setForce(bool $force): void
{
$this->force = $force;
}
/**
* @param Carbon $date
*/
public function setDate(Carbon $date): void
{
$this->date = $date;
}
/**
* @return bool
@@ -48,17 +78,25 @@ class RecurringCronjob extends AbstractCronjob
$diff = time() - $lastTime;
$diffForHumans = Carbon::now()->diffForHumans(Carbon::createFromTimestamp($lastTime), true);
if (0 === $lastTime) {
Log::info('Recurring transactions cronjob has never fired before.');
Log::info('Recurring transactions cron-job has never fired before.');
}
// less than half a day ago:
if ($lastTime > 0 && $diff <= 43200) {
Log::info(sprintf('It has been %s since the recurring transactions cronjob has fired. It will not fire now.', $diffForHumans));
Log::info(sprintf('It has been %s since the recurring transactions cron-job has fired.', $diffForHumans));
if (false === $this->force) {
Log::info('The cron-job will not fire now.');
return false;
return false;
}
// fire job regardless.
if (true === $this->force) {
Log::info('Execution of the recurring transaction cron-job has been FORCED.');
}
}
if ($lastTime > 0 && $diff > 43200) {
Log::info(sprintf('It has been %s since the recurring transactions cronjob has fired. It will fire now!', $diffForHumans));
Log::info(sprintf('It has been %s since the recurring transactions cron-job has fired. It will fire now!', $diffForHumans));
}
try {
@@ -68,7 +106,8 @@ class RecurringCronjob extends AbstractCronjob
Log::error($e->getTraceAsString());
throw new FireflyException(sprintf('Could not run recurring transaction cron job: %s', $e->getMessage()));
}
Preferences::mark();
app('preferences')->mark();
return true;
}
@@ -79,8 +118,9 @@ class RecurringCronjob extends AbstractCronjob
*/
private function fireRecurring(): void
{
$job = new CreateRecurringTransactions(new Carbon);
$job = new CreateRecurringTransactions($this->date);
$job->setForce($this->force);
$job->handle();
app('fireflyconfig')->set('last_rt_job', time());
app('fireflyconfig')->set('last_rt_job', $this->date->format('U'));
}
}

View File

@@ -161,9 +161,26 @@ class ImportTransaction
'opposing-number' => 'opposingNumber',
];
// overrule some old role values.
if ('original-source' === $role) {
$role = 'original_source';
$replaceOldRoles = [
'original-source' => 'original_source',
'sepa-cc' => 'sepa_cc',
'sepa-ct-op' => 'sepa_ct_op',
'sepa-ct-id' => 'sepa_ct_id',
'sepa-db' => 'sepa_db',
'sepa-country' => 'sepa_country',
'sepa-ep' => 'sepa_ep',
'sepa-ci' => 'sepa_ci',
'sepa-batch-id' => 'sepa_batch_id',
'internal-reference' => 'internal_reference',
'date-interest' => 'date_interest',
'date-invoice' => 'date_invoice',
'date-book' => 'date_book',
'date-payment' => 'date_payment',
'date-process' => 'date_process',
'date-due' => 'date_due',
];
if (in_array($role, array_keys($replaceOldRoles))) {
$role = $replaceOldRoles[$role];
}
if (isset($basics[$role])) {
@@ -201,7 +218,7 @@ class ImportTransaction
return;
}
$modifiers = ['generic-debit-credit'];
$modifiers = ['generic-debit-credit', 'ing-debit-credit', 'rabo-debit-credit'];
if (in_array($role, $modifiers, true)) {
$this->modifiers[$role] = $columnValue->getValue();
@@ -235,18 +252,6 @@ class ImportTransaction
}
}
/**
* Returns the mapped value if it exists in the ColumnValue object.
*
* @param ColumnValue $columnValue
*
* @return int
*/
private function getMappedValue(ColumnValue $columnValue): int
{
return $columnValue->getMappedValue() > 0 ? $columnValue->getMappedValue() : (int)$columnValue->getValue();
}
/**
* Calculate the amount of this transaction.
*
@@ -294,40 +299,6 @@ class ImportTransaction
return $result;
}
/**
* This methods decides which input value to use for the amount calculation.
*
* @return array
*/
private function selectAmountInput(): array
{
$info = [];
$converterClass = '';
if (null !== $this->amount) {
Log::debug('Amount value is not NULL, assume this is the correct value.');
$converterClass = Amount::class;
$info['amount'] = $this->amount;
}
if (null !== $this->amountDebit) {
Log::debug('Amount DEBIT value is not NULL, assume this is the correct value (overrules Amount).');
$converterClass = AmountDebit::class;
$info['amount'] = $this->amountDebit;
}
if (null !== $this->amountCredit) {
Log::debug('Amount CREDIT value is not NULL, assume this is the correct value (overrules Amount and AmountDebit).');
$converterClass = AmountCredit::class;
$info['amount'] = $this->amountCredit;
}
if (null !== $this->amountNegated) {
Log::debug('Amount NEGATED value is not NULL, assume this is the correct value (overrules Amount and AmountDebit and AmountCredit).');
$converterClass = AmountNegated::class;
$info['amount'] = $this->amountNegated;
}
$info['class'] = $converterClass;
return $info;
}
/**
* The method that calculates the foreign amount isn't nearly as complex,\
* because Firefly III only supports one foreign amount field. So the foreign amount is there
@@ -424,4 +395,50 @@ class ImportTransaction
];
}
/**
* Returns the mapped value if it exists in the ColumnValue object.
*
* @param ColumnValue $columnValue
*
* @return int
*/
private function getMappedValue(ColumnValue $columnValue): int
{
return $columnValue->getMappedValue() > 0 ? $columnValue->getMappedValue() : (int)$columnValue->getValue();
}
/**
* This methods decides which input value to use for the amount calculation.
*
* @return array
*/
private function selectAmountInput(): array
{
$info = [];
$converterClass = '';
if (null !== $this->amount) {
Log::debug('Amount value is not NULL, assume this is the correct value.');
$converterClass = Amount::class;
$info['amount'] = $this->amount;
}
if (null !== $this->amountDebit) {
Log::debug('Amount DEBIT value is not NULL, assume this is the correct value (overrules Amount).');
$converterClass = AmountDebit::class;
$info['amount'] = $this->amountDebit;
}
if (null !== $this->amountCredit) {
Log::debug('Amount CREDIT value is not NULL, assume this is the correct value (overrules Amount and AmountDebit).');
$converterClass = AmountCredit::class;
$info['amount'] = $this->amountCredit;
}
if (null !== $this->amountNegated) {
Log::debug('Amount NEGATED value is not NULL, assume this is the correct value (overrules Amount and AmountDebit and AmountCredit).');
$converterClass = AmountNegated::class;
$info['amount'] = $this->amountNegated;
}
$info['class'] = $converterClass;
return $info;
}
}