From 45bc23b8af161621370d4991f22c151f2f6018e5 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 25 Sep 2016 08:32:53 +0200 Subject: [PATCH] Some issues fixed for scrutiniser. --- app/Http/Requests/JournalFormRequest.php | 42 +++++++++++++++++------- app/Import/Setup/CsvSetup.php | 30 ++++------------- config/csv.php | 16 +++++++++ 3 files changed, 53 insertions(+), 35 deletions(-) diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php index 51013ec45e..c45c97862f 100644 --- a/app/Http/Requests/JournalFormRequest.php +++ b/app/Http/Requests/JournalFormRequest.php @@ -39,31 +39,31 @@ class JournalFormRequest extends Request */ public function getJournalData() { - $tags = $this->get('tags') ?? ''; + $tags = $this->getFieldOrEmptyString('tags'); return [ 'what' => $this->get('what'), 'description' => $this->get('description'), 'source_account_id' => intval($this->get('source_account_id')), - 'source_account_name' => $this->get('source_account_name') ?? '', + 'source_account_name' => $this->getFieldOrEmptyString('source_account_name'), 'destination_account_id' => intval($this->get('destination_account_id')), - 'destination_account_name' => $this->get('destination_account_name') ?? '', + 'destination_account_name' => $this->getFieldOrEmptyString('destination_account_name'), 'amount' => round($this->get('amount'), 2), 'user' => auth()->user()->id, 'amount_currency_id_amount' => intval($this->get('amount_currency_id_amount')), 'date' => new Carbon($this->get('date')), - 'interest_date' => $this->get('interest_date') ? new Carbon($this->get('interest_date')) : null, - 'book_date' => $this->get('book_date') ? new Carbon($this->get('book_date')) : null, - 'process_date' => $this->get('process_date') ? new Carbon($this->get('process_date')) : null, + 'interest_date' => $this->getDateOrNull('interest_date'), + 'book_date' => $this->getDateOrNull('book_date'), + 'process_date' => $this->getDateOrNull('process_date'), 'budget_id' => intval($this->get('budget_id')), - 'category' => $this->get('category') ?? '', + 'category' => $this->getFieldOrEmptyString('category'), 'tags' => explode(',', $tags), - 'piggy_bank_id' => $this->get('piggy_bank_id') ? intval($this->get('piggy_bank_id')) : 0, + 'piggy_bank_id' => intval($this->get('piggy_bank_id')), // new custom fields here: - 'due_date' => $this->get('due_date') ? new Carbon($this->get('due_date')) : null, - 'payment_date' => $this->get('payment_date') ? new Carbon($this->get('payment_date')) : null, - 'invoice_date' => $this->get('invoice_date') ? new Carbon($this->get('invoice_date')) : null, + 'due_date' => $this->getDateOrNull('due_date'), + 'payment_date' => $this->getDateOrNull('payment_date'), + 'invoice_date' => $this->getDateOrNull('invoice_date'), 'internal_reference' => $this->get('internal_reference'), 'notes' => $this->get('notes'), @@ -119,4 +119,24 @@ class JournalFormRequest extends Request return $rules; } + + /** + * @param string $field + * + * @return Carbon|null + */ + private function getDateOrNull(string $field) + { + return $this->get($field) ? new Carbon($this->get($field)) : null; + } + + /** + * @param string $field + * + * @return string + */ + private function getFieldOrEmptyString(string $field): string + { + return $this->get($field) ?? ''; + } } diff --git a/app/Import/Setup/CsvSetup.php b/app/Import/Setup/CsvSetup.php index 50b5d1274b..545d79e1e1 100644 --- a/app/Import/Setup/CsvSetup.php +++ b/app/Import/Setup/CsvSetup.php @@ -33,7 +33,6 @@ use Symfony\Component\HttpFoundation\FileBag; */ class CsvSetup implements SetupInterface { - const EXAMPLE_ROWS = 5; /** @var Account */ public $defaultImportAccount; /** @var ImportJob */ @@ -50,29 +49,13 @@ class CsvSetup implements SetupInterface /** * Create initial (empty) configuration array. * - * - * * @return bool */ public function configure(): bool { if (is_null($this->job->configuration) || (is_array($this->job->configuration) && count($this->job->configuration) === 0)) { Log::debug('No config detected, will create empty one.'); - - $config = [ - 'has-headers' => false, // assume - 'date-format' => 'Ymd', // assume - 'delimiter' => ',', // assume - 'import-account' => 0, // none, - 'specifics' => [], // none - 'column-count' => 0, // unknown - 'column-roles' => [], // unknown - 'column-do-mapping' => [], // not yet set which columns must be mapped - 'column-roles-complete' => false, // not yet configured roles for columns - 'column-mapping-config' => [], // no mapping made yet. - 'column-mapping-complete' => false, // so mapping is not complete. - ]; - $this->job->configuration = $config; + $this->job->configuration = config('csv.default_config'); $this->job->save(); return true; @@ -149,6 +132,7 @@ class CsvSetup implements SetupInterface * the import job. * * @return string + * @throws FireflyException */ public function getViewForSettings(): string { @@ -159,9 +143,7 @@ class CsvSetup implements SetupInterface if ($this->doColumnMapping()) { return 'import.csv.map'; } - - echo 'no view for settings'; - exit; + throw new FireflyException('There is no view for the current CSV import step.'); } /** @@ -172,8 +154,8 @@ class CsvSetup implements SetupInterface */ public function requireUserSettings(): bool { - Log::debug('doColumnMapping is ' . ($this->doColumnMapping() ? 'true' : 'false')); - Log::debug('doColumnRoles is ' . ($this->doColumnRoles() ? 'true' : 'false')); + Log::debug(sprintf('doColumnMapping is %s', $this->doColumnMapping())); + Log::debug(sprintf('doColumnRoles is %s', $this->doColumnRoles())); if ($this->doColumnMapping() || $this->doColumnRoles()) { Log::debug('Return true'); @@ -448,7 +430,7 @@ class CsvSetup implements SetupInterface $reader = Reader::createFromString($content); $reader->setDelimiter($config['delimiter']); $start = $config['has-headers'] ? 1 : 0; - $end = $start + self::EXAMPLE_ROWS; // first X rows + $end = $start + config('csv.example_rows'); // collect example data in $data['columns'] while ($start < $end) { diff --git a/config/csv.php b/config/csv.php index 5644b0f60f..57d2413841 100644 --- a/config/csv.php +++ b/config/csv.php @@ -276,4 +276,20 @@ return [ 'field' => 'description', ], ], + + // number of example rows: + 'example_rows' => 5, + 'default_config' => [ + 'has-headers' => false, // assume + 'date-format' => 'Ymd', // assume + 'delimiter' => ',', // assume + 'import-account' => 0, // none, + 'specifics' => [], // none + 'column-count' => 0, // unknown + 'column-roles' => [], // unknown + 'column-do-mapping' => [], // not yet set which columns must be mapped + 'column-roles-complete' => false, // not yet configured roles for columns + 'column-mapping-config' => [], // no mapping made yet. + 'column-mapping-complete' => false, // so mapping is not complete. + ], ];