1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Improve handling invalid date with CSV import

This commit is contained in:
Hillel Coren 2017-07-02 20:41:13 +03:00
parent 41bfea1edb
commit 9a21f754d5
4 changed files with 27 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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.',
);

View File

@ -10,14 +10,18 @@
<p>&nbsp;</p>
@if (! empty($warning))
<div class="alert alert-warning">{{ trans('texts.import_warning_' . $warning) }}</div>
@endif
<table class="table invoice-table">
<thead>
<tr>
<th>{{ trans('texts.column') }}</th>
<th class="col_sample">{{ trans('texts.sample') }}</th>
<th>{{ trans('texts.import_to') }}</th>
</tr>
</thead>
</tr>
</thead>
@for ($i=0; $i<count($headers); $i++)
<tr>
<td>{{ $headers[$i] }}</td>