<?php

class Sistema_Model_Categorias extends Agro_Db_Table_Abstract {
    const ID_EMPRESA_VISTORIA = 10;

	public $_pairs = array('key' => 'id', 'field' => 'ds_nome_categoria');
	protected $_schema = 'sistema';
	protected $_name = 'categorias';
	protected $_primary = 'id';
	public $_order = 'ds_nome_categoria';
	public $_columnExcel = array(
		'id' => 'ID',
		'ds_nome_categoria' => 'Categoria',
		'ds_chave' => 'Chave'
	);
	
	public function save(array $data) {
		$Row = $this->getRow($data['id']);

		$Row->ds_nome_categoria = $data['ds_nome_categoria'];
		$Row->ds_chave = $data['ds_chave'];

		return $Row->save();
	}

	public function beforeDelete($id) {
		$Empresas = new Sistema_Model_Empresas();
		$Rs = $Empresas->fetchAll("id_categoria = $id");

		if ($Rs->count()) {
			foreach ($Rs as $Row) {
				$Empresas->delete($Row->id, 'id');
			}
		}
	}

	public function getIdCategoria($tipo) {
		return $this->getAdapter()->fetchOne($this->select()->from('sistema.categorias', 'id')->where('ds_chave = ?', $tipo));
	}
}