<?php
require_once(LIB_PATH . '/' . 'database.php');

class Staff extends DatabaseObject
{
    protected static $table_name = "salstructure";
    public $id;
    public $gl;
    public $rent;
    public $gross;
   
    public $basic;
    public $name;
    public $dpunitid;
    public $fullname;
    public $paygrade;
    public $ltitle;
    public $amounttaken;
    public $amountpaid;
    public $startdate;
    public $enddate;

   
    public static function search_loan($dname)
    {
      global $database;

      $dname = $database->escape_value($dname);
      $sql="SELECT * FROM loan WHERE stbiodataid=$dname";
     return $result_array = self::find_by_sql($sql);
      
    }

    public static function insert_staff_loan($dname,$deptid, $title,$amount,$repayment,$noofrepayment,$startingdate,$enddate)
    {
      global $database;

      $dname = $database->escape_value($dname);
      $deptid = $database->escape_value($deptid);
      
      $title = $database->escape_value($title);

      $amount = $database->escape_value($amount);

      $repayment = $database->escape_value($repayment);
      $noofrepayment = $database->escape_value($noofrepayment);
      $startingdate = $database->escape_value($startingdate);

      $enddate = $database->escape_value($enddate);
    

      $sql = "INSERT INTO loan (";
            $sql .= "stbiodataid,deptid,ltitle,amounttaken,amountpaid,lastcount,startdate,enddate";
    
            $sql .= ") VALUES ('";
            $sql .= $dname . "','";
            $sql .= $deptid . "','";
            $sql .= $title . "','";
            $sql .= $amount . "','";
            $sql .= $repayment . "','";
            $sql .= $noofrepayment . "','";
            $sql .= $startingdate . "','";
            $sql .= $enddate . "'
         
             
           )";
    
            if ($database->query($sql)) {
             return $id=$database->insert_id();
             // return true;
               
            } else {
                return false;
            }

    }
    public static function insert_staff_details($salaryid,$accid,$dept,$staffid,$fullname,$status,$paygrade,$designation,$email,$phone)
    {
      global $database;

      
      $salaryid = $database->escape_value($salaryid);
      $accid = $database->escape_value($accid);
      $dept = $database->escape_value($dept);

      $staffid = $database->escape_value($staffid);

      $fullname = $database->escape_value($fullname);
      $status = $database->escape_value($status);
      $paygrade = $database->escape_value($paygrade);
      $designation = $database->escape_value($designation);
      $email = $database->escape_value($email);

      $phone = $database->escape_value($phone);
    

      $sql = "INSERT INTO stbiodata (";
            $sql .= "salstructureid,finid,deptid,loanid,lrepaymentstatus,deductionid,staffid,fullname,status,paygrade,designation,email,phone";
    
            $sql .= ") VALUES ('";
            $sql .= $salaryid . "','";
            $sql .= $accid . "','";
            $sql .= $dept . "','";
            $sql .= '' . "','";
            $sql .= '' . "','";
            $sql .= '' . "','";
            $sql .= $staffid . "','";
            $sql .= $fullname . "','";
            $sql .= $status . "','";
            $sql .= $paygrade . "','";
            $sql .= $designation . "','";
            $sql .= $email . "','";
            
            $sql .= $phone . "' 
           )";
    
            if ($database->query($sql)) {
             return $id=$database->insert_id();
             // return true;
               
            } else {
                return false;
            }
    }

    


    public static function insert_acct_details($bank,$accnumber)
    {
      global $database;
     
      $bank = $database->escape_value($bank);
      $accnumber = $database->escape_value($accnumber);
      $sql = "INSERT INTO fin (";
            $sql .= "bankname,accountnumber";
    
            $sql .= ") VALUES ('";
           
            $sql .= $bank . "','";
            $sql .= $accnumber . "' 
           )";
    
            if ($database->query($sql)) {
             return $id=$database->insert_id();
             // return true;
               
            } else {
                return false;
            }
    }


    public static function get_staff_level_academics($level="")
    {
      global $database;
      $sql="SELECT * FROM conpcass WHERE gl='$level' LIMIT 1";
      $result_array = self::find_by_sql($sql);
      return !empty($result_array) ? array_shift($result_array) : false;
    }

    public static function get_staff_level_nonacademics($level="")
    {
      global $database;
      $sql="SELECT * FROM contediss WHERE gl='$level' LIMIT 1";
      $result_array = self::find_by_sql($sql);
      return !empty($result_array) ? array_shift($result_array) : false;
    }


   public static function update_department($deptid="",$dp="")
   {
    global $database;
   
    $sql = "UPDATE dpunit SET name='$dp' WHERE id=$deptid  LIMIT 1";
   
   
    if ($database->query($sql)) {
      $id=$database->insert_id();
      return true;
       
    } else {
        return false;
    }
   } 



  public static function  fetch_department($dept="")
    {
      $sql="SELECT * FROM dpunit WHERE id='$dept' LIMIT 1";
      $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
    }  
    
    
    public static function  fetch_biodata_department($dept="")
    {
      $sql="SELECT * FROM stbiodata WHERE deptid=$dept ";
     return $result_array = self::find_by_sql($sql);
        // return !empty($result_array) ? array_shift($result_array) : false;
    }  

