<?
/**
 * Controller
 *
 * Laudo final Tipo LF019 Item Segurado | Safra15 - Trigo/Cevada - Geada
 *
 * @author Samir Ortiz
 * @date 30/07/2012
 *
 */
class Safra17_Lf019itemseguradochController extends Agro_Controller_Action{
	private $__ModelIS;
	private $__params;
	private $__IS;

	public function init() {
		$ModelLfIS = new Sinistro_Model_LaudosFinaisItensSegurados();

		$this->__params = $this->getRequest()->getParams();
		$this->__ModelIS = new Seguro_Model_ItensSegurados();
		$this->__IS = $this->__ModelIS->find($this->__params["id_item_segurado"])->current();

		$this->view->title = "Sinistro » Processos » Laudo Final » Item Segurado: {$this->__IS->ds_item_segurado}";

		$LFIS = $ModelLfIS->find($this->__params["id_laudo_final"], $this->__params["id_item_segurado"]);
		$this->__params["id_laudo_final_item_segurado"] = (count($LFIS) ? $LFIS["id"] : false);

	}

	public function getForm($dados = array()) {
	    return new Safra17_Form_Lf019ItemSegurado(array('dados'=>$dados));
	}

	public function getModel() {
	    return new Safra17_Model_Lf019ItensSeguradosCHAmostras();
	}

	public function getModelLf019ItensSegurados() {
	    return new Safra15_Model_Lf019ItensSegurados();
	}

    public function formAction() {

		$Model = $this->getModel();

        $this->view->back = '/'.$this->_request->module.'/lf019/form/id_laudo_final/'.$this->__params['id_laudo_final']."/id_processo/".$this->__params['id_processo'];
		$this->view->jQuery()->addStylesheet('/resources/css/Sinistro/laudo.css');
		$this->view->jQuery()->addJavascriptFile('/resources/javascript/Sinistro/laudofinalitemsegurado.js');


        $ProcessosEmpresas = new Sinistro_Model_ProcessosEmpresas();

		$Cultura = new Produto_Model_Culturas();
		$dadosForm['id_cultura'] = $Cultura->getCulturaByProposta($this->__params['id_proposta']);

		$this->view->id_cultura = $dadosForm['id_cultura'];
		$this->__params['id_cultura'] = $dadosForm['id_cultura'];

		if($this->getRequest()->isPost()){

			$dadosForm = array(
				"id_laudo_final" => $this->__params["id_laudo_final"],
				"id_item_segurado" => $this->__params["id_item_segurado"],
				"id_empresa_vistoriadora" => $ProcessosEmpresas->getEmpresaByProcesso($this->__params['id_processo']),
			);

	    	$Form = $this->getForm($dadosForm);

	        $Form->populate($this->__params);

        	if($Form->isValid($Form->getValues())) {

				$this->__params["id_laudo_final_item_segurado"] = $Model->save($this->__params);
				$this->view->showMessage($Model->getMessages(), ($this->__params["id_laudo_final_item_segurado"] ? "success" : "error"));

        	} else {
        		$this->view->showMessage($this->getAllFormErros(array($Form),$_POST), 'error');
        	}
        }

        //$this->populate();

        $this->view->data = array();
        $this->view->data['amostras'] = array();

        if($this->__params['id_laudo_final_item_segurado']) {
            $Model_Lf019ItensSegurados = $this->getModelLf019ItensSegurados();
			$Model_Lf019ItensSeguradosAmostras = new Safra15_Model_Lf019ItensSeguradosAmostras();

            $dataLf019ItensSegurados = $Model_Lf019ItensSegurados->fetchRow($Model_Lf019ItensSegurados->select()->where('id_laudo_final_item_segurado = ?', $this->__params['id_laudo_final_item_segurado']));

            if ($dataLf019ItensSegurados) {
                $this->__params['vl_percent_total_perda_final_chocho'] = $dataLf019ItensSegurados['vl_percent_total_perda_final_chocho'];
                $this->__params['vl_percent_colhido_chocho'] = $dataLf019ItensSegurados['vl_percent_colhido_chocho'];

                $this->__params = array_merge($this->__params, $Model_Lf019ItensSeguradosAmostras->get($this->__params["id_laudo_final_item_segurado"]));
            }

            $this->__params['amostras'] = $Model->getAmostras($this->__params['id_laudo_final_item_segurado']);
        }

    	$dadosForm = array(
			"id_laudo_final" => $this->__params["id_laudo_final"],
			"id_item_segurado" => $this->__params["id_item_segurado"],
			"id_empresa_vistoriadora" => $ProcessosEmpresas->getEmpresaByProcesso($this->__params['id_processo']),
    		"id_cultura" => $this->__params['id_cultura']
		);

    	$Form = $this->getForm($dadosForm);

        $Form->populate($this->__params);

		$this->view->form = $Form;
		$this->view->data['amostras'] = $this->__params['amostras'];
        $this->view->data['id_proposta'] = $this->__params['id_proposta'];
        $this->view->data['id_processo'] = $this->__params['id_processo'];
        $this->view->data['id_item_segurado'] = $this->__params['id_item_segurado'];
        $this->view->data['id_laudo_final'] = $this->__params['id_laudo_final'];
        $this->view->data['id_laudo_final_item_segurado'] = $this->__params['id_laudo_final_item_segurado'];
        $this->view->data['vl_percent_colhido_chocho'] = $this->__params['vl_percent_colhido_chocho'];
        $this->view->data['vl_percent_total_perda_final_chocho'] = $this->__params['vl_percent_perdas_chocho'];

		// Verifica se o laudo deve ser exibido como "somente leitura"
		$Model_LaudosFinais = new Sinistro_Model_LaudosFinais();
		$this->view->data['fl_readonly'] = $Model_LaudosFinais->isReadonly($this->__params['id_laudo_final']);

    }
}
