<?php

require_once(LIB_PATH . DS . 'database.php');

class Pay_settings extends DatabaseObject
{
    protected static $table_name = "payment_settings";
    public $id;
    public $code;
    public $fees_description;
    public $amount;
    public $level;
    public $pay_cat;
    public $mode;


    public static function authenticate($code = "", $level = "")
    {
        global $database;

//echo "Mode ".$mode = $user_data->mode;
        $sql = "SELECT * FROM " . self::$table_name . " ";
        $sql .= " WHERE code='{$code}'and level='{$level}'  LIMIT 1 ";
//$sql .= "AND mode ='{$mode}' LIMIT 1";	

        $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
//return $result_array;
    }

    public function fullname()
    {

        if (isset($this->formno) && isset($this->username)) {
            return $this->formno . " " . $this->username;
        } else {
            return "";
        }
    }


//Common database Methods
    public static function find_all($code = 0, $level = "", $pay_cat = "", $adm_type = "")
    {
        global $database;
        return self::find_by_sql("SELECT * FROM " . self:: $table_name . " WHERE code={$code} and level='{$level}' and pay_cat='{$pay_cat}'and mode='{$adm_type}'");
        //return $result_set;

    }

    public static function find_by_id($code = 0, $level = "")
    {
        global $database;
        $result_array = self::find_by_sql("SELECT * FROM  " . self::$table_name . " WHERE code={$code} and level='{$level}'  LIMIT 1");
        //$found = $database->fetch_array($result_set);
        //return $found;
        return !empty($result_array) ? array_shift($result_array) : false;

    }
    
    public static function find_sch_fees($code, $level = "", $pay_cat = "",$adm_type=""){
		                                   // 3, $level, $pay_cat, $adm_type,$status
		$sql = "SELECT * FROM  " . self::$table_name . " WHERE code=$code AND level='{$level}' AND pay_cat='{$pay_cat}' AND mode='{$adm_type}'";
		return self::find_by_sql($sql);
	}

    public static function find_by_sql($sql = "")
    {
        global $database;
        $result_set = $database->query($sql);
        $object_array = array();
        while ($row = $database->fetch_array($result_set)) {
            $object_array[] = self::instantiate($row);
        }
        return $object_array;

    }

    private static function instantiate($record)
    {

        $object = new self;
        foreach ($record as $attribute => $value) {
            if ($object->has_attribute($attribute)) {
                $object->$attribute = $value;
            }

        }
        return $object;
    }

    private function has_attribute($attribute)
    {

        $object_vars = get_object_vars($this);
        return array_key_exists($attribute, $object_vars);
    }

    public function save()
    {
        return isset($this->id) ? $this->update() : $this->create();
    }

    public function create()
    {
        global $database;
        $sql = "INSERT INTO " . self::$table_name . " (";
        $sql .= "formno, email";
        $sql .= ") 	VALUES ('";
        $sql .= $database->escape_value($this->formno) . "', '";
        $sql .= $database->escape_value($this->email) . "') '";
        if ($database->query($sql)) {
            $this->id = $database->insert_id();
            return true;
        } else {
            return false;
        }

    }

    public function update()
    {

        global $database;
        $sql = "UPDATE " . self::$table_name . "(";
        $sql .= "formno = '" . $database->escape_value($this->formno) . "',";
        $sql .= "email ='" . $database->escape_value($this->email) . "', ";
        $sql .= "WHERE id=" . $database->escape_value($this->id);

        $database->query($sql);
        return ($database->affected_rows() == 1) ? true : false;
    }

    public function delete()
    {

        global $database;
        $sql = "DELETE FROM " . self::$table_name . " ";
        $sql .= "WHERE id=" . $database->escape_value($this->id);
        $sql .= " LIMIT 1";
        $database->query($sql);
        return ($database->affected_rows() == 1) ? true : false;
    }

//Closing tags for user's class
}

$pay_settings = new Pay_settings();
?>