From 9a21f754d51f5f59ff324d5916f232c20da6824d Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 2 Jul 2017 20:41:13 +0300 Subject: [PATCH] Improve handling invalid date with CSV import --- app/Ninja/Import/BaseTransformer.php | 2 ++ app/Services/ImportService.php | 19 ++++++++++++++++++- resources/lang/en/texts.php | 1 + .../views/accounts/partials/map.blade.php | 8 ++++++-- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/Ninja/Import/BaseTransformer.php b/app/Ninja/Import/BaseTransformer.php index 0d672a0c2dd8..d718fd6824d8 100644 --- a/app/Ninja/Import/BaseTransformer.php +++ b/app/Ninja/Import/BaseTransformer.php @@ -5,6 +5,7 @@ namespace App\Ninja\Import; use Carbon; use League\Fractal\TransformerAbstract; use Utils; +use Exception; /** * Class BaseTransformer. @@ -158,6 +159,7 @@ class BaseTransformer extends TransformerAbstract $date = new Carbon($date); } catch (Exception $e) { // if we fail to parse return blank + $date = false; } } diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index a215785f3ce9..18cb4edc84b9 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -30,6 +30,7 @@ use parsecsv; use Session; use stdClass; use Utils; +use Carbon; /** * Class ImportService. @@ -183,7 +184,7 @@ class ImportService if ($transformer->hasProduct($jsonProduct['product_key'])) { continue; } - + $productValidate = EntityModel::validate($jsonProduct, ENTITY_PRODUCT); if ($productValidate === true) { $product = $this->productRepo->save($jsonProduct); @@ -594,8 +595,24 @@ class ImportService 'hasHeaders' => $hasHeaders, 'columns' => $columns, 'mapped' => $mapped, + 'warning' => false, ]; + // check that dates are valid + if (count($data['data']) > 1) { + $row = $data['data'][1]; + foreach ($mapped as $index => $field) { + if (! strstr($field, 'date')) { + continue; + } + try { + $date = new Carbon($row[$index]); + } catch(Exception $e) { + $data['warning'] = 'invalid_date'; + } + } + } + return $data; } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index ca331eada932..e23e742d2476 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2296,6 +2296,7 @@ $LANG = array( 'credit_number_help' => 'Specify a prefix or use a custom pattern to dynamically set the credit number for negative invoices.', 'next_credit_number' => 'The next credit number is :number.', 'padding_help' => 'The number of zero\'s to pad the number.', + 'import_warning_invalid_date' => 'Warning: The date format appears to be invalid.', ); diff --git a/resources/views/accounts/partials/map.blade.php b/resources/views/accounts/partials/map.blade.php index fab25f4158ad..7a1dd2d3e26d 100644 --- a/resources/views/accounts/partials/map.blade.php +++ b/resources/views/accounts/partials/map.blade.php @@ -10,14 +10,18 @@

 

+ @if (! empty($warning)) +
{{ trans('texts.import_warning_' . $warning) }}
+ @endif + - - + + @for ($i=0; $i
{{ trans('texts.column') }} {{ trans('texts.sample') }} {{ trans('texts.import_to') }}
{{ $headers[$i] }}