<?php

class Sistema_Model_Bancos extends Agro_Db_Table_Abstract {
    const NUMERO_BANCO_BRASIL = 1;
    const NUMERO_BANCO_ITAU = 341;

	protected $_schema = 'sistema';
	protected $_name = 'bancos';
	protected $_primary = 'id';
	public $_order = 'ds_nome_banco';

	public function save(array $data) {
		$Row = $this->getRow($data['id']);

		$Row->ds_nome_banco = $data['ds_nome_banco'];
		$Row->nr_banco = $data['nr_banco'];

		return $Row->save();
	}

	public function getNomeBanco($id) {
		$Row = $this->find($id)->current();
		return $Row->ds_nome_banco;
	}

	public function getBancos(string $order = 'nr_banco') {
		$select = $this->select()->setIntegrityCheck(false);
		$select->from(array('b' => 'sistema.bancos'),array('id','ds_nome_banco','nr_banco'));
		$select->where('fl_ativo = ?', 'true');
		$select->order($order);
		$Row = $this->fetchAll($select);
		return $Row;
	}

	public function getCombo() {
		$Rs = $this->fetchAll('fl_ativo = true')->toArray();

		if (is_array($Rs)) {
			$option[] = '';
			foreach ($Rs as $Row) {
				$option[$Row['id']] = $Row['ds_nome_banco'];
			}
			return $option;
		} else {
			return false;
		}
	}
}
