PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/typo3conf/ext/baumer_serial/Classes/Command/ShipmentImportCommandController.php

https://gitlab.com/uralgenc/bourdon-haenni.com
PHP | 184 lines | 105 code | 20 blank | 59 comment | 17 complexity | 0eb843c8ace07587daa9bc8dc000ad39 MD5 | raw file
  1. <?php
  2. namespace Baumer\BaumerSerial\Command;
  3. /*****************************************************************
  4. * Copyright notice
  5. *
  6. * (c) 2012 Lars Trebing <lars.trebing@typovision.de>
  7. * 2014 Oliver Schulz <oliver.schulz@typovision.de>
  8. *
  9. * All rights reserved
  10. *
  11. * This script is part of the TYPO3 project. The TYPO3 project is
  12. * free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * The GNU General Public License can be found at
  18. * http://www.gnu.org/copyleft/gpl.html.
  19. *
  20. * This script is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * This copyright notice MUST APPEAR in all copies of the script!
  26. *****************************************************************/
  27. use \Baumer\Baumer\Command\AbstractCommandController;
  28. use \TYPO3\CMS\Core\Messaging\FlashMessageQueue;
  29. use \TYPO3\CMS\Core\Utility\GeneralUtility;
  30. use \TYPO3\CMS\Core\Messaging\FlashMessage;
  31. /**
  32. * Class MigrationCommandController
  33. *
  34. * @author Oliver Schulz <oliver.schulz@typovision.de>
  35. *
  36. * @package BaumerSerial
  37. * @subpackage Command
  38. */
  39. class ShipmentImportCommandController extends AbstractCommandController
  40. {
  41. /**
  42. * Import serial numbers from the newest file in the given directory
  43. *
  44. * ./typo3/cli_dispatch.phpsh extbase shipmentimport:import /your/absolute/directory/
  45. *
  46. * @param string $directory Path containing the serial number files
  47. *
  48. * @return TRUE if successful
  49. * @throws \RuntimeException if not successful
  50. */
  51. public function importCommand($directory)
  52. {
  53. $fileMD5 = '';
  54. if (($handle = @opendir($directory)) === false) {
  55. /** @var FlashMessage $flashMessage */
  56. $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, 'Error reading directory',
  57. 'Import failed', FlashMessage::ERROR);
  58. $this->flashMessageQueue->addMessage($flashMessage);
  59. throw new \RuntimeException('Error reading directory', 1410388009);
  60. }
  61. $filectime = 0;
  62. while (false !== ($fileName = readdir($handle))) {
  63. $file = $directory . '/' . $fileName;
  64. // Get newest file
  65. if (mime_content_type($file) === 'text/plain' && filectime($file) > $filectime) {
  66. $filectime = filectime($file);
  67. $fileMD5 = md5_file($file);
  68. $fileToImport = $file;
  69. }
  70. }
  71. if (!isset($fileToImport) || !isset($fileMD5)) {
  72. /** @var FlashMessage $flashMessage */
  73. $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, 'No file to import found',
  74. 'Import failed', FlashMessage::ERROR);
  75. $this->flashMessageQueue->addMessage($flashMessage);
  76. throw new \RuntimeException('No file to import found', 1410390402);
  77. }
  78. // Check if file was already imported
  79. $lastImportedFile = $this->db->exec_SELECTgetSingleRow('*', 'tx_baumerserial_import_lastfile',
  80. 'md5 = ' . $this->db->fullQuoteStr($fileMD5, 'tx_baumerserial_import_lastfile'));
  81. if ($lastImportedFile !== null && $lastImportedFile !== false) {
  82. /** @var FlashMessage $flashMessage */
  83. $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, 'File already imported', 'Import failed',
  84. FlashMessage::ERROR);
  85. $this->flashMessageQueue->addMessage($flashMessage);
  86. throw new \RuntimeException('File already imported', 1410390508);
  87. }
  88. $this->db->exec_INSERTquery('tx_baumerserial_import_lastfile', array(
  89. 'file' => $fileToImport,
  90. 'md5' => $fileMD5,
  91. 'tstamp' => time()
  92. ));
  93. $fileHandle = @fopen($fileToImport, 'r');
  94. if ($fileHandle === false) {
  95. /** @var FlashMessage $flashMessage */
  96. $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, 'Error reading file', 'Import failed',
  97. FlashMessage::ERROR);
  98. $this->flashMessageQueue->addMessage($flashMessage);
  99. throw new \RuntimeException('Error reading file', 1410390563);
  100. }
  101. $count = 0;
  102. $currentRecordFingerprint = '';
  103. $imported = false;
  104. while (($line = fgets($fileHandle)) !== false) {
  105. $line = trim($line);
  106. if (preg_match('/^(\d{10})\|(\d{18})\|(\d{18})\|(\d{10})\|(.{132})\|(\d{7}) \|[ ]{10}\|[ ]{10}\|\d{6}\|$/',
  107. $line, $match)) {
  108. $recordFingerprint = $match[1] . $match[2] . $match[3] . $match[4] . $match[6];
  109. if (isset($shipment) && $recordFingerprint === $currentRecordFingerprint) {
  110. $shipment['description'] .= "\n" . trim(iconv('ISO-8859-1', 'UTF-8', $match[5]));
  111. } else {
  112. if (isset($shipment)) {
  113. // save
  114. $imported = true;
  115. $this->db->exec_INSERTquery('tx_baumerserial_domain_model_shipment', $shipment);
  116. unset($shipment);
  117. }
  118. $shipment = array(
  119. 'pid' => 0,
  120. 'crdate' => time(),
  121. 'tstamp' => time(),
  122. 'commission_number' => ltrim($match[1], '0'),
  123. 'part_number' => ltrim($match[2], '0'),
  124. 'serial_number' => ltrim($match[3], '0'),
  125. 'invoice_number' => ltrim($match[4], '0'),
  126. 'description' => trim(iconv('ISO-8859-1', 'UTF-8', $match[5])),
  127. 'invoice_date' => $this->parseDate($match[6])
  128. );
  129. $currentRecordFingerprint = $recordFingerprint;
  130. }
  131. }
  132. $count++;
  133. }
  134. if (isset($shipment)) {
  135. // save
  136. $imported = true;
  137. $this->db->exec_INSERTquery('tx_baumerserial_domain_model_shipment', $shipment);
  138. unset($shipment);
  139. }
  140. if ($imported === false) {
  141. /** @var FlashMessage $flashMessage */
  142. $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, 'No matching lines in file found',
  143. 'Import failed', FlashMessage::ERROR);
  144. $this->flashMessageQueue->addMessage($flashMessage);
  145. throw new \RuntimeException('No matching lines in file found', 1410390682);
  146. }
  147. return true;
  148. }
  149. /**
  150. * Parse a YYYMMDD date string (where YYY is the number of years since 1900,
  151. * e.g. 114 for 2014) to a Unix timestamp
  152. *
  153. * @param $dateString
  154. *
  155. * @return integer $dateString
  156. */
  157. protected function parseDate($dateString)
  158. {
  159. $year = 1900 + (int)substr($dateString, 0, 3);
  160. $month = (int)substr($dateString, 3, 2);
  161. $day = (int)substr($dateString, 5, 2);
  162. return mktime(12, 0, 0, $month, $day, $year);
  163. }
  164. }