public static function update_basic($level='',$step='',$basic='')
{
  global $database;
  $sql = "UPDATE salstructure SET basic='$basic' WHERE level='$level' AND step='$step' LIMIT 1";
 
 
  if ($database->query($sql)) {
    $id=$database->insert_id();
    return true;
     
  } else {
      return false;
  }
}

public static function  delete_department($dept='')
{
  global $database;
  $sql="DELETE FROM dpunit WHERE id='$dept' LIMIT 1";
  if ($database->query($sql)) {
    $id=$database->insert_id();
    return true;
     
  } else {
      return false;
  }
}


public static function  delete_basic($level='',$step='',$basic='')
{
  global $database;
  $sql="DELETE FROM salstructure WHERE level='$level' AND step='$step' AND basic='$basic' LIMIT 1";
  if ($database->query($sql)) {
    $id=$database->insert_id();
    return true;
     
  } else {
      return false;
  }


}
public static function fetch_basic($level='',$step='') 
{
  global $database;

  $sql="SELECT * FROM salstructure WHERE level='$level' AND step='$step' LIMIT 1";
  $result_array = self::find_by_sql($sql);
    return !empty($result_array) ? array_shift($result_array) : false;
} 





public static function fetch_salarystru($table=''){

  global $database;

   $sql="SELECT * FROM $table";
  return $result_array=self::find_by_sql($sql);

}   


public static function fetch_salarystru_step($lid=''){

  global $database;

   $sql="SELECT * FROM salstructure WHERE level='$lid'";
  return $result_array=self::find_by_sql($sql);

}   


public static function fetch_all_department($dpid=''){

  global $database;

  $sql="SELECT * FROM dpunit WHERE dpunitid=$dpid ORDER BY name ";
  return $result_array=self::find_by_sql($sql);

}

public static function insert_department($dname='',$dpid='')
{
  global $database;

  $dname = $database->escape_value($dname);
  $dpid = $database->escape_value($dpid);
  $sql = "INSERT INTO dpunit (";
        $sql .= "name, dpunitid";

        $sql .= ") VALUES ('";
        $sql .= $dname . "','";
        $sql .= $dpid . "' 
       )";

        if ($database->query($sql)) {
          $id=$database->insert_id();
          return true;
           
        } else {
            return false;
        }
}
public static function insert_salary($level="", $step="", $basic=""){
        global $database;

        $level = $database->escape_value($level);
        $step = $database->escape_value($step);
        $basic = $database->escape_value($basic);
       
            
       
        $sql = "INSERT INTO salstructure (";
        $sql .= "level, step, basic";

        $sql .= ") VALUES ('";
        $sql .= $level . "', '";
      
        $sql .= $step . "', '";
                
        $sql .= $basic . "'
          
       )";

        if ($database->query($sql)) {
          $id=$database->insert_id();
          return true;
           
        } else {
            return false;
        }


    }








public static function get_access_level($id="", $role="")
{
  global $database;

  $sql="SELECT log.*,rank.*,opr.* FROM tbl_login log inner join  tbl_rank rank on log.rid=rank.id inner join tbl_operation opr ON rank.id=opr.created_access WHERE opr.created_access=$role and rank.id=$id LIMIT 1";

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


public static function get_staff_rank($menuid=""){

  global $database;
  
      $sql = "SELECT rk.*,br.* FROM tbl_rank rk INNER JOIN tbl_staffs st ON st.staff_branch_id=rk.id INNER JOIN tbl_branches br ON st.staff_branch_id=br.branch_id WHERE rk.id=$menuid LIMIT 1";
      $result_array = self::find_by_sql($sql);
    return !empty($result_array) ? array_shift($result_array) : false;       
}

public static function add_hidden_submenu_links($menuid="", $menuList="")
{
  global $database; 

  $sql = "SELECT * FROM ".self::$tbl_operation." WHERE mainmenuid=menuid AND hidden=0  AND created_access IN ($menuList) order by mainmenuid desc";
     return $result_array = self::find_by_sql($sql);
    //return !empty($result_array) ? array_shift($result_array) : false;                    

 }
public static function add_hidden_submenu_descending_links($menuid="", $menuList="")
{
  global $database; 

  $sql = "SELECT * FROM ".self::$tbl_operation." WHERE mainmenuid=menuid AND hidden=0  AND created_access IN ($menuList) order by mainmenuid desc";
     return $result_array = self::find_by_sql($sql);
    //return !empty($result_array) ? array_shift($result_array) : false;                    

 }


}

//Common database Methods
 $staff = new Staff();
?>