174 lines
6.4 KiB
PHP
174 lines
6.4 KiB
PHP
<?php
|
|
ini_set('display_errors', 0); // in AJAX gli errori devono NON sporcare il JSON
|
|
error_reporting(E_ALL);
|
|
ini_set('log_errors', 1);
|
|
ini_set('error_log', __DIR__ . '/import_debug.log');
|
|
|
|
include('include/headscript.php');
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// Legge il body JSON
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (
|
|
!$input ||
|
|
!isset($input['template_id']) ||
|
|
!isset($input['importreferencecode']) ||
|
|
!isset($input['filename']) ||
|
|
!isset($input['columns']) ||
|
|
!isset($input['batch_rows']) ||
|
|
!isset($input['batch_excelrows'])
|
|
) {
|
|
echo json_encode(['ok' => false, 'error' => 'Richiesta non valida']);
|
|
exit;
|
|
}
|
|
|
|
$template_id = intval($input['template_id']);
|
|
$importReferenceCode = (string)$input['importreferencecode'];
|
|
$newFilename = (string)$input['filename'];
|
|
$columns = $input['columns']; // array
|
|
$batchRows = $input['batch_rows']; // array di righe (ognuna array di celle)
|
|
$batchExcelrows = $input['batch_excelrows']; // array di excelrow paralleli a batchRows
|
|
|
|
$user_id = $iduserlogin ?? 1;
|
|
|
|
function normalizeColName($s): string
|
|
{
|
|
$s = (string)$s;
|
|
$s = str_replace(
|
|
["\xC2\xA0", "\xE2\x80\xAF", "\xE2\x80\x87", "\xE2\x80\x89", "\xE2\x80\x8A", "\xEF\xBB\xBF"],
|
|
' ',
|
|
$s
|
|
);
|
|
$s = preg_replace('/\s+/u', ' ', $s);
|
|
return trim((string)$s);
|
|
}
|
|
|
|
$db = DBHandlerSelect::getInstance();
|
|
$pdo = $db->getConnection();
|
|
|
|
// Recupera i mapping
|
|
$stmt = $pdo->prepare("SELECT id, excel_column, data_type, is_required, manual_default, is_manual, field_label, field_id, main_field, auto_value FROM template_mapping WHERE template_id = ?");
|
|
$stmt->execute([$template_id]);
|
|
$allMappings = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (empty($allMappings)) {
|
|
echo json_encode(['ok' => false, 'error' => 'Nessun mapping trovato per il template']);
|
|
exit;
|
|
}
|
|
|
|
// idclient di default (una volta sola)
|
|
$template_stmt = $pdo->prepare("SELECT idclient FROM excel_templates WHERE id = ?");
|
|
$template_stmt->execute([$template_id]);
|
|
$tpl = $template_stmt->fetch(PDO::FETCH_ASSOC);
|
|
$default_idclient = $tpl['idclient'] ?? null;
|
|
|
|
// lims_user_id per il campo 244 (una volta sola)
|
|
$stmtUser = $pdo->prepare("SELECT lims_user_id FROM auth_users WHERE id = ? LIMIT 1");
|
|
$stmtUser->execute([(int)$user_id]);
|
|
$limsUserId = $stmtUser->fetchColumn();
|
|
$limsUserId = ($limsUserId !== false && $limsUserId !== null && $limsUserId !== '') ? (string)$limsUserId : '';
|
|
|
|
$stmtMap = $pdo->prepare("SELECT id FROM template_mapping WHERE template_id = ? AND field_id = 244 LIMIT 1");
|
|
$stmtMap->execute([(int)$template_id]);
|
|
$mappingId244 = (int)$stmtMap->fetchColumn();
|
|
|
|
$insertedIds = [];
|
|
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
$insDatadb = $pdo->prepare("INSERT INTO datadb (templateid, importreferencecode, filename_import, status, user_id, limscode, importdate, excelrow, idclient) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
|
$insDetail = $pdo->prepare("INSERT INTO import_data_details (id, mapping_id, field_value) VALUES (?, ?, ?)");
|
|
|
|
foreach ($batchRows as $i => $row) {
|
|
$excelrow = $batchExcelrows[$i] ?? null;
|
|
if ($row === null || $excelrow === null) {
|
|
continue;
|
|
}
|
|
|
|
$insDatadb->execute([
|
|
$template_id,
|
|
$importReferenceCode,
|
|
$newFilename,
|
|
'i',
|
|
$user_id,
|
|
null,
|
|
date('Y-m-d'),
|
|
$excelrow,
|
|
$default_idclient
|
|
]);
|
|
|
|
$iddatadb = $pdo->lastInsertId();
|
|
$insertedIds[] = $iddatadb;
|
|
|
|
foreach ($allMappings as $mapping) {
|
|
$fieldValue = null;
|
|
if (!$mapping['is_manual']) {
|
|
$excelColumn = trim($mapping['excel_column']);
|
|
$excelColumnIndex = array_search(
|
|
normalizeColName($excelColumn),
|
|
array_map('normalizeColName', $columns)
|
|
);
|
|
if ($excelColumnIndex !== false && isset($row[$excelColumnIndex]) && $row[$excelColumnIndex] !== '') {
|
|
$fieldValue = $row[$excelColumnIndex];
|
|
} else {
|
|
$fieldValue = $mapping['manual_default'] ?? '';
|
|
}
|
|
switch ($mapping['data_type']) {
|
|
case 'INT':
|
|
$fieldValue = is_numeric($fieldValue) ? (int)$fieldValue : ($mapping['manual_default'] ?? 0);
|
|
break;
|
|
case 'DATE':
|
|
$fieldValue = !empty($fieldValue) ? date('Y-m-d', strtotime($fieldValue)) : ($mapping['manual_default'] === 'today' ? date('Y-m-d') : ($mapping['manual_default'] ?? ''));
|
|
break;
|
|
case 'CHAR':
|
|
$fieldValue = !empty($fieldValue) ? substr((string)$fieldValue, 0, 1) : ($mapping['manual_default'] ?? '');
|
|
break;
|
|
case 'Testo':
|
|
case 'VARCHAR':
|
|
default:
|
|
$fieldValue = !empty($fieldValue) ? (string)$fieldValue : ($mapping['manual_default'] ?? '');
|
|
break;
|
|
}
|
|
} else {
|
|
$fieldValue = $mapping['manual_default'] ?? '';
|
|
if ($mapping['data_type'] === 'DATE' && $mapping['manual_default'] === 'today') {
|
|
$fieldValue = date('Y-m-d');
|
|
}
|
|
}
|
|
if (($fieldValue === null || $fieldValue === '') && !empty($mapping['auto_value']) && $mapping['auto_value'] !== 'none') {
|
|
if ($mapping['auto_value'] === 'import_date') {
|
|
$fieldValue = date('Y-m-d');
|
|
} elseif ($mapping['auto_value'] === 'import_time') {
|
|
$fieldValue = date('H:i');
|
|
}
|
|
}
|
|
|
|
$insDetail->execute([$iddatadb, $mapping['id'], $fieldValue]);
|
|
}
|
|
|
|
// Campo 244 (Accettatore) con lims_user_id
|
|
if ($limsUserId !== '' && $mappingId244 > 0) {
|
|
$insDetail->execute([$iddatadb, $mappingId244, $limsUserId]);
|
|
}
|
|
}
|
|
|
|
$pdo->commit();
|
|
|
|
echo json_encode([
|
|
'ok' => true,
|
|
'inserted' => count($insertedIds)
|
|
]);
|
|
exit;
|
|
} catch (Exception $e) {
|
|
if ($pdo->inTransaction()) $pdo->rollBack();
|
|
error_log("[BATCH IMPORT] " . $e->getMessage());
|
|
echo json_encode([
|
|
'ok' => false,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
exit;
|
|
}
|