<?php

class Seguro_Model_ItensSeguradosComplementos extends Agro_Db_Table_Abstract {
	protected $_log = true;
	protected $_schema = 'seguro';
	protected $_name = 'itens_segurados_complemento';
	protected $_primary = 'id_item_segurado';
	
	public function save($unidade, $itemSegurado, $arRequired, bool $origemEndossoImportacaoItensLaudo = false) {
        if ($itemSegurado) {
            $Row = $this->fetchRow("id_item_segurado = $itemSegurado");
            if (!$Row) $Row = $this->createRow();
            $Row->id_item_segurado = $itemSegurado;
            $Row->id_portaenxerto = ($unidade['IS_PORTAENXERTO'] ? $unidade['IS_PORTAENXERTO'] : '0');
            $Row->id_ciclo = ($unidade['IS_CICLO_VARIEDADE'] ? $unidade['IS_CICLO_VARIEDADE'] : 16);
            $Row->ds_tipo_conducao = $unidade['IS_TIPO_CONDUCAO'];
            $Row->ds_primeiro_ano_plantio = $unidade['IS_PRIMEIRO_ANO_PLANTIO'];
            $Row->nr_plantas_metro_linear = $unidade['IS_NRO_PLANTAS_M_LINEAR'];
            $Row->vl_producao_minima = Agro_Util::formatCurrencyDb($unidade['IS_PRODUCAO_MINIMA']);
            $Row->vl_lmi_pos_granizo = Agro_Util::formatCurrencyDb($unidade['IS_LMI_POS_GRANIZO']);
            $Row->vl_producao_garantida = Agro_Util::formatCurrencyDb($unidade['IS_PRODUCAO_GARANTIDA']);
            $Row->vl_idade_tela = $unidade['IS_IDADE_TELA'];
            $Row->ds_local = $unidade['IS_LOCAL'];
            $Row->ds_coordenadas_geograficas = $unidade['IS_COORDENADAS_GEOGRAFICAS'];
            $Row->vl_altitude = $unidade['IS_ALTITUDE'];
            $Row->id_sistema_plantio = $unidade['IS_SISTEMA_PLANTIO'];
            $Row->id_tipo_solo = ($unidade['IS_TIPO_SOLO_AD']) ? $unidade['IS_TIPO_SOLO_AD'] : $unidade['IS_TIPO_SOLO'];
            $Row->ds_paf = $unidade['IS_PEQUENA_AREA_FORRAGEIRA'];

            if ($unidade['IS_DT_INICIO_COLHEITA'])
                $Row->dt_inicio_colheita = Agro_Util::formatDate($unidade['IS_DT_INICIO_COLHEITA'], Zend_Date::W3C);

            if ($unidade['IS_DT_FINAL_COLHEITA'])
                $Row->dt_final_colheita = Agro_Util::formatDate($unidade['IS_DT_FINAL_COLHEITA'], Zend_Date::W3C);

            if ($unidade['IS_DATA_PODA'])
                $Row->dt_poda = Agro_Util::formatDate($unidade['IS_DATA_PODA'], Zend_Date::W3C);

            if ($unidade['IS_DATA_PLANTIO_TRANSPLANTE'])
                $Row->dt_plantio = Agro_Util::formatDate($unidade['IS_DATA_PLANTIO_TRANSPLANTE'], Zend_Date::W3C);

            if($arRequired['IS_DATA_SEMEADURA'] == true){
                if ($unidade['IS_DATA_SEMEADURA']){
                    $Row->dt_semeadura = Agro_Util::formatDate($unidade['IS_DATA_SEMEADURA'], Zend_Date::W3C);
                } else {
                    if (!$origemEndossoImportacaoItensLaudo) {
                        return false;
                    }
                }
            }

            if($unidade['DS_PAF']) {
                $Row->ds_paf = $unidade['DS_PAF'];
            }

            return $Row->save();
        }
	}

    public function setCoordenadas(int $idItem, $coordenadas){
		$coordenadas = implode(' , ', $coordenadas);
		return $this->update(
			['ds_coordenadas_geograficas' => $coordenadas],
			"id_item_segurado = {$idItem}"
		);
		
	}
}