✏️ 正在编辑: Fatores.php
路径:
/srv/systems_dir/yuppiecred-lidernegocios/library/Core/Banco/Planilha/Fatores.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Core_Banco_Planilha_Fatores { private $_inicioLaco = 1; protected $_colunas = [ 'banco' => 'A', 'tabela' => 'B', 'convenio' => 'C', 'prazo' => 'D', 'fator' => 'E', 'tipo' => 'F', 'dia' => 'G', 'limite_saque' => 'H', 'codigo_tabela' => 'I', ]; public function getColuna($coluna) { return isset($this->_colunas[$coluna]) ? trim($this->_colunas[$coluna]) : null; } public function __construct() { } protected function _uploadAndReadFile() { $upload = new Zend_File_Transfer_Adapter_Http(); $upload->setDestination(UPLOAD_PATH); try { $upload->receive(); } catch ( Zend_File_Transfer_Exception $e ) { $this->_helper->flashMessenger(array( 'error' => $e->getMessage() )); } $extension = strtolower(pathinfo($upload->getFileName(), PATHINFO_EXTENSION)); if (in_array($extension, array( 'csv', 'txt' ))) { $objReader = PHPExcel_IOFactory::createReader('CSV'); $objReader->setDelimiter($extension == 'csv' ? ';' : '#'); } else { $objReader = PHPExcel_IOFactory::createReaderForFile($upload->getFileName()); $fileinfo = $objReader->listWorksheetInfo($upload->getFileName()); $firstSheetName = $fileinfo[0]['worksheetName']; // $totalColumns = $fileinfo[0]['totalColumns']; $objReader->setReadDataOnly(true); $objReader->setLoadSheetsOnly($firstSheetName); } try { $objPHPExcel = $objReader->load($upload->getFileName()); } catch ( Exception $e ) { if ($e->getMessage() == "Cannot read encrypted file") { $this->_helper->flashMessenger(array( 'alert' => 'O arquivo não pode ser lido. Verifique se ele ' . 'está protegido por senha e tente novamente.' )); } } if (isset($objPHPExcel) && is_object($objPHPExcel)) { $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true); $objPHPExcel->disconnectWorksheets(); unset($objPHPExcel); unset($objReader); unlink($upload->getFileName()); return $sheetData; } } public function importar() { $sheetData = $this->_uploadAndReadFile(); if (count($sheetData) <= 0) { throw new Core_Exception("Nenhuma informação lida no arquivo"); } for ($i = $this->_inicioLaco; $i <= count($sheetData); $i++) { try{ if (empty($sheetData[$i])) { continue; } $dados = [ 'banco_id' => null, 'tabela_id' => null, 'convenio_id' => null, 'prazo' => null, 'fator' => null, 'tipo' => null, 'dia' => null, 'limite_saque' => null ]; $this->_getBanco($sheetData[$i], $dados); $this->_getConvenio($sheetData[$i], $dados); $this->_getTabela($sheetData[$i], $dados); $this->_getTipo($sheetData[$i], $dados); $this->_getPrazo($sheetData[$i], $dados); $this->_getFator($sheetData[$i], $dados); $this->_getDia($sheetData[$i], $dados); $this->_getLimiteSaque($sheetData[$i], $dados); $this->_salvarLinha($dados); } catch( Exception $e ) { } } } protected function _salvarLinha($dados) { $dbFator = new Model_DbTable_TabelaFator(); $existe = $dbFator->fetchRow( "banco_id = {$dados['banco_id']} AND tabela_id = {$dados['tabela_id']} " . "AND convenio_id = {$dados['convenio_id']} AND prazo = '{$dados['prazo']}' " . "AND tipo = '{$dados['tipo']}' " . "AND dia " . ($dados['dia'] ? '= ' . trim($dados['dia']) : 'IS NULL') ); if (!$existe) { $result = $dbFator->insert([ 'banco_id' => $dados['banco_id'], 'tabela_id' => $dados['tabela_id'], 'convenio_id' => $dados['convenio_id'], 'prazo' => $dados['prazo'], 'fator' => $dados['fator'], 'tipo' => $dados['tipo'], 'dia' => $dados['dia'], 'limite_saque' => $dados['limite_saque'] ]); } else { $existe->fator = $dados['fator']; $result = $existe->save(); } return $result; } protected function _getBanco(array $sheetData, array &$dados) { $coluna = !empty($sheetData[$this->getColuna('banco')]) ? $sheetData[$this->getColuna('banco')] : null; if (empty($coluna)) { throw new Core_Exception("O banco não foi informado"); } $dados['banco_id'] = (new Model_Banco())->bancoPorNome($coluna); if (empty($dados['banco_id'])) { throw new Core_Exception("Banco não localizado"); } } protected function _getConvenio(array $sheetData, array &$dados) { $coluna = !empty($sheetData[$this->getColuna('convenio')]) ? $sheetData[$this->getColuna('convenio')] : null; if (empty($coluna)) { throw new Core_Exception("O convênio não foi informado"); } $dados['convenio_id'] = (new Model_Convenio())->getOrCreateConvenio($coluna); } protected function _getTabela(array $sheetData, array &$dados) { $coluna = !empty($sheetData[$this->getColuna('tabela')]) ? $sheetData[$this->getColuna('tabela')] : null; $colunaCodigo = !empty($sheetData[$this->getColuna('codigo_tabela')]) ? $sheetData[$this->getColuna('codigo_tabela')] : null; if (empty($coluna)) { throw new Core_Exception("A tabela não foi informada"); } if (!empty($colunaCodigo)) { $dados['tabela_id'] = (new Model_Tabela())->getOrCreateTabelaByCodigo($colunaCodigo, $coluna, $dados['convenio_id'], $dados['banco_id']); } else { $dados['tabela_id'] = (new Model_Tabela())->getOrCreateTabelaByNome(null, $coluna, $dados['convenio_id'], $dados['banco_id']); } } protected function _getPrazo(array $sheetData, array &$dados) { $coluna = !empty($sheetData[$this->getColuna('prazo')]) ? $sheetData[$this->getColuna('prazo')] : null; if (empty($coluna)) { throw new Core_Exception("O prazo não foi informado"); } $dados['prazo'] = $coluna; } protected function _getFator(array $sheetData, array &$dados) { $coluna = !empty($sheetData[$this->getColuna('fator')]) ? $sheetData[$this->getColuna('fator')] : null; if (empty($coluna)) { throw new Core_Exception("O fator não foi informado"); } $dados['fator'] = str_replace(['.', ','], ['', '.'], $coluna); } protected function _getTipo(array $sheetData, array &$dados) { $coluna = !empty($sheetData[$this->getColuna('tipo')]) ? $sheetData[$this->getColuna('tipo')] : null; if (empty($coluna)) { throw new Core_Exception("O tipo não foi informado"); } $dados['tipo'] = $coluna; } protected function _getDia(array $sheetData, array &$dados) { $coluna = !empty($sheetData[$this->getColuna('dia')]) ? $sheetData[$this->getColuna('dia')] : null; if (!empty($coluna)) { $dados['dia'] = String_Refatorar::soNumeros($coluna); } } protected function _getLimiteSaque(array $sheetData, array &$dados) { $coluna = !empty($sheetData[$this->getColuna('limite_saque')]) ? $sheetData[$this->getColuna('limite_saque')] : null; if (!empty($coluna)) { $dados['limite_saque'] = String_Refatorar::parseStringToFloat($coluna); } } }
💾 保存文件
← 返回文件管理器