<?php
class PersonData {
	public static $tablename = "person";


	public function PersonData(){
		$this->name = "";
		$this->lastname = "";
		$this->email = "";
		$this->image = "";
		$this->password = "";
		$this->created_at = "NOW()";
	}

	public function add_provider(){
		$sql = "insert into person (is_company,rut,name,lastname,address1,giro_id,city_id,comun_id,email1,phone1,phone2,contact,website,kind,created_at) ";
		echo $sql .= "value (\"$this->is_company\",\"$this->rut\",\"$this->name\",\"$this->lastname\",\"$this->address1\",$this->giro_id,$this->city_id,$this->comun_id,\"$this->email1\",\"$this->phone1\",\"$this->phone2\",\"$this->contact\",\"$this->website\",2,$this->created_at)";
		Executor::doit($sql);
	}

	public function add_client(){
		$sql = "insert into person (is_company,rut,name,lastname,address1,giro_id,city_id,comun_id,email1,phone1,phone2,contact,website,kind,created_at) ";
		$sql .= "value (\"$this->is_company\",\"$this->rut\",\"$this->name\",\"$this->lastname\",\"$this->address1\",$this->giro_id,$this->city_id,$this->comun_id,\"$this->email1\",\"$this->phone1\",\"$this->phone2\",\"$this->contact\",\"$this->website\",3,$this->created_at)";
		Executor::doit($sql);
	}

	public static function delById($id){
		$sql = "delete from ".self::$tablename." where id=$id";
		Executor::doit($sql);
	}
	public function del(){
		$sql = "delete from ".self::$tablename." where id=$this->id";
		Executor::doit($sql);
	}

// partiendo de que ya tenemos creado un objecto PersonData previamente utilizamos el contexto
	public function update(){
		$sql = "update ".self::$tablename." set name=\"$this->name\",email1=\"$this->email1\",address1=\"$this->address1\",lastname=\"$this->lastname\",phone1=\"$this->phone1\" where id=$this->id";
		Executor::doit($sql);
	}

	public function update_client(){
		$sql = "update ".self::$tablename." set name=\"$this->name\",email1=\"$this->email1\",address1=\"$this->address1\",lastname=\"$this->lastname\",phone1=\"$this->phone1\",is_active_access=\"$this->is_active_access\",password=\"$this->password\",has_credit=\"$this->has_credit\",credit_limit=\"$this->credit_limit\" where id=$this->id";
		Executor::doit($sql);
	}

	public function update_provider(){
		$sql = "update ".self::$tablename." set is_company=\"$this->is_company\",rut=\"$this->rut\",name=\"$this->name\",email1=\"$this->email1\",address1=\"$this->address1\",lastname=\"$this->lastname\",phone1=\"$this->phone1\",phone2=\"$this->phone2\",giro_id=$this->giro_id,city_id=$this->city_id,comun_id=$this->comun_id,contact=\"$this->contact\",website=\"$this->website\" where id=$this->id";
		Executor::doit($sql);
	}

	public function update_passwd(){
		$sql = "update ".self::$tablename." set password=\"$this->password\" where id=$this->id";
		Executor::doit($sql);
	}


	public static function getById($id){
		$sql = "select * from ".self::$tablename." where id=$id";
		$query = Executor::doit($sql);
		return Model::one($query[0],new PersonData());
	}



	public static function getAll(){
		$sql = "select * from ".self::$tablename;
		$query = Executor::doit($sql);
		return Model::many($query[0],new PersonData());

	}

	public static function getClients(){
		$sql = "select * from ".self::$tablename." where kind=3 order by name,lastname";
		$query = Executor::doit($sql);
		return Model::many($query[0],new PersonData());
	}

	public static function getClientsWithCredit(){
		$sql = "select * from ".self::$tablename." where kind=1 and has_credit=1 order by name,lastname";
		$query = Executor::doit($sql);
		return Model::many($query[0],new PersonData());
	}


	public static function getProviders(){
		$sql = "select * from ".self::$tablename." where kind=2 order by name,lastname";
		$query = Executor::doit($sql);
		return Model::many($query[0],new PersonData());

	}

	public static function getLike($q){
		$sql = "select * from ".self::$tablename." where name like '%$q%'";
		$query = Executor::doit($sql);
		return Model::many($query[0],new PersonData());

	}


}

?>