omlinkimporter.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }
  39. }
  40. }
  41. }
  42. private function loadCSV(string $path): ?array
  43. {
  44. $fh = fopen($path, "r");
  45. $csvData = [];
  46. while (($d = fgetcsv($fh, 0, ';'))) {
  47. if ($d)
  48. $csvData[] = $d;
  49. }
  50. fclose($fh);
  51. return $csvData;
  52. }
  53. private function importOnDatabase(string $fileName, array $data)
  54. {
  55. $this->dbMysql->query("TRUNCATE TABLE `distinte`");
  56. foreach ($data as $dataRow) {
  57. $descr = $this->dbMysql->escape_string($dataRow[10]);
  58. $sql = <<<EOL
  59. INSERT INTO distinte (
  60. origine, consegna, commessa, ordine_produzione, data_creazione, time_creazione, destinazione_merci, barcode, descrizione, qta_prelevare, qta_mancante
  61. ) VALUES (
  62. '$dataRow[16]', '$dataRow[0]', '$dataRow[2]', '$dataRow[3]', str_to_date('$dataRow[6]','%d/%m/%Y'), '$dataRow[7]', '$dataRow[15]','$dataRow[9]',
  63. '$descr', '$dataRow[11]', 0
  64. );
  65. EOL;
  66. if (!$this->dbMysql->query($sql)) {
  67. echo "Error on query: {$sql}" . PHP_EOL;
  68. echo $this->dbMysql->error . PHP_EOL;
  69. return;
  70. }
  71. }
  72. echo "{$fileName} successfully imported" . PHP_EOL;
  73. $this->markFileAsDone($fileName);
  74. }
  75. private function markFileAsDone(string $csvPath)
  76. {
  77. rename($csvPath, "{$csvPath}_imported");
  78. }
  79. private function mysqlLoadCSV(string $CSVPath, string $tableName)
  80. {
  81. $this->dbMysql->query("TRUNCATE TABLE `{$tableName}`");
  82. $sql = <<<EOF
  83. LOAD DATA INFILE '$CSVPath' INTO TABLE `$tableName`
  84. FIELDS TERMINATED BY ';' ENCLOSED BY '' LINES TERMINATED BY '\\n';
  85. EOF;
  86. if (!$this->dbMysql->query($sql)) {
  87. echo "Error on query: {$sql}" . PHP_EOL;
  88. echo $this->dbMysql->error . PHP_EOL;
  89. return;
  90. }
  91. echo "{$CSVPath} successfully imported" . PHP_EOL;
  92. $this->markFileAsDone($CSVPath);
  93. }
  94. }
  95. echo PHP_EOL . 'Scanning import folder for new CSV to import...' . PHP_EOL . PHP_EOL;
  96. $impoter = new CSVImporter('import', 'omlink_lamborghini', 'root', 'giancarlo');
  97. $impoter->ScanAndStoreData();
  98. echo PHP_EOL . '... end import' . PHP_EOL;