File: /var/www/dtw.bradford/app/Imports/FlexlineDiaryBookImport.php
<?php
namespace App\Imports;
use Exception;
class FlexlineDiaryBookImport extends AbstractChunksImport
{
protected function getExpectedHeaders(): array
{
return [
'fecha', 'correlativo', 'tipo_comprobante', 'num_linea', 'cuenta', 'glosa', 'referencia',
'debe', 'haber', 'descripcion', 'tipo_documento', 'centro_costos', 'proveedores',
'sostenedores', 'item_gasto', 'cod_analisis', 'personal', 'otros', 'item_flujo',
'no_importacion', 'area', 'banco', 'fecha_de_vencimiento', 'paridad_moneda', 'usuario',
];
}
protected function getTableName(): string
{
return 'flexline_diary_book';
}
protected function getSheetName()
{
return 'Hoja1';
}
protected function normalizeRow(array $row, int $numRow): ?array
{
$validation_errors = 0;
$validation_messages = '';
try {
$fecha = !empty($row['fecha']) && isValidExcelDate($row['fecha']) ? excelDateToYmd($row['fecha']) : null;
if (empty($row['fecha']) || !isValidExcelDate($row['fecha'])) {
$validation_messages .= ++$validation_errors . ': fecha inválida. ';
}
$fecha_de_vencimiento = !empty($row['fecha_de_vencimiento']) && isValidExcelDate($row['fecha_de_vencimiento'])
? excelDateToYmd($row['fecha_de_vencimiento'])
: null;
if (!empty($row['fecha_de_vencimiento']) && !isValidExcelDate($row['fecha_de_vencimiento'])) {
$validation_messages .= ++$validation_errors . ': fecha_de_vencimiento inválida. ';
}
$paridad_moneda = !empty($row['paridad_moneda']) ? round(floatval($row['paridad_moneda']), 4) : null;
$usuario = $row['usuario'] ?? null;
if ($validation_errors > 0) {
$this->hasErrors = true;
$this->error_data[] = [
'numero_de_linea' => $numRow,
'errores' => $validation_messages,
'fecha_de_carga' => date('d-m-Y H:i'),
];
return null;
}
return [
'date' => $fecha,
'correlative' => intval($row['correlativo'] ?? 0),
'voucher_type' => $row['tipo_comprobante'] ?? null,
'line_number' => intval($row['num_linea'] ?? 0),
'account' => intval($row['cuenta'] ?? 0),
'description_detail' => $row['glosa'] ?? null,
'reference' => intval($row['referencia'] ?? 0),
'debit' => floatval($row['debe'] ?? 0),
'credit' => floatval($row['haber'] ?? 0),
'balance' => floatval($row['debe'] ?? 0) - floatval($row['haber'] ?? 0),
'description' => $row['descripcion'] ?? null,
'document_type' => $row['tipo_documento'] ?? null,
'cost_center' => $row['centro_costos'] ?? null,
'providers' => $row['proveedores'] ?? null,
'sponsors' => $row['sostenedores'] ?? null,
'expense_item' => intval($row['item_gasto'] ?? 0),
'analysis_code' => $row['cod_analisis'] ?? null,
'personnel' => $row['personal'] ?? null,
'others' => $row['otros'] ?? null,
'cash_flow_item' => $row['item_flujo'] ?? null,
'no_import' => $row['no_importacion'] ?? null,
'area' => $row['area'] ?? null,
'bank' => $row['banco'] ?? null,
'due_date' => $fecha_de_vencimiento,
'currency_rate' => $paridad_moneda,
'user' => $usuario,
'created_at' => ahoraServidor(),
];
} catch (\Throwable $e) {
$this->error_data[] = [
'numero_de_linea' => $numRow,
'errores' => 'Excepción: ' . $e->getMessage(),
'fecha_de_carga' => date('d-m-Y H:i'),
];
return null;
}
}
}