From a67ba6ee6853f0d8c050489f706b57737587b711 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Thu, 14 Nov 2024 13:54:59 +0100 Subject: [PATCH] fix(Import): distinguish between date only and date time on xlsx imports Signed-off-by: Arthur Schiwon --- lib/Service/ColumnTypes/DatetimeDateBusiness.php | 4 ++-- lib/Service/ImportService.php | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/Service/ColumnTypes/DatetimeDateBusiness.php b/lib/Service/ColumnTypes/DatetimeDateBusiness.php index 70b5b6922..5cbc8abaf 100644 --- a/lib/Service/ColumnTypes/DatetimeDateBusiness.php +++ b/lib/Service/ColumnTypes/DatetimeDateBusiness.php @@ -17,7 +17,7 @@ class DatetimeDateBusiness extends SuperBusiness implements IColumnTypeBusiness * @return string */ public function parseValue($value, ?Column $column = null): string { - return json_encode($this->isValidDate($value, 'Y-m-d') ? $value : ''); + return json_encode($this->isValidDate((string)$value, 'Y-m-d') ? (string)$value : ''); } /** @@ -26,7 +26,7 @@ public function parseValue($value, ?Column $column = null): string { * @return bool */ public function canBeParsed($value, ?Column $column = null): bool { - return $this->isValidDate($value, 'Y-m-d'); + return $this->isValidDate((string)$value, 'Y-m-d'); } } diff --git a/lib/Service/ImportService.php b/lib/Service/ImportService.php index cf701cdd6..d7c1fbd98 100644 --- a/lib/Service/ImportService.php +++ b/lib/Service/ImportService.php @@ -136,7 +136,7 @@ private function getPreviewData(Worksheet $worksheet): array { $columns[] = [ 'title' => $title, 'type' => $this->rawColumnDataTypes[$colIndex]['type'], - 'subtype' => $this->rawColumnDataTypes[$colIndex]['subtype'], + 'subtype' => $this->rawColumnDataTypes[$colIndex]['subtype'] ?? null, 'numberDecimals' => $this->rawColumnDataTypes[$colIndex]['number_decimals'] ?? 0, 'numberPrefix' => $this->rawColumnDataTypes[$colIndex]['number_prefix'] ?? '', 'numberSuffix' => $this->rawColumnDataTypes[$colIndex]['number_suffix'] ?? '', @@ -367,8 +367,10 @@ private function createRow(Row $row): void { $value = $cell->getValue(); $hasData = $hasData || !empty($value); - if ($column->getType() === 'datetime') { + if ($column->getType() === 'datetime' && $column->getSubtype() === '') { $value = Date::excelToDateTimeObject($value)->format('Y-m-d H:i'); + } elseif ($column->getType() === 'datetime' && $column->getSubtype() === 'date') { + $value = Date::excelToDateTimeObject($value)->format('Y-m-d'); } elseif ($column->getType() === 'number' && $column->getNumberSuffix() === '%') { $value = $value * 100; } elseif ($column->getType() === 'selection' && $column->getSubtype() === 'check') { @@ -477,6 +479,7 @@ private function parseColumnDataType(Cell $cell): array { if (Date::isDateTime($cell) || $originDataType === DataType::TYPE_ISO_DATE) { $dataType = [ 'type' => 'datetime', + 'subtype' => $cell->getCalculateDateTimeType() === Cell::CALCULATE_DATE_TIME_ASIS ? 'date' : '', ]; } elseif ($originDataType === DataType::TYPE_NUMERIC) { if (str_contains($formattedValue, '%')) {