<?php

class Sinistro2_LaudosperitoController extends Agro_Controller_Action {
	public function init() {
		$this->view->title = "Sinistro » Laudos para peritos";
	}

	public function getModel() {
		return new Sinistro2_Model_LaudosPerito();
	}

	public function getForm(){
		return new Sinistro2_Form_LaudosPerito();
	}

	public function getFilter() {
		$User = Zend_Auth::getInstance()->getIdentity();
		$Model = $this->getModel();
		$where = array();
		$filter = array();

		if ($this->getRequest()->getParam('id_safra')) {
			$filter["id_safra"] = $this->getRequest()->getParam('id_safra');
			$where[] = array(
				'field' => 'id_safra',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('id_safra')
			);
		}

		if ($this->getRequest()->getParam('id_produto')) {
			$filter["id_produto"] = $this->getRequest()->getParam('id_produto');
		}

		if ($this->getRequest()->getParam('id_cobertura')) {
			$filter["id_cobertura"] = $this->getRequest()->getParam('id_cobertura');
		}

		if ($this->getRequest()->getParam('fl_ativo') != null) {
			$where[] = array(
				'field' => 'fl_ativo',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('fl_ativo')
			);
		}

		$laudos = $Model->buscaLaudos($filter);
		if (is_array($laudos)) {
			$where[] = array(
				'field' => 'id',
				'operator' => 'in',
				'value' => "(".implode(", ", $laudos).")"
			);
		}

		return $where;
	}

	public function excelAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		ini_set('memory_limit','2048M');
		$where = $this->getFilter();

		$Module = $this->getRequest()->getModuleName();
		$Controller = $this->getRequest()->getControllerName();
		$Model = $this->getModel();

		if ($this->getRequest()->getParam("o")) {
			$Model->_order = trim($this->getRequest()->getParam("o") .' '. $this->getRequest()->getParam("s"));
		}

		$Rs = $Model->fetchAllPaginator($where);
		$arr = array();
		$count = 0;

		foreach($Rs as $Row){
			$arr[$count]['chave']       = $Row['chave'];
			$arr[$count]['id_safra']	= $Row['id_safra'];
			$arr[$count]['laudo']       = $Row['tipo'];
			$arr[$count]['produto/cobertura']  	= $Model->buscaProdutosCoberturasExcel($Row['id'],$Row['tipo']);
			//quando a safra for maior que 17 é validada se é digitável pelo schema regulação
			//caso contrario, valida pelos schemas safraX onde X é o id da safra
			if ($Row['id_safra'] > 17) {
				$arr[$count]['digitavel'] = $Row['digitavel'];
			} else {
				$arr[$count]['digitavel'] = $Model->isDigitavel($Row['tipo'],$Row['id_safra'],$Row['chave']) ? 'Sim' : 'Não';	
			}

			$count++;
		}

		if ($Model->_columnExcel) {
			$Csv = new Agro_Plugin_Rs2Csv();
			$Csv->setTitles($Model->_columnExcel);
			$Csv->setRecordSet($arr);
			$Csv->setType("application/octet-stream");
			$Csv->setFileName("pesquisa.csv");
			$Csv->setDisposition("attachment");
			$Csv->setSeparator(";");
			$Csv->getCsv();
		} else {
			$Csv = new Agro_Plugin_Rs2Csv();
			$Csv->setTitles($Model->_columnExcel);
			$Csv->setRecordSet(array('Excel não configurado'));
			$Csv->setType("application/octet-stream");
			$Csv->setFileName("notfound.csv");
			$Csv->setDisposition("attachment");
			$Csv->setSeparator(";");
			$Csv->getCsv();
		}
	}

}
