omlinkimporter.php 3.6 KB

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