Last active
October 11, 2015 05:57
-
-
Save paulohp/3813598 to your computer and use it in GitHub Desktop.
upload a txt and save the data in the db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Autor : Hermiro Manoel da Silva Carvalho / Paulo Henrique | |
* Descrição: Controller responsável pela lógica da captação das linhas do arquivo e | |
* salvamento dentro da tabela Remittences | |
* Model: Remittences | |
*/ | |
App::uses('File', 'Utility'); | |
class RemittencesController extends AppController { | |
public $helpers = array('Html', 'Form'); | |
public $components = array('Session','Search.Prg'); | |
public $name = 'Remittences'; | |
public $presetVars = true; // using the model configuration | |
public function index(){ | |
} | |
public function search(){ | |
} | |
public function results() { | |
$this->Prg->commonProcess(); | |
$this->paginate['conditions'] = $this->Remittence->parseCriteria($this->passedArgs); | |
$this->set('remittences', $this->paginate()); | |
} | |
#paginacao | |
public $paginate = array( | |
'limit' => 10, | |
'order' => array( | |
'Remittence.nomeSacado' => 'asc' | |
) | |
); | |
public function TrataArquivo(){ | |
//Início - Variáveis para uso dentro da função. | |
$nome_arquivo = null; | |
$nome_remessa = null; | |
$linhas_arquivo = null; | |
$extensao_permitida = 'txt'; | |
$dados_salvar_linhas = null; | |
$dados_salvar_repositorio = null; | |
$nome_arquivo = $this->data['Remittence']['arquivo']['tmp_name'];//Endereço físico do programa para leitura | |
$nome_remessa = $this->data['Remittence']['arquivo']['name'];//Nome do programa | |
$valida_programa = $this->Remittence->find('first', array('Remittence.nome_remessa =' => $nome_remessa)); | |
$data_atual = date('Y-m-d G:i:s'); | |
$this->loadModel('Repositorio'); | |
// FAZ LEITURA DE DADOS DO ARQUIVO TEMPORARIO | |
$nome_arquivo_up = new File($this->request->data['Remittence']['arquivo']['tmp_name'],false); | |
$dados = $nome_arquivo_up->read(); | |
$nome_arquivo_up->close(); | |
// PREPARA ARQUIVO REAL | |
$nome_arquivo_up = new File(WWW_ROOT.'files'.DS.'remessas'.DS.$nome_remessa,false,0755); | |
if($nome_arquivo_up->create()) { | |
// ESCREVE ARQUIVO REAL | |
$nome_arquivo_up->write($dados); | |
$nome_arquivo_up->close(); | |
} else { | |
$this->Session->setFlash('Erro ao tentar fazer upload','msg_erro'); | |
} | |
if ($nome_arquivo == false){ | |
$this->Session->setFlash('Não foi possível ler o arquivo !'); | |
$this->redirect(array('action' => 'index')); | |
} | |
$arquivo_extensao = strtolower(end(explode('.', $nome_remessa)));//Captura a extensão do arquivo | |
if ($arquivo_extensao <> $extensão_permitida){ | |
$this->Session->setFlash('Envie somente arquivo de texto !'); | |
$this->redirect(array('action' => 'index')); | |
} | |
if ($valida_programa['Remittence']['nome_remessa'] == $nome_remessa){ | |
$this->Session->setFlash('Já existe um arquivo com o mesmo nome no servidor !','flash_fail'); | |
$this->redirect(array('action' => 'index')); | |
} | |
$linhas_arquivo = file($nome_arquivo); | |
foreach ($linhas_arquivo as $key => $value) { | |
$dados_salvar_linhas = array('Remittence' => array | |
( | |
'nro_linha' => $key, | |
'nome_remessa' => $nome_remessa, | |
'agencia' => '28878', | |
'conta' => '212229', | |
'carteira' => '09', | |
'nossoNumero' => substr($value,70,81), | |
'vencimento' => substr($value,120,125), | |
'nomeSacado' => substr($value,234,273), | |
'inscricao' => substr($value,220,232) | |
) | |
); | |
if($this->Remittence->saveAll($dados_salvar_linhas) == false){ //Se não conseguir ler a linha mostra erro e para o processamento | |
$this->Session->setFlash('Erro ao ler o arquivo !'); | |
$this->redirect(array('action' => 'index')); | |
} | |
} | |
$dados_salvar_repositorio = array( | |
'Repositorio' => array | |
( | |
'nome_remessa' => $nome_remessa, | |
'dt_criacao' => $data_atual | |
) | |
); | |
$this->Repositorio->saveAll($dados_salvar_repositorio); | |
$this->Session->setFlash('Programa cadastrado com sucesso !', 'flash_success'); | |
$this->redirect(array('controller' => 'repositorios','action' => 'index')); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment