<?php


abstract class BaseUsuarios extends BaseObject  implements Persistent {


	
	protected static $peer;


	
	protected $id;


	
	protected $id_perfil;


	
	protected $username;


	
	protected $pass;


	
	protected $estado = 1;


	
	protected $nitocc;


	
	protected $fk_username;


	
	protected $created_at;


	
	protected $update_at;

	
	protected $aPerfiles;

	
	protected $aTerceros;

	
	protected $collLogs;

	
	protected $lastLogCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function getId()
	{

		return $this->id;
	}

	
	public function getIdPerfil()
	{

		return $this->id_perfil;
	}

	
	public function getUsername()
	{

		return $this->username;
	}

	
	public function getPass()
	{

		return $this->pass;
	}

	
	public function getEstado()
	{

		return $this->estado;
	}

	
	public function getNitocc()
	{

		return $this->nitocc;
	}

	
	public function getFkUsername()
	{

		return $this->fk_username;
	}

	
	public function getCreatedAt($format = 'Y-m-d')
	{

		if ($this->created_at === null || $this->created_at === '') {
			return null;
		} elseif (!is_int($this->created_at)) {
						$ts = strtotime($this->created_at);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse value of [created_at] as date/time value: " . var_export($this->created_at, true));
			}
		} else {
			$ts = $this->created_at;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	
	public function getUpdateAt($format = 'Y-m-d')
	{

		if ($this->update_at === null || $this->update_at === '') {
			return null;
		} elseif (!is_int($this->update_at)) {
						$ts = strtotime($this->update_at);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse value of [update_at] as date/time value: " . var_export($this->update_at, true));
			}
		} else {
			$ts = $this->update_at;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	
	public function setId($v)
	{

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = UsuariosPeer::ID;
		}

	} 
	
	public function setIdPerfil($v)
	{

		if ($this->id_perfil !== $v) {
			$this->id_perfil = $v;
			$this->modifiedColumns[] = UsuariosPeer::ID_PERFIL;
		}

		if ($this->aPerfiles !== null && $this->aPerfiles->getId() !== $v) {
			$this->aPerfiles = null;
		}

	} 
	
	public function setUsername($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->username !== $v) {
			$this->username = $v;
			$this->modifiedColumns[] = UsuariosPeer::USERNAME;
		}

	} 
	
	public function setPass($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->pass !== $v) {
			$this->pass = $v;
			$this->modifiedColumns[] = UsuariosPeer::PASS;
		}

	} 
	
	public function setEstado($v)
	{

		if ($this->estado !== $v || $v === 1) {
			$this->estado = $v;
			$this->modifiedColumns[] = UsuariosPeer::ESTADO;
		}

	} 
	
	public function setNitocc($v)
	{

		if ($this->nitocc !== $v) {
			$this->nitocc = $v;
			$this->modifiedColumns[] = UsuariosPeer::NITOCC;
		}

		if ($this->aTerceros !== null && $this->aTerceros->getNitocc() !== $v) {
			$this->aTerceros = null;
		}

	} 
	
	public function setFkUsername($v)
	{

		if ($this->fk_username !== $v) {
			$this->fk_username = $v;
			$this->modifiedColumns[] = UsuariosPeer::FK_USERNAME;
		}

	} 
	
	public function setCreatedAt($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse date/time value for [created_at] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->created_at !== $ts) {
			$this->created_at = $ts;
			$this->modifiedColumns[] = UsuariosPeer::CREATED_AT;
		}

	} 
	
	public function setUpdateAt($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse date/time value for [update_at] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->update_at !== $ts) {
			$this->update_at = $ts;
			$this->modifiedColumns[] = UsuariosPeer::UPDATE_AT;
		}

	} 
	
	public function hydrate(ResultSet $rs, $startcol = 1)
	{
		try {

			$this->id = $rs->getFloat($startcol + 0);

			$this->id_perfil = $rs->getFloat($startcol + 1);

			$this->username = $rs->getString($startcol + 2);

			$this->pass = $rs->getString($startcol + 3);

			$this->estado = $rs->getFloat($startcol + 4);

			$this->nitocc = $rs->getFloat($startcol + 5);

			$this->fk_username = $rs->getFloat($startcol + 6);

			$this->created_at = $rs->getDate($startcol + 7, null);

			$this->update_at = $rs->getDate($startcol + 8, null);

			$this->resetModified();

			$this->setNew(false);

						return $startcol + 9; 
		} catch (Exception $e) {
			throw new PropelException("Error populating Usuarios object", $e);
		}
	}

	
	public function delete($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(UsuariosPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			UsuariosPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	public function save($con = null)
	{
    if ($this->isNew() && !$this->isColumnModified(UsuariosPeer::CREATED_AT))
    {
      $this->setCreatedAt(time());
    }

		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(UsuariosPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			$affectedRows = $this->doSave($con);
			$con->commit();
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	protected function doSave($con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;


												
			if ($this->aPerfiles !== null) {
				if ($this->aPerfiles->isModified()) {
					$affectedRows += $this->aPerfiles->save($con);
				}
				$this->setPerfiles($this->aPerfiles);
			}

			if ($this->aTerceros !== null) {
				if ($this->aTerceros->isModified()) {
					$affectedRows += $this->aTerceros->save($con);
				}
				$this->setTerceros($this->aTerceros);
			}


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = UsuariosPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setNew(false);
				} else {
					$affectedRows += UsuariosPeer::doUpdate($this, $con);
				}
				$this->resetModified(); 			}

			if ($this->collLogs !== null) {
				foreach($this->collLogs as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;
		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


												
			if ($this->aPerfiles !== null) {
				if (!$this->aPerfiles->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aPerfiles->getValidationFailures());
				}
			}

			if ($this->aTerceros !== null) {
				if (!$this->aTerceros->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aTerceros->getValidationFailures());
				}
			}


			if (($retval = UsuariosPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collLogs !== null) {
					foreach($this->collLogs as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = UsuariosPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getIdPerfil();
				break;
			case 2:
				return $this->getUsername();
				break;
			case 3:
				return $this->getPass();
				break;
			case 4:
				return $this->getEstado();
				break;
			case 5:
				return $this->getNitocc();
				break;
			case 6:
				return $this->getFkUsername();
				break;
			case 7:
				return $this->getCreatedAt();
				break;
			case 8:
				return $this->getUpdateAt();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = UsuariosPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getIdPerfil(),
			$keys[2] => $this->getUsername(),
			$keys[3] => $this->getPass(),
			$keys[4] => $this->getEstado(),
			$keys[5] => $this->getNitocc(),
			$keys[6] => $this->getFkUsername(),
			$keys[7] => $this->getCreatedAt(),
			$keys[8] => $this->getUpdateAt(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = UsuariosPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setIdPerfil($value);
				break;
			case 2:
				$this->setUsername($value);
				break;
			case 3:
				$this->setPass($value);
				break;
			case 4:
				$this->setEstado($value);
				break;
			case 5:
				$this->setNitocc($value);
				break;
			case 6:
				$this->setFkUsername($value);
				break;
			case 7:
				$this->setCreatedAt($value);
				break;
			case 8:
				$this->setUpdateAt($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = UsuariosPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setIdPerfil($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setUsername($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setPass($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setEstado($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setNitocc($arr[$keys[5]]);
		if (array_key_exists($keys[6], $arr)) $this->setFkUsername($arr[$keys[6]]);
		if (array_key_exists($keys[7], $arr)) $this->setCreatedAt($arr[$keys[7]]);
		if (array_key_exists($keys[8], $arr)) $this->setUpdateAt($arr[$keys[8]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(UsuariosPeer::DATABASE_NAME);

		if ($this->isColumnModified(UsuariosPeer::ID)) $criteria->add(UsuariosPeer::ID, $this->id);
		if ($this->isColumnModified(UsuariosPeer::ID_PERFIL)) $criteria->add(UsuariosPeer::ID_PERFIL, $this->id_perfil);
		if ($this->isColumnModified(UsuariosPeer::USERNAME)) $criteria->add(UsuariosPeer::USERNAME, $this->username);
		if ($this->isColumnModified(UsuariosPeer::PASS)) $criteria->add(UsuariosPeer::PASS, $this->pass);
		if ($this->isColumnModified(UsuariosPeer::ESTADO)) $criteria->add(UsuariosPeer::ESTADO, $this->estado);
		if ($this->isColumnModified(UsuariosPeer::NITOCC)) $criteria->add(UsuariosPeer::NITOCC, $this->nitocc);
		if ($this->isColumnModified(UsuariosPeer::FK_USERNAME)) $criteria->add(UsuariosPeer::FK_USERNAME, $this->fk_username);
		if ($this->isColumnModified(UsuariosPeer::CREATED_AT)) $criteria->add(UsuariosPeer::CREATED_AT, $this->created_at);
		if ($this->isColumnModified(UsuariosPeer::UPDATE_AT)) $criteria->add(UsuariosPeer::UPDATE_AT, $this->update_at);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(UsuariosPeer::DATABASE_NAME);

		$criteria->add(UsuariosPeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setIdPerfil($this->id_perfil);

		$copyObj->setUsername($this->username);

		$copyObj->setPass($this->pass);

		$copyObj->setEstado($this->estado);

		$copyObj->setNitocc($this->nitocc);

		$copyObj->setFkUsername($this->fk_username);

		$copyObj->setCreatedAt($this->created_at);

		$copyObj->setUpdateAt($this->update_at);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach($this->getLogs() as $relObj) {
				$copyObj->addLog($relObj->copy($deepCopy));
			}

		} 

		$copyObj->setNew(true);

		$copyObj->setId(NULL); 
	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new UsuariosPeer();
		}
		return self::$peer;
	}

	
	public function setPerfiles($v)
	{


		if ($v === null) {
			$this->setIdPerfil(NULL);
		} else {
			$this->setIdPerfil($v->getId());
		}


		$this->aPerfiles = $v;
	}


	
	public function getPerfiles($con = null)
	{
		if ($this->aPerfiles === null && ($this->id_perfil > 0)) {
						$this->aPerfiles = PerfilesPeer::retrieveByPK($this->id_perfil, $con);

			
		}
		return $this->aPerfiles;
	}

	
	public function setTerceros($v)
	{


		if ($v === null) {
			$this->setNitocc(NULL);
		} else {
			$this->setNitocc($v->getNitocc());
		}


		$this->aTerceros = $v;
	}


	
	public function getTerceros($con = null)
	{
		if ($this->aTerceros === null && ($this->nitocc > 0)) {
						$this->aTerceros = TercerosPeer::retrieveByPK($this->nitocc, $con);

			
		}
		return $this->aTerceros;
	}

	
	public function initLogs()
	{
		if ($this->collLogs === null) {
			$this->collLogs = array();
		}
	}

	
	public function getLogs($criteria = null, $con = null)
	{
				if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collLogs === null) {
			if ($this->isNew()) {
			   $this->collLogs = array();
			} else {

				$criteria->add(LogPeer::ID_USUARIO, $this->getId());

				LogPeer::addSelectColumns($criteria);
				$this->collLogs = LogPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(LogPeer::ID_USUARIO, $this->getId());

				LogPeer::addSelectColumns($criteria);
				if (!isset($this->lastLogCriteria) || !$this->lastLogCriteria->equals($criteria)) {
					$this->collLogs = LogPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastLogCriteria = $criteria;
		return $this->collLogs;
	}

	
	public function countLogs($criteria = null, $distinct = false, $con = null)
	{
				if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(LogPeer::ID_USUARIO, $this->getId());

		return LogPeer::doCount($criteria, $distinct, $con);
	}

	
	public function addLog(Log $l)
	{
		$this->collLogs[] = $l;
		$l->setUsuarios($this);
	}

} 