omlinkimporter.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @author Open Mind
  4. * @date 11/04/2018
  5. * @time 09:48
  6. */
  7. const DISTINTA_COLONNE = 20;
  8. const ARTICLES_COLONNE = 15;
  9. class CSVImporter
  10. {
  11. private $currentFileName;
  12. private $folderToWatch;
  13. private $dbMysql;
  14. public function __construct($folderPath, $databaseName, $dbusername, $dbpassword)
  15. {
  16. $this->currentFileName = '';
  17. $this->folderToWatch = $folderPath;
  18. $this->dbMysql = new mysqli('localhost', $dbusername, $dbpassword, $databaseName);
  19. if ($this->dbMysql->connect_error) {
  20. die('Database connection error (' . $this->dbMysql->connect_errno . ') ' . $this->dbMysql->connect_error);
  21. }
  22. }
  23. public function ScanAndStoreData()
  24. {
  25. foreach (glob("{$this->folderToWatch}/*.csv") as $fname) {
  26. if (is_file($fname)) {
  27. $allData = $this->loadCSV($fname);
  28. // Esce se il file non è una distinta, lo riconosce solamente dal numero di colonne del file CSV
  29. if ($allData == NULL)
  30. return;
  31. switch (count($allData[0])) {
  32. case DISTINTA_COLONNE:
  33. $this->importOnDatabase($fname, $allData);
  34. break;
  35. case ARTICLES_COLONNE:
  36. $this->mysqlLoadCSV(addslashes(realpath($fname)), 'articles');
  37. break;
  38. default:
  39. echo "Numero di colonne non riconosciute ne come DISTINTA e ne come LISTA ARTICOLI per il file '{$fname}'";
  40. }
  41. }
  42. }
  43. }
  44. private function loadCSV(string $path): ?array
  45. {
  46. $fh = fopen($path, "r");
  47. $csvData = [];
  48. while (($d = fgetcsv($fh, 0, ';'))) {
  49. if ($d)
  50. $csvData[] = $d;
  51. }
  52. fclose($fh);
  53. return $csvData;
  54. }
  55. private function importOnDatabase(string $fileName, array $data)
  56. {
  57. $this->dbMysql->query("TRUNCATE TABLE `distinte`");
  58. foreach ($data as $dataRow) {
  59. $descr = $this->dbMysql->escape_string($dataRow[10]);
  60. $sql = <<<EOL
  61. INSERT INTO distinte (
  62. origine, consegna, commessa, ordine_produzione, data_creazione, time_creazione, destinazione_merci, barcode, descrizione, qta_prelevare, qta_mancante
  63. ) VALUES (
  64. '$dataRow[16]', '$dataRow[0]', '$dataRow[2]', '$dataRow[3]', str_to_date('$dataRow[6]','%d/%m/%Y'), '$dataRow[7]', '$dataRow[15]','$dataRow[9]',
  65. '$descr', '$dataRow[11]', 0
  66. );
  67. EOL;
  68. if (!$this->dbMysql->query($sql)) {
  69. echo "Error on query: {$sql}" . PHP_EOL;
  70. echo $this->dbMysql->error . PHP_EOL;
  71. return;
  72. }
  73. }
  74. echo "{$fileName} successfully imported" . PHP_EOL;
  75. $this->markFileAsDone($fileName);
  76. }
  77. private function markFileAsDone(string $csvPath)
  78. {
  79. rename($csvPath, "{$csvPath}_imported");
  80. }
  81. private function mysqlLoadCSV(string $CSVPath, string $tableName)
  82. {
  83. $this->dbMysql->query("TRUNCATE TABLE `{$tableName}`");
  84. $sql = <<<EOF
  85. LOAD DATA INFILE '$CSVPath' INTO TABLE `$tableName`
  86. FIELDS TERMINATED BY ';' ENCLOSED BY '' LINES TERMINATED BY '\\n';
  87. EOF;
  88. if (!$this->dbMysql->query($sql)) {
  89. echo "Error on query: {$sql}" . PHP_EOL;
  90. echo $this->dbMysql->error . PHP_EOL;
  91. return;
  92. }
  93. echo "{$CSVPath} successfully imported" . PHP_EOL;
  94. $this->markFileAsDone($CSVPath);
  95. }
  96. }
  97. echo PHP_EOL . 'Scanning import folder for new CSV to import...' . PHP_EOL . PHP_EOL;
  98. $impoter = new CSVImporter('import', 'omlink_lamborghini', 'root', 'giancarlo');
  99. $impoter->ScanAndStoreData();
  100. echo PHP_EOL . '... end import' . PHP_EOL;