<?php

class Sinistro2_TransmissaoavisosessorController extends Agro_Controller_Proposta {
	protected $_paginator = false;
	public $wsdl;
	public $propostas_erros = array();
	public $propostas_enviadas = array();

	public function getModel() {
		return new Sinistro2_Model_TransmissaoAvisosEssor();
	}

	public function init() {
	    ini_set('memory_limit','1024M');
		$this->view->title = 'Avisos » Transmissão Essor';
		require_once(APPLICATION_PATH . "/../library/Agro/nusoap/nusoap.php");
	}

	/**
	 * Monta a tela de pesquisa de propostas
	 *
	 */
	public function indexAction() {
		$this->view->form = new Sinistro2_Form_TransmissaoSearch();
	}

	public function getFilter() {
		$where = array();

		if ($this->getRequest()->getParam('id_safra')) {
			if(strcmp($this->getRequest()->getParam('pesquisar_por'), 'id') != 0){
				$where[] = array(
					'field' => 'id_safra',
					'operator' => '=',
					'value' => $this->getRequest()->getParam('id_safra')
				);
			}
		}

		if ($this->getRequest()->getParam('id_produto')) {
			$where[] = array(
				'field' => 'id_produto',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('id_produto')
			);
		}

		if ($this->getRequest()->getParam('id_corretor')) {
			$where[] = array(
				'field' => 'id_usuario',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('id_corretor')
			);
		}

		if ($this->getRequest()->getParam('vl_pesquisar_por')) {
			switch ($this->getRequest()->getParam('pesquisar_por')) {
				case 'ds_nome_proponente':
					$field = $this->getRequest()->getParam('pesquisar_por');
					$value = "%".$this->getRequest()->getParam('vl_pesquisar_por')."%";
					$operator = 'ilike';
				case 'ds_cpf_proponente':
					$field = 'pp.cpf_cnpj';
					$value = "%".$this->getRequest()->getParam('vl_pesquisar_por')."%";
					$operator = 'ilike';
					break;
				case 'id':
					$field = 'id_proposta';
					$value = $this->getRequest()->getParam('vl_pesquisar_por');
					$operator = '=';
					break;
				default:
					$field = $this->getRequest()->getParam('pesquisar_por');
					$value = $this->getRequest()->getParam('vl_pesquisar_por');
					$operator = '=';
					break;
			}

			$where[] = array (
				'field' => $field,
				'operator' => $operator,
				'value' => $value
			);
		}
		
		return $where;
	}

	public function transmitirAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		
		$return       = array("status" => true, "dados" => "", "message" => "Processamento concluído com sucesso.");
		$arquivo_OK   = time()."_TRANSMISSAO_AVISOS_OK.txt";
		$arquivo_ERRO = time()."_TRANSMISSAO_AVISOS_ERRO.txt";

		if ($this->getRequest()->getParam("id_aviso")) {
            $ListAvisos = $this->getRequest()->getParam("id_aviso");

            /*
             * Reordena o array para que seja sempre feito o envio do aviso mais antigo para o mais novo
             */
            asort($ListAvisos);

			foreach($ListAvisos as $arAviso) {
				$arAviso   = explode("_", $arAviso);
				$aviso     = $arAviso[0];
				$cobertura = $arAviso[1];

				Agro_Webservice::transmitirSinistroEssor($aviso, $cobertura, $arquivo_OK, $arquivo_ERRO);
			}
		} else {
			$return['status'] = false;
			$return['message'] = "Marque ao menos um aviso.";
		}

		$return['arquivo_ok']   = $arquivo_OK;
		$return['arquivo_erro'] = $arquivo_ERRO;
		
		$this->getResponse()->setBody(Zend_Json::encode($return));
	}

	public function marcapropostasAction() {
		$this->isAjax();

		$Adapter = new Zend_File_Transfer_Adapter_Http();
		$Adapter->setDestination(APPLICATION_PATH.'/upload_files/temp/');

		$file = $Adapter->getFileInfo();
		if(!$Adapter->receive($file['propostas']['name'])){
			$result['status'] = false;
		} else {
			$dados = file_get_contents(APPLICATION_PATH.'/upload_files/temp/' . $file['propostas']['name']);
			$dados = str_replace("\r\n", ",", $dados);
			$result['propostas'] = $dados;
			$result['status'] = true;
		}
		$this->getResponse()->setBody(Zend_Json::encode($result));
	}

}