CSVImporter.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Giancarlo
  5. * Date: 24/07/2018
  6. * Time: 17:36
  7. */
  8. namespace OpenMind\DataManager;
  9. class ENUM_DIMATEC_CSV
  10. {
  11. public const MXORDI = 7;
  12. public const MXARTI = 10;
  13. public const MXDESC = 12;
  14. public const MXQTMI = 14;
  15. public const MXUBIC = 16;
  16. }
  17. class CSVImporter
  18. {
  19. const LAMBORGHINI_DISTINTA_COLONNE = 20;
  20. const LAMBORGHINI_ARTICLES_COLONNE = 15;
  21. const DIMATEC_LISTA_MOVIMENTI_AS400 = 32;
  22. private $folderToWatch;
  23. private $dbMysql;
  24. private $output;
  25. private $user;
  26. /**
  27. * @return mixed
  28. */
  29. public function getOutput()
  30. {
  31. return $this->output;
  32. }
  33. public function __construct(string $folderPath)
  34. {
  35. $this->user = (int)\F3::get('USER')->id;
  36. $this->folderToWatch = $folderPath;
  37. $this->dbMysql = \F3::get('DB');
  38. }
  39. /**
  40. * Create MySQL temp table for smart operations
  41. *
  42. * @param string $table
  43. * @return string temporary name
  44. */
  45. private function createTmpTableFrom(string $table)
  46. {
  47. $tempTableName = 'tmp' . rand(10, 200);
  48. $this->dbMysql->exec("CREATE TEMPORARY TABLE IF NOT EXISTS `{$tempTableName}` AS (SELECT * FROM `{$table}`)");
  49. return $tempTableName;
  50. }
  51. public function ScanAndStoreData()
  52. {
  53. foreach(glob("{$this->folderToWatch}/*.csv") as $fname) {
  54. if (is_file($fname)) {
  55. $allData = $this->loadCSV($fname);
  56. // Esce se il file non è una distinta, lo riconosce solamente dal numero di colonne del file CSV
  57. if ($allData == NULL)
  58. return;
  59. switch (count($allData[0])) {
  60. case CSVImporter::LAMBORGHINI_DISTINTA_COLONNE:
  61. $this->importOnDatabase($fname, $allData);
  62. break;
  63. case CSVImporter::LAMBORGHINI_ARTICLES_COLONNE:
  64. $this->mysqlLoadCSV(addslashes(realpath($fname)), 'articles');
  65. break;
  66. case CSVImporter::DIMATEC_LISTA_MOVIMENTI_AS400:
  67. //$tmpTableName = $this->createTmpTableFrom('distinte');
  68. $this->dynamicImporter($fname, $allData, function ($data) {
  69. if ($data[0] == "MXIDDM")
  70. return;
  71. try {
  72. $distinta = new \DB\SQL\Mapper($this->dbMysql, 'distinte');
  73. $distinta->idUtente = $this->user;
  74. $distinta->commessa = $data[ENUM_DIMATEC_CSV::MXORDI];
  75. $distinta->barcode = $data[ENUM_DIMATEC_CSV::MXARTI];
  76. $distinta->descrizione = $data[ENUM_DIMATEC_CSV::MXDESC];
  77. $distinta->qta_prelevare = $data[ENUM_DIMATEC_CSV::MXQTMI];
  78. $distinta->save();
  79. } catch (PDOException $e) {
  80. $this->output .= $e->getMessage();
  81. }
  82. });
  83. /* $this->dbMysql->exec(
  84. "INSERT INTO `distinta_esplosa` (commessa, barcode, descrizione, qta_prelevare) " .
  85. "SELECT commessa, barcode, descrizione, SUM(qta_prelevare) as qta_prelevare " .
  86. "FROM `distinte` group by barcode order by commessa"
  87. );*/
  88. $this->markFileAsDone($fname);
  89. break;
  90. default:
  91. $this->output .= "Numero di colonne non riconosciute ne come DISTINTA e ne come LISTA ARTICOLI per il file '{$fname}'";
  92. }
  93. }
  94. }
  95. }
  96. private function loadCSV(string $path): ?array
  97. {
  98. $fh = fopen($path, "r");
  99. $csvData = [];
  100. while (($d = fgetcsv($fh, 0, ';'))) {
  101. if ($d)
  102. $csvData[] = $d;
  103. }
  104. fclose($fh);
  105. return $csvData;
  106. }
  107. /**
  108. * Iterate on $data and call $func to make operations on rows
  109. *
  110. * @param string $fileName
  111. * @param array $data
  112. * @param function(array $dataRow) $func
  113. */
  114. private function dynamicImporter(string $fileName, array $data, $func)
  115. {
  116. foreach($data as $dataRow) {
  117. $func($dataRow);
  118. }
  119. $this->output .= "{$fileName} successfully imported<br/>";
  120. }
  121. /**
  122. * @deprecated from 24/07/2018
  123. * @param string $fileName
  124. * @param array $data
  125. */
  126. private function importOnDatabase(string $fileName, array $data)
  127. {
  128. global $f3;
  129. $this->dbMysql->query("TRUNCATE TABLE `distinte`");
  130. foreach($data as $dataRow) {
  131. $descr = addslashes($dataRow[10]);
  132. $sql = <<<EOL
  133. INSERT INTO distinte (
  134. origine, consegna, commessa, ordine_produzione, data_creazione, time_creazione, destinazione_merci, barcode, descrizione, qta_prelevare, qta_mancante
  135. ) VALUES (
  136. '$dataRow[16]', '$dataRow[0]', '$dataRow[2]', '$dataRow[3]', str_to_date('$dataRow[6]','%d/%m/%Y'), '$dataRow[7]', '$dataRow[15]','$dataRow[9]',
  137. '$descr', '$dataRow[11]', 0
  138. );
  139. EOL;
  140. if (!$this->dbMysql->query($sql)) {
  141. echo "Error on query: {$sql}" . PHP_EOL;
  142. echo $this->dbMysql->error . PHP_EOL;
  143. return;
  144. }
  145. }
  146. echo "{$fileName} successfully imported" . PHP_EOL;
  147. $this->markFileAsDone($fileName);
  148. }
  149. private function markFileAsDone(string $csvPath)
  150. {
  151. rename($csvPath, "{$csvPath}_imported");
  152. }
  153. private function mysqlLoadCSV(string $CSVPath, string $tableName)
  154. {
  155. $this->dbMysql->query("TRUNCATE TABLE `{$tableName}`");
  156. $sql = <<<EOF
  157. LOAD DATA INFILE '$CSVPath' INTO TABLE `$tableName`
  158. FIELDS TERMINATED BY ';' ENCLOSED BY '' LINES TERMINATED BY '\\n';
  159. EOF;
  160. if (!$this->dbMysql->query($sql)) {
  161. echo "Error on query: {$sql}" . PHP_EOL;
  162. echo $this->dbMysql->error . PHP_EOL;
  163. return;
  164. }
  165. echo "{$CSVPath} successfully imported" . PHP_EOL;
  166. $this->markFileAsDone($CSVPath);
  167. }
  168. }