*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*/
/**
* \file datanorm.php
* \ingroup importzugferd
* \brief Datanorm catalog management page
*/
// Load Dolibarr environment
$res = 0;
if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
$res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
}
$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
$tmp2 = realpath(__FILE__);
$i = strlen($tmp) - 1;
$j = strlen($tmp2) - 1;
while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
$i--;
$j--;
}
if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) {
$res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
}
if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) {
$res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
}
if (!$res && file_exists("../main.inc.php")) {
$res = @include "../main.inc.php";
}
if (!$res && file_exists("../../main.inc.php")) {
$res = @include "../../main.inc.php";
}
if (!$res) {
die("Include of main fails");
}
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once './class/datanorm.class.php';
require_once './class/datanormparser.class.php';
require_once './lib/importzugferd.lib.php';
$langs->loadLangs(array('importzugferd@importzugferd', 'companies', 'products'));
// Access control
if (!$user->hasRight('importzugferd', 'datanorm', 'write')) {
accessforbidden();
}
// Parameters
$action = GETPOST('action', 'aZ09');
$confirm = GETPOST('confirm', 'alpha');
$fk_soc = GETPOSTINT('fk_soc');
$id = GETPOSTINT('id');
// Objects
$form = new Form($db);
$datanorm = new Datanorm($db);
/*
* Actions
*/
// Upload Datanorm file
if ($action == 'upload' && !empty($_FILES['datanormfile']['name']) && $fk_soc > 0) {
$error = 0;
// Check supplier exists and is a supplier
$supplier = new Societe($db);
if ($supplier->fetch($fk_soc) <= 0 || $supplier->fournisseur != 1) {
setEventMessages($langs->trans('ErrorSupplierNotFound'), null, 'errors');
$error++;
}
if (!$error) {
// Create upload directory
$upload_dir = $conf->importzugferd->dir_output.'/datanorm/'.$fk_soc;
if (!dol_is_dir($upload_dir)) {
dol_mkdir($upload_dir);
}
// Handle file upload
$uploaded_files = array();
// Check if multiple files or single file
if (is_array($_FILES['datanormfile']['name'])) {
$file_count = count($_FILES['datanormfile']['name']);
for ($i = 0; $i < $file_count; $i++) {
if ($_FILES['datanormfile']['error'][$i] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES['datanormfile']['tmp_name'][$i];
$name = $_FILES['datanormfile']['name'][$i];
$dest = $upload_dir.'/'.$name;
if (dol_move_uploaded_file($tmp_name, $dest, 1) > 0) {
$uploaded_files[] = $dest;
}
}
}
} else {
if ($_FILES['datanormfile']['error'] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES['datanormfile']['tmp_name'];
$name = $_FILES['datanormfile']['name'];
$dest = $upload_dir.'/'.$name;
if (dol_move_uploaded_file($tmp_name, $dest, 1) > 0) {
$uploaded_files[] = $dest;
}
}
}
if (empty($uploaded_files)) {
setEventMessages($langs->trans('ErrorUploadFailed'), null, 'errors');
$error++;
}
}
if (!$error && !empty($uploaded_files)) {
// Use streaming import for large files (directory-based)
$delete_existing = GETPOST('delete_existing', 'int') ? true : false;
$imported = $datanorm->importFromDirectoryStreaming($user, $fk_soc, $upload_dir, $delete_existing);
if ($imported > 0) {
setEventMessages($langs->trans('DatanormImportSuccess', $imported), null, 'mesgs');
} elseif ($imported == 0) {
setEventMessages($langs->trans('DatanormNoArticlesFound'), null, 'warnings');
} else {
setEventMessages($langs->trans('DatanormImportFailed').': '.$datanorm->error, null, 'errors');
}
}
}
// Delete all articles for supplier
if ($action == 'delete' && $confirm == 'yes' && $fk_soc > 0) {
$result = $datanorm->deleteAllBySupplier($user, $fk_soc);
if ($result >= 0) {
setEventMessages($langs->trans('DatanormDeleted', $result), null, 'mesgs');
} else {
setEventMessages($langs->trans('DatanormDeleteFailed').': '.$datanorm->error, null, 'errors');
}
$action = '';
}
/*
* View
*/
$title = $langs->trans('DatanormCatalogs');
llxHeader('', $title, '', '', 0, 0, '', '', '', 'mod-importzugferd page-datanorm');
print load_fiche_titre($title, '', 'fa-database');
// Confirmation dialog for delete
if ($action == 'delete' && $fk_soc > 0) {
$supplier = new Societe($db);
$supplier->fetch($fk_soc);
$formconfirm = $form->formconfirm(
$_SERVER["PHP_SELF"].'?fk_soc='.$fk_soc,
$langs->trans('DeleteDatanorm'),
$langs->trans('ConfirmDeleteDatanorm', $supplier->name),
'delete',
'',
0,
1
);
print $formconfirm;
}
// Upload form
print '
';
print '
';
print '
';
print '';
print '| '.$langs->trans('UploadDatanorm').' | ';
print '
';
print '';
print '| ';
print '';
print ' | ';
print '
';
print '
';
print '
';
print '
';
// List of suppliers with Datanorm data
$suppliers = $datanorm->getSuppliersWithData();
print '
';
print '
';
print '';
print '| '.$langs->trans('Supplier').' | ';
print ''.$langs->trans('ArticleCount').' | ';
print ''.$langs->trans('LastImport').' | ';
print ''.$langs->trans('Actions').' | ';
print '
';
if (!empty($suppliers)) {
foreach ($suppliers as $sup) {
print '';
// Supplier name with link
print '| ';
$supplier = new Societe($db);
$supplier->fetch($sup['fk_soc']);
print $supplier->getNomUrl(1, 'supplier');
print ' | ';
// Article count
print '';
print ''.$sup['article_count'].'';
print ' | ';
// Last import
print '';
print dol_print_date($sup['last_import'], 'dayhour');
print ' | ';
// Actions
print '';
// View articles button
print '';
print img_picto($langs->trans('ViewArticles'), 'list');
print '';
// Delete button
print '';
print img_picto($langs->trans('Delete'), 'delete');
print '';
print ' | ';
print '
';
}
} else {
print '';
print '| '.$langs->trans('NoDatanormData').' | ';
print '
';
}
print '
';
print '
';
print '
';
// Settings info
print '
';
print '';
llxFooter();
$db->close();