![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/cartforge.co/app/code/Xtento/StockImport/Model/Processor/ |
<?php /** * Product: Xtento_StockImport * ID: u66QkJ5rBwmimhUzUElhIKqqWRvsbhC3WLqSMk5AjmQ= * Last Modified: 2023-10-26T10:54:57+00:00 * File: app/code/Xtento/StockImport/Model/Processor/Csv.php * Copyright: Copyright (c) XTENTO GmbH & Co. KG <[email protected]> / All rights reserved. */ namespace Xtento\StockImport\Model\Processor; use Magento\Framework\Exception\LocalizedException; class Csv extends AbstractProcessor { protected $config = []; protected $headerRow; protected $rowData; protected function initConfiguration() { if (!$this->config) { # Load configuration: $this->config = [ 'IMPORT_SKIP_HEADER' => $this->getConfigValue('skip_header'), 'IMPORT_DELIMITER' => $this->getConfigValue('delimiter'), 'IMPORT_ENCLOSURE' => $this->getConfigValue('enclosure'), ]; # Get mapping model $this->mappingModel = $this->mappingFieldsFactory->create(); $this->mappingModel->setMappingData($this->getConfigValue('mapping')); # Load mapping $this->mapping = $this->mappingModel->getMapping(); if ($this->mappingModel->getMappedFieldsForField('product_identifier') === false) { throw new LocalizedException( __( 'Please configure the CSV processor in the configuration section of this import profile. The Product Identifier field may not be empty and must be mapped.' ) ); } if ($this->config['IMPORT_DELIMITER'] == '') { throw new LocalizedException( __( 'Please configure the CSV processor in the configuration section of this import profile. The Field Delimiter may not be empty.' ) ); } if ($this->config['IMPORT_ENCLOSURE'] == '') { $this->config['IMPORT_ENCLOSURE'] = '"'; } if (strtolower($this->config['IMPORT_DELIMITER']) == 'tab' || $this->config['IMPORT_DELIMITER'] == '\t' || $this->config['IMPORT_DELIMITER'] == chr(9) ) { $this->config['IMPORT_DELIMITER'] = "\t"; } if (strtolower($this->config['IMPORT_DELIMITER']) == 'flf') { $this->config['IMPORT_FIXED_LENGTH_FORMAT'] = true; } else { $this->config['IMPORT_FIXED_LENGTH_FORMAT'] = false; } } } public function getRowsToProcess($filesToProcess) { try { @ini_set('auto_detect_line_endings', 1); } catch (\Exception $e) {} $magentoMSISupport = $this->entityHelper->getMagentoMSISupport(); # Updates to process, later the result $updatesInFilesToProcess = []; $this->initConfiguration(); foreach ($filesToProcess as $importFile) { $data = $importFile['data']; $filename = $importFile['filename']; unset($importFile['data']); // Remove UTF8 BOM $bom = pack('H*', 'EFBBBF'); $data = preg_replace("/^$bom/", '', $data); $updatesToProcess = []; $foundFields = []; $rowCounter = 0; if ($this->config['IMPORT_FIXED_LENGTH_FORMAT']) { // Fixed length format foreach (explode("\n", $data) as $line) { $this->rowData = $line; $rowCounter++; if ($rowCounter == 1) { // Skip the header if ($this->config['IMPORT_SKIP_HEADER'] == true) { continue; } } $skipRow = false; $stockId = false; // First run: Get product identifier for row $rowIdentifier = ""; foreach ($this->mappingModel->getMapping() as $fieldId => $fieldData) { if ($fieldData['field'] == 'product_identifier') { $rowIdentifier = $this->getFieldData($fieldData); } if ($fieldData['field'] == 'stock_id' || $fieldData['field'] == 'source_code') { $stockId = $this->getFieldData($fieldData); } // Check if row should be skipped. if (true === $this->fieldsConfiguration->checkSkipImport( $fieldData['field'], $fieldData['config'], $this ) ) { $skipRow = true; } } if (empty($rowIdentifier)) { continue; } // Check if stock ID was specified, if not, use default stock ID if ($magentoMSISupport && empty($stockId)) { $stockId = 'default'; } if (empty($stockId)) { $stockId = 1; } if (!isset($updatesToProcess[$stockId])) { $updatesToProcess[$stockId] = []; } if ($skipRow) { $rowIdentifier .= '_SKIP'; } if (!isset($updatesToProcess[$stockId][$rowIdentifier])) { $updatesToProcess[$stockId][$rowIdentifier] = []; $rowArray = []; } else { $rowArray = $updatesToProcess[$stockId][$rowIdentifier]; } foreach ($this->mappingModel->getMapping() as $fieldId => $fieldData) { if (isset($fieldData['disabled']) && $fieldData['disabled']) { continue; } $fieldName = $fieldData['field']; $fieldValue = $this->getFieldData($fieldData); if ($fieldValue !== '') { if (!in_array($fieldName, $foundFields)) { $foundFields[] = $fieldName; } #$rowArray[$fieldName] = $this->mappingModel->formatField($fieldName, $fieldValue); if (isset($fieldData['group']) && !empty($fieldData['group'])) { $rowArray[$fieldData['group']][$rowCounter - 1][$fieldName] = $this->mappingModel->formatField( $fieldName, $fieldValue ); } else { if ($fieldName == 'qty' && array_key_exists($fieldName, $rowArray)) { // Sum up multiple warehouses qtys for example to $updates $origFieldValue = $fieldValue; $fieldValue = (float)$fieldValue + (float)$rowArray[$fieldName]; if ($origFieldValue[0] == '-' || $origFieldValue[0] == '+') { if ($fieldValue >= 0) { $fieldValue = "+" . $fieldValue; } else { $fieldValue = (string)$fieldValue; // Required so formatField is able to check this properly } } } $rowArray[$fieldName] = $this->mappingModel->formatField($fieldName, $fieldValue); } } } if ($skipRow) { // Field in field_configuration XML determined that this row should be skipped. "<skip>" parameter in XML field config $rowArray['SKIP_FLAG'] = true; } $updatesToProcess[$stockId][$rowIdentifier] = $rowArray; } } else { // Traditional CSV format $fileHandle = fopen('php://memory', 'rw'); fwrite($fileHandle, $data); rewind($fileHandle); $this->headerRow = []; while (($rowData = fgetcsv( $fileHandle, 0, $this->config['IMPORT_DELIMITER'], $this->config['IMPORT_ENCLOSURE'] )) !== false) { $this->rowData = $rowData; $rowCounter++; if ($rowCounter == 1) { // Skip the header line but parse it for field names. $numberOfFields = count($rowData); for ($i = 0; $i < $numberOfFields; $i++) { $this->headerRow[$rowData[$i]] = $i; } if ($this->config['IMPORT_SKIP_HEADER'] == true) { continue; } } $skipRow = false; $stockId = false; // First run: Get product identifier for row $rowIdentifier = ""; foreach ($this->mappingModel->getMapping() as $fieldId => $fieldData) { if ($fieldData['field'] == 'product_identifier') { $fieldValue = $this->getFieldData($fieldData); if (!empty($fieldValue)) { $rowIdentifier = $fieldValue; } } if ($fieldData['field'] == 'stock_id' || $fieldData['field'] == 'source_code') { $stockId = $this->getFieldData($fieldData); } // Check if row should be skipped. if (true === $this->fieldsConfiguration->checkSkipImport( $fieldData['field'], $fieldData['config'], $this ) ) { $skipRow = true; } } if (empty($rowIdentifier)) { continue; } // Check if stock ID was specified, if not, use default stock ID if ($magentoMSISupport && empty($stockId)) { $stockId = 'default'; } if (empty($stockId)) { $stockId = 1; } if (!isset($updatesToProcess[$stockId])) { $updatesToProcess[$stockId] = []; } if ($skipRow) { $rowIdentifier .= '_SKIP'; } if (!isset($updatesToProcess[$stockId][$rowIdentifier])) { $updatesToProcess[$stockId][$rowIdentifier] = []; $rowArray = []; } else { $rowArray = $updatesToProcess[$stockId][$rowIdentifier]; } foreach ($this->mappingModel->getMapping() as $fieldId => $fieldData) { if (isset($fieldData['disabled']) && $fieldData['disabled']) { continue; } $fieldName = $fieldData['field']; $fieldValue = $this->getFieldData($fieldData); if ($fieldValue !== '') { if (!in_array($fieldName, $foundFields)) { $foundFields[] = $fieldName; } if (isset($fieldData['group']) && !empty($fieldData['group'])) { $rowArray[$fieldData['group']][$rowCounter - 1][$fieldName] = $this->mappingModel->formatField( $fieldName, $fieldValue ); } else { if ($fieldName == 'qty' && array_key_exists($fieldName, $rowArray)) { // Sum up multiple warehouses qtys for example to $updates $origFieldValue = $fieldValue; $fieldValue = (float)$fieldValue + (float)$rowArray[$fieldName]; if ($origFieldValue[0] == '-' || $origFieldValue[0] == '+') { if ($fieldValue >= 0) { $fieldValue = "+" . $fieldValue; } else { $fieldValue = (string)$fieldValue; // Required so formatField is able to check this properly } } } $rowArray[$fieldName] = $this->mappingModel->formatField($fieldName, $fieldValue); } } } if ($skipRow) { // Field in field_configuration XML determined that this row should be skipped. "<skip>" parameter in XML field config $rowArray['SKIP_FLAG'] = true; } $updatesToProcess[$stockId][$rowIdentifier] = $rowArray; } } // Output the header row in a nicer string $hasHeaderRow = ($this->config['IMPORT_SKIP_HEADER']) ? "Yes" : "No"; $headerRowTemp = $this->headerRow ? $this->headerRow : []; array_walk($headerRowTemp, function(&$i, $k) { $i = " \"$k\"=\"$i\""; }); // File processed $updatesInFilesToProcess[] = [ "FILE_INFORMATION" => $importFile, "HEADER_ROW" => "Skip header row: " . $hasHeaderRow . " | Header row:" . implode("", $headerRowTemp), "FIELDS" => $foundFields, "ITEMS" => $updatesToProcess ]; } try { @ini_set('auto_detect_line_endings', 0); } catch (\Exception $e) {} /*ini_set('xdebug.var_display_max_depth', 10); \Zend_Debug::dump($updatesToProcess); die();*/ return $updatesInFilesToProcess; } public function getFieldPos($mappedField) { if (!is_numeric($mappedField) && isset($this->headerRow[$mappedField])) { return $this->headerRow[$mappedField]; } else { return $mappedField; } } /** * @param $fieldData * * @return mixed * Wrapper function to manipulate field data returned */ public function getFieldData($fieldData) { $returnData = $this->getFieldDataRaw($fieldData); $returnData = $this->fieldsConfiguration->manipulateFieldFetched( $fieldData['field'], $returnData, $fieldData['config'], $this ); return $returnData; } public function getFieldDataRaw($fieldData, $bypassFieldConfiguration = false) { if ($this->config['IMPORT_FIXED_LENGTH_FORMAT']) { $fieldPosition = explode("-", $this->getFieldPos($fieldData['value'])); if (!isset($fieldPosition[1])) { $data = ""; if (isset($fieldData['id'])) { $data = $this->mappingModel->getDefaultValue($fieldData['id']); } } else { $field = $fieldData['field']; $data = trim(substr($this->rowData, $fieldPosition[0] - 1, $fieldPosition[1] - $fieldPosition[0])); if (!$bypassFieldConfiguration) { $data = $this->fieldsConfiguration->handleField($field, $data, $fieldData['config']); } if (($data === '' || $data === null || $data === false) && isset($fieldData['id'])) { // Try to get the default value at least.. otherwise '' $data = $this->mappingModel->getDefaultValue($fieldData['id']); } } } else { $field = $fieldData['field']; $fieldPos = $this->getFieldPos($fieldData['value']); if (isset($this->rowData[$fieldPos])) { $data = $this->rowData[$fieldPos]; if (!$bypassFieldConfiguration) { $data = $this->fieldsConfiguration->handleField($field, $data, $fieldData['config']); } if (($data === '' || $data === null || $data === false) && isset($fieldData['id'])) { // Try to get the default value at least.. otherwise '' $data = $this->mappingModel->getDefaultValue($fieldData['id']); } } else { if (!$bypassFieldConfiguration) { $data = $this->fieldsConfiguration->handleField($field, '', $fieldData['config']); } else { $data = ''; } if (empty($data) && isset($fieldData['id'])) { // Try to get the default value at least.. otherwise '' $data = $this->mappingModel->getDefaultValue($fieldData['id']); } } } return trim($data); } }