mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-20 00:20:03 +00:00
Various code cleanup.
This commit is contained in:
@@ -71,6 +71,7 @@ class RecurrenceController extends Controller
|
||||
*/
|
||||
public function events(Request $request): JsonResponse
|
||||
{
|
||||
$occurrences = [];
|
||||
$return = [];
|
||||
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
|
||||
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
|
||||
@@ -106,20 +107,19 @@ class RecurrenceController extends Controller
|
||||
$repetition->weekend = (int)$request->get('weekend');
|
||||
$actualEnd = clone $end;
|
||||
|
||||
switch ($endsAt) {
|
||||
default:
|
||||
case 'forever':
|
||||
// simply generate up until $end. No change from default behavior.
|
||||
$occurrences = $this->recurring->getOccurrencesInRange($repetition, $actualStart, $actualEnd);
|
||||
break;
|
||||
case 'until_date':
|
||||
$actualEnd = $endDate ?? clone $end;
|
||||
$occurrences = $this->recurring->getOccurrencesInRange($repetition, $actualStart, $actualEnd);
|
||||
break;
|
||||
case 'times':
|
||||
$occurrences = $this->recurring->getXOccurrences($repetition, $actualStart, $repetitions);
|
||||
break;
|
||||
if('until_date' === $endsAt) {
|
||||
$actualEnd = $endDate ?? clone $end;
|
||||
$occurrences = $this->recurring->getOccurrencesInRange($repetition, $actualStart, $actualEnd);
|
||||
}
|
||||
if('times' === $endsAt) {
|
||||
$occurrences = $this->recurring->getXOccurrences($repetition, $actualStart, $repetitions);
|
||||
}
|
||||
if('times' !== $endsAt && 'until_date' !== $endsAt) {
|
||||
// 'forever'
|
||||
$occurrences = $this->recurring->getOccurrencesInRange($repetition, $actualStart, $actualEnd);
|
||||
|
||||
}
|
||||
|
||||
/** @var Carbon $current */
|
||||
foreach ($occurrences as $current) {
|
||||
if ($current->gte($start)) {
|
||||
|
||||
@@ -67,7 +67,7 @@ class TriggerController extends Controller
|
||||
foreach ($groups as $group) {
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($group->transactionJournals as $journal) {
|
||||
Log::debug(sprintf('Set date of journal #%d to today!', $journal->id, $date));
|
||||
Log::debug(sprintf('Set date of journal #%d to today!', $journal->id));
|
||||
$journal->date = Carbon::today();
|
||||
$journal->save();
|
||||
}
|
||||
|
||||
@@ -170,9 +170,10 @@ class EditController extends Controller
|
||||
]
|
||||
)->render();
|
||||
} catch (Throwable $e) {
|
||||
Log::debug(sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage()));
|
||||
$message = sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage());
|
||||
Log::debug($message);
|
||||
Log::error($e->getTraceAsString());
|
||||
throw new FireflyException($result, 0, $e);
|
||||
throw new FireflyException($message, 0, $e);
|
||||
}
|
||||
$index++;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ namespace FireflyIII\Http\Controllers\System;
|
||||
use Artisan;
|
||||
use Cache;
|
||||
use Exception;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Support\Facades\Preferences;
|
||||
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
|
||||
@@ -165,7 +166,17 @@ class InstallController extends Controller
|
||||
$index++;
|
||||
continue;
|
||||
}
|
||||
$result = $this->executeCommand($command, $args);
|
||||
try {
|
||||
$result = $this->executeCommand($command, $args);
|
||||
} catch (FireflyException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
|
||||
$this->lastError = self::BASEDIR_ERROR;
|
||||
}
|
||||
$result = false;
|
||||
$this->lastError = sprintf('%s %s', self::OTHER_ERROR, $e->getMessage());
|
||||
}
|
||||
if (false === $result) {
|
||||
$response['errorMessage'] = $this->lastError;
|
||||
$response['error'] = true;
|
||||
@@ -184,7 +195,7 @@ class InstallController extends Controller
|
||||
/**
|
||||
* @param string $command
|
||||
* @param array $args
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @return bool
|
||||
*/
|
||||
private function executeCommand(string $command, array $args): bool
|
||||
@@ -199,16 +210,7 @@ class InstallController extends Controller
|
||||
Log::debug(Artisan::output());
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
|
||||
$this->lastError = self::BASEDIR_ERROR;
|
||||
|
||||
return false;
|
||||
}
|
||||
$this->lastError = sprintf('%s %s', self::OTHER_ERROR, $e->getMessage());
|
||||
|
||||
return false;
|
||||
throw new FireflyException($e->getMessage(), 0, $e);
|
||||
}
|
||||
// clear cache as well.
|
||||
Cache::clear();
|
||||
|
||||
@@ -109,27 +109,34 @@ class RecurrenceFormRequest extends FormRequest
|
||||
$return['transactions'][0]['source_name'] = null;
|
||||
$return['transactions'][0]['destination_id'] = null;
|
||||
$return['transactions'][0]['destination_name'] = null;
|
||||
// fill in source and destination account data
|
||||
switch ($this->convertString('transaction_type')) {
|
||||
default:
|
||||
throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->convertString('transaction_type')));
|
||||
case 'withdrawal':
|
||||
$return['transactions'][0]['source_id'] = $this->convertInteger('source_id');
|
||||
$return['transactions'][0]['destination_id'] = $this->convertInteger('withdrawal_destination_id');
|
||||
break;
|
||||
case 'deposit':
|
||||
$return['transactions'][0]['source_id'] = $this->convertInteger('deposit_source_id');
|
||||
$return['transactions'][0]['destination_id'] = $this->convertInteger('destination_id');
|
||||
break;
|
||||
case 'transfer':
|
||||
$return['transactions'][0]['source_id'] = $this->convertInteger('source_id');
|
||||
$return['transactions'][0]['destination_id'] = $this->convertInteger('destination_id');
|
||||
break;
|
||||
$throwError = true;
|
||||
$type = $this->convertString('transaction_type');
|
||||
if ('withdrawal' === $type) {
|
||||
$throwError = false;
|
||||
$return['transactions'][0]['source_id'] = $this->convertInteger('source_id');
|
||||
$return['transactions'][0]['destination_id'] = $this->convertInteger('withdrawal_destination_id');
|
||||
}
|
||||
if ('deposit' === $type) {
|
||||
$throwError = false;
|
||||
$return['transactions'][0]['source_id'] = $this->convertInteger('deposit_source_id');
|
||||
$return['transactions'][0]['destination_id'] = $this->convertInteger('destination_id');
|
||||
}
|
||||
if ('transfer' === $type) {
|
||||
$throwError = false;
|
||||
$return['transactions'][0]['source_id'] = $this->convertInteger('source_id');
|
||||
$return['transactions'][0]['destination_id'] = $this->convertInteger('destination_id');
|
||||
}
|
||||
if (true === $throwError) {
|
||||
throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->convertString('transaction_type')));
|
||||
}
|
||||
|
||||
// replace category name with a new category:
|
||||
$factory = app(CategoryFactory::class);
|
||||
$factory->setUser(auth()->user());
|
||||
/**
|
||||
* @var int $index
|
||||
* @var array $transaction
|
||||
*/
|
||||
foreach ($return['transactions'] as $index => $transaction) {
|
||||
$categoryName = $transaction['category_name'] ?? null;
|
||||
if (null !== $categoryName) {
|
||||
@@ -239,23 +246,19 @@ class RecurrenceFormRequest extends FormRequest
|
||||
}
|
||||
|
||||
// switch on type to expand rules for source and destination accounts:
|
||||
switch ($this->convertString('transaction_type')) {
|
||||
case strtolower(TransactionType::WITHDRAWAL):
|
||||
$rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
|
||||
$rules['destination_name'] = 'between:1,255|nullable';
|
||||
break;
|
||||
case strtolower(TransactionType::DEPOSIT):
|
||||
$rules['source_name'] = 'between:1,255|nullable';
|
||||
$rules['destination_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
|
||||
break;
|
||||
case strtolower(TransactionType::TRANSFER):
|
||||
// this may not work:
|
||||
$rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:destination_id';
|
||||
$rules['destination_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:source_id';
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new FireflyException(sprintf('Cannot handle transaction type of type "%s"', $this->convertString('transaction_type')));
|
||||
$type = strtolower($this->convertString('transaction_type'));
|
||||
if (strtolower(TransactionType::WITHDRAWAL) === $type) {
|
||||
$rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
|
||||
$rules['destination_name'] = 'between:1,255|nullable';
|
||||
}
|
||||
if (strtolower(TransactionType::DEPOSIT) === $type) {
|
||||
$rules['source_name'] = 'between:1,255|nullable';
|
||||
$rules['destination_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
|
||||
}
|
||||
if (strtolower(TransactionType::TRANSFER) === $type) {
|
||||
// this may not work:
|
||||
$rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:destination_id';
|
||||
$rules['destination_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:source_id';
|
||||
}
|
||||
|
||||
// update some rules in case the user is editing a post:
|
||||
@@ -309,23 +312,28 @@ class RecurrenceFormRequest extends FormRequest
|
||||
$destinationId = null;
|
||||
|
||||
// TODO typeOverrule: the account validator may have another opinion the transaction type.
|
||||
|
||||
switch ($this->convertString('transaction_type')) {
|
||||
default:
|
||||
throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->convertString('transaction_type')));
|
||||
case 'withdrawal':
|
||||
$sourceId = (int)$data['source_id'];
|
||||
$destinationId = (int)$data['withdrawal_destination_id'];
|
||||
break;
|
||||
case 'deposit':
|
||||
$sourceId = (int)$data['deposit_source_id'];
|
||||
$destinationId = (int)$data['destination_id'];
|
||||
break;
|
||||
case 'transfer':
|
||||
$sourceId = (int)$data['source_id'];
|
||||
$destinationId = (int)$data['destination_id'];
|
||||
break;
|
||||
// TODO either use 'withdrawal' or the strtolower() variant, not both.
|
||||
$type = $this->convertString('transaction_type');
|
||||
$throwError = true;
|
||||
if('withdrawal' === $type) {
|
||||
$throwError = false;
|
||||
$sourceId = (int)$data['source_id'];
|
||||
$destinationId = (int)$data['withdrawal_destination_id'];
|
||||
}
|
||||
if('deposit' === $type) {
|
||||
$throwError = false;
|
||||
$sourceId = (int)$data['deposit_source_id'];
|
||||
$destinationId = (int)$data['destination_id'];
|
||||
}
|
||||
if('transfer' === $type) {
|
||||
$throwError = false;
|
||||
$sourceId = (int)$data['source_id'];
|
||||
$destinationId = (int)$data['destination_id'];
|
||||
}
|
||||
if(true === $throwError) {
|
||||
throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->convertString('transaction_type')));
|
||||
}
|
||||
|
||||
// validate source account.
|
||||
$validSource = $accountValidator->validateSource(['id' => $sourceId,]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user